Leviathan  0.8.0.0
Leviathan game engine
Leviathan::AnimationTimeAdder Class Reference

Handles updating time of Ogre animations. More...

#include <Systems.h>

Public Member Functions

DLLEXPORT void Run (GameWorld &world, std::unordered_map< ObjectID, Animated *> &index, int tick, int timeintick)
 

Detailed Description

Handles updating time of Ogre animations.

Definition at line 115 of file Systems.h.

Member Function Documentation

◆ Run()

DLLEXPORT void AnimationTimeAdder::Run ( GameWorld world,
std::unordered_map< ObjectID, Animated *> &  index,
int  tick,
int  timeintick 
)

Definition at line 219 of file Systems.cpp.

221 {
222  float timeNow = (tick * TICKSPEED + timeintick) / 1000.f;
223 
224  const float passed = timeNow - LastSeconds;
225 
226  LastSeconds = timeNow;
227 
228  for(auto iter = index.begin(); iter != index.end(); ++iter) {
229 
230  Animated& animated = *iter->second;
231 
232  if(!animated.GraphicalObject)
233  continue;
234 
235  auto skeleton = animated.GraphicalObject->getSkeletonInstance();
236 
237  if(!skeleton) {
238 
239  LOG_WARNING("Animated (entity id: " + std::to_string(iter->first) +
240  ") has an Item that has no skeleton instance");
241  continue;
242  }
243 
244  if(animated.Marked) {
245  animated.Marked = false;
246 
247  // Apply all properties and stop not playing animations
248  for(const auto& animation : animated.Animations) {
249 
250  try {
251 
252  // Documentation says that this throws if not found
253  Ogre::SkeletonAnimation* ogreAnim = skeleton->getAnimation(animation.Name);
254 
255  ogreAnim->setEnabled(true);
256  ogreAnim->setLoop(animation.Loop);
257 
258  } catch(const Ogre::Exception& e) {
259 
260  LOG_WARNING("Animated (entity id: " + std::to_string(iter->first) +
261  ") has no animation named: " + animation.ReadableName +
262  ", exception: " + e.what());
263  }
264  }
265 
266  // Then disable
267  for(const auto& ogreAnimation : skeleton->getAnimations()) {
268 
269  bool found = false;
270 
271  for(const auto& animation : animated.Animations) {
272 
273  if(animation.Name == ogreAnimation.getName()) {
274  found = true;
275  break;
276  }
277  }
278 
279  if(!found) {
280 
281  skeleton->getAnimation(ogreAnimation.getName())->setEnabled(false);
282  }
283  }
284  }
285 
286  // Update animation time
287  for(const auto& animation : animated.Animations) {
288 
289  if(animation.Paused)
290  return;
291 
292  try {
293 
294  // Documentation says that this throws if not found
295  Ogre::SkeletonAnimation* ogreAnim = skeleton->getAnimation(animation.Name);
296  ogreAnim->addTime(passed * animation.SpeedFactor);
297 
298  } catch(const Ogre::Exception& e) {
299 
300  LOG_WARNING("Animated (entity id: " + std::to_string(iter->first) +
301  ") has no animation named (in update): " + animation.ReadableName +
302  ", exception: " + e.what());
303  }
304  }
305  }
306 }
constexpr auto TICKSPEED
Number of milliseconds between engine and world ticks.
Definition: Define.h:21
#define LOG_WARNING(x)
Definition: Define.h:82
Ogre::Item * GraphicalObject
The entity that is played animations on.
Definition: Components.h:291
Entity plays animations on an Ogre::Item.
Definition: Components.h:284
std::vector< SimpleAnimation > Animations
Definition: Components.h:296

The documentation for this class was generated from the following files: