replace Animation* in Timeline.h with a shared_ptr<Animation>

master
Vectozavr 2021-10-31 01:07:14 +07:00
parent 18afc2c646
commit a294b1de24
4 changed files with 20 additions and 24 deletions

View File

@ -44,25 +44,25 @@ void PlayerController::update() {
std::shared_ptr<Object> camera = _player->attached(ObjectNameTag("Camera")); std::shared_ptr<Object> camera = _player->attached(ObjectNameTag("Camera"));
if(camera != nullptr && _inRunning && _player->inCollision()) { if(camera != nullptr && _inRunning && _player->inCollision()) {
if (!Timeline::isInAnimList(AnimationListTag("camera_hor_oscil"))) { if (!Timeline::isInAnimList(AnimationListTag("camera_hor_oscil"))) {
Timeline::animate(AnimationListTag("camera_hor_oscil"), new ATranslate(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_hor_oscil"), std::make_shared<ATranslate>(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::InterpolationType::Cos));
Timeline::animate(AnimationListTag("camera_hor_oscil"), new AWait(0)); Timeline::animate(AnimationListTag("camera_hor_oscil"), std::make_shared<AWait>(0));
Timeline::animate(AnimationListTag("camera_hor_oscil"), new ATranslate(camera, camera->left() / 6, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_hor_oscil"), std::make_shared<ATranslate>(camera, camera->left() / 6, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos));
Timeline::animate(AnimationListTag("camera_vert_oscil"), new ATranslate(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared<ATranslate>(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos));
Timeline::animate(AnimationListTag("camera_vert_oscil"), new AWait(0)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared<AWait>(0));
Timeline::animate(AnimationListTag("camera_vert_oscil"), new ATranslate(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared<ATranslate>(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos));
Timeline::animate(AnimationListTag("camera_vert_oscil"), new AWait(0)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared<AWait>(0));
Timeline::animate(AnimationListTag("camera_vert_oscil"), new ATranslate(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared<ATranslate>(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos));
Timeline::animate(AnimationListTag("camera_vert_oscil"), new AWait(0)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared<AWait>(0));
Timeline::animate(AnimationListTag("camera_vert_oscil"), new ATranslate(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared<ATranslate>(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos));
Timeline::animate(AnimationListTag("camera_init"), new ATranslateToPoint( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_init"), std::make_shared<ATranslateToPoint>( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos));
} }
} else if(camera != nullptr && inRunning_old && !_inRunning) { } else if(camera != nullptr && inRunning_old && !_inRunning) {
Timeline::deleteAnimationList(AnimationListTag("camera_hor_oscil")); Timeline::deleteAnimationList(AnimationListTag("camera_hor_oscil"));
Timeline::deleteAnimationList(AnimationListTag("camera_vert_oscil")); Timeline::deleteAnimationList(AnimationListTag("camera_vert_oscil"));
Timeline::deleteAnimationList(AnimationListTag("camera_init")); Timeline::deleteAnimationList(AnimationListTag("camera_init"));
Timeline::animate(AnimationListTag("camera_init"), new ATranslateToPoint( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_init"), std::make_shared<ATranslateToPoint>( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos));
} }
// Left and right // Left and right

View File

@ -263,8 +263,8 @@ void Shooter::addFireTrace(const Vec3D &from, const Vec3D &to) {
world->addBody(std::make_shared<RigidBody>(Mesh::LineTo(ObjectNameTag(traceName), from, to, 0.05))); world->addBody(std::make_shared<RigidBody>(Mesh::LineTo(ObjectNameTag(traceName), from, to, 0.05)));
world->body(ObjectNameTag(traceName))->setCollider(false); world->body(ObjectNameTag(traceName))->setCollider(false);
Timeline::animate(AnimationListTag(traceName + "_fadeOut"), new AColor(world->body(ObjectNameTag(traceName)), {150, 150, 150, 0})); Timeline::animate(AnimationListTag(traceName + "_fadeOut"), std::make_shared<AColor>(world->body(ObjectNameTag(traceName)), sf::Color{150, 150, 150, 0}));
Timeline::animate(AnimationListTag(traceName + "_delete"), new AFunction([this, traceName](){ removeFireTrace(ObjectNameTag(traceName)); }, 1, 2)); Timeline::animate(AnimationListTag(traceName + "_delete"), std::make_shared<AFunction>([this, traceName](){ removeFireTrace(ObjectNameTag(traceName)); }, 1, 2));
} }
void Shooter::removeFireTrace(const ObjectNameTag& traceName) { void Shooter::removeFireTrace(const ObjectNameTag& traceName) {
@ -279,7 +279,7 @@ void Shooter::addBonus(const string &bonusName, const Vec3D &position) {
world->addBody(std::make_shared<RigidBody>(ObjectNameTag(bonusName), "obj/" + name + ".obj", Vec3D{3, 3, 3})); world->addBody(std::make_shared<RigidBody>(ObjectNameTag(bonusName), "obj/" + name + ".obj", Vec3D{3, 3, 3}));
world->body(ObjectNameTag(bonusName))->translateToPoint(position); world->body(ObjectNameTag(bonusName))->translateToPoint(position);
world->body(ObjectNameTag(bonusName))->setCollider(false); world->body(ObjectNameTag(bonusName))->setCollider(false);
Timeline::animate(AnimationListTag(bonusName + "_rotation"), new ARotate(world->body(ObjectNameTag(bonusName)), Vec3D{0, 2*Consts::PI, 0}, 4, Animation::LoopOut::Continue, Animation::InterpolationType::Linear)); Timeline::animate(AnimationListTag(bonusName + "_rotation"), std::make_shared<ARotate>(world->body(ObjectNameTag(bonusName)), Vec3D{0, 2*Consts::PI, 0}, 4, Animation::LoopOut::Continue, Animation::InterpolationType::Linear));
} }
void Shooter::removeBonus(const ObjectNameTag &bonusName) { void Shooter::removeBonus(const ObjectNameTag &bonusName) {

View File

@ -5,6 +5,7 @@
#include <list> #include <list>
#include "Animation.h" #include "Animation.h"
#include "Timeline.h" #include "Timeline.h"
#include <iostream>
Timeline* Timeline::_instance = nullptr; Timeline* Timeline::_instance = nullptr;
bool Timeline::_validInstance = false; bool Timeline::_validInstance = false;
@ -14,7 +15,7 @@ void Timeline::init() {
_validInstance = true; _validInstance = true;
} }
void Timeline::animate(const AnimationListTag& listName, Animation* anim) { void Timeline::animate(const AnimationListTag& listName, std::shared_ptr<Animation> anim) {
if(!_validInstance) { if(!_validInstance) {
return; return;
} }
@ -28,10 +29,6 @@ void Timeline::deleteAllAnimations() {
} }
for (auto& [listName, animationList] : _instance->_animations) { for (auto& [listName, animationList] : _instance->_animations) {
auto it = animationList.begin();
while(it != animationList.end()) {
delete *(it++);
}
animationList.clear(); animationList.clear();
} }
_instance->_animations.clear(); _instance->_animations.clear();
@ -60,8 +57,8 @@ void Timeline::update() {
} }
for (auto& [listName, animationList] : _instance->_animations) { for (auto& [listName, animationList] : _instance->_animations) {
if (animationList.empty()) { if (animationList.empty()) {
_instance->_animations.erase(listName);
continue; continue;
} }
auto it = animationList.begin(); auto it = animationList.begin();

View File

@ -21,8 +21,7 @@ public:
class Timeline { class Timeline {
private: private:
// TODO: replace Animation* with shared_ptr<Animation> & check for possible memory leaks std::map<AnimationListTag, std::list<std::shared_ptr<Animation>>> _animations;
std::map<AnimationListTag, std::list<Animation*>> _animations;
static Timeline* _instance; static Timeline* _instance;
static bool _validInstance; static bool _validInstance;
@ -33,7 +32,7 @@ public:
Timeline& operator=(Timeline&) = delete; Timeline& operator=(Timeline&) = delete;
static void update(); static void update();
static void animate(const AnimationListTag& listName, Animation* anim); static void animate(const AnimationListTag& listName, std::shared_ptr<Animation> anim);
static void deleteAllAnimations(); static void deleteAllAnimations();
static void deleteAnimationList(const AnimationListTag& listName); static void deleteAnimationList(const AnimationListTag& listName);