From c8663de4fbdb30534723df6c8d1331535dcdadc2 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sat, 12 Nov 2022 23:01:24 +0100 Subject: Reorganize to make animations easily changeable: Animations now return rectangles so the texture is independent from them and they don't store the sprite either. Many Animations may have the same texture. Changing from one Animation to another is just changing a reference. --- src/main.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 4 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 509f691..38d1e06 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,17 +1,63 @@ #include +#include #include +#include #include "graphics/animation.h" #include "entity.h" -class Unit: public Entity{ +class Unit: Entity{ + private: + sf::Texture spritesheet_; public: - Graphics::Animation animation; + + enum class Anim : int{ + standing = 0, + walking = 1, + shooting = 2, + complaining = 3, + dying = 4, + }; + std::vector animation_frame_count { 6, 8, 6, 1, 14 }; + Graphics::Animation walking_animation; + Graphics::Animation standing_animation; + Graphics::Animation shooting_animation; + Graphics::Animation complaining_animation; + Graphics::Animation dying_animation; + Graphics::Animation& current_animation = standing_animation; + sf::Sprite sprite; + + sf::Vector2f position; + sf::Vector2f size; + sf::Vector2f speed; + Unit(){ - animation = Graphics::Animation("angle1.png", 6, 60); + spritesheet_.loadFromFile("angle1_all.png"); + standing_animation = Graphics::Animation(spritesheet_, 6, 60, 44, 48, 0); + walking_animation = Graphics::Animation(spritesheet_, 8, 60, 44, 48, 1); + shooting_animation = Graphics::Animation(spritesheet_, 6, 60, 44, 48, 2); + complaining_animation = Graphics::Animation(spritesheet_, 1, 60, 44, 48, 3); + dying_animation = Graphics::Animation(spritesheet_, 14, 60, 44, 48, 4); + + position = sf::Vector2f{0,0}; + speed = sf::Vector2f{0,0}; }; + void walk(float vx, float vy){ + speed.y = vy; + speed.x = vx; + current_animation.reset(); + current_animation = walking_animation; + walking_animation.reset(); + } ~Unit(){}; + void update(int dt){ + 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); + } }; int main() @@ -24,16 +70,27 @@ int main() renderWindow.setFramerateLimit(60); + long elapsed_time = 0; + bool once = true; + while (renderWindow.isOpen()){ int dt = clock.getElapsedTime().asMilliseconds(); + elapsed_time += dt; clock.restart(); while (renderWindow.pollEvent(event)){ if (event.type == sf::Event::EventType::Closed) renderWindow.close(); } + if(elapsed_time > 1000 && once){ + unit.walk(0.3, 0); + once = false; + } + + unit.update(dt); + renderWindow.clear(); - renderWindow.draw(unit.animation.next(dt)); + renderWindow.draw(unit.sprite); renderWindow.display(); } } -- cgit v1.2.3