diff options
Diffstat (limited to 'src/systems/animating.cpp')
-rw-r--r-- | src/systems/animating.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp new file mode 100644 index 0000000..50a32fc --- /dev/null +++ b/src/systems/animating.cpp | |||
@@ -0,0 +1,103 @@ | |||
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 | if (sprite.active) | ||
17 | { | ||
18 | if (!sprite.frozen) | ||
19 | { | ||
20 | sprite.countdown++; | ||
21 | } | ||
22 | |||
23 | const Animation& anim = sprite.getAnimation(); | ||
24 | if (sprite.countdown >= anim.getDelay()) | ||
25 | { | ||
26 | sprite.frame++; | ||
27 | sprite.countdown = 0; | ||
28 | |||
29 | if (sprite.frame >= anim.getFirstFrame() + anim.getNumFrames()) | ||
30 | { | ||
31 | sprite.frame = anim.getFirstFrame(); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | if (sprite.flickering) | ||
36 | { | ||
37 | sprite.flickerTimer = (sprite.flickerTimer + 1) % 6; | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | |||
43 | void AnimatingSystem::render(Texture& texture) | ||
44 | { | ||
45 | std::set<id_type> spriteEntities = | ||
46 | game_.getEntityManager().getEntitiesWithComponents< | ||
47 | AnimatableComponent, | ||
48 | TransformableComponent>(); | ||
49 | |||
50 | for (id_type entity : spriteEntities) | ||
51 | { | ||
52 | auto& sprite = game_.getEntityManager(). | ||
53 | getComponent<AnimatableComponent>(entity); | ||
54 | |||
55 | if (sprite.active) | ||
56 | { | ||
57 | auto& transform = game_.getEntityManager(). | ||
58 | getComponent<TransformableComponent>(entity); | ||
59 | |||
60 | double alpha = 1.0; | ||
61 | if (sprite.flickering && (sprite.flickerTimer < 3)) | ||
62 | { | ||
63 | alpha = 0.0; | ||
64 | } | ||
65 | |||
66 | Rectangle dstrect { | ||
67 | static_cast<int>(transform.pos.x()), | ||
68 | static_cast<int>(transform.pos.y()), | ||
69 | transform.size.w(), | ||
70 | transform.size.h()}; | ||
71 | |||
72 | const AnimationSet& aset = sprite.animationSet; | ||
73 | game_.getRenderer().blit( | ||
74 | aset.getTexture(), | ||
75 | texture, | ||
76 | aset.getFrameRect(sprite.frame), | ||
77 | dstrect, | ||
78 | alpha); | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | void AnimatingSystem::initPrototype(id_type entity) | ||
84 | { | ||
85 | auto& sprite = game_.getEntityManager(). | ||
86 | getComponent<AnimatableComponent>(entity); | ||
87 | |||
88 | startAnimation(entity, sprite.origAnimation); | ||
89 | |||
90 | sprite.countdown = 0; | ||
91 | sprite.flickering = false; | ||
92 | sprite.flickerTimer = 0; | ||
93 | sprite.frozen = false; | ||
94 | } | ||
95 | |||
96 | void AnimatingSystem::startAnimation(id_type entity, std::string animation) | ||
97 | { | ||
98 | auto& sprite = game_.getEntityManager(). | ||
99 | getComponent<AnimatableComponent>(entity); | ||
100 | |||
101 | sprite.animation = std::move(animation); | ||
102 | sprite.frame = sprite.getAnimation().getFirstFrame(); | ||
103 | } | ||