diff options
Diffstat (limited to 'src/systems/animating.cpp')
-rw-r--r-- | src/systems/animating.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
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 @@ | |||
1 | #include "animating.h" | ||
2 | #include "game.h" | ||
3 | #include "components/animatable.h" | ||
4 | |||
5 | void AnimatingSystem::tick(double) | ||
6 | { | ||
7 | std::set<id_type> spriteEntities = | ||
8 | game_.getEntityManager().getEntitiesWithComponents<AnimatableComponent>(); | ||
9 | |||
10 | for (id_type entity : spriteEntities) | ||
11 | { | ||
12 | auto& sprite = game_.getEntityManager(). | ||
13 | getComponent<AnimatableComponent>(entity); | ||
14 | |||
15 | sprite.setCountdown(sprite.getCountdown() + 1); | ||
16 | |||
17 | const Animation& anim = sprite.getAnimation(); | ||
18 | if (sprite.getCountdown() >= anim.getDelay()) | ||
19 | { | ||
20 | sprite.setFrame(sprite.getFrame() + 1); | ||
21 | sprite.setCountdown(0); | ||
22 | |||
23 | if (sprite.getFrame() >= anim.getFirstFrame() + anim.getNumFrames()) | ||
24 | { | ||
25 | sprite.setFrame(anim.getFirstFrame()); | ||
26 | } | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | |||
31 | void AnimatingSystem::startAnimation(id_type entity, std::string animation) | ||
32 | { | ||
33 | auto& sprite = game_.getEntityManager(). | ||
34 | getComponent<AnimatableComponent>(entity); | ||
35 | |||
36 | sprite.setAnimation(animation); | ||
37 | sprite.setFrame(sprite.getAnimation().getFirstFrame()); | ||
38 | } | ||