-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameManager.cpp
212 lines (178 loc) · 5.7 KB
/
GameManager.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "GameManager.h"
#include "ScoreSystem.h"
#include "Sound.h"
#include <iomanip>
std::string GameManager::menuScene = "menu.json";
std::string GameManager::gameScene = "scene.json";
std::string GameManager::prefabs = "prefab.json";
GameManager::GameManager()
{
curScene = menuScene;
sceneManager = new SceneManager();
sceneManager->LoadPrefabs(prefabs);
isGameOver = false;
}
GameManager& GameManager::getInstance()
{
static GameManager instance;
return instance;
}
void GameManager::identifyPlayerAndPlayerSpawner()
{
auto& coordinator = EntityCoordinator::getInstance();
for (auto const& e : sceneManager->entities)
{
if (coordinator.entityHasTag(Tag::PLAYERSPAWNER, e))
{
playerRespawner = e;
}
}
}
EntityID GameManager::PlayerRespawnerID()
{
return playerRespawner;
}
void GameManager::countGameFrame()
{
gameFrameCount++;
}
int GameManager::getCurrGameFrame()
{
return gameFrameCount;
}
bool GameManager::GameIsPaused()
{
return gamePaused;
}
void GameManager::PauseGame()
{
Sound::getInstance().playMusic(THEME);
gamePaused = true;
}
void GameManager::UnpauseGame()
{
Sound::getInstance().playMusic(BATTLE);
gamePaused = false;
}
void GameManager::handleGameOver() {
Sound::getInstance().playMusic(THEME);
isGameOver = true;
json scoreJsonArray = json::parse(FileManager::readScoreFile("scores.json"));
json currentScore;
currentScore["score"] = ScoreSystem::score;
currentScore["timestamp"] = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
scoreJsonArray.push_back(currentScore);
std::sort(scoreJsonArray.begin(), scoreJsonArray.end(),
[](const json& a, const json& b) -> bool {
if (a["score"] > b["score"])
return true;
else if (a["score"] < b["score"])
return false;
else
return a["timestamp"] < b["timestamp"];
}
);
while (scoreJsonArray.size() > 5) {
scoreJsonArray.erase(5);
}
FileManager::writeTextFile("scores.json", scoreJsonArray.dump());
PauseGame();
vector<string> dates = {
"----/--/-- --:--",
"----/--/-- --:--",
"----/--/-- --:--",
"----/--/-- --:--",
"----/--/-- --:--",
};
vector<string> scores = {
"0","0","0","0","0"
};
for (int i = 0; i < 5; i++) {
if (scoreJsonArray.size() > i) {
scores[i] = std::to_string(scoreJsonArray[i]["score"].get<int>());
std::time_t t = scoreJsonArray[i]["timestamp"].get<int>();
std:tm tm;
localtime_s(&tm, &t);
stringstream s;
s << std::put_time(&tm, "%Y/%m/%d %H:%M");
dates[i] = s.str();
}
}
createGameOverOverlay(ScoreSystem::score, dates, scores);
}
/// <summary>
/// Create the game over overlay on top of the current scene.
/// Note that we are only displaying the top 5 highest scores.
/// </summary>
/// <param name="dates">The dates the scores were gotten.</param>
/// <param name="scores">The scores that were gotten.</param>
void GameManager::createGameOverOverlay(int playerScore, vector<string> dates, vector<string> scores) {
GameEntityCreator& creator = GameEntityCreator::getInstance();
float viewHeight = Renderer::getInstance()->getCamera()->getViewHeight();
float viewWidth = Renderer::getInstance()->getCamera()->getViewWidth();
float yPos = viewHeight / 2 - 2; // use this so we can change the starting yPos easily
// create the panel
creator.CreateUI(0, 0, viewHeight * 0.8, viewWidth * 0.6, 0, 0, 0, {Tag::GAME_OVER_UI});
// create the title and user score
creator.CreateText("GAME OVER", 0, yPos, 1, 0, 0, 1.5, { Tag::GAME_OVER_UI });
yPos -= 0.75;
creator.CreateText("SCORE: " + to_string(playerScore), 0, yPos, 1, 1, 1, 1, { Tag::GAME_OVER_UI });
yPos -= 1;
// create the highscore section
creator.CreateText("HIGHSCORES", 0, yPos, 1, 1, 1, 1, { Tag::GAME_OVER_UI });
yPos -= 1;
int highscoresCount = 5;
for (int i = 0; i < highscoresCount; i++) {
string date;
try {
date = dates.at(i);
}
catch (out_of_range) {
date = "-----";
}
string score;
try {
score = scores.at(i);
}
catch (out_of_range) {
score = "0";
}
creator.CreateText(date, -3, yPos, 1, 1, 1, 1, TextAlign::LEFT, { Tag::GAME_OVER_UI });
creator.CreateText(score, 3, yPos, 1, 1, 1, 1, TextAlign::RIGHT, { Tag::GAME_OVER_UI });
yPos -= 0.75;
}
// replay instruction
yPos -= 0.25;
creator.CreateText("J to replay. Q to quit", 0, yPos, 1, 0, 0, 1, {Tag::GAME_OVER_UI});
}
void GameManager::replay() {
isGameOver = false;
hasActiveStar = false;
ScoreSystem::score = 0;
loadScene(gameScene);
Notify(Event::PLAYER_REPLAY, {});
}
void GameManager::Receive(Event e, void* args) {
if (e == Event::INPUT_SHOOT) {
if (isGameOver) {
replay();
}
else if (curScene == menuScene) {
loadScene(gameScene);
}
}
else if (e == Event::PLAYER_DIES) handleGameOver();
}
std::string GameManager::getCurScene() {
return curScene;
}
void GameManager::loadScene(std::string scene) {
if (scene != GameManager::menuScene && scene != GameManager::gameScene) return;
curScene = scene;
EntityCoordinator::getInstance().deactivateAllEntitiesAndPhysicsBodies();
sceneManager->EmptyEntitiesList();
sceneManager->LoadScene(curScene);
sceneManager->CreateEntities();
identifyPlayerAndPlayerSpawner();
UnpauseGame();
}