summary refs log tree commit diff stats
path: root/src/systems
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-02-11 12:34:52 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-02-11 12:34:52 -0500
commit77be863f4f15d2481a64e4e8dadb4060a6e4e590 (patch)
treeca571702d2148a75b5b847e77d26270257f54ebc /src/systems
parent1400ade977e13e3b535d3c2fddb6e15de3c9b5a5 (diff)
downloadtherapy-77be863f4f15d2481a64e4e8dadb4060a6e4e590.tar.gz
therapy-77be863f4f15d2481a64e4e8dadb4060a6e4e590.tar.bz2
therapy-77be863f4f15d2481a64e4e8dadb4060a6e4e590.zip
Implemented map rendering and basic collision
Only wall and platform collision currently works, and map edges are not currently implemented.
Diffstat (limited to 'src/systems')
-rw-r--r--src/systems/controlling.cpp22
-rw-r--r--src/systems/mapping.cpp143
-rw-r--r--src/systems/mapping.h19
-rw-r--r--src/systems/pondering.cpp248
-rw-r--r--src/systems/pondering.h16
5 files changed, 431 insertions, 17 deletions
diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index 3647ff8..fa09d11 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp
@@ -123,7 +123,7 @@ void ControllingSystem::walkLeft(id_type entity)
123 123
124 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); 124 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
125 125
126 if (ponderable.getState() == PonderableComponent::state::grounded) 126 if (ponderable.getState() == PonderableComponent::State::grounded)
127 { 127 {
128 animating.startAnimation(entity, "walkingLeft"); 128 animating.startAnimation(entity, "walkingLeft");
129 } else { 129 } else {
@@ -141,7 +141,7 @@ void ControllingSystem::walkRight(id_type entity)
141 141
142 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); 142 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
143 143
144 if (ponderable.getState() == PonderableComponent::state::grounded) 144 if (ponderable.getState() == PonderableComponent::State::grounded)
145 { 145 {
146 animating.startAnimation(entity, "walkingRight"); 146 animating.startAnimation(entity, "walkingRight");
147 } else { 147 } else {
@@ -156,7 +156,7 @@ void ControllingSystem::stopWalking(id_type entity)
156 156
157 ponderable.setVelocityX(0); 157 ponderable.setVelocityX(0);
158 158
159 if (ponderable.getState() == PonderableComponent::state::grounded) 159 if (ponderable.getState() == PonderableComponent::State::grounded)
160 { 160 {
161 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); 161 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
162 162
@@ -173,13 +173,13 @@ void ControllingSystem::jump(id_type entity)
173{ 173{
174 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); 174 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
175 175
176 if (ponderable.getState() == PonderableComponent::state::grounded) 176 if (ponderable.getState() == PonderableComponent::State::grounded)
177 { 177 {
178 playSound("res/Randomize87.wav", 0.25); 178 playSound("res/Randomize87.wav", 0.25);
179 179
180 ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); 180 ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3));
181 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); 181 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3));
182 ponderable.setState(PonderableComponent::state::jumping); 182 ponderable.setState(PonderableComponent::State::jumping);
183 } 183 }
184} 184}
185 185
@@ -187,10 +187,10 @@ void ControllingSystem::stopJumping(id_type entity)
187{ 187{
188 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); 188 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
189 189
190 if (ponderable.getState() == PonderableComponent::state::jumping) 190 if (ponderable.getState() == PonderableComponent::State::jumping)
191 { 191 {
192 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); 192 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233));
193 ponderable.setState(PonderableComponent::state::falling); 193 ponderable.setState(PonderableComponent::State::falling);
194 } 194 }
195} 195}
196 196
@@ -199,12 +199,12 @@ void ControllingSystem::drop(id_type entity, bool start)
199 auto& droppable = game_.getEntityManager().getComponent<DroppableComponent>(entity); 199 auto& droppable = game_.getEntityManager().getComponent<DroppableComponent>(entity);
200 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); 200 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
201 201
202 if (start && (ponderable.getState() == PonderableComponent::state::grounded)) 202 if (start && (ponderable.getState() == PonderableComponent::State::grounded))
203 { 203 {
204 ponderable.setState(PonderableComponent::state::dropping); 204 ponderable.setState(PonderableComponent::State::dropping);
205 } else if ((!start) && (ponderable.getState() == PonderableComponent::state::dropping)) 205 } else if ((!start) && (ponderable.getState() == PonderableComponent::State::dropping))
206 { 206 {
207 ponderable.setState(PonderableComponent::state::grounded); 207 ponderable.setState(PonderableComponent::State::grounded);
208 } 208 }
209 droppable.setDroppable(start); 209 droppable.setDroppable(start);
210} 210}
diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp new file mode 100644 index 0000000..8723e16 --- /dev/null +++ b/src/systems/mapping.cpp
@@ -0,0 +1,143 @@
1#include "mapping.h"
2#include "components/mappable.h"
3#include "game.h"
4#include "consts.h"
5
6#include <iostream>
7
8template <typename Storage>
9inline void addBoundary(
10 Storage& boundaries,
11 int axis,
12 int lower,
13 int upper,
14 MappableComponent::Boundary::Type type)
15{
16 boundaries.emplace(std::piecewise_construct,
17 std::tie(axis),
18 std::tie(axis, lower, upper, type));
19}
20
21void MappingSystem::render(Texture& texture)
22{
23 auto entities = game_.getEntityManager().getEntitiesWithComponents<
24 MappableComponent>();
25
26 for (id_type entity : entities)
27 {
28 auto& mappable = game_.getEntityManager().
29 getComponent<MappableComponent>(entity);
30
31 const Map& map = game_.getWorld().getMap(mappable.getMapId());
32
33 for (int i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++)
34 {
35 int x = i % MAP_WIDTH;
36 int y = i / MAP_WIDTH;
37 int tile = map.getTiles()[i];
38
39 if (tile > 0)
40 {
41 Rectangle dst {
42 x * TILE_WIDTH,
43 y * TILE_HEIGHT,
44 TILE_WIDTH,
45 TILE_HEIGHT};
46
47 Rectangle src {
48 (tile % TILESET_COLS) * TILE_WIDTH,
49 (tile / TILESET_COLS) * TILE_HEIGHT,
50 TILE_WIDTH,
51 TILE_HEIGHT};
52
53 texture.blit(mappable.getTileset(), std::move(src), std::move(dst));
54 }
55 }
56
57 int startX = ((GAME_WIDTH / TILE_WIDTH) / 2) - (map.getTitle().size() / 2);
58 for (size_t i = 0; i < map.getTitle().size(); i++)
59 {
60 Rectangle src {
61 (map.getTitle()[i] % FONT_COLS) * TILE_WIDTH,
62 (map.getTitle()[i] / FONT_COLS) * TILE_HEIGHT,
63 TILE_WIDTH,
64 TILE_HEIGHT};
65
66 Rectangle dst {
67 (startX + static_cast<int>(i)) * TILE_WIDTH,
68 24 * TILE_HEIGHT,
69 TILE_WIDTH,
70 TILE_HEIGHT};
71
72 texture.blit(mappable.getFont(), std::move(src), std::move(dst));
73 }
74 }
75}
76
77void MappingSystem::loadMap(size_t mapId)
78{
79 id_type mapEntity = game_.getEntityManager().emplaceEntity();
80
81 auto& mappable = game_.getEntityManager().
82 emplaceComponent<MappableComponent>(mapEntity,
83 Texture("res/tiles.png"),
84 Texture("res/font.bmp"));
85
86 mappable.setMapId(mapId);
87
88 const Map& map = game_.getWorld().getMap(mappable.getMapId());
89
90 for (size_t i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++)
91 {
92 size_t x = i % MAP_WIDTH;
93 size_t y = i / MAP_WIDTH;
94 int tile = map.getTiles()[i];
95
96 if ((tile >= 5) && (tile <= 7))
97 {
98 addBoundary(
99 mappable.getDownBoundaries(),
100 y * TILE_HEIGHT,
101 x * TILE_WIDTH,
102 (x + 1) * TILE_WIDTH,
103 MappableComponent::Boundary::Type::platform);
104 } else if ((tile > 0) && (tile < 28))
105 {
106 addBoundary(
107 mappable.getRightBoundaries(),
108 x * TILE_WIDTH,
109 y * TILE_HEIGHT,
110 (y+1) * TILE_HEIGHT,
111 MappableComponent::Boundary::Type::wall);
112
113 addBoundary(
114 mappable.getLeftBoundaries(),
115 (x+1) * TILE_WIDTH,
116 y * TILE_HEIGHT,
117 (y+1) * TILE_HEIGHT,
118 MappableComponent::Boundary::Type::wall);
119
120 addBoundary(
121 mappable.getDownBoundaries(),
122 y * TILE_HEIGHT,
123 x * TILE_WIDTH,
124 (x+1) * TILE_WIDTH,
125 MappableComponent::Boundary::Type::wall);
126
127 addBoundary(
128 mappable.getUpBoundaries(),
129 (y+1) * TILE_HEIGHT,
130 x * TILE_WIDTH,
131 (x+1) * TILE_WIDTH,
132 MappableComponent::Boundary::Type::wall);
133 } else if (tile == 42)
134 {
135 addBoundary(
136 mappable.getDownBoundaries(),
137 y * TILE_HEIGHT,
138 x * TILE_WIDTH,
139 (x+1) * TILE_WIDTH,
140 MappableComponent::Boundary::Type::danger);
141 }
142 }
143}
diff --git a/src/systems/mapping.h b/src/systems/mapping.h new file mode 100644 index 0000000..53d054b --- /dev/null +++ b/src/systems/mapping.h
@@ -0,0 +1,19 @@
1#ifndef MAPPING_H_33FC2294
2#define MAPPING_H_33FC2294
3
4#include "system.h"
5
6class MappingSystem : public System {
7public:
8
9 MappingSystem(Game& game) : System(game)
10 {
11 }
12
13 void render(Texture& texture);
14
15 void loadMap(size_t mapId);
16
17};
18
19#endif /* end of include guard: MAPPING_H_33FC2294 */
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index e40db1d..26a6f56 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp
@@ -2,6 +2,8 @@
2#include "game.h" 2#include "game.h"
3#include "components/ponderable.h" 3#include "components/ponderable.h"
4#include "components/transformable.h" 4#include "components/transformable.h"
5#include "components/droppable.h"
6#include "consts.h"
5 7
6void PonderingSystem::tick(double dt) 8void PonderingSystem::tick(double dt)
7{ 9{
@@ -9,17 +11,251 @@ void PonderingSystem::tick(double dt)
9 PonderableComponent, 11 PonderableComponent,
10 TransformableComponent>(); 12 TransformableComponent>();
11 13
14 auto maps = game_.getEntityManager().getEntitiesWithComponents<
15 MappableComponent>();
16
12 for (id_type entity : entities) 17 for (id_type entity : entities)
13 { 18 {
14 auto& transformable = game_.getEntityManager().getComponent<TransformableComponent>(entity); 19 auto& transformable = game_.getEntityManager().
15 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); 20 getComponent<TransformableComponent>(entity);
21
22 auto& ponderable = game_.getEntityManager().
23 getComponent<PonderableComponent>(entity);
16 24
17 // Accelerate 25 // Accelerate
18 ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt); 26 ponderable.setVelocityX(
19 ponderable.setVelocityY(ponderable.getVelocityY() + ponderable.getAccelY() * dt); 27 ponderable.getVelocityX() + ponderable.getAccelX() * dt);
28
29 ponderable.setVelocityY(
30 ponderable.getVelocityY() + ponderable.getAccelY() * dt);
31
32 const double oldX = transformable.getX();
33 const double oldY = transformable.getY();
34 const double oldRight = oldX + transformable.getW();
35 const double oldBottom = oldY + transformable.getH();
36
37 double newX = oldX + ponderable.getVelocityX() * dt;
38 double newY = oldY + ponderable.getVelocityY() * dt;
39
40 if (ponderable.getVelocityY() > 0.0)
41 {
42 ponderable.setState(PonderableComponent::State::falling);
43 }
44
45 for (id_type mapEntity : maps)
46 {
47 auto& mappable = game_.getEntityManager().
48 getComponent<MappableComponent>(mapEntity);
49
50 if (newX < oldX)
51 {
52 for (auto it = mappable.getLeftBoundaries().lower_bound(oldX);
53 (it != std::end(mappable.getLeftBoundaries())) && (it->first >= newX);
54 it++)
55 {
56 if ((oldBottom > it->second.getLower())
57 && (oldY < it->second.getUpper()))
58 {
59 // We have a collision!
60 processCollision(
61 entity,
62 Direction::left,
63 newX,
64 newY,
65 it->first,
66 it->second.getType());
67
68 break;
69 }
70 }
71 } else if (newX > oldX)
72 {
73 for (auto it = mappable.getRightBoundaries().lower_bound(oldRight);
74 (it != std::end(mappable.getRightBoundaries()))
75 && (it->first <= (newX + transformable.getW()));
76 it++)
77 {
78 if ((oldBottom > it->second.getLower())
79 && (oldY < it->second.getUpper()))
80 {
81 // We have a collision!
82 processCollision(
83 entity,
84 Direction::right,
85 newX,
86 newY,
87 it->first,
88 it->second.getType());
89
90 break;
91 }
92 }
93 }
94
95 if (newY < oldY)
96 {
97 for (auto it = mappable.getUpBoundaries().lower_bound(oldY);
98 (it != std::end(mappable.getUpBoundaries())) && (it->first >= newY);
99 it++)
100 {
101 if ((oldRight > it->second.getLower())
102 && (oldX < it->second.getUpper()))
103 {
104 // We have a collision!
105 processCollision(
106 entity,
107 Direction::up,
108 newX,
109 newY,
110 it->first,
111 it->second.getType());
112
113 break;
114 }
115 }
116 } else if (newY > oldY)
117 {
118 for (auto it = mappable.getDownBoundaries().lower_bound(oldBottom);
119 (it != std::end(mappable.getDownBoundaries()))
120 && (it->first <= (newY + transformable.getH()));
121 it++)
122 {
123 if ((oldRight > it->second.getLower())
124 && (oldX < it->second.getUpper()))
125 {
126 // We have a collision!
127 processCollision(
128 entity,
129 Direction::down,
130 newX,
131 newY,
132 it->first,
133 it->second.getType());
134
135 break;
136 }
137 }
138 }
139 }
20 140
21 // Move 141 // Move
22 transformable.setX(transformable.getX() + ponderable.getVelocityX() * dt); 142 transformable.setX(newX);
23 transformable.setY(transformable.getY() + ponderable.getVelocityY() * dt); 143 transformable.setY(newY);
144 }
145}
146
147void PonderingSystem::initializeBody(
148 id_type entity,
149 PonderableComponent::Type type)
150{
151 auto& ponderable = game_.getEntityManager().
152 emplaceComponent<PonderableComponent>(entity, type);
153
154 if (type == PonderableComponent::Type::freefalling)
155 {
156 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233));
157 ponderable.setState(PonderableComponent::State::falling);
158 }
159}
160
161void PonderingSystem::processCollision(
162 id_type entity,
163 Direction dir,
164 double& newX,
165 double& newY,
166 int axis,
167 MappableComponent::Boundary::Type type)
168{
169 auto& ponderable = game_.getEntityManager().
170 getComponent<PonderableComponent>(entity);
171
172 auto& transformable = game_.getEntityManager().
173 getComponent<TransformableComponent>(entity);
174
175 switch (type)
176 {
177 case MappableComponent::Boundary::Type::wall:
178 {
179 switch (dir)
180 {
181 case Direction::left:
182 {
183 newX = axis;
184 ponderable.setVelocityX(0.0);
185
186 break;
187 }
188
189 case Direction::right:
190 {
191 newX = axis - transformable.getW();
192 ponderable.setVelocityX(0.0);
193
194 break;
195 }
196
197 case Direction::up:
198 {
199 newY = axis;
200 ponderable.setVelocityY(0.0);
201
202 break;
203 }
204
205 case Direction::down:
206 {
207 newY = axis - transformable.getH();
208 ponderable.setVelocityY(0.0);
209
210 if (ponderable.getState() == PonderableComponent::State::falling)
211 {
212 ponderable.setState(PonderableComponent::State::grounded);
213 }
214
215 break;
216 }
217 }
218
219 break;
220 }
221
222 case MappableComponent::Boundary::Type::platform:
223 {
224 if (game_.getEntityManager().hasComponent<DroppableComponent>(entity))
225 {
226 auto& droppable = game_.getEntityManager().
227 getComponent<DroppableComponent>(entity);
228
229 if (droppable.isDroppable())
230 {
231 droppable.setDroppable(false);
232 } else {
233 newY = axis - transformable.getH();
234 ponderable.setVelocityY(0.0);
235
236 if (ponderable.getState() == PonderableComponent::State::falling)
237 {
238 ponderable.setState(PonderableComponent::State::grounded);
239 }
240 }
241 } else {
242 newY = axis - transformable.getH();
243 ponderable.setVelocityY(0.0);
244
245 if (ponderable.getState() == PonderableComponent::State::falling)
246 {
247 ponderable.setState(PonderableComponent::State::grounded);
248 }
249 }
250
251 break;
252 }
253
254 default:
255 {
256 // Not yet implemented.
257
258 break;
259 }
24 } 260 }
25} 261}
diff --git a/src/systems/pondering.h b/src/systems/pondering.h index 44e7600..a16622b 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h
@@ -2,6 +2,9 @@
2#define PONDERING_H_F2530E0E 2#define PONDERING_H_F2530E0E
3 3
4#include "system.h" 4#include "system.h"
5#include "components/mappable.h"
6#include "components/ponderable.h"
7#include "direction.h"
5 8
6class PonderingSystem : public System { 9class PonderingSystem : public System {
7public: 10public:
@@ -11,6 +14,19 @@ public:
11 } 14 }
12 15
13 void tick(double dt); 16 void tick(double dt);
17
18 void initializeBody(id_type entity, PonderableComponent::Type type);
19
20private:
21
22 void processCollision(
23 id_type entity,
24 Direction dir,
25 double& newX,
26 double& newY,
27 int axis,
28 MappableComponent::Boundary::Type type);
29
14}; 30};
15 31
16#endif /* end of include guard: PONDERING_H_F2530E0E */ 32#endif /* end of include guard: PONDERING_H_F2530E0E */