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 118 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 138 of file Systems.cpp.

140 {
141  float timeNow = (tick * TICKSPEED + timeintick) / 1000.f;
142 
143  const float passed = timeNow - LastSeconds;
144 
145  LastSeconds = timeNow;
146 
147  for(auto iter = index.begin(); iter != index.end(); ++iter) {
148 
149  Animated& animated = *iter->second;
150 
151  if(!animated.GraphicalObject)
152  continue;
153 
154  auto skeleton = animated.GraphicalObject->getSkeletonInstance();
155 
156  if(!skeleton) {
157 
158  LOG_WARNING("Animated (entity id: " + std::to_string(iter->first) +
159  ") has an Item that has no skeleton instance");
160  continue;
161  }
162 
163  if(animated.Marked) {
164  animated.Marked = false;
165 
166  // Apply all properties and stop not playing animations
167  for(const auto& animation : animated.Animations) {
168 
169  try {
170 
171  // Documentation says that this throws if not found
172  Ogre::SkeletonAnimation* ogreAnim = skeleton->getAnimation(animation.Name);
173 
174  ogreAnim->setEnabled(true);
175  ogreAnim->setLoop(animation.Loop);
176 
177  } catch(const Ogre::Exception& e) {
178 
179  LOG_WARNING("Animated (entity id: " + std::to_string(iter->first) +
180  ") has no animation named: " + animation.ReadableName +
181  ", exception: " + e.what());
182  }
183  }
184 
185  // Then disable
186  for(const auto& ogreAnimation : skeleton->getAnimations()) {
187 
188  bool found = false;
189 
190  for(const auto& animation : animated.Animations) {
191 
192  if(animation.Name == ogreAnimation.getName()) {
193  found = true;
194  break;
195  }
196  }
197 
198  if(!found) {
199 
200  skeleton->getAnimation(ogreAnimation.getName())->setEnabled(false);
201  }
202  }
203  }
204 
205  // Update animation time
206  for(const auto& animation : animated.Animations) {
207 
208  if(animation.Paused)
209  return;
210 
211  try {
212 
213  // Documentation says that this throws if not found
214  Ogre::SkeletonAnimation* ogreAnim = skeleton->getAnimation(animation.Name);
215  ogreAnim->addTime(passed * animation.SpeedFactor);
216 
217  } catch(const Ogre::Exception& e) {
218 
219  LOG_WARNING("Animated (entity id: " + std::to_string(iter->first) +
220  ") has no animation named (in update): " + animation.ReadableName +
221  ", exception: " + e.what());
222  }
223  }
224  }
225 }
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:284
Entity plays animations on an Ogre::Item.
Definition: Components.h:277
std::vector< SimpleAnimation > Animations
Definition: Components.h:289

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