From 8996810b1356c2224d4f34423fd4211de20da238 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 19 Feb 2019 16:22:39 -0500 Subject: 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. --- src/entity.h | 20 +++----------------- src/enums.h | 19 +++++++++++++++++++ src/main.cpp | 13 ++++++++++++- src/simulation.cpp | 33 ++++++++++++++++++++++----------- src/tileset.h | 4 +++- 5 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 src/enums.h (limited to 'src') 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 @@ #ifndef ENTITY_H_0D6CB29A #define ENTITY_H_0D6CB29A +#include #include "vector.h" - -enum class ColliderType { - player, - train, - other -}; - -enum class Direction { - none, - left, - right, - up, - down -}; +#include "enums.h" class Entity { public: @@ -39,9 +27,7 @@ public: // Collision ColliderType colliderType = ColliderType::other; - - bool playerCanPush = false; - bool trainCanPush = false; + std::set canBePushedBy; // Temp 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 @@ +#ifndef ENUMS_H_CD0A75E4 +#define ENUMS_H_CD0A75E4 + +enum class ColliderType { + player, + train, + crate, + other +}; + +enum class Direction { + none, + left, + right, + up, + down +}; + +#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**) Entity& crate = sim.getEntity(crateId); crate.size = TILE_SIZE; crate.speed = 4.0; - crate.playerCanPush = true; + crate.colliderType = ColliderType::crate; + crate.canBePushedBy.insert(ColliderType::player); + crate.canBePushedBy.insert(ColliderType::crate); sim.setGridPos(crateId, vec2s { 4, 5 } ); + Simulation::id_type crateId2 = sim.emplaceEntity(); + Entity& crate2 = sim.getEntity(crateId2); + crate2.size = TILE_SIZE; + crate2.speed = 4.0; + crate2.colliderType = ColliderType::crate; + crate2.canBePushedBy.insert(ColliderType::player); + crate2.canBePushedBy.insert(ColliderType::crate); + sim.setGridPos(crateId2, vec2s { 6, 7 } ); + bool quit = false; 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( { Entity& block = entities_.at(blockId); - if (!block.moving && block.playerCanPush) + if (!block.moving && block.canBePushedBy.count(ColliderType::player)) { moveEntityOnGrid(blockId, lookDir); } @@ -169,6 +169,8 @@ bool Simulation::moveEntityOnGrid( Direction moveDir, bool validate) { + bool actuallyMove = true; + Entity& entity = entities_.at(id); vec2s shouldMoveTo = posInDir(entity.gridPos, moveDir); @@ -216,18 +218,20 @@ bool Simulation::moveEntityOnGrid( } } - if (entity.colliderType == ColliderType::player) + if (!level_.getTileset().canEntityMoveTo( + entity.colliderType, + level_.at(shouldMoveTo))) { - if (!level_.getTileset().canPlayerMoveTo(level_.at(shouldMoveTo))) - { - return false; - } + return false; + } - for (id_type blockId : getGridEntities(shouldMoveTo)) - { - Entity& block = entities_.at(blockId); + for (id_type blockId : getGridEntities(shouldMoveTo)) + { + Entity& block = entities_.at(blockId); - if (block.moving || !block.playerCanPush) + if (!block.moving) + { + if (!block.canBePushedBy.count(entity.colliderType)) { return false; } @@ -238,13 +242,20 @@ bool Simulation::moveEntityOnGrid( } } + double entityTimeLeft = 1.0 / entity.speed; + double blockTimeLeft = (1.0 - block.movementTween) / block.speed; + + if (entityTimeLeft < blockTimeLeft) + { + actuallyMove = false; + } } - if (!validate) + if (!validate && actuallyMove) { entity.moving = true; 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 @@ #ifndef TILESET_H_B89AE7A1 #define TILESET_H_B89AE7A1 +#include "enums.h" + class Tileset { public: - bool canPlayerMoveTo(size_t tile) const + bool canEntityMoveTo(ColliderType collider, size_t tile) const { return true; } -- cgit 1.4.1