Repository navigation
Expand file tree
/
Copy pathcGame.cpp
More file actions
196 lines (191 loc) · 4.76 KB
/
Copy pathcGame.cpp
File metadata and controls
196 lines (191 loc) · 4.76 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
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
#include "cGame.h"
#include "cScene.h"
#include "cEntity.h"
#include <memory>
#include <fstream>
#include "cGameControl.h"
#include "cGraphics.h"
#include "cWinScreen.h"
#include "cLooseScreen.h"
using namespace std;
float RandValue();
cGame::cGame() : timeLeftVisual(GetFont(), "", 50)
{
timeLeftVisual.setFillColor(sf::Color::Yellow);
}
cGame& cGame::Get()
{
static cGame game;
return game;
}
void cGame::Start()
{
auto& scene = cScene::Get();
scene.Clear();
auto hero = scene.Spawn("player");
sf::Vector2f sceneSize = scene.GetSize();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
sf::Vector2f pos;
pos.x = sceneSize.x / 4 + i * sceneSize.x / 2;
pos.y = sceneSize.y / 4 + j * sceneSize.y / 2;
auto tree = scene.Spawn("tree");
tree->ChangePosition(pos);
}
}
cGameControl::Get().ControledEntity(hero);
clockGame.restart();
clockQuant.restart();
running = true;
loose = false;
win = false;
pause = false;
cWinScreen::ResetCounter();
cLooseScreen::ResetCounter();
}
void cGame::PauseClock(bool _pauseState)
{
if (_pauseState) clockGame.stop();
else clockGame.start();
}
void cGame::Quant()
{
if (!running) return;
cScene& scene = cScene::Get();
sf::Vector2f sceneSize = scene.GetSize();
if(pause)
{
clockQuant.restart();
return;
}
const float quantPeriodMs = 10.0f;
float deltaQuant = clockQuant.getElapsedTime().asMilliseconds();
float spawnInterval = 5.0f;
switch(difficulty)
{
case cGame::EASY: spawnInterval = 7.0f;
break;
case cGame::NORMAL: spawnInterval = 5.0f;
break;
case cGame::HARD: spawnInterval = 3.0f;
break;
case cGame::INSANE: spawnInterval = 1.0f;
break;
default:
break;
}
if(deltaQuant > quantPeriodMs)
{
if (clockEnemySpawn.getElapsedTime().asSeconds() >= spawnInterval * (RandValue() + 0.5f))
{
std::vector<string> enemyNames = { "enemyAxe", "enemySaw", "enemyChainSaw", "enemyKnife" };
std::vector<Vector2f> enemySpawnPosRel = { {-0.1, -0.1}, {1.1, -0.1}, {-0.1, 1.1}, {1.1, 1.1}, {0.35, 1.1}, {0.35, -0.1}, {0.65, 1.1}, {0.65, -0.1}/*, {0.5, 0.5}, {0.3, 0.2}, {0.7, 0.4}, {0.6, 0.5}, {0.3, 0.1}, {0.1, 0.3}, {0.4, 0.8}*/ };
auto enemy = scene.Spawn(enemyNames[std::rand() % enemyNames.size()]);
auto enemyRelSpawnPos = enemySpawnPosRel[std::rand() % enemySpawnPosRel.size()];
Vector2f enemySpawnPos = { sceneSize.x*enemyRelSpawnPos.x, sceneSize.y * enemyRelSpawnPos.y };
enemy->ChangePosition(enemySpawnPos);
clockEnemySpawn.restart();
}
cGameControl::Get().Quant();
scene.Quant(deltaQuant / 1000.0f); // ms -> sec
CheckGameOver();
clockQuant.restart();
}
}
void cGame::CheckGameOver()
{
if (clockGame.getElapsedTime().asSeconds() >= timeToWinSec)
{
win = true;
return;
}
if (cGameControl::Get().ControledEntity()->Health() <= 0)
{
loose = true;
return;
}
else
{
bool allDead = true;
for (const cEntity* entityToCheck : cScene::Get().Entities())
{
if (entityToCheck->Name() == "tree")
{
if (entityToCheck->Health() > 0)
{
allDead = false;
break;
}
}
}
loose = allDead;
}
}
void cGame::Draw(sf::RenderWindow& window)
{
cScene::Get().Draw(window);
if (!running) return;
cGameControl::Get().Draw(window);
Vector2f windowSize = window.getView().getSize();
timeElapsedSec = clockGame.getElapsedTime().asSeconds();
int timeLeftSec = timeToWinSec - timeElapsedSec;
int minutes = timeLeftSec / 60;
int seconds = timeLeftSec % 60;
timeLeftVisual.setString(std::to_string(minutes) + ":" + std::to_string(seconds));
timeLeftVisual.setPosition({ windowSize.x/2 - 45, 0});
if(!win && !loose) window.draw(timeLeftVisual);
}
const string& AssetsPath();
void cGame::Save(const std::string& _name) const
{
string _path = AssetsPath() + "saves/" + _name + ".txt";
std::ofstream file(_path);
string whatToSave = "time " + std::to_string(timeElapsedSec) + "\n";
whatToSave += cScene::Get().Save();
file.clear();
file << whatToSave;
}
void cGame::Load(const std::string& _name)
{
if (!IsRunning())
{
Start(); Pause(true);
}
string _path = AssetsPath() + "saves/" + _name + ".txt";
auto file = std::fstream(_path);
if (file.is_open())
{
int lineNumb = 0;
vector<string> loadedEntities;
std::string line;
auto words = std::string();
while (getline(file, words))
{
if (lineNumb == 0)
{
stringstream s(words);
string str;
s >> str;
if (str == "time")
{
while (s >> str)
{
SetRemainingTime(std::stof(str));
}
}
lineNumb++;
}
else
{
loadedEntities.push_back(words);
}
}
cScene::Get().Load(loadedEntities);
}
else
{
std::cout << "Failed to load save from: " << _path;
}
}