Advertisement
Codeblocks

cppanimating.cpp

Jul 30th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include "../Core/base.h"
  2. #include "puppeteeranimating.h"
  3.  
  4. AnimationManager::AnimationManager(Entity* pEntity)
  5. {
  6.     if (pEntity == NULL)
  7.     {
  8.         Debug::Error("AnimationManager: Entity cannot be nil.");
  9.     }
  10.  
  11.     entity = pEntity;
  12.     frameoffset = Math::Random(0, 1000);
  13.  
  14. }
  15.  
  16. AnimationManager::~AnimationManager()
  17. {
  18.     vector<Animation*>::iterator it;
  19.     for (it = animations.begin(); it != animations.end(); ++it)
  20.     {
  21.         SAFE_DELETE(*it);
  22.     }
  23.  
  24.     ClearAnimations();
  25. }
  26.  
  27. void AnimationManager::SetAnimationSequence(const std::string& pSequence, const float pSpeed, const int pBlendTime, const bool pMode, func_ptr pEndHook)
  28. {
  29.     // Check for redundant animation descriptor
  30.     if (pMode == false)
  31.     {
  32.         if (animations.size() > 0)
  33.         {
  34.             if (animations.back()->sequence == pSequence)
  35.             {
  36.                 if (animations.back()->speed == pSpeed)
  37.                 {
  38.                     // No change to blend time, so don't alter this?
  39.                     animations.back()->blendtime = pBlendTime;
  40.                     return;
  41.                 }
  42.             }
  43.         }
  44.     }
  45.  
  46.     // Create new animation descriptor and add to animation stack
  47.     Animation* animation = new Animation();
  48.     animation->blendstart = Time::GetCurrent();
  49.     animation->blendfinish = animation->blendstart + pBlendTime;
  50.     animation->sequence = pSequence;
  51.     animation->length = entity->GetAnimationLength(animation->sequence, true);
  52.     animation->speed = pSpeed;
  53.     animation->mode = pMode;
  54.     animation->endHook = pEndHook;
  55.     animation->endOfSequenceReached = false;
  56.  
  57.     animations.push_back(animation);
  58. }
  59.  
  60. void AnimationManager::ClearAnimations()
  61. {
  62.     animations.clear();
  63. }
  64.  
  65. void AnimationManager::Update()
  66. {
  67.     float blend;
  68.     float frame;
  69.     bool doanimation = false;
  70.     long currenttime = Time::GetCurrent();
  71.     int maxanim = -1;
  72.     for (int i = 0; i < animations.size(); ++i)
  73.     {
  74.         Animation* animation = animations[i];
  75.  
  76.         // Lock the matrix before the first sequence is applied
  77.         if (doanimation == false)
  78.         {
  79.             doanimation = true;
  80.             entity->LockMatrix();
  81.         }
  82.  
  83.         // Calculate blend value
  84.         blend = float(currenttime - animation->blendstart) / float(animation->blendfinish - animation->blendstart);
  85.         blend = Math::Min(1.0, blend);
  86.         System::Print(blend);
  87.  
  88.         if (animation->mode == false)
  89.         {
  90.             frame = currenttime * animation->speed + frameoffset;
  91.         }
  92.         else
  93.         {
  94.             frame = (currenttime - animation->blendstart) * animation->speed;
  95.  
  96.             if (frame >= (animation->length - 1))
  97.             {
  98.                 frame = animation->length - 1;
  99.                 maxanim = i + 1;
  100.                 if (animation->endOfSequenceReached == false)
  101.                 {
  102.                     if (animation->endHook != nullptr) {
  103.                         animation->endHook(entity, animation->sequence);
  104.                         animation->endHook = nullptr;
  105.                     }
  106.                     DevMsg("AnimationManager: endOfSequenceReached.");
  107.                     animation->endOfSequenceReached = true;
  108.                 }
  109.             }
  110.         }
  111.  
  112.         entity->SetAnimationFrame(frame, blend, animation->sequence, true);
  113.  
  114.         if (blend >= 1.0) {
  115.             maxanim = Math::Max(maxanim, i);
  116.         }
  117.     }
  118.  
  119.     if (doanimation == true)
  120.     {
  121.         entity->UnlockMatrix();
  122.     }
  123.  
  124.     // Clear blended out animation - moved this out of the loop to prevent jittering
  125.     if (maxanim > -1)
  126.     {
  127.         for (int n = 0; n < animations.size(); ++n) {
  128.             Animation* completedanimation = animations[n];
  129.  
  130.             if (n < maxanim)
  131.             {
  132.                 if (completedanimation->mode == false || completedanimation->endOfSequenceReached == true)
  133.                 {
  134.                     DevMsg("AnimationManager: Removing completedanimation. " + to_string(animations.size()));
  135.                     animations.erase(animations.begin() + n);
  136.                     --n;
  137.                 }
  138.             }
  139.             else
  140.             {
  141.                 //DevMsg("AnimationManager:Break.");
  142.                 break;
  143.             }
  144.         }
  145.     }
  146. }
  147.  
  148. // The test entity:
  149. PuppeteerAnimating::PuppeteerAnimating(Entity* pEntity) : Puppeteer(pEntity), animationmanager(pEntity)
  150. {
  151.     Start();
  152. }
  153.  
  154. PuppeteerAnimating::~PuppeteerAnimating() {}
  155.  
  156. void PuppeteerAnimating::Start() {}
  157.  
  158. static void MyCallback(Entity* entity, const std::string& seq) {
  159.     DevMsg("Animation finshed!! test if entity pts to entity: " + to_string(entity->CountAnimations()));
  160.     DevMsg("The sequence that was ran was: " + seq);
  161. }
  162.  
  163. void PuppeteerAnimating::Use(Entity* pCaller)
  164. {
  165.     DevMsg("use");
  166.     //animationmanager.SetAnimationSequence("Run", 0.04);
  167.     animationmanager.SetAnimationSequence("Death", 0.04, 300, true, &MyCallback);
  168. }
  169.  
  170.  
  171. void PuppeteerAnimating::Draw(Camera* camera)
  172. {
  173.     animationmanager.Update();
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement