shooter/engine/animation/Timeline.cpp

97 lines
2.5 KiB
C++
Raw Normal View History

//
// Created by Иван Ильин on 03.10.2021.
//
#include <list>
2021-10-31 11:39:08 +03:00
2021-10-17 10:21:10 +03:00
#include "Timeline.h"
#include "../utils/Log.h"
2021-10-31 11:39:08 +03:00
Timeline *Timeline::_instance = nullptr;
2021-10-17 19:38:16 +03:00
void Timeline::init() {
2021-11-09 22:54:20 +03:00
delete _instance;
2021-10-17 19:38:16 +03:00
_instance = new Timeline();
Log::log("Timeline::init(): animation timeline was initialized");
2021-10-17 19:38:16 +03:00
}
2021-10-17 19:38:16 +03:00
void Timeline::deleteAllAnimations() {
2021-11-09 22:54:20 +03:00
if (_instance == nullptr) {
2021-10-17 19:38:16 +03:00
return;
2021-10-28 16:58:02 +03:00
}
2021-10-17 19:38:16 +03:00
2021-11-09 22:54:20 +03:00
Log::log("Timeline::deleteAllAnimations(): all " + std::to_string(_instance->_animations.size()) + " list was deleted");
2021-10-17 19:38:16 +03:00
_instance->_animations.clear();
}
2021-10-31 11:39:08 +03:00
void Timeline::deleteAnimationList(const AnimationListTag &listName) {
2021-11-09 22:54:20 +03:00
if (_instance == nullptr) {
2021-10-17 19:38:16 +03:00
return;
2021-10-28 16:58:02 +03:00
}
2021-10-17 19:38:16 +03:00
2021-11-09 22:54:20 +03:00
auto it = _instance->_animations.find(listName);
2021-11-09 22:54:20 +03:00
if(it != _instance->_animations.end()) {
_instance->_animations.erase(it);
} else {
Log::log("Timeline::deleteAnimationList(): list '" + listName.str() + "' does not exist");
}
2021-10-17 19:38:16 +03:00
}
2021-10-31 11:39:08 +03:00
[[nodiscard]] bool Timeline::isInAnimList(const AnimationListTag &listName) {
2021-11-09 22:54:20 +03:00
if (_instance == nullptr) {
2021-10-17 19:38:16 +03:00
return false;
2021-10-28 16:58:02 +03:00
}
2021-10-17 19:38:16 +03:00
2021-11-09 22:54:20 +03:00
auto it = _instance->_animations.find(listName);
if(it != _instance->_animations.end()) {
return !it->second.empty();
}
return false;
2021-10-17 19:38:16 +03:00
}
void Timeline::update() {
2021-11-09 22:54:20 +03:00
if (_instance == nullptr) {
2021-10-17 19:38:16 +03:00
return;
2021-10-28 16:58:02 +03:00
}
2021-10-17 19:38:16 +03:00
2021-11-06 00:31:41 +03:00
for (auto iter = _instance->_animations.begin(); iter != _instance->_animations.end(); ) {
if (iter->second.empty()) {
_instance->_animations.erase(iter++);
2021-10-17 19:38:16 +03:00
continue;
2021-10-28 16:58:02 +03:00
}
2021-11-06 00:31:41 +03:00
auto& animationList = iter->second;
2021-10-17 19:38:16 +03:00
auto it = animationList.begin();
2021-11-09 22:54:20 +03:00
2022-02-23 07:51:53 +03:00
// If it the front animation is 'AWait' we should wait until waiting time is over
if ((it != animationList.end()) && (*it)->isWaitingForFinish()) {
2021-11-09 22:54:20 +03:00
if (!(*it)->updateState()) {
2021-10-17 19:38:16 +03:00
animationList.erase(it);
2021-10-28 16:58:02 +03:00
}
2022-02-23 07:51:53 +03:00
++iter;
2021-10-17 19:38:16 +03:00
continue;
}
// Otherwise we iterate over all animation until we meet animations.end() or wait animation
2021-11-09 22:54:20 +03:00
while (!animationList.empty() && (it != animationList.end()) && (!(*it)->isWaitingForFinish())) {
if (!(*it)->updateState()) {
2021-10-17 19:38:16 +03:00
animationList.erase(it++);
2021-10-28 16:58:02 +03:00
} else {
2021-11-09 22:54:20 +03:00
++it;
2021-10-28 16:58:02 +03:00
}
}
2021-11-09 22:54:20 +03:00
++iter;
}
}
2021-10-17 19:38:16 +03:00
void Timeline::free() {
Timeline::deleteAllAnimations();
delete _instance;
2021-11-09 22:54:20 +03:00
_instance = nullptr;
Log::log("Timeline::free(): pointer to 'Timeline' was freed");
2021-10-17 19:38:16 +03:00
}