summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2022-11-12 23:01:24 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2022-11-12 23:01:50 +0100
commitc8663de4fbdb30534723df6c8d1331535dcdadc2 (patch)
treed2c1b9aed268c74f4ddc83f540a182950262f9d6 /src/main.cpp
parent2e7d74e2622e13a6986cffd2c8d4423b840c5e59 (diff)
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.
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp65
1 files changed, 61 insertions, 4 deletions
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<SFML/Graphics.hpp>
+#include<vector>
#include<cstdio>
+#include<cmath>
#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<int> 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();
}
}