Advertisement
Codeblocks

AnimationManager.cpp

Jul 30th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include "AnimationManager.h"
  2. #include "Leadwerks.h"
  3.  
  4. namespace Leadwerks {
  5.     AnimationManager::AnimationManager(Entity* pEntity) {
  6.         if (pEntity == NULL) {
  7.             Debug::Error("[AnimationManager] 'pEntity' is NULL in constructor.");
  8.         }
  9.         entity = pEntity;
  10.         frameoffset = Math::Random(0, 1000);
  11.     }
  12.  
  13.     AnimationManager::~AnimationManager() {
  14.         for (auto it = animations.begin(); it != animations.end(); ++it) {
  15.             delete *it;
  16.         }
  17.         ClearAnimations();
  18.     }
  19.  
  20.     void AnimationManager::SetAnimationSequence(const std::string& pSequence, const float pSpeed, const int pBlendTime, const bool pMode, func_ptr pEndHook) {
  21.         if (pMode == false) {
  22.             if (animations.size() > 0) {
  23.                 if (animations.back()->sequence == pSequence) {
  24.                     if (animations.back()->speed == pSpeed) {
  25.                         animations.back()->blendtime = pBlendTime;
  26.                         return;
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.  
  32.         Animation* animation = new Animation();
  33.         animation->blendstart = Time::GetCurrent();
  34.         animation->blendfinish = animation->blendstart + pBlendTime;
  35.         animation->sequence = pSequence;
  36.         animation->length = entity->GetAnimationLength(animation->sequence, true);
  37.         animation->speed = pSpeed;
  38.         animation->mode = pMode;
  39.         animation->endHook = pEndHook;
  40.         animation->endOfSequenceReached = false;
  41.  
  42.         animations.push_back(animation);
  43.     }
  44.  
  45.     void AnimationManager::ClearAnimations() {
  46.         for (auto it = animations.begin(); it != animations.end(); ++it) {
  47.             delete *it;
  48.         }
  49.         animations.clear();
  50.     }
  51.  
  52.     void AnimationManager::Update() {
  53.         float blend, frame;
  54.         bool doanimation = false;
  55.         long currenttime = Time::GetCurrent();
  56.         int maxanim = -1;
  57.         for (int i = 0; i < animations.size(); ++i) {
  58.             Animation* animation = animations[i];
  59.  
  60.             if (doanimation == false) {
  61.                 doanimation = true;
  62.                 entity->LockMatrix();
  63.             }
  64.  
  65.             //Division can result in a float, so cast numerator and denominator to float.
  66.             blend = float(currenttime - animation->blendstart) / float(animation->blendfinish - animation->blendstart);
  67.             blend = Math::Min(1.0, blend);
  68.  
  69.             if (animation->mode == false) {
  70.                 frame = currenttime * animation->speed + frameoffset;
  71.             }
  72.             else {
  73.                 frame = (currenttime - animation->blendstart) * animation->speed;
  74.  
  75.                 if (frame >= (animation->length - 1)) {
  76.                     frame = animation->length - 1;
  77.                     maxanim = i + 1;
  78.                     if (animation->endOfSequenceReached == false) {
  79.                         if (animation->endHook != nullptr) {
  80.                             animation->endHook(entity, animation->sequence);
  81.                             animation->endHook = nullptr;
  82.                         }
  83.                         animation->endOfSequenceReached = true;
  84.                     }
  85.                 }
  86.             }
  87.  
  88.             entity->SetAnimationFrame(frame, blend, animation->sequence, true);
  89.  
  90.             if (blend >= 1.0) {
  91.                 maxanim = Math::Max(maxanim, i);
  92.             }
  93.         }
  94.  
  95.         if (doanimation == true) {
  96.             entity->UnlockMatrix();
  97.         }
  98.  
  99.         if (maxanim > -1)  {
  100.             for (int n = 0; n < animations.size(); ++n) {
  101.                 Animation* completedanimation = animations[n];
  102.  
  103.                 if (n < maxanim) {
  104.                     if (completedanimation->mode == false || completedanimation->endOfSequenceReached == true) {
  105.                         delete animations[n];
  106.                         animations.erase(animations.begin() + n);
  107.                     }
  108.                 }
  109.                 else {
  110.                     break;
  111.                 }
  112.             }
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement