vectozavr-shooter/engine/animation/Timeline.cpp

83 lines
2.0 KiB
C++
Raw Normal View History

//
// Created by Иван Ильин on 03.10.2021.
//
#include <list>
#include "Animation.h"
2021-10-17 10:21:10 +03:00
#include "Timeline.h"
2021-10-17 19:38:16 +03:00
Timeline* Timeline::_instance = nullptr;
2021-10-17 19:38:16 +03:00
void Timeline::init() {
_instance = new Timeline();
}
2021-10-17 19:38:16 +03:00
void Timeline::animate(const AnimationListTag& listName, Animation* anim) {
if(!_instance)
return;
2021-10-17 19:38:16 +03:00
_instance->_animations[listName].emplace_back(anim);
}
2021-10-17 19:38:16 +03:00
void Timeline::deleteAllAnimations() {
if(!_instance)
return;
for (auto& [listName, animationList] : _instance->_animations) {
auto it = animationList.begin();
while(it != animationList.end())
delete *(it++);
animationList.clear();
}
2021-10-17 19:38:16 +03:00
_instance->_animations.clear();
}
void Timeline::deleteAnimationList(const AnimationListTag& listName) {
if(!_instance)
return;
_instance->_animations[listName].clear();
_instance->_animations.erase(listName);
}
2021-10-17 19:38:16 +03:00
[[nodiscard]] bool Timeline::isInAnimList(const AnimationListTag& listName) {
if(!_instance)
return false;
return !_instance->_animations[listName].empty();
}
void Timeline::update() {
if(!_instance)
return;
for (auto& [listName, animationList] : _instance->_animations) {
if (animationList.empty())
continue;
auto it = animationList.begin();
// If it the front animation is 'a_wait()' we should wait until waiting time is over
if (it.operator*()->waitFor()) {
if (!it.operator*()->update())
animationList.erase(it);
continue;
}
// Otherwise we iterate over all animation until we meet animations.end() or wait animation
while (!animationList.empty() && (it != animationList.end()) && (!it.operator*()->waitFor())) {
if (!it.operator*()->update())
animationList.erase(it++);
else
it++;
}
}
}
2021-10-17 19:38:16 +03:00
void Timeline::free() {
Timeline::deleteAllAnimations();
delete _instance;
_instance = nullptr;
}