Advertisement
austinwebber

Clash-In-The-Catacombs-Gameplay-Loop

Aug 20th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.73 KB | Source Code | 0 0
  1. case GAMEPLAY:
  2. {
  3.     if (!knight.GetGameOver() && !hasWon && !paused)
  4.     {
  5.         // advancing timer
  6.         timer += GetFrameTime();
  7.         timer = (int)(timer * 100) / 100.0f;
  8.     }
  9.  
  10.     // check if player has paused/unpaused
  11.     if (IsKeyPressed(KEY_P))
  12.     {
  13.         paused = !paused;
  14.     }
  15.  
  16.     // draw map background
  17.     map_pos = Vector2Scale(knight.GetWorldPos(), -1.f);
  18.     DrawTextureEx(map, map_pos, 0.0, map_scale, WHITE);
  19.  
  20.     // render props
  21.     for (auto prop : props)
  22.     {
  23.         prop.Render(knight.GetWorldPos());
  24.     }
  25.  
  26.     // only update position if not paused and game over condition not met
  27.     if (!paused && !knight.GetGameOver())
  28.     {
  29.         ResumeSound(Music);
  30.  
  31.         knight.tick(GetFrameTime());
  32.  
  33.         // check if player is out of map bounds
  34.         if (knight.GetWorldPos().x < -500.f ||
  35.             knight.GetWorldPos().y < -75.f ||
  36.             knight.GetWorldPos().x + window_width - 500.f > map.width * map_scale ||
  37.             knight.GetWorldPos().y + window_height + -250.f > map.height * map_scale)
  38.         {
  39.             // setting world position equal to world position last frame
  40.             knight.undoMovement();
  41.         }
  42.  
  43.         // check for prop collisions
  44.         for (auto prop : props)
  45.         {
  46.             if (CheckCollisionRecs(prop.GetCollisionRec(knight.GetWorldPos()), knight.GetCollisionRec()))
  47.             {
  48.                 // if collision, undo movement
  49.                 knight.undoMovement();
  50.             }
  51.         }
  52.  
  53.         // tick function for enemies
  54.         for (auto enemy : enemies)
  55.         {
  56.             enemy->tick(GetFrameTime());
  57.  
  58.             // check for enemy collisions
  59.             if (CheckCollisionRecs(enemy->GetCollisionRec(), knight.GetAttackCollisionRec()))
  60.             {
  61.                 enemy->setAlive(false);
  62.             }
  63.         }
  64.  
  65.         if (IsKeyPressed(KEY_SPACE))
  66.         {
  67.             PlaySoundMulti(Slash);
  68.         }
  69.  
  70.         // draw timer. "%.2f" is sprintf formatting to only output 2 digits after comma
  71.         DrawText(TextFormat("Time: %.2f", timer), 25, 25, 30, GOLD);
  72.         if (knight.GetHealth() > 0.f)
  73.         {
  74.             DrawText(TextFormat("HEALTH: %.1f", knight.GetHealth()), window_width - 240, 25, 30, RED);
  75.         }
  76.         else
  77.         {
  78.             DrawText(TextFormat("HEALTH: 0"), window_width - 240, 25, 30, RED);
  79.         }
  80.     }
  81.  
  82.     if (paused)
  83.     {
  84.         DrawText("PAUSED", 520, window_height / 2 - 50, 50, MAROON);
  85.         PauseSound(Music);
  86.     }
  87.  
  88.     if (knight.GetGameOver())
  89.     {
  90.         // draw game over screen
  91.         DrawRectangle(0, 0, window_width, window_height, BLACK);
  92.         DrawText("GAME OVER", 500, 150, 50, MAROON);
  93.         DrawText("'ESCAPE' TO QUIT", 500, 500, 30, DARKGREEN);
  94.     }
  95.  
  96.     for (auto enemy : enemies)
  97.     {
  98.         if (!enemy->getAlive())
  99.         {
  100.             enemiesSlain++;
  101.         }
  102.     }
  103.  
  104.     if (enemiesSlain == 39)
  105.     {
  106.         hasWon = true;
  107.  
  108.         // draw win screen
  109.         DrawRectangle(0, 0, window_width, window_height, BLACK);
  110.         DrawText("YOU WIN", 500, 50, 50, GOLD);
  111.         DrawText(TextFormat("YOU WON IN: %.2f", timer), 100, 200, 30, MAROON);
  112.         DrawText("WHAT IS YOUR NICKNAME:", 100, 300, 30, DARKGREEN);
  113.         DrawRectangleLines((int)text_box.x, (int)text_box.y, 100, 50, RED);
  114.         DrawText(name, (int)text_box.x + 5, (int)text_box.y + 8, 40, GOLD);
  115.         DrawText("'ENTER' TO SUBMIT TIME", 100, 400, 30, DARKGREEN);
  116.         DrawText("'ESCAPE' TO QUIT", 500, 650, 30, DARKGREEN);
  117.  
  118.         int key = GetCharPressed();
  119.  
  120.         // check if more characters have been pressed on the same frame
  121.         while (key > 0)
  122.         {
  123.             // only allow keys in range [32..125]
  124.             if ((key >= 32) && (key <= 125) && (letter_count < 3))
  125.             {
  126.                 name[letter_count] = (char)key;
  127.                 // add null terminator at the end of the string
  128.                 name[letter_count + 1] = '\0';
  129.                 letter_count++;
  130.             }
  131.  
  132.             key = GetCharPressed();
  133.         }
  134.  
  135.         if (IsKeyPressed(KEY_BACKSPACE))
  136.         {
  137.             letter_count--;
  138.             if (letter_count < 0)
  139.                 letter_count = 0;
  140.             name[letter_count] = '\0';
  141.         }
  142.  
  143.         if (IsKeyPressed(KEY_ENTER))
  144.         {
  145.             showScore = true;
  146.             score = name;
  147.             score += ",";
  148.             float rounded_timer = floor(timer * 100) / 100;
  149.             score += std::to_string(rounded_timer);
  150.             msReply = toLeaderboard(score);
  151.         }
  152.  
  153.         if (showScore)
  154.         {
  155.             const char* c = msReply.c_str();
  156.             DrawText(c, 100, 500, 40, GOLD);
  157.         }
  158.     }
  159.     enemiesSlain = 0;
  160.  
  161. } break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement