summary refs log tree commit diff stats
path: root/src/systems/animating.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-05-17 15:55:37 -0400
committerGitHub <noreply@github.com>2018-05-17 15:55:37 -0400
commit90aadf3844386824140a20d7fbb847bc16009a94 (patch)
tree6f83fce90e71abb22b1a8f3e09c79963b2a34d5d /src/systems/animating.cpp
parentbc63fa57ced1c7329f7fdcfd168eaf7e290158bc (diff)
parent86f0106d0523825549f1e74b835688c78a10cf6c (diff)
downloadtherapy-90aadf3844386824140a20d7fbb847bc16009a94.tar.gz
therapy-90aadf3844386824140a20d7fbb847bc16009a94.tar.bz2
therapy-90aadf3844386824140a20d7fbb847bc16009a94.zip
Merge pull request #7 from hatkirby/es-rewrite
The ECS rewrite exceeds the original branch in functionality, so it is time to merge it in.
Diffstat (limited to 'src/systems/animating.cpp')
-rw-r--r--src/systems/animating.cpp103
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
6void 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
43void 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
83void 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
96void 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}