summary refs log tree commit diff stats
path: root/src/systems
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-02-18 15:25:52 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-02-18 15:25:52 -0500
commite4e2f2d2a7b6a282b9618aa0004d9453936f43c6 (patch)
treede1653dc8b5992420147f28d9fb4de052ea1845f /src/systems
parente16fb5be90c889c371cbb0ca2444735c2e12073c (diff)
downloadtherapy-e4e2f2d2a7b6a282b9618aa0004d9453936f43c6.tar.gz
therapy-e4e2f2d2a7b6a282b9618aa0004d9453936f43c6.tar.bz2
therapy-e4e2f2d2a7b6a282b9618aa0004d9453936f43c6.zip
Added player death and event scheduling
Also added ability to make sprites flicker, to freeze physics for an entity, and to freeze progression of a sprite's animation loop.
Diffstat (limited to 'src/systems')
-rw-r--r--src/systems/animating.cpp19
-rw-r--r--src/systems/controlling.cpp29
-rw-r--r--src/systems/controlling.h5
-rw-r--r--src/systems/mapping.cpp21
-rw-r--r--src/systems/orienting.cpp18
-rw-r--r--src/systems/playing.cpp85
-rw-r--r--src/systems/playing.h9
-rw-r--r--src/systems/pondering.cpp16
-rw-r--r--src/systems/scheduling.cpp54
-rw-r--r--src/systems/scheduling.h22
10 files changed, 259 insertions, 19 deletions
diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp index 22224c9..634af67 100644 --- a/src/systems/animating.cpp +++ b/src/systems/animating.cpp
@@ -13,7 +13,10 @@ void AnimatingSystem::tick(double)
13 auto& sprite = game_.getEntityManager(). 13 auto& sprite = game_.getEntityManager().
14 getComponent<AnimatableComponent>(entity); 14 getComponent<AnimatableComponent>(entity);
15 15
16 sprite.setCountdown(sprite.getCountdown() + 1); 16 if (!sprite.isFrozen())
17 {
18 sprite.setCountdown(sprite.getCountdown() + 1);
19 }
17 20
18 const Animation& anim = sprite.getAnimation(); 21 const Animation& anim = sprite.getAnimation();
19 if (sprite.getCountdown() >= anim.getDelay()) 22 if (sprite.getCountdown() >= anim.getDelay())
@@ -26,6 +29,11 @@ void AnimatingSystem::tick(double)
26 sprite.setFrame(anim.getFirstFrame()); 29 sprite.setFrame(anim.getFirstFrame());
27 } 30 }
28 } 31 }
32
33 if (sprite.isFlickering())
34 {
35 sprite.setFlickerTimer((sprite.getFlickerTimer() + 1) % 6);
36 }
29 } 37 }
30} 38}
31 39
@@ -44,6 +52,12 @@ void AnimatingSystem::render(Texture& texture)
44 auto& transform = game_.getEntityManager(). 52 auto& transform = game_.getEntityManager().
45 getComponent<TransformableComponent>(entity); 53 getComponent<TransformableComponent>(entity);
46 54
55 double alpha = 1.0;
56 if (sprite.isFlickering() && (sprite.getFlickerTimer() < 3))
57 {
58 alpha = 0.0;
59 }
60
47 Rectangle dstrect { 61 Rectangle dstrect {
48 static_cast<int>(transform.getX()), 62 static_cast<int>(transform.getX()),
49 static_cast<int>(transform.getY()), 63 static_cast<int>(transform.getY()),
@@ -55,7 +69,8 @@ void AnimatingSystem::render(Texture& texture)
55 aset.getTexture(), 69 aset.getTexture(),
56 texture, 70 texture,
57 aset.getFrameRect(sprite.getFrame()), 71 aset.getFrameRect(sprite.getFrame()),
58 dstrect); 72 dstrect,
73 alpha);
59 } 74 }
60} 75}
61 76
diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index e1609bd..e65c09a 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp
@@ -105,3 +105,32 @@ void ControllingSystem::input(int key, int action)
105{ 105{
106 actions_.push(std::make_pair(key, action)); 106 actions_.push(std::make_pair(key, action));
107} 107}
108
109void ControllingSystem::freeze(id_type entity)
110{
111 auto& controllable = game_.getEntityManager().
112 getComponent<ControllableComponent>(entity);
113
114 controllable.setFrozen(true);
115}
116
117void ControllingSystem::unfreeze(id_type entity)
118{
119 auto& controllable = game_.getEntityManager().
120 getComponent<ControllableComponent>(entity);
121
122 if (controllable.isFrozen())
123 {
124 controllable.setFrozen(false);
125
126 auto& orienting = game_.getSystemManager().getSystem<OrientingSystem>();
127
128 if (controllable.isHoldingLeft())
129 {
130 orienting.moveLeft(entity);
131 } else if (controllable.isHoldingRight())
132 {
133 orienting.moveRight(entity);
134 }
135 }
136}
diff --git a/src/systems/controlling.h b/src/systems/controlling.h index 01ed7a0..d6f0789 100644 --- a/src/systems/controlling.h +++ b/src/systems/controlling.h
@@ -12,8 +12,13 @@ public:
12 } 12 }
13 13
14 void tick(double dt); 14 void tick(double dt);
15
15 void input(int key, int action); 16 void input(int key, int action);
16 17
18 void freeze(id_type entity);
19
20 void unfreeze(id_type entity);
21
17private: 22private:
18 23
19 std::queue<std::pair<int,int>> actions_; 24 std::queue<std::pair<int,int>> actions_;
diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp index 05167c1..a3a17ec 100644 --- a/src/systems/mapping.cpp +++ b/src/systems/mapping.cpp
@@ -167,11 +167,32 @@ void MappingSystem::loadMap(size_t mapId)
167 } else if (tile == 42) 167 } else if (tile == 42)
168 { 168 {
169 addBoundary( 169 addBoundary(
170 mappable.getRightBoundaries(),
171 x * TILE_WIDTH,
172 y * TILE_HEIGHT,
173 (y+1) * TILE_HEIGHT,
174 MappableComponent::Boundary::Type::danger);
175
176 addBoundary(
177 mappable.getLeftBoundaries(),
178 (x+1) * TILE_WIDTH,
179 y * TILE_HEIGHT,
180 (y+1) * TILE_HEIGHT,
181 MappableComponent::Boundary::Type::danger);
182
183 addBoundary(
170 mappable.getDownBoundaries(), 184 mappable.getDownBoundaries(),
171 y * TILE_HEIGHT, 185 y * TILE_HEIGHT,
172 x * TILE_WIDTH, 186 x * TILE_WIDTH,
173 (x+1) * TILE_WIDTH, 187 (x+1) * TILE_WIDTH,
174 MappableComponent::Boundary::Type::danger); 188 MappableComponent::Boundary::Type::danger);
189
190 addBoundary(
191 mappable.getUpBoundaries(),
192 (y+1) * TILE_HEIGHT,
193 x * TILE_WIDTH,
194 (x+1) * TILE_WIDTH,
195 MappableComponent::Boundary::Type::danger);
175 } 196 }
176 } 197 }
177} 198}
diff --git a/src/systems/orienting.cpp b/src/systems/orienting.cpp index 187bebc..2df8f87 100644 --- a/src/systems/orienting.cpp +++ b/src/systems/orienting.cpp
@@ -93,24 +93,18 @@ void OrientingSystem::moveRight(id_type entity)
93 93
94void OrientingSystem::stopWalking(id_type entity) 94void OrientingSystem::stopWalking(id_type entity)
95{ 95{
96 auto& ponderable = game_.getEntityManager().
97 getComponent<PonderableComponent>(entity);
98
99 auto& orientable = game_.getEntityManager(). 96 auto& orientable = game_.getEntityManager().
100 getComponent<OrientableComponent>(entity); 97 getComponent<OrientableComponent>(entity);
101 98
102 orientable.setWalkState(OrientableComponent::WalkState::still); 99 orientable.setWalkState(OrientableComponent::WalkState::still);
103 100
104 if (ponderable.isGrounded()) 101 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
105 {
106 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
107 102
108 if (orientable.isFacingRight()) 103 if (orientable.isFacingRight())
109 { 104 {
110 animating.startAnimation(entity, "stillRight"); 105 animating.startAnimation(entity, "stillRight");
111 } else { 106 } else {
112 animating.startAnimation(entity, "stillLeft"); 107 animating.startAnimation(entity, "stillLeft");
113 }
114 } 108 }
115} 109}
116 110
diff --git a/src/systems/playing.cpp b/src/systems/playing.cpp index 2c6a419..40d9706 100644 --- a/src/systems/playing.cpp +++ b/src/systems/playing.cpp
@@ -7,13 +7,18 @@
7#include "components/orientable.h" 7#include "components/orientable.h"
8#include "systems/mapping.h" 8#include "systems/mapping.h"
9#include "systems/pondering.h" 9#include "systems/pondering.h"
10#include "systems/orienting.h"
11#include "systems/scheduling.h"
12#include "systems/controlling.h"
10#include "animation.h" 13#include "animation.h"
14#include "muxer.h"
11 15
12void PlayingSystem::tick(double) 16void PlayingSystem::tick(double)
13{ 17{
14 // Check if we need to change the map 18 // Check if we need to change the map
15 auto players = game_.getEntityManager().getEntitiesWithComponents< 19 auto players = game_.getEntityManager().getEntitiesWithComponents<
16 PlayableComponent>(); 20 PlayableComponent,
21 TransformableComponent>();
17 22
18 for (id_type player : players) 23 for (id_type player : players)
19 { 24 {
@@ -44,6 +49,12 @@ void PlayingSystem::tick(double)
44 49
45 playable.changingMap = false; 50 playable.changingMap = false;
46 51
52 if (playable.newMapCallback)
53 {
54 playable.newMapCallback();
55 playable.newMapCallback = nullptr;
56 }
57
47 break; 58 break;
48 } 59 }
49 } 60 }
@@ -66,7 +77,10 @@ void PlayingSystem::initPlayer()
66 77
67 game_.getEntityManager().emplaceComponent<TransformableComponent>( 78 game_.getEntityManager().emplaceComponent<TransformableComponent>(
68 player, 79 player,
69 203, 44, 10, 12); 80 game_.getWorld().getStartingX(),
81 game_.getWorld().getStartingY(),
82 10,
83 12);
70 84
71 game_.getSystemManager().getSystem<PonderingSystem>().initializeBody( 85 game_.getSystemManager().getSystem<PonderingSystem>().initializeBody(
72 player, 86 player,
@@ -74,13 +88,20 @@ void PlayingSystem::initPlayer()
74 88
75 game_.getEntityManager().emplaceComponent<ControllableComponent>(player); 89 game_.getEntityManager().emplaceComponent<ControllableComponent>(player);
76 game_.getEntityManager().emplaceComponent<OrientableComponent>(player); 90 game_.getEntityManager().emplaceComponent<OrientableComponent>(player);
77 game_.getEntityManager().emplaceComponent<PlayableComponent>(player); 91
92 auto& playable = game_.getEntityManager().
93 emplaceComponent<PlayableComponent>(player);
94
95 playable.checkpointMapId = game_.getWorld().getStartingMapId();
96 playable.checkpointX = game_.getWorld().getStartingX();
97 playable.checkpointY = game_.getWorld().getStartingY();
78} 98}
79 99
80void PlayingSystem::changeMap( 100void PlayingSystem::changeMap(
81 size_t mapId, 101 size_t mapId,
82 double x, 102 double x,
83 double y) 103 double y,
104 PlayableComponent::MapChangeCallback callback)
84{ 105{
85 auto players = game_.getEntityManager().getEntitiesWithComponents< 106 auto players = game_.getEntityManager().getEntitiesWithComponents<
86 PlayableComponent>(); 107 PlayableComponent>();
@@ -94,5 +115,61 @@ void PlayingSystem::changeMap(
94 playable.newMapId = mapId; 115 playable.newMapId = mapId;
95 playable.newMapX = x; 116 playable.newMapX = x;
96 playable.newMapY = y; 117 playable.newMapY = y;
118 playable.newMapCallback = std::move(callback);
119 }
120}
121
122void PlayingSystem::die()
123{
124 playSound("res/Hit_Hurt5.wav", 0.25);
125
126 auto players = game_.getEntityManager().getEntitiesWithComponents<
127 OrientableComponent,
128 ControllableComponent,
129 AnimatableComponent,
130 PonderableComponent,
131 PlayableComponent>();
132
133 for (id_type player : players)
134 {
135 auto& animatable = game_.getEntityManager().
136 getComponent<AnimatableComponent>(player);
137
138 auto& ponderable = game_.getEntityManager().
139 getComponent<PonderableComponent>(player);
140
141 auto& controlling = game_.getSystemManager().getSystem<ControllingSystem>();
142 controlling.freeze(player);
143
144 animatable.setFrozen(true);
145 animatable.setFlickering(true);
146 ponderable.setFrozen(true);
147 ponderable.setCollidable(false);
148
149 auto& scheduling = game_.getSystemManager().getSystem<SchedulingSystem>();
150
151 scheduling.schedule(player, 0.75, [&] (id_type player) {
152 auto& playable = game_.getEntityManager().
153 getComponent<PlayableComponent>(player);
154
155 changeMap(
156 playable.checkpointMapId,
157 playable.checkpointX,
158 playable.checkpointY,
159 [&, player] () {
160 animatable.setFrozen(false);
161 animatable.setFlickering(false);
162 ponderable.setFrozen(false);
163 ponderable.setCollidable(true);
164
165 // Reset the walk state, and then potentially let the
166 // ControllingSystem set it again.
167 auto& orienting = game_.getSystemManager().
168 getSystem<OrientingSystem>();
169 orienting.stopWalking(player);
170
171 controlling.unfreeze(player);
172 });
173 });
97 } 174 }
98} 175}
diff --git a/src/systems/playing.h b/src/systems/playing.h index c98a464..ff16808 100644 --- a/src/systems/playing.h +++ b/src/systems/playing.h
@@ -2,6 +2,7 @@
2#define PLAYING_H_70A54F7D 2#define PLAYING_H_70A54F7D
3 3
4#include "system.h" 4#include "system.h"
5#include "components/playable.h"
5 6
6class PlayingSystem : public System { 7class PlayingSystem : public System {
7public: 8public:
@@ -14,7 +15,13 @@ public:
14 15
15 void initPlayer(); 16 void initPlayer();
16 17
17 void changeMap(size_t mapId, double x, double y); 18 void changeMap(
19 size_t mapId,
20 double x,
21 double y,
22 PlayableComponent::MapChangeCallback callback = nullptr);
23
24 void die();
18 25
19}; 26};
20 27
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 2490dc9..d3601ac 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp
@@ -27,6 +27,11 @@ void PonderingSystem::tick(double dt)
27 auto& ponderable = game_.getEntityManager(). 27 auto& ponderable = game_.getEntityManager().
28 getComponent<PonderableComponent>(entity); 28 getComponent<PonderableComponent>(entity);
29 29
30 if (ponderable.isFrozen())
31 {
32 continue;
33 }
34
30 // Accelerate 35 // Accelerate
31 ponderable.setVelocityX( 36 ponderable.setVelocityX(
32 ponderable.getVelocityX() + ponderable.getAccelX() * dt); 37 ponderable.getVelocityX() + ponderable.getAccelX() * dt);
@@ -289,6 +294,17 @@ void PonderingSystem::tick(double dt)
289 break; 294 break;
290 } 295 }
291 } 296 }
297
298 break;
299 }
300
301 case Collision::Type::danger:
302 {
303 game_.getSystemManager().getSystem<PlayingSystem>().die();
304
305 stopProcessing = true;
306
307 break;
292 } 308 }
293 309
294 default: 310 default:
diff --git a/src/systems/scheduling.cpp b/src/systems/scheduling.cpp new file mode 100644 index 0000000..220efae --- /dev/null +++ b/src/systems/scheduling.cpp
@@ -0,0 +1,54 @@
1#include "scheduling.h"
2#include "game.h"
3#include "components/schedulable.h"
4#include "util.h"
5
6void SchedulingSystem::tick(double dt)
7{
8 auto entities = game_.getEntityManager().getEntitiesWithComponents<
9 SchedulableComponent>();
10
11 for (id_type entity : entities)
12 {
13 auto& schedulable = game_.getEntityManager().
14 getComponent<SchedulableComponent>(entity);
15
16 for (auto& action : schedulable.actions)
17 {
18 std::get<0>(action) -= dt;
19
20 if (std::get<0>(action) < 0)
21 {
22 std::get<1>(action)(entity);
23 }
24 }
25
26 erase_if(schedulable.actions,
27 [] (const SchedulableComponent::Action& action) {
28 return (std::get<0>(action) < 0);
29 });
30
31 if (schedulable.actions.empty())
32 {
33 game_.getEntityManager().removeComponent<SchedulableComponent>(entity);
34 }
35 }
36}
37
38void SchedulingSystem::schedule(
39 id_type entity,
40 double length,
41 std::function<void(id_type)> action)
42{
43 if (!game_.getEntityManager().hasComponent<SchedulableComponent>(entity))
44 {
45 game_.getEntityManager().emplaceComponent<SchedulableComponent>(entity);
46 }
47
48 auto& schedulable = game_.getEntityManager().
49 getComponent<SchedulableComponent>(entity);
50
51 schedulable.actions.emplace_back(
52 length,
53 std::move(action));
54}
diff --git a/src/systems/scheduling.h b/src/systems/scheduling.h new file mode 100644 index 0000000..7950d62 --- /dev/null +++ b/src/systems/scheduling.h
@@ -0,0 +1,22 @@
1#ifndef SCHEDULING_H_7B02E3E3
2#define SCHEDULING_H_7B02E3E3
3
4#include "system.h"
5
6class SchedulingSystem : public System {
7public:
8
9 SchedulingSystem(Game& game) : System(game)
10 {
11 }
12
13 void tick(double dt);
14
15 void schedule(
16 id_type entity,
17 double length,
18 std::function<void(id_type)> action);
19
20};
21
22#endif /* end of include guard: SCHEDULING_H_7B02E3E3 */