diff options
Diffstat (limited to 'src/systems/animating.cpp')
-rw-r--r-- | src/systems/animating.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp new file mode 100644 index 0000000..91fe925 --- /dev/null +++ b/src/systems/animating.cpp | |||
@@ -0,0 +1,68 @@ | |||
1 | #include "animating.h" | ||
2 | #include "game.h" | ||
3 | #include "components/animatable.h" | ||
4 | #include "components/transformable.h" | ||
5 | |||
6 | void AnimatingSystem::tick(double) | ||
7 | { | ||
8 | std::set<id_type> spriteEntities = | ||
9 | game_.getEntityManager().getEntitiesWithComponents<AnimatableComponent>(); | ||
10 | |||
11 | for (id_type entity : spriteEntities) | ||
12 | { | ||
13 | auto& sprite = game_.getEntityManager(). | ||
14 | getComponent<AnimatableComponent>(entity); | ||
15 | |||
16 | sprite.setCountdown(sprite.getCountdown() + 1); | ||
17 | |||
18 | const Animation& anim = sprite.getAnimation(); | ||
19 | if (sprite.getCountdown() >= anim.getDelay()) | ||
20 | { | ||
21 | sprite.setFrame(sprite.getFrame() + 1); | ||
22 | sprite.setCountdown(0); | ||
23 | |||
24 | if (sprite.getFrame() >= anim.getFirstFrame() + anim.getNumFrames()) | ||
25 | { | ||
26 | sprite.setFrame(anim.getFirstFrame()); | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | |||
32 | void AnimatingSystem::render(Texture& texture) | ||
33 | { | ||
34 | std::set<id_type> spriteEntities = | ||
35 | game_.getEntityManager().getEntitiesWithComponents< | ||
36 | AnimatableComponent, | ||
37 | TransformableComponent>(); | ||
38 | |||
39 | for (id_type entity : spriteEntities) | ||
40 | { | ||
41 | auto& sprite = game_.getEntityManager(). | ||
42 | getComponent<AnimatableComponent>(entity); | ||
43 | |||
44 | auto& transform = game_.getEntityManager(). | ||
45 | getComponent<TransformableComponent>(entity); | ||
46 | |||
47 | Rectangle dstrect { | ||
48 | static_cast<int>(transform.getX()), | ||
49 | static_cast<int>(transform.getY()), | ||
50 | transform.getW(), | ||
51 | transform.getH()}; | ||
52 | |||
53 | const AnimationSet& aset = sprite.getAnimationSet(); | ||
54 | texture.blit( | ||
55 | aset.getTexture(), | ||
56 | aset.getFrameRect(sprite.getFrame()), | ||
57 | dstrect); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | void AnimatingSystem::startAnimation(id_type entity, std::string animation) | ||
62 | { | ||
63 | auto& sprite = game_.getEntityManager(). | ||
64 | getComponent<AnimatableComponent>(entity); | ||
65 | |||
66 | sprite.setAnimation(animation); | ||
67 | sprite.setFrame(sprite.getAnimation().getFirstFrame()); | ||
68 | } | ||