-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOverScene.cpp
More file actions
97 lines (84 loc) · 3.63 KB
/
GameOverScene.cpp
File metadata and controls
97 lines (84 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// =================
// GAMEOVERSCENE.CPP
// implements the game over scene
// =================
#include "header/GameOverScene.h"
#include "header/GameplayScene.h"
#include "header/MainMenuScene.h"
#include "header/AudioManager.h"
#include "header/GameResources.h"
#include <iostream>
GameOverScene::GameOverScene(SceneManager& manager, AudioManager& audio, int level, int finalScore)
: sceneManager(manager), audioManager(audio), failedLevel(level), score(finalScore) {
// empty constructor body since initialization is done in the member initializer list
}
void GameOverScene::init() {
// load shared road textures from the main atlas
GameResources* res = GameResources::createInstance();
auto& atl = *res->getAtlas();
road.loadFromAtlas(
res->getTexture(),
atl["road"],
atl["road_green_left"],
atl["road_green_right"]
);
if (!font.openFromFile("assets/ka1.ttf")) {
std::cerr << "Failed to load font\n";
}
// red-tinted dark overlay for the game over screen
overlay.setSize({ 800.f, 600.f });
overlay.setFillColor(sf::Color(150, 0, 0, 150));
gameOverText.emplace(font, "GAME OVER", 72);
gameOverText->setFillColor(sf::Color::Red);
gameOverText->setOutlineColor(sf::Color::Yellow);
gameOverText->setOutlineThickness(4.f);
gameOverText->setOrigin({ gameOverText->getLocalBounds().size.x / 2.f, gameOverText->getLocalBounds().size.y / 2.f });
gameOverText->setPosition({ 400.f, 200.f });
restartText.emplace(font, "PRESS ENTER TO RESTART", 24);
restartText->setFillColor(sf::Color::White);
restartText->setOutlineColor(sf::Color::Black);
restartText->setOutlineThickness(2.f);
restartText->setOrigin({ restartText->getLocalBounds().size.x / 2.f, restartText->getLocalBounds().size.y / 2.f });
restartText->setPosition({ 400.f, 400.f });
menuText.emplace(font, "PRESS M FOR MENU", 24);
menuText->setFillColor(sf::Color::Blue);
menuText->setOutlineColor(sf::Color::White);
menuText->setOutlineThickness(2.f);
menuText->setOrigin({ menuText->getLocalBounds().size.x / 2.f, menuText->getLocalBounds().size.y / 2.f });
menuText->setPosition({ 400.f, 450.f });
// displays the player's final score from the failed run
scoreText.emplace(font, "FINAL SCORE: " + std::to_string(score), 20);
scoreText->setFillColor(sf::Color::Yellow);
scoreText->setOutlineColor(sf::Color::Black);
scoreText->setOutlineThickness(2.f);
scoreText->setOrigin({ scoreText->getLocalBounds().size.x / 2.f, 0.f });
scoreText->setPosition({ 400.f, 300.f });
}
void GameOverScene::handleInput(sf::RenderWindow& window) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter)) {
// restart on the same level the player died on
audioManager.stopCrash();
sceneManager.pushScene(std::make_unique<GameplayScene>(sceneManager, audioManager, failedLevel), true);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::M)) {
// return to main menu
audioManager.stopCrash();
sceneManager.pushScene(std::make_unique<MainMenuScene>(sceneManager), true);
}
}
void GameOverScene::update(float deltaTime) {
// toggles restart prompt visibility for flicker effect
flickerTimer += deltaTime;
if (flickerTimer >= FLICKER_RATE) {
showText = !showText;
flickerTimer = 0.f;
}
}
void GameOverScene::render(sf::RenderWindow& window) {
road.draw(window);
window.draw(overlay);
if (gameOverText) window.draw(*gameOverText);
if (showText && restartText) window.draw(*restartText);
if (menuText) window.draw(*menuText);
if (scoreText) window.draw(*scoreText);
}