diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-19 16:22:39 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-19 16:22:39 -0500 |
commit | 8996810b1356c2224d4f34423fd4211de20da238 (patch) | |
tree | 9beab3f77e6af1c769e14cff4234ad649cf857f2 /src | |
parent | 7514077a07076403f29050e57fa87f24fc614122 (diff) | |
download | dispatcher-8996810b1356c2224d4f34423fd4211de20da238.tar.gz dispatcher-8996810b1356c2224d4f34423fd4211de20da238.tar.bz2 dispatcher-8996810b1356c2224d4f34423fd4211de20da238.zip |
Restrictions for moving into a space a block is moving out of
An entity can only move into a space a block is moving out of if the time it would take the entity to move into the tile is at least the time remaining for the other block to finish moving out of the space. Also, crates can push each other now.
Diffstat (limited to 'src')
-rw-r--r-- | src/entity.h | 20 | ||||
-rw-r--r-- | src/enums.h | 19 | ||||
-rw-r--r-- | src/main.cpp | 13 | ||||
-rw-r--r-- | src/simulation.cpp | 33 | ||||
-rw-r--r-- | src/tileset.h | 4 |
5 files changed, 59 insertions, 30 deletions
diff --git a/src/entity.h b/src/entity.h index 0ff31e1..b606b43 100644 --- a/src/entity.h +++ b/src/entity.h | |||
@@ -1,21 +1,9 @@ | |||
1 | #ifndef ENTITY_H_0D6CB29A | 1 | #ifndef ENTITY_H_0D6CB29A |
2 | #define ENTITY_H_0D6CB29A | 2 | #define ENTITY_H_0D6CB29A |
3 | 3 | ||
4 | #include <set> | ||
4 | #include "vector.h" | 5 | #include "vector.h" |
5 | 6 | #include "enums.h" | |
6 | enum class ColliderType { | ||
7 | player, | ||
8 | train, | ||
9 | other | ||
10 | }; | ||
11 | |||
12 | enum class Direction { | ||
13 | none, | ||
14 | left, | ||
15 | right, | ||
16 | up, | ||
17 | down | ||
18 | }; | ||
19 | 7 | ||
20 | class Entity { | 8 | class Entity { |
21 | public: | 9 | public: |
@@ -39,9 +27,7 @@ public: | |||
39 | 27 | ||
40 | // Collision | 28 | // Collision |
41 | ColliderType colliderType = ColliderType::other; | 29 | ColliderType colliderType = ColliderType::other; |
42 | 30 | std::set<ColliderType> canBePushedBy; | |
43 | bool playerCanPush = false; | ||
44 | bool trainCanPush = false; | ||
45 | 31 | ||
46 | // Temp | 32 | // Temp |
47 | int colorVal = 25; | 33 | int colorVal = 25; |
diff --git a/src/enums.h b/src/enums.h new file mode 100644 index 0000000..9821da1 --- /dev/null +++ b/src/enums.h | |||
@@ -0,0 +1,19 @@ | |||
1 | #ifndef ENUMS_H_CD0A75E4 | ||
2 | #define ENUMS_H_CD0A75E4 | ||
3 | |||
4 | enum class ColliderType { | ||
5 | player, | ||
6 | train, | ||
7 | crate, | ||
8 | other | ||
9 | }; | ||
10 | |||
11 | enum class Direction { | ||
12 | none, | ||
13 | left, | ||
14 | right, | ||
15 | up, | ||
16 | down | ||
17 | }; | ||
18 | |||
19 | #endif /* end of include guard: ENUMS_H_CD0A75E4 */ | ||
diff --git a/src/main.cpp b/src/main.cpp index c041cf8..c77d25b 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -26,9 +26,20 @@ int main(int, char**) | |||
26 | Entity& crate = sim.getEntity(crateId); | 26 | Entity& crate = sim.getEntity(crateId); |
27 | crate.size = TILE_SIZE; | 27 | crate.size = TILE_SIZE; |
28 | crate.speed = 4.0; | 28 | crate.speed = 4.0; |
29 | crate.playerCanPush = true; | 29 | crate.colliderType = ColliderType::crate; |
30 | crate.canBePushedBy.insert(ColliderType::player); | ||
31 | crate.canBePushedBy.insert(ColliderType::crate); | ||
30 | sim.setGridPos(crateId, vec2s { 4, 5 } ); | 32 | sim.setGridPos(crateId, vec2s { 4, 5 } ); |
31 | 33 | ||
34 | Simulation::id_type crateId2 = sim.emplaceEntity(); | ||
35 | Entity& crate2 = sim.getEntity(crateId2); | ||
36 | crate2.size = TILE_SIZE; | ||
37 | crate2.speed = 4.0; | ||
38 | crate2.colliderType = ColliderType::crate; | ||
39 | crate2.canBePushedBy.insert(ColliderType::player); | ||
40 | crate2.canBePushedBy.insert(ColliderType::crate); | ||
41 | sim.setGridPos(crateId2, vec2s { 6, 7 } ); | ||
42 | |||
32 | bool quit = false; | 43 | bool quit = false; |
33 | 44 | ||
34 | SDL_Event e; | 45 | SDL_Event e; |
diff --git a/src/simulation.cpp b/src/simulation.cpp index ca6ca3d..912f7f0 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp | |||
@@ -42,7 +42,7 @@ void Simulation::tick( | |||
42 | { | 42 | { |
43 | Entity& block = entities_.at(blockId); | 43 | Entity& block = entities_.at(blockId); |
44 | 44 | ||
45 | if (!block.moving && block.playerCanPush) | 45 | if (!block.moving && block.canBePushedBy.count(ColliderType::player)) |
46 | { | 46 | { |
47 | moveEntityOnGrid(blockId, lookDir); | 47 | moveEntityOnGrid(blockId, lookDir); |
48 | } | 48 | } |
@@ -169,6 +169,8 @@ bool Simulation::moveEntityOnGrid( | |||
169 | Direction moveDir, | 169 | Direction moveDir, |
170 | bool validate) | 170 | bool validate) |
171 | { | 171 | { |
172 | bool actuallyMove = true; | ||
173 | |||
172 | Entity& entity = entities_.at(id); | 174 | Entity& entity = entities_.at(id); |
173 | 175 | ||
174 | vec2s shouldMoveTo = posInDir(entity.gridPos, moveDir); | 176 | vec2s shouldMoveTo = posInDir(entity.gridPos, moveDir); |
@@ -216,18 +218,20 @@ bool Simulation::moveEntityOnGrid( | |||
216 | } | 218 | } |
217 | } | 219 | } |
218 | 220 | ||
219 | if (entity.colliderType == ColliderType::player) | 221 | if (!level_.getTileset().canEntityMoveTo( |
222 | entity.colliderType, | ||
223 | level_.at(shouldMoveTo))) | ||
220 | { | 224 | { |
221 | if (!level_.getTileset().canPlayerMoveTo(level_.at(shouldMoveTo))) | 225 | return false; |
222 | { | 226 | } |
223 | return false; | ||
224 | } | ||
225 | 227 | ||
226 | for (id_type blockId : getGridEntities(shouldMoveTo)) | 228 | for (id_type blockId : getGridEntities(shouldMoveTo)) |
227 | { | 229 | { |
228 | Entity& block = entities_.at(blockId); | 230 | Entity& block = entities_.at(blockId); |
229 | 231 | ||
230 | if (block.moving || !block.playerCanPush) | 232 | if (!block.moving) |
233 | { | ||
234 | if (!block.canBePushedBy.count(entity.colliderType)) | ||
231 | { | 235 | { |
232 | return false; | 236 | return false; |
233 | } | 237 | } |
@@ -238,13 +242,20 @@ bool Simulation::moveEntityOnGrid( | |||
238 | } | 242 | } |
239 | } | 243 | } |
240 | 244 | ||
245 | double entityTimeLeft = 1.0 / entity.speed; | ||
246 | double blockTimeLeft = (1.0 - block.movementTween) / block.speed; | ||
247 | |||
248 | if (entityTimeLeft < blockTimeLeft) | ||
249 | { | ||
250 | actuallyMove = false; | ||
251 | } | ||
241 | } | 252 | } |
242 | 253 | ||
243 | 254 | ||
244 | 255 | ||
245 | 256 | ||
246 | 257 | ||
247 | if (!validate) | 258 | if (!validate && actuallyMove) |
248 | { | 259 | { |
249 | entity.moving = true; | 260 | entity.moving = true; |
250 | entity.destPos = shouldMoveTo; | 261 | entity.destPos = shouldMoveTo; |
diff --git a/src/tileset.h b/src/tileset.h index 610a710..8a565bc 100644 --- a/src/tileset.h +++ b/src/tileset.h | |||
@@ -1,10 +1,12 @@ | |||
1 | #ifndef TILESET_H_B89AE7A1 | 1 | #ifndef TILESET_H_B89AE7A1 |
2 | #define TILESET_H_B89AE7A1 | 2 | #define TILESET_H_B89AE7A1 |
3 | 3 | ||
4 | #include "enums.h" | ||
5 | |||
4 | class Tileset { | 6 | class Tileset { |
5 | public: | 7 | public: |
6 | 8 | ||
7 | bool canPlayerMoveTo(size_t tile) const | 9 | bool canEntityMoveTo(ColliderType collider, size_t tile) const |
8 | { | 10 | { |
9 | return true; | 11 | return true; |
10 | } | 12 | } |