Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../Core/base.h"
- #include "puppeteeranimating.h"
- AnimationManager::AnimationManager(Entity* pEntity)
- {
- if (pEntity == NULL)
- {
- Debug::Error("AnimationManager: Entity cannot be nil.");
- }
- entity = pEntity;
- frameoffset = Math::Random(0, 1000);
- }
- AnimationManager::~AnimationManager()
- {
- vector<Animation*>::iterator it;
- for (it = animations.begin(); it != animations.end(); ++it)
- {
- SAFE_DELETE(*it);
- }
- ClearAnimations();
- }
- void AnimationManager::SetAnimationSequence(const std::string& pSequence, const float pSpeed, const int pBlendTime, const bool pMode, func_ptr pEndHook)
- {
- // Check for redundant animation descriptor
- if (pMode == false)
- {
- if (animations.size() > 0)
- {
- if (animations.back()->sequence == pSequence)
- {
- if (animations.back()->speed == pSpeed)
- {
- // No change to blend time, so don't alter this?
- animations.back()->blendtime = pBlendTime;
- return;
- }
- }
- }
- }
- // Create new animation descriptor and add to animation stack
- Animation* animation = new Animation();
- animation->blendstart = Time::GetCurrent();
- animation->blendfinish = animation->blendstart + pBlendTime;
- animation->sequence = pSequence;
- animation->length = entity->GetAnimationLength(animation->sequence, true);
- animation->speed = pSpeed;
- animation->mode = pMode;
- animation->endHook = pEndHook;
- animation->endOfSequenceReached = false;
- animations.push_back(animation);
- }
- void AnimationManager::ClearAnimations()
- {
- animations.clear();
- }
- void AnimationManager::Update()
- {
- float blend;
- float frame;
- bool doanimation = false;
- long currenttime = Time::GetCurrent();
- int maxanim = -1;
- for (int i = 0; i < animations.size(); ++i)
- {
- Animation* animation = animations[i];
- // Lock the matrix before the first sequence is applied
- if (doanimation == false)
- {
- doanimation = true;
- entity->LockMatrix();
- }
- // Calculate blend value
- blend = float(currenttime - animation->blendstart) / float(animation->blendfinish - animation->blendstart);
- blend = Math::Min(1.0, blend);
- System::Print(blend);
- if (animation->mode == false)
- {
- frame = currenttime * animation->speed + frameoffset;
- }
- else
- {
- frame = (currenttime - animation->blendstart) * animation->speed;
- if (frame >= (animation->length - 1))
- {
- frame = animation->length - 1;
- maxanim = i + 1;
- if (animation->endOfSequenceReached == false)
- {
- if (animation->endHook != nullptr) {
- animation->endHook(entity, animation->sequence);
- animation->endHook = nullptr;
- }
- DevMsg("AnimationManager: endOfSequenceReached.");
- animation->endOfSequenceReached = true;
- }
- }
- }
- entity->SetAnimationFrame(frame, blend, animation->sequence, true);
- if (blend >= 1.0) {
- maxanim = Math::Max(maxanim, i);
- }
- }
- if (doanimation == true)
- {
- entity->UnlockMatrix();
- }
- // Clear blended out animation - moved this out of the loop to prevent jittering
- if (maxanim > -1)
- {
- for (int n = 0; n < animations.size(); ++n) {
- Animation* completedanimation = animations[n];
- if (n < maxanim)
- {
- if (completedanimation->mode == false || completedanimation->endOfSequenceReached == true)
- {
- DevMsg("AnimationManager: Removing completedanimation. " + to_string(animations.size()));
- animations.erase(animations.begin() + n);
- --n;
- }
- }
- else
- {
- //DevMsg("AnimationManager:Break.");
- break;
- }
- }
- }
- }
- // The test entity:
- PuppeteerAnimating::PuppeteerAnimating(Entity* pEntity) : Puppeteer(pEntity), animationmanager(pEntity)
- {
- Start();
- }
- PuppeteerAnimating::~PuppeteerAnimating() {}
- void PuppeteerAnimating::Start() {}
- static void MyCallback(Entity* entity, const std::string& seq) {
- DevMsg("Animation finshed!! test if entity pts to entity: " + to_string(entity->CountAnimations()));
- DevMsg("The sequence that was ran was: " + seq);
- }
- void PuppeteerAnimating::Use(Entity* pCaller)
- {
- DevMsg("use");
- //animationmanager.SetAnimationSequence("Run", 0.04);
- animationmanager.SetAnimationSequence("Death", 0.04, 300, true, &MyCallback);
- }
- void PuppeteerAnimating::Draw(Camera* camera)
- {
- animationmanager.Update();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement