summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-05-29 17:11:12 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-05-29 17:11:12 -0400
commit9b1c8e98592cc0e5cec471720498e7bb2a60a449 (patch)
tree324b79d54fd4e6e83151f2deaca8a8c0beed75bd
parent72e83df4876d799cc7b7993813533041a112e250 (diff)
downloadether-9b1c8e98592cc0e5cec471720498e7bb2a60a449.tar.gz
ether-9b1c8e98592cc0e5cec471720498e7bb2a60a449.tar.bz2
ether-9b1c8e98592cc0e5cec471720498e7bb2a60a449.zip
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.
-rw-r--r--src/main.cpp189
1 files 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 {
45 Lamp 45 Lamp
46}; 46};
47 47
48enum class LoseState {
49 None,
50 PoppingLamps,
51 PoppingPlayer,
52 Outro
53};
54
48const int GAME_WIDTH = 640*2; 55const int GAME_WIDTH = 640*2;
49const int GAME_HEIGHT = 480*2; 56const int GAME_HEIGHT = 480*2;
50const int TILE_WIDTH = 8*2; 57const int TILE_WIDTH = 8*2;
@@ -85,10 +92,13 @@ public:
85 std::list<Kickup> kickups; 92 std::list<Kickup> kickups;
86 int lightedSpots = 0; 93 int lightedSpots = 0;
87 bool dirtyLighting = true; 94 bool dirtyLighting = true;
95 size_t numLamps = 0;
96 size_t numDust = 0;
88}; 97};
89 98
90int player_x = VIEW_WIDTH / 2; 99int player_x = VIEW_WIDTH / 2;
91int player_y = VIEW_HEIGHT / 2; 100int player_y = VIEW_HEIGHT / 2;
101bool renderPlayer = true;
92 102
93void render( 103void render(
94 SDL_Renderer* ren, 104 SDL_Renderer* ren,
@@ -104,7 +114,7 @@ void render(
104 { 114 {
105 bool draw = true; 115 bool draw = true;
106 116
107 if (player_x == x && player_y == y) 117 if ((player_x == x && player_y == y) && renderPlayer)
108 { 118 {
109 SDL_SetRenderDrawColor(ren, 255, 255, 0, 255); 119 SDL_SetRenderDrawColor(ren, 255, 255, 0, 255);
110 } else if (!map.lighting.at(x+VIEW_WIDTH*y)) 120 } else if (!map.lighting.at(x+VIEW_WIDTH*y))
@@ -217,6 +227,7 @@ void movePlayer(int x, int y, Map& map)
217 if (map.tiles[player_x+player_y*VIEW_WIDTH] == Tile::Floor) 227 if (map.tiles[player_x+player_y*VIEW_WIDTH] == Tile::Floor)
218 { 228 {
219 map.tiles[player_x+player_y*VIEW_WIDTH] = Tile::Dust; 229 map.tiles[player_x+player_y*VIEW_WIDTH] = Tile::Dust;
230 map.numDust++;
220 } 231 }
221 232
222 player_x = x; 233 player_x = x;
@@ -279,7 +290,7 @@ void recalculateLighting(Map& map, fov_settings_type* fov)
279 { 290 {
280 for (int x = 0; x < VIEW_WIDTH; x++) 291 for (int x = 0; x < VIEW_WIDTH; x++)
281 { 292 {
282 if ((player_x == x && player_y == y) || 293 if ((player_x == x && player_y == y && renderPlayer) ||
283 map.tiles[x+VIEW_WIDTH*y] == Tile::Dust || 294 map.tiles[x+VIEW_WIDTH*y] == Tile::Dust ||
284 map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) 295 map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp)
285 { 296 {
@@ -327,7 +338,6 @@ void processKeys(Map& map, const Input& keystate)
327 338
328void kickUpDust(Map& map, int x, int y, size_t chain) 339void kickUpDust(Map& map, int x, int y, size_t chain)
329{ 340{
330 std::cout << "kickup " << x << " " << y << " " << chain << std::endl;
331 Kickup dk; 341 Kickup dk;
332 dk.x = x; 342 dk.x = x;
333 dk.y = y; 343 dk.y = y;
@@ -338,6 +348,20 @@ void kickUpDust(Map& map, int x, int y, size_t chain)
338 map.kickups.push_back(dk); 348 map.kickups.push_back(dk);
339} 349}
340 350
351void popLamp(Map& map, int x, int y, size_t chain)
352{
353 if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp)
354 {
355 map.numLamps--;
356 }
357
358 map.tiles[x+VIEW_WIDTH*y] = Tile::Dust;
359 map.numDust++;
360 map.dirtyLighting = true;
361
362 kickUpDust(map, x, y, chain);
363}
364
341void processKickup(Map& map) 365void processKickup(Map& map)
342{ 366{
343 for (Kickup& kickup : map.kickups) 367 for (Kickup& kickup : map.kickups)
@@ -347,6 +371,7 @@ void processKickup(Map& map)
347 if (map.tiles[kickup.x+VIEW_WIDTH*kickup.y] == Tile::Floor) 371 if (map.tiles[kickup.x+VIEW_WIDTH*kickup.y] == Tile::Floor)
348 { 372 {
349 map.tiles[kickup.x+VIEW_WIDTH*kickup.y] = Tile::Dust; 373 map.tiles[kickup.x+VIEW_WIDTH*kickup.y] = Tile::Dust;
374 map.numDust++;
350 } 375 }
351 376
352 std::unique_ptr<fov_settings_type> dusty(new fov_settings_type); 377 std::unique_ptr<fov_settings_type> dusty(new fov_settings_type);
@@ -373,13 +398,11 @@ void processKickup(Map& map)
373 if (map.tiles[x+VIEW_WIDTH*y] == Tile::Floor) 398 if (map.tiles[x+VIEW_WIDTH*y] == Tile::Floor)
374 { 399 {
375 map.tiles[x+VIEW_WIDTH*y] = Tile::Dust; 400 map.tiles[x+VIEW_WIDTH*y] = Tile::Dust;
401 map.numDust++;
376 map.dirtyLighting = true; 402 map.dirtyLighting = true;
377 } else if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) 403 } else if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp)
378 { 404 {
379 map.tiles[x+VIEW_WIDTH*y] = Tile::Dust; 405 popLamp(map, x, y, kickup.chain + 1);
380 map.dirtyLighting = true;
381
382 kickUpDust(map, x, y, kickup.chain + 1);
383 } 406 }
384 } 407 }
385 }); 408 });
@@ -454,6 +477,7 @@ int main(int, char**)
454 tick(map); 477 tick(map);
455 478
456 bool quit = false; 479 bool quit = false;
480 LoseState losing = LoseState::None;
457 Input keystate; 481 Input keystate;
458 SDL_Event e; 482 SDL_Event e;
459 483
@@ -463,6 +487,12 @@ int main(int, char**)
463 size_t inputDt = 50; 487 size_t inputDt = 50;
464 size_t inputAcc = 0; 488 size_t inputAcc = 0;
465 489
490 size_t losePopLampDt = 1000;
491 size_t losePopLampAcc = losePopLampDt;
492
493 size_t losePopPlayerDt = 4000;
494 size_t losePopPlayerAcc = 0;
495
466 size_t lastTime = SDL_GetTicks(); 496 size_t lastTime = SDL_GetTicks();
467 497
468 while (!quit) 498 while (!quit)
@@ -471,50 +501,60 @@ int main(int, char**)
471 size_t frameTime = currentTime - lastTime; 501 size_t frameTime = currentTime - lastTime;
472 lastTime = currentTime; 502 lastTime = currentTime;
473 503
474 //bool input = false;
475 //int presses = 0;
476 bool pressedSpace = false;
477 while (SDL_PollEvent(&e)) 504 while (SDL_PollEvent(&e))
478 { 505 {
479 if (e.type == SDL_QUIT) 506 if (e.type == SDL_QUIT)
480 { 507 {
481 quit = true; 508 if (losing != LoseState::None)
509 {
510 quit = true;
511 } else {
512 losing = LoseState::PoppingLamps;
513 }
482 } else if (e.type == SDL_KEYDOWN) 514 } else if (e.type == SDL_KEYDOWN)
483 { 515 {
484 //presses++;
485
486 switch (e.key.keysym.sym) 516 switch (e.key.keysym.sym)
487 { 517 {
488 case SDLK_ESCAPE: 518 case SDLK_ESCAPE:
489 { 519 {
490 quit = true; 520 if (losing != LoseState::None)
521 {
522 quit = true;
523 } else {
524 losing = LoseState::PoppingLamps;
525 }
526
491 break; 527 break;
492 } 528 }
493 529
494 case SDLK_SPACE: 530 case SDLK_SPACE:
495 { 531 {
496 pressedSpace = true; 532 if (losing == LoseState::None)
497
498 setIfValid(map, player_x , player_y , Tile::Lamp);
499
500 for (int i = 0; i < 5; i++)
501 { 533 {
502 processKeys(map, keystate); 534 if (map.tiles[player_x+VIEW_WIDTH*player_y] == Tile::Floor)
503 535 {
504 tick( 536 map.tiles[player_x+VIEW_WIDTH*player_y] = Tile::Lamp;
505 map, 537 map.numLamps++;
506 player_x - (RADIUS - 1), 538 map.dirtyLighting = true;
507 player_y - (RADIUS - 1), 539 kickUpDust(map, player_x, player_y, 0);
508 player_x + RADIUS, 540
509 player_y + RADIUS); 541 for (int i = 0; i < 5; i++)
510 542 {
511 //render(ren.get(), map, false); 543 processKeys(map, keystate);
512 //SDL_Delay(30); 544
545 tick(
546 map,
547 player_x - (RADIUS - 1),
548 player_y - (RADIUS - 1),
549 player_x + RADIUS,
550 player_y + RADIUS);
551
552 //render(ren.get(), map, false);
553 //SDL_Delay(30);
554 }
555 }
513 } 556 }
514 557
515 map.dirtyLighting = true;
516 kickUpDust(map, player_x, player_y, 0);
517
518 break; 558 break;
519 } 559 }
520 } 560 }
@@ -527,8 +567,6 @@ int main(int, char**)
527 keystate.up = state[SDL_SCANCODE_UP]; 567 keystate.up = state[SDL_SCANCODE_UP];
528 keystate.down = state[SDL_SCANCODE_DOWN]; 568 keystate.down = state[SDL_SCANCODE_DOWN];
529 569
530 bool input = keystate.left || keystate.right || keystate.up || keystate.down || pressedSpace;
531
532 dustAcc += frameTime; 570 dustAcc += frameTime;
533 inputAcc += frameTime; 571 inputAcc += frameTime;
534 572
@@ -546,16 +584,89 @@ int main(int, char**)
546 } 584 }
547 } 585 }
548 586
587 map.numDust = 0;
588
549 processKickup(map); 589 processKickup(map);
550 590
551 dustAcc -= dustDt; 591 dustAcc -= dustDt;
552 } 592 }
553 593
554 while (inputAcc >= inputDt) 594 switch (losing)
555 { 595 {
556 processKeys(map, keystate); 596 case LoseState::None:
597 {
598 while (inputAcc >= inputDt)
599 {
600 processKeys(map, keystate);
601
602 inputAcc -= inputDt;
603 }
557 604
558 inputAcc -= inputDt; 605 break;
606 }
607
608 case LoseState::PoppingLamps:
609 {
610 if (map.numLamps == 0)
611 {
612 if (map.numDust == 0)
613 {
614 losing = LoseState::PoppingPlayer;
615 }
616 } else {
617 losePopLampAcc += frameTime;
618
619 while (losePopLampAcc >= losePopLampDt)
620 {
621 std::vector<std::tuple<int, int>> lamps;
622
623 for (int y = 0; y < VIEW_HEIGHT; y++)
624 {
625 for (int x = 0; x < VIEW_WIDTH; x++)
626 {
627 if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp)
628 {
629 lamps.emplace_back(x, y);
630 }
631 }
632 }
633
634 std::uniform_int_distribution<int> lampDist(0, lamps.size() - 1);
635 std::tuple<int, int> popPos = lamps[lampDist(rng)];
636
637 popLamp(map, std::get<0>(popPos), std::get<1>(popPos), 0);
638
639 losePopLampAcc -= losePopLampDt;
640 }
641 }
642
643 break;
644 }
645
646 case LoseState::PoppingPlayer:
647 {
648 losePopPlayerAcc += frameTime;
649
650 if (losePopPlayerAcc >= losePopPlayerDt)
651 {
652 popLamp(map, player_x, player_y, 10);
653 renderPlayer = false;
654
655 losing = LoseState::Outro;
656 }
657
658 break;
659 }
660
661 case LoseState::Outro:
662 {
663 if (map.numDust == 0)
664 {
665 quit = true;
666 }
667
668 break;
669 }
559 } 670 }
560 671
561 if (map.dirtyLighting) 672 if (map.dirtyLighting)
@@ -594,4 +705,4 @@ int main(int, char**)
594 SDL_Quit(); 705 SDL_Quit();
595 706
596 return 0; 707 return 0;
597} \ No newline at end of file 708}