From 4b6e5eb5d3e0733c239a7a231da91a9977c38246 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 12 Mar 2022 14:12:42 -0500 Subject: dynamic wall and floor textures --- src/main.cpp | 298 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 224 insertions(+), 74 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 5178110..149e1cf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,15 +5,25 @@ #include "game.h" #include "renderer.h" -void incrementIfSet(Game& game, int& count, int x, int y, Tile val = Tile::Wall) +inline bool isTileSetOrNotLit(const Map& map, int x, int y) { - if (game.map.inBounds(x, y) && game.map.at(x,y).tile == val) + return (map.inBounds(x, y) && (map.at(x,y).tile == Tile::Wall || !map.at(x,y).lit)); +} + +inline bool isTileSet(const Map& map, int x, int y, Tile val = Tile::Wall) +{ + return (map.inBounds(x, y) && map.at(x,y).tile == val); +} + +inline void incrementIfSet(const Game& game, int& count, int x, int y, Tile val = Tile::Wall) +{ + if (isTileSet(game.map, x, y, val)) { count++; } } -int getZoomLevel(const Game& game) { +inline int getZoomLevel(const Game& game) { return (game.curZoom - INIT_ZOOM) / 2; } @@ -65,6 +75,11 @@ void tick( } else { temp.at(x,y).tile = Tile::Floor; } + + if (temp.at(x,y).tile != game.map.at(x,y).tile) { + temp.at(x,y).dirtyRender = true; + game.dirtyRender = true; + } } } @@ -98,9 +113,13 @@ bool movePlayer(Game& game, int x, int y) game.numDust++; } + game.player_oldx = game.player_x; + game.player_oldy = game.player_y; game.player_x = x; game.player_y = y; game.muxer.setPlayerLoc(x, y); + game.moving = true; + game.moveProgress.start(33); game.dirtyLighting = true; @@ -119,12 +138,17 @@ bool movePlayer(Game& game, int x, int y) void recalculateLighting(Game& game) { game.litSpots = 0; + game.dirtyRender = true; for (MapData& md : game.map.data()) { md.wasLit = md.lit; md.lit = false; md.litTiles.clear(); + + if (md.tile == Tile::Wall) { + md.dirtyRender = true; + } } fov_settings_type fov; @@ -203,6 +227,103 @@ void recalculateLighting(Game& game) game.dirtyLighting = false; } +void recalculateRender(Game& game) { + for (int y = game.map.getTop(); y < game.map.getBottom(); y++) + { + for (int x = game.map.getLeft(); x < game.map.getRight(); x++) + { + if (game.map.at(x,y).dirtyRender) { + game.map.at(x,y).dirtyRender = false; + + if (game.map.at(x,y).tile == Tile::Floor) { + if (std::bernoulli_distribution(0.05)(game.rng)) { + static const std::vector furnishings { + TilesetIndex(20, 16), + TilesetIndex(21, 2), + TilesetIndex(22, 2), + TilesetIndex(21, 3), + TilesetIndex(22, 3)}; + game.map.at(x,y).renderId = furnishings.at(std::uniform_int_distribution(0, furnishings.size()-1)(game.rng)); + } else { + game.map.at(x,y).renderId = -1; + } + } else if (game.map.at(x,y).tile == Tile::Wall) { + static bool initWalls = false; + static std::vector wallRenders(256, TilesetIndex(0, 0)); + if (!initWalls) { + initWalls = true; + + // Start in top left and go clockwise. + wallRenders[0b00011100] = TilesetIndex(16, 14); + wallRenders[0b00111100] = TilesetIndex(16, 14); + wallRenders[0b00011110] = TilesetIndex(16, 14); + wallRenders[0b00111110] = TilesetIndex(16, 14); + + wallRenders[0b00011111] = TilesetIndex(17, 14); + wallRenders[0b10011111] = TilesetIndex(17, 14); + wallRenders[0b00111111] = TilesetIndex(17, 14); + wallRenders[0b10111111] = TilesetIndex(17, 14); + + wallRenders[0b00000111] = TilesetIndex(18, 14); + wallRenders[0b00001111] = TilesetIndex(18, 14); + wallRenders[0b10000111] = TilesetIndex(18, 14); + wallRenders[0b10001111] = TilesetIndex(18, 14); + + wallRenders[0b01111100] = TilesetIndex(16, 15); + wallRenders[0b11111100] = TilesetIndex(16, 15); + wallRenders[0b01111110] = TilesetIndex(16, 15); + wallRenders[0b11111110] = TilesetIndex(16, 15); + + wallRenders[0b11000111] = TilesetIndex(18, 15); + wallRenders[0b11001111] = TilesetIndex(18, 15); + wallRenders[0b11100111] = TilesetIndex(18, 15); + wallRenders[0b11101111] = TilesetIndex(18, 15); + + wallRenders[0b01110000] = TilesetIndex(16, 16); + wallRenders[0b01111000] = TilesetIndex(16, 16); + wallRenders[0b11110000] = TilesetIndex(16, 16); + wallRenders[0b11111000] = TilesetIndex(16, 16); + + wallRenders[0b11110001] = TilesetIndex(17, 16); + wallRenders[0b11110011] = TilesetIndex(17, 16); + wallRenders[0b11111001] = TilesetIndex(17, 16); + wallRenders[0b11111011] = TilesetIndex(17, 16); + + wallRenders[0b11000001] = TilesetIndex(18, 16); + wallRenders[0b11000011] = TilesetIndex(18, 16); + wallRenders[0b11100001] = TilesetIndex(18, 16); + wallRenders[0b11100011] = TilesetIndex(18, 16); + + + wallRenders[0b11110111] = TilesetIndex(21, 14); + wallRenders[0b11111101] = TilesetIndex(22, 14); + wallRenders[0b11011111] = TilesetIndex(21, 15); + wallRenders[0b01111111] = TilesetIndex(22, 15); + } + + int renderDesc = 0; + if (isTileSetOrNotLit(game.map, x-1, y-1)) renderDesc |= (1 << 7); + if (isTileSetOrNotLit(game.map, x , y-1)) renderDesc |= (1 << 6); + if (isTileSetOrNotLit(game.map, x+1, y-1)) renderDesc |= (1 << 5); + if (isTileSetOrNotLit(game.map, x+1, y )) renderDesc |= (1 << 4); + if (isTileSetOrNotLit(game.map, x+1, y+1)) renderDesc |= (1 << 3); + if (isTileSetOrNotLit(game.map, x , y+1)) renderDesc |= (1 << 2); + if (isTileSetOrNotLit(game.map, x-1, y+1)) renderDesc |= (1 << 1); + if (isTileSetOrNotLit(game.map, x-1, y )) renderDesc |= (1 << 0); + + game.map.at(x,y).renderId = wallRenders.at(renderDesc); + + if (wallRenders.at(renderDesc) == 0 && renderDesc != 255) { + std::cout << renderDesc << std::endl; + } + } + } + } + } + + game.dirtyRender = false; +} + bool processKeys(Game& game, const Input& keystate) { int px = game.player_x; @@ -435,6 +556,80 @@ void setZoom(Game& game, size_t zoom) } } +void performDash(Game& game, std::mt19937& rng) { + if (game.map.at(game.player_x, game.player_y).tile == + Tile::Floor) + { + 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()) + { + 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); + game.muxer.playSoundAtPosition("drop", game.player_x, game.player_y); + + 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)); + } + + //game.muxer.playSoundAtPosition("dash", game.player_x, game.player_y); + } + } +} + int main(int, char**) { std::random_device randomEngine; @@ -527,76 +722,10 @@ int main(int, char**) { if (losing == LoseState::None) { - if (game.map.at(game.player_x, game.player_y).tile == - Tile::Floor) - { - 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()) - { - 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); - game.muxer.playSoundAtPosition("drop", game.player_x, game.player_y); - - 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)); - } - - //game.muxer.playSoundAtPosition("dash", game.player_x, game.player_y); - } + if (game.moving) { + game.queueDash = true; + } else { + performDash(game, rng); } } @@ -617,6 +746,13 @@ int main(int, char**) game.alreadyBumped = false; } + if (game.queueDash && !game.moving) { + game.queueDash = false; + if (state[SDL_SCANCODE_SPACE]) { + performDash(game, rng); + } + } + if (keystate.left || keystate.right || keystate.up || keystate.down) { game.firstInput = true; @@ -657,9 +793,18 @@ int main(int, char**) { case LoseState::None: { + if (game.moving) { + game.moveProgress.tick(frameTime); + if (game.moveProgress.isComplete()) { + game.moving = false; + } + } + while (inputAcc >= inputDt) { - processKeys(game, keystate); + if (!game.moving) { + processKeys(game, keystate); + } inputAcc -= inputDt; } @@ -747,6 +892,7 @@ int main(int, char**) } else { game.map.at(x,y).tile = Tile::Floor; } + game.map.at(x,y).dirtyRender = true; } } } @@ -759,6 +905,10 @@ int main(int, char**) setZoom(game, game.litSpots / 1500 * 2 + INIT_ZOOM); } + if (game.dirtyRender) { + recalculateRender(game); + } + zoomAcc += frameTime; while (zoomAcc >= zoomDt) -- cgit 1.4.1