From 7514077a07076403f29050e57fa87f24fc614122 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 16 Feb 2019 12:39:24 -0500 Subject: Shift-pushing --- src/entity.h | 2 +- src/simulation.cpp | 118 ++++++++++++++++++++++++++++++++++------------------- src/simulation.h | 2 + 3 files changed, 78 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/entity.h b/src/entity.h index 8d28b98..0ff31e1 100644 --- a/src/entity.h +++ b/src/entity.h @@ -42,7 +42,7 @@ public: bool playerCanPush = false; bool trainCanPush = false; - + // Temp int colorVal = 25; diff --git a/src/simulation.cpp b/src/simulation.cpp index 328b4f4..ca6ca3d 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -15,40 +15,68 @@ void Simulation::tick( if (entity.controllable && !entity.moving) { - if (keystate[SDL_SCANCODE_LEFT] && - moveEntityOnGrid(id, Direction::left, true)) + if (keystate[SDL_SCANCODE_LSHIFT] || keystate[SDL_SCANCODE_RSHIFT]) { - entity.shouldMoveTo = Direction::left; - } - else if (keystate[SDL_SCANCODE_UP] && - moveEntityOnGrid(id, Direction::up, true)) - { - entity.shouldMoveTo = Direction::up; - } - else if (keystate[SDL_SCANCODE_RIGHT] && - moveEntityOnGrid(id, Direction::right, true)) - { - entity.shouldMoveTo = Direction::right; - } - else if (keystate[SDL_SCANCODE_DOWN] && - moveEntityOnGrid(id, Direction::down, true)) - { - entity.shouldMoveTo = Direction::down; - } - else - { - entity.shouldMoveTo = Direction::none; - } - - if (entity.shouldMoveTo != Direction::none) - { - moveEntityOnGrid(id, entity.shouldMoveTo); - } - - - if (entity.moving) - { - + Direction lookDir = Direction::none; + + if (keystate[SDL_SCANCODE_LEFT]) + { + lookDir = Direction::left; + } + else if (keystate[SDL_SCANCODE_UP]) + { + lookDir = Direction::up; + } + else if (keystate[SDL_SCANCODE_RIGHT]) + { + lookDir = Direction::right; + } + else if (keystate[SDL_SCANCODE_DOWN]) + { + lookDir = Direction::down; + } + + vec2s lookPos = posInDir(entity.gridPos, lookDir); + + for (id_type blockId : getGridEntities(lookPos)) + { + Entity& block = entities_.at(blockId); + + if (!block.moving && block.playerCanPush) + { + moveEntityOnGrid(blockId, lookDir); + } + } + } else { + if (keystate[SDL_SCANCODE_LEFT] && + moveEntityOnGrid(id, Direction::left, true)) + { + entity.shouldMoveTo = Direction::left; + } + else if (keystate[SDL_SCANCODE_UP] && + moveEntityOnGrid(id, Direction::up, true)) + { + entity.shouldMoveTo = Direction::up; + } + else if (keystate[SDL_SCANCODE_RIGHT] && + moveEntityOnGrid(id, Direction::right, true)) + { + entity.shouldMoveTo = Direction::right; + } + else if (keystate[SDL_SCANCODE_DOWN] && + moveEntityOnGrid(id, Direction::down, true)) + { + entity.shouldMoveTo = Direction::down; + } + else + { + entity.shouldMoveTo = Direction::none; + } + + if (entity.shouldMoveTo != Direction::none) + { + moveEntityOnGrid(id, entity.shouldMoveTo); + } } } @@ -143,7 +171,7 @@ bool Simulation::moveEntityOnGrid( { Entity& entity = entities_.at(id); - vec2s shouldMoveTo = entity.gridPos; + vec2s shouldMoveTo = posInDir(entity.gridPos, moveDir); switch (moveDir) { @@ -154,8 +182,6 @@ bool Simulation::moveEntityOnGrid( return false; } - shouldMoveTo -= vec2s { 1, 0 }; - break; } @@ -166,8 +192,6 @@ bool Simulation::moveEntityOnGrid( return false; } - shouldMoveTo += vec2s { 1, 0 }; - break; } @@ -178,8 +202,6 @@ bool Simulation::moveEntityOnGrid( return false; } - shouldMoveTo -= vec2s { 0, 1 }; - break; } @@ -190,8 +212,6 @@ bool Simulation::moveEntityOnGrid( return false; } - shouldMoveTo += vec2s { 0, 1 }; - break; } } @@ -207,7 +227,7 @@ bool Simulation::moveEntityOnGrid( { Entity& block = entities_.at(blockId); - if (!block.playerCanPush) + if (block.moving || !block.playerCanPush) { return false; } @@ -233,3 +253,15 @@ bool Simulation::moveEntityOnGrid( return true; } + +vec2s Simulation::posInDir(vec2s orig, Direction dir) +{ + switch (dir) + { + case Direction::left: return orig - vec2s { 1, 0 }; + case Direction::right: return orig + vec2s { 1, 0 }; + case Direction::up: return orig - vec2s { 0, 1 }; + case Direction::down: return orig + vec2s { 0, 1 }; + case Direction::none: return orig; + } +} diff --git a/src/simulation.h b/src/simulation.h index 2f80f9f..4d41b49 100644 --- a/src/simulation.h +++ b/src/simulation.h @@ -49,6 +49,8 @@ public: void setGridPos(id_type id, vec2s pos); + static vec2s posInDir(vec2s orig, Direction dir); + private: -- cgit 1.4.1