summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2022-11-13 12:05:43 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2022-11-13 12:05:43 +0100
commit3293d06b364bc91348f6a305f97e607ff610eb98 (patch)
tree25b5c0801cb96208c225121a7a18c9d896ba5ac6 /src
parentc8663de4fbdb30534723df6c8d1331535dcdadc2 (diff)
Make animations use runtime polimorphism:
Lets us make different animation types but use them interchangeably
Diffstat (limited to 'src')
-rw-r--r--src/graphics/animation.cpp6
-rw-r--r--src/graphics/animation.h15
-rw-r--r--src/main.cpp31
3 files changed, 32 insertions, 20 deletions
diff --git a/src/graphics/animation.cpp b/src/graphics/animation.cpp
index b93387e..577324a 100644
--- a/src/graphics/animation.cpp
+++ b/src/graphics/animation.cpp
@@ -64,6 +64,10 @@ namespace Graphics{
rect_.top = row_ * h_;
}
+ bool Animation::finished(){
+ return false;
+ }
+
Animation::~Animation(){}
// ONESHOT ANIMATION
@@ -75,7 +79,7 @@ namespace Graphics{
}
bool OneShotAnimation::finished(){
- return i_ == count_ - 1;
+ return (i_ == count_ - 1);
}
sf::IntRect& OneShotAnimation::next(int deltaTime){
diff --git a/src/graphics/animation.h b/src/graphics/animation.h
index 9f5dfa5..503b75d 100644
--- a/src/graphics/animation.h
+++ b/src/graphics/animation.h
@@ -21,20 +21,21 @@ namespace Graphics{
~Animation();
Animation(const sf::Texture& texture, int count, int switchTime,
int height, int width, int row);
- sf::IntRect& next(int deltaTime);
- void reset();
+ virtual sf::IntRect& next(int deltaTime);
+ virtual void reset();
+ virtual bool finished();
};
- class OneShotAnimation : Animation {
+ class OneShotAnimation : public Animation {
public:
OneShotAnimation();
OneShotAnimation(const sf::Texture& texture, int count, int
switchTime, int height, int width, int row);
- bool finished();
- sf::IntRect& next(int deltaTime);
+ sf::IntRect& next(int deltaTime) override;
+ bool finished() override;
};
- class BouncingAnimation : Animation{
+ class BouncingAnimation : public Animation{
private:
bool up_;
public:
@@ -42,7 +43,7 @@ namespace Graphics{
BouncingAnimation(const sf::Texture& texture, int count, int
switchTime, int height, int width, int row);
~BouncingAnimation();
- sf::IntRect& next(int deltaTime);
+ sf::IntRect& next(int deltaTime) override;
};
}
#endif // GRAPHICS_ANIMATION_H
diff --git a/src/main.cpp b/src/main.cpp
index 38d1e06..c72d597 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -21,10 +21,10 @@ class Unit: Entity{
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;
+ Graphics::OneShotAnimation shooting_animation;
+ Graphics::OneShotAnimation complaining_animation;
+ Graphics::OneShotAnimation dying_animation;
+ Graphics::Animation* current_animation = &standing_animation;
sf::Sprite sprite;
sf::Vector2f position;
@@ -36,9 +36,9 @@ class Unit: Entity{
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);
+ shooting_animation = Graphics::OneShotAnimation(spritesheet_, 6, 60, 44, 48, 2);
+ complaining_animation = Graphics::OneShotAnimation(spritesheet_, 1, 60, 44, 48, 3);
+ dying_animation = Graphics::OneShotAnimation(spritesheet_, 14, 60, 44, 48, 4);
position = sf::Vector2f{0,0};
speed = sf::Vector2f{0,0};
@@ -46,16 +46,23 @@ class Unit: Entity{
void walk(float vx, float vy){
speed.y = vy;
speed.x = vx;
- current_animation.reset();
- current_animation = walking_animation;
- walking_animation.reset();
+ current_animation->reset();
+ current_animation = &walking_animation;
+ }
+ void shoot(){
+ 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.setTextureRect(current_animation->next(dt));
sprite.setPosition(position.x, position.y);
}
};
@@ -83,7 +90,7 @@ int main()
}
if(elapsed_time > 1000 && once){
- unit.walk(0.3, 0);
+ unit.shoot();
once = false;
}