summary refs log tree commit diff stats
path: root/src/systems
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-02-08 12:34:42 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-02-08 12:34:42 -0500
commitcefe66cdbb8786dc455657376e36f0ff8785d5bc (patch)
tree7e90536fad90f2954b3834dc4959f402883c32b3 /src/systems
parentcec0ed92c4035c4421d3cc2448f5423fcbb7f7d4 (diff)
downloadtherapy-cefe66cdbb8786dc455657376e36f0ff8785d5bc.tar.gz
therapy-cefe66cdbb8786dc455657376e36f0ff8785d5bc.tar.bz2
therapy-cefe66cdbb8786dc455657376e36f0ff8785d5bc.zip
Introduced animated sprites
Also restyled a lot of the code.
Diffstat (limited to 'src/systems')
-rw-r--r--src/systems/animating.cpp38
-rw-r--r--src/systems/animating.h20
-rw-r--r--src/systems/controlling.cpp138
-rw-r--r--src/systems/controlling.h36
-rw-r--r--src/systems/pondering.cpp10
-rw-r--r--src/systems/pondering.h10
-rw-r--r--src/systems/rendering.cpp40
-rw-r--r--src/systems/rendering.h15
8 files changed, 217 insertions, 90 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
5void 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
31void 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}
diff --git a/src/systems/animating.h b/src/systems/animating.h new file mode 100644 index 0000000..150a74a --- /dev/null +++ b/src/systems/animating.h
@@ -0,0 +1,20 @@
1#ifndef ANIMATING_H_5BBF0094
2#define ANIMATING_H_5BBF0094
3
4#include "system.h"
5#include <string>
6
7class AnimatingSystem : public System {
8public:
9
10 AnimatingSystem(Game& game) : System(game)
11 {
12 }
13
14 void tick(double dt);
15
16 void startAnimation(id_type entity, std::string animation);
17
18};
19
20#endif /* end of include guard: ANIMATING_H_5BBF0094 */
diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index ec62e9a..3647ff8 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp
@@ -4,21 +4,30 @@
4#include "components/ponderable.h" 4#include "components/ponderable.h"
5#include "components/animatable.h" 5#include "components/animatable.h"
6#include "components/droppable.h" 6#include "components/droppable.h"
7#include "components/orientable.h"
8#include "systems/animating.h"
7#include "direction.h" 9#include "direction.h"
8#include "muxer.h" 10#include "muxer.h"
9#include "consts.h" 11#include "consts.h"
10 12
11void ControllingSystem::tick(double dt) 13void ControllingSystem::tick(double)
12{ 14{
13 while (!actions.empty()) 15 while (!actions_.empty())
14 { 16 {
15 int key = actions.front().first; 17 int key = actions_.front().first;
16 int action = actions.front().second; 18 int action = actions_.front().second;
19
20 auto entities = game_.getEntityManager().getEntitiesWithComponents<
21 ControllableComponent,
22 PonderableComponent,
23 AnimatableComponent,
24 DroppableComponent,
25 OrientableComponent>();
17 26
18 auto entities = game.getEntityManager().getEntitiesWithComponents<ControllableComponent, PonderableComponent, AnimatableComponent, DroppableComponent>();
19 for (auto entity : entities) 27 for (auto entity : entities)
20 { 28 {
21 auto& controllable = game.getEntityManager().getComponent<ControllableComponent>(entity); 29 auto& controllable = game_.getEntityManager().
30 getComponent<ControllableComponent>(entity);
22 31
23 if (action == GLFW_PRESS) 32 if (action == GLFW_PRESS)
24 { 33 {
@@ -95,74 +104,107 @@ void ControllingSystem::tick(double dt)
95 } 104 }
96 } 105 }
97 106
98 actions.pop(); 107 actions_.pop();
99 } 108 }
100} 109}
101 110
102void ControllingSystem::input(int key, int action) 111void ControllingSystem::input(int key, int action)
103{ 112{
104 actions.push(std::make_pair(key, action)); 113 actions_.push(std::make_pair(key, action));
105} 114}
106 115
107void ControllingSystem::walkLeft(int entity) 116void ControllingSystem::walkLeft(id_type entity)
108{ 117{
109 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); 118 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
110 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); 119 auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity);
111 120
121 orientable.setFacingRight(false);
112 ponderable.setVelocityX(-90); 122 ponderable.setVelocityX(-90);
113 123
114 animatable.setDirection(Direction::Left); 124 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
115 animatable.setWalking(true); 125
126 if (ponderable.getState() == PonderableComponent::state::grounded)
127 {
128 animating.startAnimation(entity, "walkingLeft");
129 } else {
130 animating.startAnimation(entity, "stillLeft");
131 }
116} 132}
117 133
118void ControllingSystem::walkRight(int entity) 134void ControllingSystem::walkRight(id_type entity)
119{ 135{
120 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); 136 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
121 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); 137 auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity);
122 138
139 orientable.setFacingRight(true);
123 ponderable.setVelocityX(90); 140 ponderable.setVelocityX(90);
124 141
125 animatable.setDirection(Direction::Right); 142 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
126 animatable.setWalking(true); 143
144 if (ponderable.getState() == PonderableComponent::state::grounded)
145 {
146 animating.startAnimation(entity, "walkingRight");
147 } else {
148 animating.startAnimation(entity, "stillRight");
149 }
127} 150}
128 151
129void ControllingSystem::stopWalking(int entity) 152void ControllingSystem::stopWalking(id_type entity)
130{ 153{
131 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); 154 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
132 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); 155 auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity);
133 156
134 ponderable.setVelocityX(0); 157 ponderable.setVelocityX(0);
135 158
136 animatable.setWalking(false); 159 if (ponderable.getState() == PonderableComponent::state::grounded)
160 {
161 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
162
163 if (orientable.isFacingRight())
164 {
165 animating.startAnimation(entity, "stillRight");
166 } else {
167 animating.startAnimation(entity, "stillLeft");
168 }
169 }
137} 170}
138 171
139void ControllingSystem::jump(int entity) 172void ControllingSystem::jump(id_type entity)
140{ 173{
141 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); 174 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
142 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); 175
143 176 if (ponderable.getState() == PonderableComponent::state::grounded)
144 playSound("res/Randomize87.wav", 0.25); 177 {
145 178 playSound("res/Randomize87.wav", 0.25);
146 ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); 179
147 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); 180 ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3));
148 181 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3));
149 animatable.setJumping(true); 182 ponderable.setState(PonderableComponent::state::jumping);
183 }
150} 184}
151 185
152void ControllingSystem::stopJumping(int entity) 186void ControllingSystem::stopJumping(id_type entity)
153{ 187{
154 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); 188 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
155 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); 189
156 190 if (ponderable.getState() == PonderableComponent::state::jumping)
157 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); 191 {
158 animatable.setJumping(false); 192 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233));
193 ponderable.setState(PonderableComponent::state::falling);
194 }
159} 195}
160 196
161void ControllingSystem::drop(int entity, bool start) 197void ControllingSystem::drop(id_type entity, bool start)
162{ 198{
163 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); 199 auto& droppable = game_.getEntityManager().getComponent<DroppableComponent>(entity);
164 auto& droppable = game.getEntityManager().getComponent<DroppableComponent>(entity); 200 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
165 201
202 if (start && (ponderable.getState() == PonderableComponent::state::grounded))
203 {
204 ponderable.setState(PonderableComponent::state::dropping);
205 } else if ((!start) && (ponderable.getState() == PonderableComponent::state::dropping))
206 {
207 ponderable.setState(PonderableComponent::state::grounded);
208 }
166 droppable.setDroppable(start); 209 droppable.setDroppable(start);
167 animatable.setCrouching(start);
168} 210}
diff --git a/src/systems/controlling.h b/src/systems/controlling.h index 30210b3..1f1e8a0 100644 --- a/src/systems/controlling.h +++ b/src/systems/controlling.h
@@ -3,24 +3,28 @@
3 3
4#include "system.h" 4#include "system.h"
5#include <queue> 5#include <queue>
6#include "entity_manager.h"
6 7
7class ControllingSystem : public System { 8class ControllingSystem : public System {
8 public: 9public:
9 ControllingSystem(Game& game) 10
10 : System(game) {} 11 ControllingSystem(Game& game) : System(game)
11 12 {
12 void tick(double dt); 13 }
13 void input(int key, int action); 14
14 15 void tick(double dt);
15 private: 16 void input(int key, int action);
16 void walkLeft(int entity); 17
17 void walkRight(int entity); 18private:
18 void stopWalking(int entity); 19
19 void jump(int entity); 20 void walkLeft(id_type entity);
20 void stopJumping(int entity); 21 void walkRight(id_type entity);
21 void drop(int entity, bool start); 22 void stopWalking(id_type entity);
22 23 void jump(id_type entity);
23 std::queue<std::pair<int,int>> actions; 24 void stopJumping(id_type entity);
25 void drop(id_type entity, bool start);
26
27 std::queue<std::pair<int,int>> actions_;
24}; 28};
25 29
26#endif /* end of include guard: CONTROLLING_H_80B1BB8D */ 30#endif /* end of include guard: CONTROLLING_H_80B1BB8D */
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 50a8bc8..e40db1d 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp
@@ -5,12 +5,14 @@
5 5
6void PonderingSystem::tick(double dt) 6void PonderingSystem::tick(double dt)
7{ 7{
8 auto entities = game.getEntityManager().getEntitiesWithComponents<PonderableComponent, TransformableComponent>(); 8 auto entities = game_.getEntityManager().getEntitiesWithComponents<
9 PonderableComponent,
10 TransformableComponent>();
9 11
10 for (auto entity : entities) 12 for (id_type entity : entities)
11 { 13 {
12 auto& transformable = game.getEntityManager().getComponent<TransformableComponent>(entity); 14 auto& transformable = game_.getEntityManager().getComponent<TransformableComponent>(entity);
13 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); 15 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
14 16
15 // Accelerate 17 // Accelerate
16 ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt); 18 ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt);
diff --git a/src/systems/pondering.h b/src/systems/pondering.h index 3fe5473..44e7600 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h
@@ -4,11 +4,13 @@
4#include "system.h" 4#include "system.h"
5 5
6class PonderingSystem : public System { 6class PonderingSystem : public System {
7 public: 7public:
8 PonderingSystem(Game& game)
9 : System(game) {}
10 8
11 void tick(double dt); 9 PonderingSystem(Game& game) : System(game)
10 {
11 }
12
13 void tick(double dt);
12}; 14};
13 15
14#endif /* end of include guard: PONDERING_H_F2530E0E */ 16#endif /* end of include guard: PONDERING_H_F2530E0E */
diff --git a/src/systems/rendering.cpp b/src/systems/rendering.cpp index 251c2bc..8219732 100644 --- a/src/systems/rendering.cpp +++ b/src/systems/rendering.cpp
@@ -3,19 +3,35 @@
3#include "components/animatable.h" 3#include "components/animatable.h"
4#include "components/transformable.h" 4#include "components/transformable.h"
5 5
6void RenderingSystem::tick(double dt) 6void RenderingSystem::tick(double)
7{ 7{
8 texture.fill(texture.entirety(), 0, 0, 0); 8 texture_.fill(texture_.entirety(), 0, 0, 0);
9 9
10 std::set<int> spriteEntities = game.getEntityManager().getEntitiesWithComponents<AnimatableComponent, TransformableComponent>(); 10 std::set<id_type> spriteEntities =
11 for (int entity : spriteEntities) 11 game_.getEntityManager().getEntitiesWithComponents<
12 AnimatableComponent,
13 TransformableComponent>();
14
15 for (id_type entity : spriteEntities)
12 { 16 {
13 auto& sprite = game.getEntityManager().getComponent<AnimatableComponent>(entity); 17 auto& sprite = game_.getEntityManager().
14 auto& transform = game.getEntityManager().getComponent<TransformableComponent>(entity); 18 getComponent<AnimatableComponent>(entity);
15 Rectangle dstrect {(int) transform.getX(), (int) transform.getY(), transform.getW(), transform.getH()}; 19
16 20 auto& transform = game_.getEntityManager().
17 texture.blit(sprite.getTexture(), sprite.getFrameRect(), dstrect); 21 getComponent<TransformableComponent>(entity);
22
23 Rectangle dstrect {
24 static_cast<int>(transform.getX()),
25 static_cast<int>(transform.getY()),
26 transform.getW(),
27 transform.getH()};
28
29 const AnimationSet& aset = sprite.getAnimationSet();
30 texture_.blit(
31 aset.getTexture(),
32 aset.getFrameRect(sprite.getFrame()),
33 dstrect);
18 } 34 }
19 35
20 texture.renderScreen(); 36 texture_.renderScreen();
21} 37}
diff --git a/src/systems/rendering.h b/src/systems/rendering.h index cec72e2..a53ee64 100644 --- a/src/systems/rendering.h +++ b/src/systems/rendering.h
@@ -6,14 +6,17 @@
6#include "consts.h" 6#include "consts.h"
7 7
8class RenderingSystem : public System { 8class RenderingSystem : public System {
9 public: 9public:
10 RenderingSystem(Game& game)
11 : System(game) {}
12 10
13 void tick(double dt); 11 RenderingSystem(Game& game) : System(game)
12 {
13 }
14 14
15 private: 15 void tick(double dt);
16 Texture texture {GAME_WIDTH, GAME_HEIGHT}; 16
17private:
18
19 Texture texture_ {GAME_WIDTH, GAME_HEIGHT};
17}; 20};
18 21
19#endif /* end of include guard: RENDERING_H_76ABC02A */ 22#endif /* end of include guard: RENDERING_H_76ABC02A */