summary refs log tree commit diff stats
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp94
1 files changed, 77 insertions, 17 deletions
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)
79 onlyDark); 79 onlyDark);
80} 80}
81 81
82void movePlayer(Game& game, int x, int y) 82bool movePlayer(Game& game, int x, int y)
83{ 83{
84 if (game.map.inBounds(x, y) && game.map.at(x,y).tile == Tile::Floor) 84 if (game.map.inBounds(x, y) && game.map.at(x,y).tile == Tile::Floor)
85 { 85 {
@@ -94,6 +94,10 @@ void movePlayer(Game& game, int x, int y)
94 game.player_y = y; 94 game.player_y = y;
95 95
96 game.dirtyLighting = true; 96 game.dirtyLighting = true;
97
98 return true;
99 } else {
100 return false;
97 } 101 }
98} 102}
99 103
@@ -184,7 +188,7 @@ void recalculateLighting(Game& game)
184 game.dirtyLighting = false; 188 game.dirtyLighting = false;
185} 189}
186 190
187void processKeys(Game& game, const Input& keystate) 191bool processKeys(Game& game, const Input& keystate)
188{ 192{
189 int px = game.player_x; 193 int px = game.player_x;
190 int py = game.player_y; 194 int py = game.player_y;
@@ -211,7 +215,9 @@ void processKeys(Game& game, const Input& keystate)
211 215
212 if (!(game.player_x == px && game.player_y == py)) 216 if (!(game.player_x == px && game.player_y == py))
213 { 217 {
214 movePlayer(game, px, py); 218 return movePlayer(game, px, py);
219 } else {
220 return false;
215 } 221 }
216} 222}
217 223
@@ -460,21 +466,69 @@ int main(int, char**)
460 if (game.map.at(game.player_x, game.player_y).tile == 466 if (game.map.at(game.player_x, game.player_y).tile ==
461 Tile::Floor) 467 Tile::Floor)
462 { 468 {
463 game.map.at(game.player_x, game.player_y).tile = Tile::Lamp; 469 std::vector<coord> freeSpaces;
464 game.numLamps++; 470
465 game.dirtyLighting = true; 471 auto addIfFree = [&] (int x, int y) {
466 kickUpDust(game, game.player_x, game.player_y, 0); 472 if (game.map.inBounds(x,y) &&
467 473 game.map.at(x,y).tile == Tile::Floor)
468 for (int i = 0; i < 5; i++) 474 {
475 freeSpaces.emplace_back(x, y);
476 }
477 };
478
479 addIfFree(game.player_x - 1, game.player_y - 1);
480 addIfFree(game.player_x , game.player_y - 1);
481 addIfFree(game.player_x + 1, game.player_y - 1);
482 addIfFree(game.player_x - 1, game.player_y );
483 addIfFree(game.player_x + 1, game.player_y );
484 addIfFree(game.player_x - 1, game.player_y + 1);
485 addIfFree(game.player_x , game.player_y + 1);
486 addIfFree(game.player_x + 1, game.player_y + 1);
487
488 if (!freeSpaces.empty())
469 { 489 {
470 processKeys(game, keystate); 490 game.map.at(game.player_x, game.player_y).tile = Tile::Lamp;
471 491 game.numLamps++;
472 tick( 492 game.dirtyLighting = true;
473 game, 493 kickUpDust(game, game.player_x, game.player_y, 0);
474 game.player_x - (RADIUS - 1), 494
475 game.player_y - (RADIUS - 1), 495 if (game.firstInput)
476 game.player_x + RADIUS, 496 {
477 game.player_y + RADIUS); 497 for (int i = 0; i < 5; i++)
498 {
499 if (!processKeys(game, game.lastInput))
500 {
501 std::uniform_int_distribution<int> freeDist(
502 0, freeSpaces.size() - 1);
503
504 int freeIndex = freeDist(rng);
505 coord& moveTo = freeSpaces[freeIndex];
506
507 movePlayer(
508 game,
509 std::get<0>(moveTo),
510 std::get<1>(moveTo));
511 }
512
513 tick(
514 game,
515 game.player_x - (RADIUS - 1),
516 game.player_y - (RADIUS - 1),
517 game.player_x + RADIUS,
518 game.player_y + RADIUS);
519 }
520 } else {
521 std::uniform_int_distribution<int> freeDist(
522 0, freeSpaces.size() - 1);
523
524 int freeIndex = freeDist(rng);
525 coord& moveTo = freeSpaces[freeIndex];
526
527 movePlayer(
528 game,
529 std::get<0>(moveTo),
530 std::get<1>(moveTo));
531 }
478 } 532 }
479 } 533 }
480 } 534 }
@@ -491,6 +545,12 @@ int main(int, char**)
491 keystate.up = state[SDL_SCANCODE_UP]; 545 keystate.up = state[SDL_SCANCODE_UP];
492 keystate.down = state[SDL_SCANCODE_DOWN]; 546 keystate.down = state[SDL_SCANCODE_DOWN];
493 547
548 if (keystate.left || keystate.right || keystate.up || keystate.down)
549 {
550 game.firstInput = true;
551 game.lastInput = keystate;
552 }
553
494 dustAcc += frameTime; 554 dustAcc += frameTime;
495 inputAcc += frameTime; 555 inputAcc += frameTime;
496 556