Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************************
- * Copyright (c) 2014-2016, rHetorical Studios. All rights reserved. *
- * *
- * This product contains software technology licensed from *
- * Leadwerks Software. All Rights Reserved. *
- ************************************************************************/
- #include "../Core/base.h"
- #include "puppeteeranimating.h"
- class Animation
- {
- public:
- Animation() {};
- virtual ~Animation() {};
- long blendstart;
- long blendfinish;
- float blendtime;
- std::string sequence;
- long length;
- float speed;
- bool mode;
- long starttime;
- bool endOfSequenceReached = false;
- void endHook() {};
- int endFrame;
- };
- vector<Animation*> animations;
- 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(std::string pSequence, const float pSpeed, const float pBlendTime, const bool pMode, void EndHook(), int pEndFrame)
- {
- // Check for redundant animation descriptor
- if (pMode == false)
- {
- if (animations.size() > 0)
- {
- if (animations[animations.size()]->sequence == pSequence)
- {
- if (animations[animations.size()]->speed == pSpeed)
- {
- // No change to blend time, so don't alter this?
- animations[animations.size()]->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->starttime = animation->blendstart;
- //animation->endHook = EndHook();
- animation->endOfSequenceReached = false;
- animation->endFrame = pEndFrame;
- animations.push_back(animation);
- }
- void AnimationManager::ClearAnimations()
- {
- animations.clear();
- }
- void AnimationManager::Update()
- {
- long blend;
- long 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 = (currenttime - animation->blendstart) / (animation->blendfinish - animation->blendstart);
- blend = Math::Min(1.0, blend);
- if (animation->mode == false)
- {
- frame = currenttime * animation->speed + frameoffset;
- }
- else
- {
- frame = (currenttime - animation->blendstart) * animation->speed;
- // If a certain frame was specified, call the hook early and remove it
- if (animation->endFrame != NULL)
- {
- if (frame > animation->endFrame)
- {
- //animation->endHook();
- animation->endFrame = NULL;
- //animation->endHook = NULL;
- }
- }
- if (frame >= (animation->length - 1))
- {
- frame = animation->length - 1;
- maxanim = i + 1;
- if (animation->endOfSequenceReached == false)
- {
- DevMsg("AnimationManager: endOfSequenceReached.");
- animation->endOfSequenceReached = true;
- }
- }
- } // if (animation->mode)
- // Apply the animation
- if (animation->sequence != S_NULL)
- {
- entity->SetAnimationFrame(frame, blend, animation->sequence, true);
- }
- // If this animation is blended in 100%, all previous looping animation sequences can be dropped
- if (blend >= 1.0)
- {
- maxanim = Math::Max(maxanim, i);
- }
- } // forloop
- // Unlock entity matrix if any animations were applied
- 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);
- }
- }
- else
- {
- //DevMsg("AnimationManager:Break.");
- break;
- }
- }
- }
- }
- // The test entity:
- PuppeteerAnimating::PuppeteerAnimating(Entity* pEntity) : Puppeteer(pEntity)
- {
- Start();
- }
- PuppeteerAnimating::~PuppeteerAnimating()
- {
- SAFE_DELETE(animationmanager);
- }
- void PuppeteerAnimating::Start()
- {
- animationmanager = new AnimationManager(entity);
- }
- void PuppeteerAnimating::Use(Entity* pCaller)
- {
- DevMsg("use");
- //animationmanager->SetAnimationSequence("Run", 0.04);
- animationmanager->SetAnimationSequence("Death", 0.04, 300, true);
- animationmanager->SetAnimationSequence("Attack1", 0.05, 300, true);
- animationmanager->SetAnimationSequence("Attack2", 0.05, 300, true);
- }
- void PuppeteerAnimating::Draw(Camera* camera)
- {
- animationmanager->Update();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement