From 9b1c8e98592cc0e5cec471720498e7bb2a60a449 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 29 May 2018 17:11:12 -0400 Subject: quitting the game causes all of the lamps to pop followed by the player fixed a bug where dashing when dropping a lamp would cause dust kickup to originate from a different tile than the actual lamp. --- src/main.cpp | 189 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 150 insertions(+), 39 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e26a5a8..c39cdb6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,6 +45,13 @@ enum class Tile { Lamp }; +enum class LoseState { + None, + PoppingLamps, + PoppingPlayer, + Outro +}; + const int GAME_WIDTH = 640*2; const int GAME_HEIGHT = 480*2; const int TILE_WIDTH = 8*2; @@ -85,10 +92,13 @@ public: std::list kickups; int lightedSpots = 0; bool dirtyLighting = true; + size_t numLamps = 0; + size_t numDust = 0; }; int player_x = VIEW_WIDTH / 2; int player_y = VIEW_HEIGHT / 2; +bool renderPlayer = true; void render( SDL_Renderer* ren, @@ -104,7 +114,7 @@ void render( { bool draw = true; - if (player_x == x && player_y == y) + if ((player_x == x && player_y == y) && renderPlayer) { SDL_SetRenderDrawColor(ren, 255, 255, 0, 255); } else if (!map.lighting.at(x+VIEW_WIDTH*y)) @@ -217,6 +227,7 @@ void movePlayer(int x, int y, Map& map) if (map.tiles[player_x+player_y*VIEW_WIDTH] == Tile::Floor) { map.tiles[player_x+player_y*VIEW_WIDTH] = Tile::Dust; + map.numDust++; } player_x = x; @@ -279,7 +290,7 @@ void recalculateLighting(Map& map, fov_settings_type* fov) { for (int x = 0; x < VIEW_WIDTH; x++) { - if ((player_x == x && player_y == y) || + if ((player_x == x && player_y == y && renderPlayer) || map.tiles[x+VIEW_WIDTH*y] == Tile::Dust || map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) { @@ -327,7 +338,6 @@ void processKeys(Map& map, const Input& keystate) void kickUpDust(Map& map, int x, int y, size_t chain) { - std::cout << "kickup " << x << " " << y << " " << chain << std::endl; Kickup dk; dk.x = x; dk.y = y; @@ -338,6 +348,20 @@ void kickUpDust(Map& map, int x, int y, size_t chain) map.kickups.push_back(dk); } +void popLamp(Map& map, int x, int y, size_t chain) +{ + if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) + { + map.numLamps--; + } + + map.tiles[x+VIEW_WIDTH*y] = Tile::Dust; + map.numDust++; + map.dirtyLighting = true; + + kickUpDust(map, x, y, chain); +} + void processKickup(Map& map) { for (Kickup& kickup : map.kickups) @@ -347,6 +371,7 @@ void processKickup(Map& map) if (map.tiles[kickup.x+VIEW_WIDTH*kickup.y] == Tile::Floor) { map.tiles[kickup.x+VIEW_WIDTH*kickup.y] = Tile::Dust; + map.numDust++; } std::unique_ptr dusty(new fov_settings_type); @@ -373,13 +398,11 @@ void processKickup(Map& map) if (map.tiles[x+VIEW_WIDTH*y] == Tile::Floor) { map.tiles[x+VIEW_WIDTH*y] = Tile::Dust; + map.numDust++; map.dirtyLighting = true; } else if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) { - map.tiles[x+VIEW_WIDTH*y] = Tile::Dust; - map.dirtyLighting = true; - - kickUpDust(map, x, y, kickup.chain + 1); + popLamp(map, x, y, kickup.chain + 1); } } }); @@ -454,6 +477,7 @@ int main(int, char**) tick(map); bool quit = false; + LoseState losing = LoseState::None; Input keystate; SDL_Event e; @@ -463,6 +487,12 @@ int main(int, char**) size_t inputDt = 50; size_t inputAcc = 0; + size_t losePopLampDt = 1000; + size_t losePopLampAcc = losePopLampDt; + + size_t losePopPlayerDt = 4000; + size_t losePopPlayerAcc = 0; + size_t lastTime = SDL_GetTicks(); while (!quit) @@ -471,50 +501,60 @@ int main(int, char**) size_t frameTime = currentTime - lastTime; lastTime = currentTime; - //bool input = false; - //int presses = 0; - bool pressedSpace = false; while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { - quit = true; + if (losing != LoseState::None) + { + quit = true; + } else { + losing = LoseState::PoppingLamps; + } } else if (e.type == SDL_KEYDOWN) { - //presses++; - switch (e.key.keysym.sym) { case SDLK_ESCAPE: { - quit = true; + if (losing != LoseState::None) + { + quit = true; + } else { + losing = LoseState::PoppingLamps; + } + break; } case SDLK_SPACE: { - pressedSpace = true; - - setIfValid(map, player_x , player_y , Tile::Lamp); - - for (int i = 0; i < 5; i++) + if (losing == LoseState::None) { - processKeys(map, keystate); - - tick( - map, - player_x - (RADIUS - 1), - player_y - (RADIUS - 1), - player_x + RADIUS, - player_y + RADIUS); - - //render(ren.get(), map, false); - //SDL_Delay(30); + if (map.tiles[player_x+VIEW_WIDTH*player_y] == Tile::Floor) + { + map.tiles[player_x+VIEW_WIDTH*player_y] = Tile::Lamp; + map.numLamps++; + map.dirtyLighting = true; + kickUpDust(map, player_x, player_y, 0); + + for (int i = 0; i < 5; i++) + { + processKeys(map, keystate); + + tick( + map, + player_x - (RADIUS - 1), + player_y - (RADIUS - 1), + player_x + RADIUS, + player_y + RADIUS); + + //render(ren.get(), map, false); + //SDL_Delay(30); + } + } } - map.dirtyLighting = true; - kickUpDust(map, player_x, player_y, 0); - break; } } @@ -527,8 +567,6 @@ int main(int, char**) keystate.up = state[SDL_SCANCODE_UP]; keystate.down = state[SDL_SCANCODE_DOWN]; - bool input = keystate.left || keystate.right || keystate.up || keystate.down || pressedSpace; - dustAcc += frameTime; inputAcc += frameTime; @@ -546,16 +584,89 @@ int main(int, char**) } } + map.numDust = 0; + processKickup(map); dustAcc -= dustDt; } - while (inputAcc >= inputDt) + switch (losing) { - processKeys(map, keystate); + case LoseState::None: + { + while (inputAcc >= inputDt) + { + processKeys(map, keystate); + + inputAcc -= inputDt; + } - inputAcc -= inputDt; + break; + } + + case LoseState::PoppingLamps: + { + if (map.numLamps == 0) + { + if (map.numDust == 0) + { + losing = LoseState::PoppingPlayer; + } + } else { + losePopLampAcc += frameTime; + + while (losePopLampAcc >= losePopLampDt) + { + std::vector> lamps; + + for (int y = 0; y < VIEW_HEIGHT; y++) + { + for (int x = 0; x < VIEW_WIDTH; x++) + { + if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) + { + lamps.emplace_back(x, y); + } + } + } + + std::uniform_int_distribution lampDist(0, lamps.size() - 1); + std::tuple popPos = lamps[lampDist(rng)]; + + popLamp(map, std::get<0>(popPos), std::get<1>(popPos), 0); + + losePopLampAcc -= losePopLampDt; + } + } + + break; + } + + case LoseState::PoppingPlayer: + { + losePopPlayerAcc += frameTime; + + if (losePopPlayerAcc >= losePopPlayerDt) + { + popLamp(map, player_x, player_y, 10); + renderPlayer = false; + + losing = LoseState::Outro; + } + + break; + } + + case LoseState::Outro: + { + if (map.numDust == 0) + { + quit = true; + } + + break; + } } if (map.dirtyLighting) @@ -594,4 +705,4 @@ int main(int, char**) SDL_Quit(); return 0; -} \ No newline at end of file +} -- cgit 1.4.1