From d851eae8e7cb4192dcd7213dd1a8b60064156b15 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 5 Jun 2018 22:17:15 -0400 Subject: players must move when dropping lamps if no direction is pressed, player dashes in direction of most recent movement. if no movement has occured, choose a random direction. if player cannot move in the chosen direction, choose a random direction. if the player cannot move in any direction, prevent dropping a lamp. --- src/main.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 17 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 8f20635..a0085cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -79,7 +79,7 @@ void tick(Game& game, bool onlyDark = false) onlyDark); } -void movePlayer(Game& game, int x, int y) +bool movePlayer(Game& game, int x, int y) { if (game.map.inBounds(x, y) && game.map.at(x,y).tile == Tile::Floor) { @@ -94,6 +94,10 @@ void movePlayer(Game& game, int x, int y) game.player_y = y; game.dirtyLighting = true; + + return true; + } else { + return false; } } @@ -184,7 +188,7 @@ void recalculateLighting(Game& game) game.dirtyLighting = false; } -void processKeys(Game& game, const Input& keystate) +bool processKeys(Game& game, const Input& keystate) { int px = game.player_x; int py = game.player_y; @@ -211,7 +215,9 @@ void processKeys(Game& game, const Input& keystate) if (!(game.player_x == px && game.player_y == py)) { - movePlayer(game, px, py); + return movePlayer(game, px, py); + } else { + return false; } } @@ -460,21 +466,69 @@ int main(int, char**) if (game.map.at(game.player_x, game.player_y).tile == Tile::Floor) { - game.map.at(game.player_x, game.player_y).tile = Tile::Lamp; - game.numLamps++; - game.dirtyLighting = true; - kickUpDust(game, game.player_x, game.player_y, 0); - - for (int i = 0; i < 5; i++) + std::vector freeSpaces; + + auto addIfFree = [&] (int x, int y) { + if (game.map.inBounds(x,y) && + game.map.at(x,y).tile == Tile::Floor) + { + freeSpaces.emplace_back(x, y); + } + }; + + addIfFree(game.player_x - 1, game.player_y - 1); + addIfFree(game.player_x , game.player_y - 1); + addIfFree(game.player_x + 1, game.player_y - 1); + addIfFree(game.player_x - 1, game.player_y ); + addIfFree(game.player_x + 1, game.player_y ); + addIfFree(game.player_x - 1, game.player_y + 1); + addIfFree(game.player_x , game.player_y + 1); + addIfFree(game.player_x + 1, game.player_y + 1); + + if (!freeSpaces.empty()) { - processKeys(game, keystate); - - tick( - game, - game.player_x - (RADIUS - 1), - game.player_y - (RADIUS - 1), - game.player_x + RADIUS, - game.player_y + RADIUS); + game.map.at(game.player_x, game.player_y).tile = Tile::Lamp; + game.numLamps++; + game.dirtyLighting = true; + kickUpDust(game, game.player_x, game.player_y, 0); + + if (game.firstInput) + { + for (int i = 0; i < 5; i++) + { + if (!processKeys(game, game.lastInput)) + { + std::uniform_int_distribution freeDist( + 0, freeSpaces.size() - 1); + + int freeIndex = freeDist(rng); + coord& moveTo = freeSpaces[freeIndex]; + + movePlayer( + game, + std::get<0>(moveTo), + std::get<1>(moveTo)); + } + + tick( + game, + game.player_x - (RADIUS - 1), + game.player_y - (RADIUS - 1), + game.player_x + RADIUS, + game.player_y + RADIUS); + } + } else { + std::uniform_int_distribution freeDist( + 0, freeSpaces.size() - 1); + + int freeIndex = freeDist(rng); + coord& moveTo = freeSpaces[freeIndex]; + + movePlayer( + game, + std::get<0>(moveTo), + std::get<1>(moveTo)); + } } } } @@ -491,6 +545,12 @@ int main(int, char**) keystate.up = state[SDL_SCANCODE_UP]; keystate.down = state[SDL_SCANCODE_DOWN]; + if (keystate.left || keystate.right || keystate.up || keystate.down) + { + game.firstInput = true; + game.lastInput = keystate; + } + dustAcc += frameTime; inputAcc += frameTime; -- cgit 1.4.1