From cefe66cdbb8786dc455657376e36f0ff8785d5bc Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 8 Feb 2018 12:34:42 -0500 Subject: Introduced animated sprites Also restyled a lot of the code. --- src/systems/animating.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/systems/animating.cpp (limited to 'src/systems/animating.cpp') diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp new file mode 100644 index 0000000..fcbfca5 --- /dev/null +++ b/src/systems/animating.cpp @@ -0,0 +1,38 @@ +#include "animating.h" +#include "game.h" +#include "components/animatable.h" + +void AnimatingSystem::tick(double) +{ + std::set spriteEntities = + game_.getEntityManager().getEntitiesWithComponents(); + + for (id_type entity : spriteEntities) + { + auto& sprite = game_.getEntityManager(). + getComponent(entity); + + sprite.setCountdown(sprite.getCountdown() + 1); + + const Animation& anim = sprite.getAnimation(); + if (sprite.getCountdown() >= anim.getDelay()) + { + sprite.setFrame(sprite.getFrame() + 1); + sprite.setCountdown(0); + + if (sprite.getFrame() >= anim.getFirstFrame() + anim.getNumFrames()) + { + sprite.setFrame(anim.getFirstFrame()); + } + } + } +} + +void AnimatingSystem::startAnimation(id_type entity, std::string animation) +{ + auto& sprite = game_.getEntityManager(). + getComponent(entity); + + sprite.setAnimation(animation); + sprite.setFrame(sprite.getAnimation().getFirstFrame()); +} -- cgit 1.4.1