From 7cd515fcfa1a5aac5605fcc63381033563fbf5d7 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 14 Mar 2022 14:11:05 -0400 Subject: map is now "infinite" using chunks that dynamically load like in diamond&pearl game is also slow as shit now --- src/game.cpp | 123 ++++++++++++++++++----------------------------------------- 1 file changed, 37 insertions(+), 86 deletions(-) (limited to 'src/game.cpp') diff --git a/src/game.cpp b/src/game.cpp index a7c94db..62e755b 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -8,25 +8,11 @@ Game::Game(std::mt19937& rng, Muxer& muxer) : rng(rng), - muxer(muxer), - map( - -INIT_ZOOM * ZOOM_X_FACTOR / 2, - -INIT_ZOOM * ZOOM_Y_FACTOR / 2, - INIT_ZOOM * ZOOM_X_FACTOR, - INIT_ZOOM * ZOOM_Y_FACTOR) + muxer(muxer) { losePopLampTimer.accumulate(losePopLampTimer.getDt()); - for (MapData& md : map.data()) - { - if (std::bernoulli_distribution(0.5)(rng)) - { - md.tile = Tile::Wall; - } - } - - tick(); - tick(); + loadMap(); for (int y = -1; y <= 1; y++) { @@ -39,12 +25,12 @@ Game::Game(std::mt19937& rng, Muxer& muxer) : tick(); } -inline bool isTileSetOrNotLit(const Map& map, int x, int y) +inline bool isTileSetOrNotLit(const Map& map, int x, int y) { 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) +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); } @@ -69,7 +55,7 @@ void Game::tick( bool invert, bool onlyDark) { - Map temp(map); + std::vector temp(map.data()); for (int y = map.getTop(); y < map.getBottom(); y++) { @@ -102,21 +88,22 @@ void Game::tick( incrementIfSet(*this, count, x+1, y ); incrementIfSet(*this, count, x+1, y+1); + int tempIndex = map.getTrueX(x) + map.getTrueY(y) * map.getWidth(); if (count >= 5) { - temp.at(x,y).tile = Tile::Wall; + temp[tempIndex].tile = Tile::Wall; } else { - temp.at(x,y).tile = Tile::Floor; + temp[tempIndex].tile = Tile::Floor; } - if (temp.at(x,y).tile != map.at(x,y).tile) { - temp.at(x,y).dirtyRender = true; + if (temp[tempIndex].tile != map.at(x,y).tile) { + temp[tempIndex].dirtyRender = true; dirtyRender = true; } } } - map = std::move(temp); + map.data() = std::move(temp); } void Game::tick(bool onlyDark) @@ -132,10 +119,10 @@ void Game::tick(bool onlyDark) bool Game::movePlayer(int x, int y) { - if (x >= curBoundX && + if (/*x >= curBoundX && y >= curBoundY && x < curBoundX + curZoom * ZOOM_X_FACTOR && - y < curBoundY + curZoom * ZOOM_Y_FACTOR && + y < curBoundY + curZoom * ZOOM_Y_FACTOR &&*/ map.at(x,y).tile == Tile::Floor) { if (map.at(player_x, player_y).tile == Tile::Floor) @@ -154,6 +141,13 @@ bool Game::movePlayer(int x, int y) moveProgress.start(66); dirtyLighting = true; + int chunkX, chunkY, old_chunkX, old_chunkY; + toChunkPos(player_x, player_y, chunkX, chunkY); + toChunkPos(player_oldx, player_oldy, old_chunkX, old_chunkY); + if ((chunkX != old_chunkX) || (chunkY != old_chunkY)) { + loadMap(); + } + return true; } else { if (!alreadyBumped) { @@ -501,39 +495,25 @@ void Game::processKickup() }); } -void Game::growMap(size_t zoom) -{ - int ol = map.getLeft(); - int ot = map.getTop(); - int ow = map.getWidth(); - int oh = map.getHeight(); +void Game::loadMap() { + int newChunksHoriz = std::ceil(static_cast(curZoom) * ZOOM_X_FACTOR / CHUNK_WIDTH) + 4; + int newChunksVert = std::ceil(static_cast(curZoom) * ZOOM_Y_FACTOR / CHUNK_HEIGHT) + 4; + int curPlayerChunkX, curPlayerChunkY; + toChunkPos(player_x, player_y, curPlayerChunkX, curPlayerChunkY); - map.resize( - -zoom * ZOOM_X_FACTOR / 2, - -zoom * ZOOM_Y_FACTOR / 2, - zoom * ZOOM_X_FACTOR, - zoom * ZOOM_Y_FACTOR); + map.load( + curPlayerChunkX - newChunksHoriz / 2, + curPlayerChunkY - newChunksVert / 2, + newChunksHoriz, + newChunksVert, + rng); - maxZoom = zoom; - - for (int y = map.getTop(); y < map.getBottom(); y++) - { - for (int x = map.getLeft(); x < map.getRight(); x++) - { - if (!(x >= ol && x < (ol + ow) && y >= ot && y < (ot + oh))) - { - if (std::bernoulli_distribution(0.5)(rng)) - { - map.at(x,y).tile = Tile::Wall; - } - } - } - } + tick(); + tick(); + tick(); - for (int i = 0; i < 3; i++) - { - tick(ol, ot, ol + ow, ot + oh, true); - } + dirtyLighting = true; + dirtyRender = true; } void Game::setZoom(size_t zoom) @@ -543,40 +523,11 @@ void Game::setZoom(size_t zoom) return; } - if (zoom > maxZoom) - { - growMap(zoom); - } - - std::tie( - lastZoomLeft, - lastZoomTop, - lastZoomWidth, - lastZoomHeight) = - Renderer::calculateZoomRect(*this); - zoomProgress = 0; zoomLength = std::abs(static_cast(zoom - curZoom)) * TILE_WIDTH; curZoom = zoom; zooming = true; - - curBoundX = player_x - zoom * ZOOM_X_FACTOR / 2; - if (curBoundX < map.getLeft()) - { - curBoundX = map.getLeft(); - } else if (curBoundX + zoom * ZOOM_X_FACTOR >= map.getRight()) - { - curBoundX = map.getRight() - zoom * ZOOM_X_FACTOR; - } - - curBoundY = player_y - zoom * ZOOM_Y_FACTOR / 2; - if (curBoundY < map.getTop()) - { - curBoundY = map.getTop(); - } else if (curBoundY + zoom * ZOOM_Y_FACTOR >= map.getBottom()) - { - curBoundY = map.getBottom() - zoom * ZOOM_Y_FACTOR; - } + loadMap(); int zoomLevel = getZoomLevel(*this); if (zoomLevel == 0) { -- cgit 1.4.1