From 82598dbaac1bc99a0a4f99833b68915ffbec0495 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Thu, 17 Nov 2022 16:59:15 +0100 Subject: Simplify animations --- src/main.cpp | 56 ++++++++++++++++---------------------------------------- 1 file changed, 16 insertions(+), 40 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index bd330fc..02697d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,20 +14,8 @@ class Unit: Entity{ sf::Texture spritesheet_; public: - enum class Anim : int{ - standing = 0, - walking = 1, - shooting = 2, - complaining = 3, - dying = 4, - }; - std::vector animation_frame_count { 6, 8, 6, 1, 14 }; - gph::Animation walking_animation; - gph::Animation standing_animation; - gph::FlippedAnimation shooting_animation; - gph::OneShotAnimation complaining_animation; - gph::OneShotAnimation dying_animation; - gph::Animation* current_animation = &standing_animation; + gph::Animation dying_animation; + gph::Animation& current_animation = dying_animation; sf::Sprite sprite; sf::Vector2f position; @@ -37,11 +25,8 @@ class Unit: Entity{ Unit(sf::Texture& spritesheet){ spritesheet_ = spritesheet; - standing_animation = gph::Animation(spritesheet_, 6, 60, 44, 48, 0); - walking_animation = gph::Animation(spritesheet_, 8, 60, 44, 48, 1); - shooting_animation = gph::FlippedAnimation(spritesheet_, 6, 60, 44, 48, 2); - complaining_animation = gph::OneShotAnimation(spritesheet_, 1, 60, 44, 48, 3); - dying_animation = gph::OneShotAnimation(spritesheet_, 14, 60, 44, 48, 4); + dying_animation = gph::Animation(spritesheet_, 14, sf::milliseconds(1400), false); + current_animation = dying_animation; position = sf::Vector2f{0,0}; speed = sf::Vector2f{0,0}; @@ -49,25 +34,16 @@ class Unit: Entity{ void walk(float vx, float vy){ speed.y = vy; speed.x = vx; - current_animation->reset(); - current_animation = &walking_animation; + current_animation.reset(); + // current_animation = &walking_animation; } void shoot(){ - current_animation->reset(); - current_animation = &shooting_animation; + current_animation.reset(); + // current_animation = &shooting_animation; } ~Unit(){}; - void update(int dt){ - //if (current_animation->finished()){ - // current_animation->reset(); - // current_animation = &standing_animation; - //} - position.x += round(speed.x * dt); - sprite.setTexture(spritesheet_); - sprite.setScale(8,8); - sprite.setTextureRect(current_animation->next(dt)); - sprite.setPosition(position.x, position.y); - sprite = sprite; + void update(sf::Time dt){ + current_animation.update( dt); } }; @@ -88,17 +64,17 @@ int main() sf::Clock clock; ResourceManager TextureManager; - TextureManager.load( TextureId::Angle1, "assets/img/Player/Player Angle 1 Sheet.png"); + TextureManager.load( TextureId::Angle1, "assets/img/Player/angle1-dying.png"); Unit unit = Unit(TextureManager.get(TextureId::Angle1)); renderWindow.setFramerateLimit(60); - long elapsed_time = 0; + sf::Time elapsed_time = sf::Time::Zero; bool once = true; while (renderWindow.isOpen()){ - int dt = clock.getElapsedTime().asMilliseconds(); + sf::Time dt = clock.getElapsedTime(); elapsed_time += dt; clock.restart(); while (renderWindow.pollEvent(event)){ @@ -106,15 +82,15 @@ int main() renderWindow.close(); } - if(elapsed_time > 1000 && once){ - unit.shoot(); + if(elapsed_time > sf::milliseconds(4000) && once){ + unit.current_animation.reset(); once = false; } unit.update(dt); renderWindow.clear(); - renderWindow.draw(unit.sprite); + renderWindow.draw(unit.current_animation); renderWindow.display(); } } -- cgit v1.2.3