-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
320 lines (288 loc) · 9.71 KB
/
Copy pathGame.cpp
File metadata and controls
320 lines (288 loc) · 9.71 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "Game.h"
bool Game::Run() {
//some initializations
engine_.GetContext().replay = context_.replay;
engine_.GetContext().level = context_.level;
engine_.GetContext().replayFile = context_.replayFile;
exit_ = false;
SetupSystems();
if(context_.replay){
if(not(ReadHighscoreFile())){
return true;
}
}
LoadLevel();
InitMissileQueue();
ak_->LoadLaunchSound();
ak_->StartTimer();
//////MAIN GAME LOOP
while (!exit_)
{
ak_->NextEvent();
//The eventqueue gets read out as fast as possible
//When the update is called, the most recent mouse and keyinput get passed on via the context
if(ak_->IsSpaceBarPushed()){
engine_.keyInput = Engine::KEY_SPACE;
}
else if(ak_->IsMouseClicked()){
engine_.keyInput = Engine::KEY_MOUSE_DOWN;
}
else if(ak_->IsMouseReleased()){
engine_.keyInput = Engine::KEY_MOUSE_UP;
}
else if(ak_->HasMouseMoved()){
engine_.mouseinput = ak_->GetMouse();
}
//the update is called when the event is a timerevent
if(ak_->IsTimerEvent()){
engine_.Update();
engine_.GetContext().timer += 1;
engine_.keyInput = Engine::KEY_NONE;
}
if(ak_->IsWindowClosed()){
ak_->StopTimer();
exit_ = true;
}
//Was used for debugging so the game could quickly be restarted
if(ak_->IsArrowKeyDownPushed()){
ak_->StopTimer();
exit_= true;
}
if (engine_.GetContext().targetcounter==0){
engine_.Update();
ak_->StopTimer();
EndGame();
}
}
return true;
}
void Game::EndGame(){
Color c(10,10,10);
if(not(context_.replay)){
//Get the highscore of the respective highscorefile
std::ifstream inFile;
if(engine_.GetContext().level == std::string("./assets/levels/level1.txt")){
inFile.open("./assets/highscores/highscore_1.txt");
}
else if(engine_.GetContext().level == std::string("./assets/levels/level2.txt")){
inFile.open("./assets/highscores/highscore_2.txt");
}
else if(engine_.GetContext().level == std::string("./assets/levels/level3.txt")){
inFile.open("./assets/highscores/highscore_3.txt");
}
int highscore = 99999;
for(std::string line; getline(inFile,line);){
if(line == std::string("[SCORE]")){
getline(inFile,line);
std::stringstream ss;
ss << line;
int s;
ss >> s;
highscore = s;
}
}
inFile.close();
//If you got a beter score in the played game overwrite this highscore
if(engine_.GetContext().timer / 60 < highscore){
std::ofstream outFile;
if(engine_.GetContext().level == std::string("./assets/levels/level1.txt")){
outFile.open("./assets/highscores/highscore_1.txt");
}
else if(engine_.GetContext().level == std::string("./assets/levels/level2.txt")){
outFile.open("./assets/highscores/highscore_2.txt");
}
else if(engine_.GetContext().level == std::string("./assets/levels/level3.txt")){
outFile.open("./assets/highscores/highscore_3.txt");
}
outFile << "[LEVEL]" << std::endl;
outFile << engine_.GetContext().level << std::endl;
outFile << "[SEED]" << std::endl;
outFile << std::to_string(engine_.GetContext().seed) << std::endl;
outFile << "[MISSILES]" << std::endl;
for(std::vector<std::string>::iterator it = engine_.GetContext().missiles.begin();it!=engine_.GetContext().missiles.end();++it){
outFile << (*it) << std::endl;
}
outFile << "[ACTIONS]" << std::endl;
for(std::vector<std::string>::iterator it = engine_.GetContext().actions.begin();it!=engine_.GetContext().actions.end();++it){
outFile << (*it) << std::endl;
}
outFile << "[SCORE]" << std::endl;
outFile << std::to_string(engine_.GetContext().timer / 60) << std::endl;
outFile.close();
Point hp(SCREEN_WIDTH/2, SCREEN_HEIGHT/2-100);
std::string h("NEW HIGHSCORE");
ak_->DrawString(h,hp,c,Allkit::ALIGN_CENTER,true);
}
}
//PRESS ENTER to continue code
Point p(SCREEN_WIDTH/2,SCREEN_HEIGHT/2-30);
std::string s("<PRESS ENTER>");
ak_->DrawString(s,p,c,Allkit::ALIGN_CENTER,true);
ak_->DrawOnScreen();
exit_ = false;
while(!exit_){
ak_->NextEvent();
if(ak_->IsEnterKeyPushed() || ak_->IsWindowClosed()){
exit_ = true;
}
}
}
bool Game::ReadHighscoreFile(){
std::ifstream infile;
infile.open(engine_.GetContext().replayFile);
if(!infile){
std::cerr << "Unable to open replayFile: "<<engine_.GetContext().replayFile<<std::endl;
return false;
}
enum reading{
missiles,
actions
}
reading;
int detectedTags = 0;
for(std::string line; getline(infile,line);){
if(line == std::string("[MISSILES]")){
detectedTags += 1;
reading = missiles;
}
else if(line == std::string("[ACTIONS]")){
detectedTags += 1;
reading = actions;
}
else if(line == std::string("[LEVEL]")){
detectedTags += 1;
getline(infile,line);
engine_.GetContext().level = line;
}
else if(line == std::string("[SCORE]")){
detectedTags += 1;
getline(infile,line);
//Do something with score
}
else if(line == std::string("[SEED]")){
detectedTags += 1;
getline(infile,line);
std::stringstream ss;
ss << line;
int s;
ss >> s;
engine_.GetContext().seed = s;
}
else{
switch(reading){
case missiles:
engine_.GetContext().missiles.push_back(line);
break;
case actions:
engine_.GetContext().actions.push_back(line);
}
}
}
infile.close();
//Check if all tags are present
if(detectedTags == 5){
return true;
}
else{
std::cout << "Incorrect file format: not all tags were detected" << std::endl;
return false;
}
}
void Game::SetupSystems(){
//adds the systems to the engine and gives a the engine as an attribute to the systems
LauncherSystem* ls = new LauncherSystem();
ls->SetEngine(&engine_);
engine_.AddSystem(ls);
MissileSystem* ms = new MissileSystem();
ms->SetEngine(&engine_);
engine_.AddSystem(ms);
TargetSystem* ts = new TargetSystem();
ts->SetEngine(&engine_);
engine_.AddSystem(ts);
LevelSystem* lvls = new LevelSystem();
lvls->SetEngine(&engine_);
engine_.AddSystem(lvls);
RenderSystem* rs = new RenderSystem();
rs->SetEngine(&engine_);
engine_.AddSystem(rs);
}
void Game::LoadLevel(){
//Catapult things
Entity* entity = new Entity;
entity->Add(new SpriteComponent(SPRT_LAUNCHER,Point(100,235)));
engine_.AddEntity(entity);
std::ifstream inFile;
inFile.open(engine_.GetContext().level);
if(!inFile){
std::cerr << "Unable to open levelFile: "<<engine_.GetContext().level<<std::endl;
exit(1);
}
char c;
int i =0;
int mx,my,rx,ry;
engine_.GetContext().targetcounter=0;
//loading of the level, creating new levelelements as enities with the right components
//completes the levelmatrix
while(inFile >> c){
mx = i%8;
my = 7 - floor(i/8);
rx = 600 + 35*mx;
ry = 125 + 35*my;
Entity* entity = new Entity;
entity->Add(new LevelElementComponent(Point(mx,my)));
entity->Add(new PositionComponent(Point(rx,ry)));
if(c =='B'){
entity->Add(new BoxComponent());
engine_.AddEntity(entity);
}
else if(c == 'S'){
entity->Add(new StoneComponent());
engine_.AddEntity(entity);
}
else if(c == 'T'){
entity->Add(new TargetComponent());
engine_.AddEntity(entity);
engine_.GetContext().targetcounter+=1; //counts all targets in a level
}
else{
entity = NULL;
}
engine_.GetContext().levelmatrix_[mx][my] = entity;
i++;
}
std::cout <<"aantaltargets"<<" "<<engine_.GetContext().targetcounter<< std::endl; //prints the remaining targets in the terminal
inFile.close();
}
void Game::InitMissileQueue(){
//initialise the whole misselqueue
if(engine_.GetContext().replay){
srand(engine_.GetContext().seed);
}
else{
int r = rand()%1000;
engine_.GetContext().seed = r;
srand(r);
}
Entity* me = new Entity;
me->Add(new PositionComponent(Point(140,220)));
me->Add(new Missile1Component());
me->Add(new MissileQueueComponent(0));
engine_.AddEntity(me);
int r;
for(int i = 1;i<4;i++){
Entity* me = new Entity;
me->Add(new PositionComponent(Point(80 - 40*(i-1),125)));
r = rand()%3;
if(r == 0){
me->Add(new Missile1Component());
}
else if(r == 1){
me->Add(new Missile2Component());
}
else if(r == 2){
me->Add(new Missile3Component());
}
me->Add(new MissileQueueComponent(i));
engine_.AddEntity(me);
}
}