Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case GAMEPLAY:
- {
- if (!knight.GetGameOver() && !hasWon && !paused)
- {
- // advancing timer
- timer += GetFrameTime();
- timer = (int)(timer * 100) / 100.0f;
- }
- // check if player has paused/unpaused
- if (IsKeyPressed(KEY_P))
- {
- paused = !paused;
- }
- // draw map background
- map_pos = Vector2Scale(knight.GetWorldPos(), -1.f);
- DrawTextureEx(map, map_pos, 0.0, map_scale, WHITE);
- // render props
- for (auto prop : props)
- {
- prop.Render(knight.GetWorldPos());
- }
- // only update position if not paused and game over condition not met
- if (!paused && !knight.GetGameOver())
- {
- ResumeSound(Music);
- knight.tick(GetFrameTime());
- // check if player is out of map bounds
- if (knight.GetWorldPos().x < -500.f ||
- knight.GetWorldPos().y < -75.f ||
- knight.GetWorldPos().x + window_width - 500.f > map.width * map_scale ||
- knight.GetWorldPos().y + window_height + -250.f > map.height * map_scale)
- {
- // setting world position equal to world position last frame
- knight.undoMovement();
- }
- // check for prop collisions
- for (auto prop : props)
- {
- if (CheckCollisionRecs(prop.GetCollisionRec(knight.GetWorldPos()), knight.GetCollisionRec()))
- {
- // if collision, undo movement
- knight.undoMovement();
- }
- }
- // tick function for enemies
- for (auto enemy : enemies)
- {
- enemy->tick(GetFrameTime());
- // check for enemy collisions
- if (CheckCollisionRecs(enemy->GetCollisionRec(), knight.GetAttackCollisionRec()))
- {
- enemy->setAlive(false);
- }
- }
- if (IsKeyPressed(KEY_SPACE))
- {
- PlaySoundMulti(Slash);
- }
- // draw timer. "%.2f" is sprintf formatting to only output 2 digits after comma
- DrawText(TextFormat("Time: %.2f", timer), 25, 25, 30, GOLD);
- if (knight.GetHealth() > 0.f)
- {
- DrawText(TextFormat("HEALTH: %.1f", knight.GetHealth()), window_width - 240, 25, 30, RED);
- }
- else
- {
- DrawText(TextFormat("HEALTH: 0"), window_width - 240, 25, 30, RED);
- }
- }
- if (paused)
- {
- DrawText("PAUSED", 520, window_height / 2 - 50, 50, MAROON);
- PauseSound(Music);
- }
- if (knight.GetGameOver())
- {
- // draw game over screen
- DrawRectangle(0, 0, window_width, window_height, BLACK);
- DrawText("GAME OVER", 500, 150, 50, MAROON);
- DrawText("'ESCAPE' TO QUIT", 500, 500, 30, DARKGREEN);
- }
- for (auto enemy : enemies)
- {
- if (!enemy->getAlive())
- {
- enemiesSlain++;
- }
- }
- if (enemiesSlain == 39)
- {
- hasWon = true;
- // draw win screen
- DrawRectangle(0, 0, window_width, window_height, BLACK);
- DrawText("YOU WIN", 500, 50, 50, GOLD);
- DrawText(TextFormat("YOU WON IN: %.2f", timer), 100, 200, 30, MAROON);
- DrawText("WHAT IS YOUR NICKNAME:", 100, 300, 30, DARKGREEN);
- DrawRectangleLines((int)text_box.x, (int)text_box.y, 100, 50, RED);
- DrawText(name, (int)text_box.x + 5, (int)text_box.y + 8, 40, GOLD);
- DrawText("'ENTER' TO SUBMIT TIME", 100, 400, 30, DARKGREEN);
- DrawText("'ESCAPE' TO QUIT", 500, 650, 30, DARKGREEN);
- int key = GetCharPressed();
- // check if more characters have been pressed on the same frame
- while (key > 0)
- {
- // only allow keys in range [32..125]
- if ((key >= 32) && (key <= 125) && (letter_count < 3))
- {
- name[letter_count] = (char)key;
- // add null terminator at the end of the string
- name[letter_count + 1] = '\0';
- letter_count++;
- }
- key = GetCharPressed();
- }
- if (IsKeyPressed(KEY_BACKSPACE))
- {
- letter_count--;
- if (letter_count < 0)
- letter_count = 0;
- name[letter_count] = '\0';
- }
- if (IsKeyPressed(KEY_ENTER))
- {
- showScore = true;
- score = name;
- score += ",";
- float rounded_timer = floor(timer * 100) / 100;
- score += std::to_string(rounded_timer);
- msReply = toLeaderboard(score);
- }
- if (showScore)
- {
- const char* c = msReply.c_str();
- DrawText(c, 100, 500, 40, GOLD);
- }
- }
- enemiesSlain = 0;
- } break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement