diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2022-03-14 14:11:05 -0400 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2022-03-14 14:11:05 -0400 |
commit | 7cd515fcfa1a5aac5605fcc63381033563fbf5d7 (patch) | |
tree | 2a375db2fe6c1476a111c1f3e83e50fd2b68124e /src/game.cpp | |
parent | 042da1570181dc80ee76860582f6a3c97641c14a (diff) | |
download | ether-7cd515fcfa1a5aac5605fcc63381033563fbf5d7.tar.gz ether-7cd515fcfa1a5aac5605fcc63381033563fbf5d7.tar.bz2 ether-7cd515fcfa1a5aac5605fcc63381033563fbf5d7.zip |
map is now "infinite" using chunks that dynamically load like in diamond&pearl
game is also slow as shit now
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 123 |
1 files changed, 37 insertions, 86 deletions
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 @@ | |||
8 | 8 | ||
9 | Game::Game(std::mt19937& rng, Muxer& muxer) : | 9 | Game::Game(std::mt19937& rng, Muxer& muxer) : |
10 | rng(rng), | 10 | rng(rng), |
11 | muxer(muxer), | 11 | muxer(muxer) |
12 | map( | ||
13 | -INIT_ZOOM * ZOOM_X_FACTOR / 2, | ||
14 | -INIT_ZOOM * ZOOM_Y_FACTOR / 2, | ||
15 | INIT_ZOOM * ZOOM_X_FACTOR, | ||
16 | INIT_ZOOM * ZOOM_Y_FACTOR) | ||
17 | { | 12 | { |
18 | losePopLampTimer.accumulate(losePopLampTimer.getDt()); | 13 | losePopLampTimer.accumulate(losePopLampTimer.getDt()); |
19 | 14 | ||
20 | for (MapData& md : map.data()) | 15 | loadMap(); |
21 | { | ||
22 | if (std::bernoulli_distribution(0.5)(rng)) | ||
23 | { | ||
24 | md.tile = Tile::Wall; | ||
25 | } | ||
26 | } | ||
27 | |||
28 | tick(); | ||
29 | tick(); | ||
30 | 16 | ||
31 | for (int y = -1; y <= 1; y++) | 17 | for (int y = -1; y <= 1; y++) |
32 | { | 18 | { |
@@ -39,12 +25,12 @@ Game::Game(std::mt19937& rng, Muxer& muxer) : | |||
39 | tick(); | 25 | tick(); |
40 | } | 26 | } |
41 | 27 | ||
42 | inline bool isTileSetOrNotLit(const Map<MapData>& map, int x, int y) | 28 | inline bool isTileSetOrNotLit(const Map& map, int x, int y) |
43 | { | 29 | { |
44 | return (map.inBounds(x, y) && (map.at(x,y).tile == Tile::Wall || !map.at(x,y).lit)); | 30 | return (map.inBounds(x, y) && (map.at(x,y).tile == Tile::Wall || !map.at(x,y).lit)); |
45 | } | 31 | } |
46 | 32 | ||
47 | inline bool isTileSet(const Map<MapData>& map, int x, int y, Tile val = Tile::Wall) | 33 | inline bool isTileSet(const Map& map, int x, int y, Tile val = Tile::Wall) |
48 | { | 34 | { |
49 | return (map.inBounds(x, y) && map.at(x,y).tile == val); | 35 | return (map.inBounds(x, y) && map.at(x,y).tile == val); |
50 | } | 36 | } |
@@ -69,7 +55,7 @@ void Game::tick( | |||
69 | bool invert, | 55 | bool invert, |
70 | bool onlyDark) | 56 | bool onlyDark) |
71 | { | 57 | { |
72 | Map<MapData> temp(map); | 58 | std::vector<MapData> temp(map.data()); |
73 | 59 | ||
74 | for (int y = map.getTop(); y < map.getBottom(); y++) | 60 | for (int y = map.getTop(); y < map.getBottom(); y++) |
75 | { | 61 | { |
@@ -102,21 +88,22 @@ void Game::tick( | |||
102 | incrementIfSet(*this, count, x+1, y ); | 88 | incrementIfSet(*this, count, x+1, y ); |
103 | incrementIfSet(*this, count, x+1, y+1); | 89 | incrementIfSet(*this, count, x+1, y+1); |
104 | 90 | ||
91 | int tempIndex = map.getTrueX(x) + map.getTrueY(y) * map.getWidth(); | ||
105 | if (count >= 5) | 92 | if (count >= 5) |
106 | { | 93 | { |
107 | temp.at(x,y).tile = Tile::Wall; | 94 | temp[tempIndex].tile = Tile::Wall; |
108 | } else { | 95 | } else { |
109 | temp.at(x,y).tile = Tile::Floor; | 96 | temp[tempIndex].tile = Tile::Floor; |
110 | } | 97 | } |
111 | 98 | ||
112 | if (temp.at(x,y).tile != map.at(x,y).tile) { | 99 | if (temp[tempIndex].tile != map.at(x,y).tile) { |
113 | temp.at(x,y).dirtyRender = true; | 100 | temp[tempIndex].dirtyRender = true; |
114 | dirtyRender = true; | 101 | dirtyRender = true; |
115 | } | 102 | } |
116 | } | 103 | } |
117 | } | 104 | } |
118 | 105 | ||
119 | map = std::move(temp); | 106 | map.data() = std::move(temp); |
120 | } | 107 | } |
121 | 108 | ||
122 | void Game::tick(bool onlyDark) | 109 | void Game::tick(bool onlyDark) |
@@ -132,10 +119,10 @@ void Game::tick(bool onlyDark) | |||
132 | 119 | ||
133 | bool Game::movePlayer(int x, int y) | 120 | bool Game::movePlayer(int x, int y) |
134 | { | 121 | { |
135 | if (x >= curBoundX && | 122 | if (/*x >= curBoundX && |
136 | y >= curBoundY && | 123 | y >= curBoundY && |
137 | x < curBoundX + curZoom * ZOOM_X_FACTOR && | 124 | x < curBoundX + curZoom * ZOOM_X_FACTOR && |
138 | y < curBoundY + curZoom * ZOOM_Y_FACTOR && | 125 | y < curBoundY + curZoom * ZOOM_Y_FACTOR &&*/ |
139 | map.at(x,y).tile == Tile::Floor) | 126 | map.at(x,y).tile == Tile::Floor) |
140 | { | 127 | { |
141 | if (map.at(player_x, player_y).tile == Tile::Floor) | 128 | if (map.at(player_x, player_y).tile == Tile::Floor) |
@@ -154,6 +141,13 @@ bool Game::movePlayer(int x, int y) | |||
154 | moveProgress.start(66); | 141 | moveProgress.start(66); |
155 | dirtyLighting = true; | 142 | dirtyLighting = true; |
156 | 143 | ||
144 | int chunkX, chunkY, old_chunkX, old_chunkY; | ||
145 | toChunkPos(player_x, player_y, chunkX, chunkY); | ||
146 | toChunkPos(player_oldx, player_oldy, old_chunkX, old_chunkY); | ||
147 | if ((chunkX != old_chunkX) || (chunkY != old_chunkY)) { | ||
148 | loadMap(); | ||
149 | } | ||
150 | |||
157 | return true; | 151 | return true; |
158 | } else { | 152 | } else { |
159 | if (!alreadyBumped) { | 153 | if (!alreadyBumped) { |
@@ -501,39 +495,25 @@ void Game::processKickup() | |||
501 | }); | 495 | }); |
502 | } | 496 | } |
503 | 497 | ||
504 | void Game::growMap(size_t zoom) | 498 | void Game::loadMap() { |
505 | { | 499 | int newChunksHoriz = std::ceil(static_cast<double>(curZoom) * ZOOM_X_FACTOR / CHUNK_WIDTH) + 4; |
506 | int ol = map.getLeft(); | 500 | int newChunksVert = std::ceil(static_cast<double>(curZoom) * ZOOM_Y_FACTOR / CHUNK_HEIGHT) + 4; |
507 | int ot = map.getTop(); | 501 | int curPlayerChunkX, curPlayerChunkY; |
508 | int ow = map.getWidth(); | 502 | toChunkPos(player_x, player_y, curPlayerChunkX, curPlayerChunkY); |
509 | int oh = map.getHeight(); | ||
510 | 503 | ||
511 | map.resize( | 504 | map.load( |
512 | -zoom * ZOOM_X_FACTOR / 2, | 505 | curPlayerChunkX - newChunksHoriz / 2, |
513 | -zoom * ZOOM_Y_FACTOR / 2, | 506 | curPlayerChunkY - newChunksVert / 2, |
514 | zoom * ZOOM_X_FACTOR, | 507 | newChunksHoriz, |
515 | zoom * ZOOM_Y_FACTOR); | 508 | newChunksVert, |
509 | rng); | ||
516 | 510 | ||
517 | maxZoom = zoom; | 511 | tick(); |
518 | 512 | tick(); | |
519 | for (int y = map.getTop(); y < map.getBottom(); y++) | 513 | tick(); |
520 | { | ||
521 | for (int x = map.getLeft(); x < map.getRight(); x++) | ||
522 | { | ||
523 | if (!(x >= ol && x < (ol + ow) && y >= ot && y < (ot + oh))) | ||
524 | { | ||
525 | if (std::bernoulli_distribution(0.5)(rng)) | ||
526 | { | ||
527 | map.at(x,y).tile = Tile::Wall; | ||
528 | } | ||
529 | } | ||
530 | } | ||
531 | } | ||
532 | 514 | ||
533 | for (int i = 0; i < 3; i++) | 515 | dirtyLighting = true; |
534 | { | 516 | dirtyRender = true; |
535 | tick(ol, ot, ol + ow, ot + oh, true); | ||
536 | } | ||
537 | } | 517 | } |
538 | 518 | ||
539 | void Game::setZoom(size_t zoom) | 519 | void Game::setZoom(size_t zoom) |
@@ -543,40 +523,11 @@ void Game::setZoom(size_t zoom) | |||
543 | return; | 523 | return; |
544 | } | 524 | } |
545 | 525 | ||
546 | if (zoom > maxZoom) | ||
547 | { | ||
548 | growMap(zoom); | ||
549 | } | ||
550 | |||
551 | std::tie( | ||
552 | lastZoomLeft, | ||
553 | lastZoomTop, | ||
554 | lastZoomWidth, | ||
555 | lastZoomHeight) = | ||
556 | Renderer::calculateZoomRect(*this); | ||
557 | |||
558 | zoomProgress = 0; | 526 | zoomProgress = 0; |
559 | zoomLength = std::abs(static_cast<long>(zoom - curZoom)) * TILE_WIDTH; | 527 | zoomLength = std::abs(static_cast<long>(zoom - curZoom)) * TILE_WIDTH; |
560 | curZoom = zoom; | 528 | curZoom = zoom; |
561 | zooming = true; | 529 | zooming = true; |
562 | 530 | loadMap(); | |
563 | curBoundX = player_x - zoom * ZOOM_X_FACTOR / 2; | ||
564 | if (curBoundX < map.getLeft()) | ||
565 | { | ||
566 | curBoundX = map.getLeft(); | ||
567 | } else if (curBoundX + zoom * ZOOM_X_FACTOR >= map.getRight()) | ||
568 | { | ||
569 | curBoundX = map.getRight() - zoom * ZOOM_X_FACTOR; | ||
570 | } | ||
571 | |||
572 | curBoundY = player_y - zoom * ZOOM_Y_FACTOR / 2; | ||
573 | if (curBoundY < map.getTop()) | ||
574 | { | ||
575 | curBoundY = map.getTop(); | ||
576 | } else if (curBoundY + zoom * ZOOM_Y_FACTOR >= map.getBottom()) | ||
577 | { | ||
578 | curBoundY = map.getBottom() - zoom * ZOOM_Y_FACTOR; | ||
579 | } | ||
580 | 531 | ||
581 | int zoomLevel = getZoomLevel(*this); | 532 | int zoomLevel = getZoomLevel(*this); |
582 | if (zoomLevel == 0) { | 533 | if (zoomLevel == 0) { |