-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.h
484 lines (462 loc) · 13.5 KB
/
room.h
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#ifndef ROOM_H_
#define ROOM_H_
#include <QPainter>
#include <QRect>
#include <QImage>
#include <string>
#include <fstream>
#include "wall.h"
#include "ground.h"
#include "block.h"
#include "torch.h"
#include "door.h"
#include "hole.h"
#include "enemy.h"
#include "switch.h"
#include "key.h"
#include "heart.h"
#include "warp.h"
#include "shot.h"
#include <vector>
#include <ctime>
class Room { //I've said before many times, but all the magic happens here.
private:
std::string name;
std::ifstream in;
//QImage image;
std::string safe; //Squares you can walk on.
//QRect rect;
LoZObject* data[15][11]; //Every single object in the room. If it's not defined by external input file, it is Ground
std::vector<LoZObject*> ens; //All the enemies
bool visited; //Prevents some enemies from respawning
LoZObject* shot; //The shot
public:
void setVisited(bool b) {
visited = b;
}
void init() { //Reinitializes based on the input file
ens.clear();
in.open((name+".in").c_str());
for (int i = 0; i < 15; i++)
for (int j = 0; j < 11; j++)
data[i][j] = new Ground(30 * i, 30 * j);
int a, b;
char c;
int d, e, f;
while (!in.eof()) { //Reads input file for what LoZObjects to place in the room.
in >> a >> b >> c;
if (c == 'W') {
in >> d >> e >> f;
for (int i = a; i <= e; i++)
for (int j = b; j <= f; j++)
data[i][j] = new Wall(30 * i, 30 * j, d);
}
else if (c == 'B') {
data[a][b] = new Block(30 * a, 30 * b, false);
}
else if (c == '@') {
data[a][b] = new Torch(30 * a, 30 * b);
}
else if (c == 'D') {
in >> d >> e;
data[a][b] = new Door(30 * a, 30 * b, d, e);
}
else if (c == 'M') {
data[a][b] = new Block(30 * a, 30 * b, true);
}
else if (c == 'H') {
//data[a][b] = new Hole (30 * a, 30 * b);
in >> e >> f;
for (int i = a; i <= e; i++)
for (int j = b; j <= f; j++)
data[i][j] = new Hole(30 * i, 30 * j);
}
else if (c == 'E') {
in >> d;
//data[a][b] = new Enemy (30 * a, 30 * b, d);
if (!visited) {
ens.push_back(new Enemy(30 * a, 30 * b, d));
data[a][b] = ens[ens.size()-1];
}
}
else if (c == 'S') {
in >> d >> e;
data[a][b] = new Switch(30 * a, 30 * b, d == 1, e);
}
else if (c == '<') {
data[a][b] = new Heart(30 * a, 30 * b);
}
else if(c == '?') {
in >> d;
data[a][b] = new Warp(30 * a, 30 * b, d);
}
}
in.close();
}
Room(std::string fname) {
srand(time(NULL));
ens.clear();
safe = "GESDHK<?";
name = fname;
shot = new Shot(0, 0);
//image.load((fname+".png").c_str());
//rect = image.rect();
init();
visited = false;
}
void draw(QPainter& g) { //Draws everything
for (int i = 0; i < 15; i++)
for (int j = 0; j < 11; j++)
if (data[i][j] != NULL)
data[i][j]->draw(g);
shot->draw(g);
data[0][0]->draw(g);
if (name == "room/room1i") { //If at the game over room...
g.setPen(QPen(QColor(0,0,0)));
g.setBrush(QBrush(QColor(0,0,0), Qt::SolidPattern));
QFont f("Courier", 15);
QFontMetrics fo(f);
int tew = fo.width("End of the line");
g.setFont(f);
g.translate(QPoint(225, 60));
g.drawText(-tew/2, 0, "End of the line");
tew = fo.width("Play Again?");
g.translate(QPoint(0, 30));
g.drawText(-tew/2, 0, "Play Again?");
tew = fo.width("Yes No");
g.translate(QPoint(0, 30));
g.drawText(-tew/2, 0, "Yes No");
g.translate(QPoint(-225, -120));
}
}
LoZObject* getMap(int i, int j) { //Unused function...
return data[i][j];
}
bool conts(std::string s, char c) { //Because C++ doesn't have a string.contains(char) function naturally.
for (int i = 0; i < s.size(); i++)
if (s[i] == c)
return true;
return false;
}
void move (int m, int n, int dir) { //Moves a movable block if possible
if (name == "room/room1g")
if (data[10][3]->getID() == 'H')
return;
switch (dir) {
case 2: if (conts(safe, data[m][n+1]->getID())) {
data[m][n] = new Ground(30 * m, 30 * n);
if (data[m][n+1]->getID() != 'H')
data[m][n+1] = new Block(30 * m, 30 * (n+1), false);
}
break;
case 4: if (conts(safe, data[m-1][n]->getID())) {
data[m][n] = new Ground(30 * m, 30 * n);
if (data[m-1][n]->getID() != 'H')
data[m-1][n] = new Block(30 * (m-1), 30 * n, false);
}
break;
case 6: if (conts(safe, data[m+1][n]->getID())) {
data[m][n] = new Ground(30 * m, 30 * n);
if (data[m+1][n]->getID() != 'H')
data[m+1][n] = new Block(30 * (m+1), 30 * n, false);
}
break;
case 8: if (conts(safe, data[m][n-1]->getID())) {
data[m][n] = new Ground(30 * m, 30 * n);
if (data[m][n-1]->getID() != 'H')
data[m][n-1] = new Block(30 * m, 30 * (n-1), false);
}
break;
}
}
void checkDoors() { //Check to see if closed doors can be opened
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 11; j++) {
if (data[i][j]->getID() == 'X') {
switch (((Door*)data[i][j])->getHash()) {
case 1: if (data[7][3]->getID() == 'B')
((Door*)data[i][j])->setHash(0);
break;
case 3: if (ens.empty()) {
((Door*)data[i][j])->setHash(0);
visited = true;
}
break;
case 4: //Covered by switch
break;
case 5: if (data[10][7]->getID() != 'M')
((Door*)data[i][j])->setHash(0);
break;
}
}
}
}
}
void checkSwitches(int m, int n) { //Checks if any switches are pressed and does stuff if they are
if (m % 30 != 0 || n % 30 != 0)
return;
if (data[m / 30][n / 30]->getID() == 'S') {
switch (((Switch*)data[m / 30][n / 30])->getHash()) {
case 1: data[7][5] = new Key(210, 150);
((Switch*)data[m / 30][n / 30])->press();
break;
case 2: ((Door*)data[7][0])->setHash(0);
((Switch*)data[m/30][n/30])->press();
break;
}
}
}
int checkCollision(int x, int y, int d, bool k) { //Checks collisions between Link and objects
int m = x / 30;
int n = y / 30;
int num = -2;
if (!conts(safe, data[m][n]->getID()))
num = -1;
if (x % 30 != 0)
if (!conts(safe, data[m+1][n]->getID()))
num = -1;
if (y % 30 != 0)
if (!conts(safe, data[m][n+1]->getID()))
num = -1;
if (x % 30 != 0 && y % 30 != 0)
if (!conts(safe, data[m+1][n+1]->getID()))
num = -1;
//Check doors
/*if (data[m][n]->getID() == 'D')
num = ((Door*)data[m][n])->getNext();
if (x % 30 != 0)
if (data[m+1][n]->getID() == 'D')
num = ((Door*)data[m+1][n])->getNext();
if (y % 30 != 0)
if (data[m][n+1]->getID() == 'D')
num = ((Door*)data[m][n+1])->getNext();
if (x % 30 != 0 && y % 30 != 0)
if (data[m+1][n+1]->getID() == 'D')
num = ((Door*)data[m+1][n+1])->getNext();*/
if (x % 30 == 0 && y % 30 == 0)
if (data[m][n]->getID() == 'D')
num = ((Door*)data[m][n])->getNext();
//Check Locks
if (data[m][n]->getID() == '*' && k)
((Door*)data[m][n])->setHash(0);
if (x % 30 != 0)
if (data[m+1][n]->getID() == '*' && k)
((Door*)data[m+1][n])->setHash(0);
if (y % 30 != 0)
if (data[m][n+1]->getID() == '*' && k)
((Door*)data[m][n+1])->setHash(0);
if (x % 30 != 0 && y % 30 != 0)
if (data[m+1][n+1]->getID() == '*' && k)
((Door*)data[m+1][n+1])->setHash(0);
//Check blocks
if (data[m][n]->getID() == 'M')
move(m, n, d);
if (x % 30 != 0)
if (data[m+1][n]->getID() == 'M')
move(m+1, n, d);
if (y % 30 != 0)
if (data[m][n+1]->getID() == 'M')
move(m, n+1, d);
if (x % 30 != 0 && y % 30 != 0)
if (data[m+1][n+1]->getID() == 'M')
move(m+1, n+1, d);
//Check holes
if (data[m][n]->getID() == 'H')
num = -10;
if (x % 30 != 0)
if (data[m+1][n]->getID() == 'H')
num = -10;
if (y % 30 != 0)
if (data[m][n+1]->getID() == 'H')
num = -10;
if (x % 30 != 0 && y % 30 != 0)
if (data[m+1][n+1]->getID() == 'H')
num = -10;
//Check Warps
if (data[m][n]->getID() == '?')
num = 100 + ((Warp*)data[m][n])->getNext();
if (x % 30 != 0)
if (data[m+1][n]->getID() == '?')
num = 100 + ((Warp*)data[m+1][n])->getNext();
if (y % 30 != 0)
if (data[m][n+1]->getID() == '?')
num = 100 + ((Warp*)data[m][n+1])->getNext();
if (x % 30 != 0 && y % 30 != 0)
if (data[m+1][n+1]->getID() == '?')
num = 100 + ((Warp*)data[m+1][n+1])->getNext();
//Check Heart Pieces
if (data[m][n]->getID() == '<') {
data[m][n] = new Ground(30 * m, 30 * n);
num = -13;
}
if (x % 30 != 0)
if (data[m+1][n]->getID() == '<') {
data[m+1][n] = new Ground (30 * m + 30, 30 * n);
num = -13;
}
if (y % 30 != 0)
if (data[m][n+1]->getID() == '<') {
data[m][n+1] = new Ground (30 * m, 30 * n + 30);
num = -13;
}
if (x % 30 != 0 && y % 30 != 0)
if (data[m+1][n+1]->getID() == '<') {
data[m+1][n+1] = new Ground (30 * m + 30, 30 * n + 30);
num = -13;
}
//Check keys
if (data[m][n]->getID() == 'K') {
data[m][n] = new Ground(30 * m, 30 * n);
num = -8;
}
if (x % 30 != 0)
if (data[m+1][n]->getID() == 'K') {
data[m+1][n] = new Ground (30 * m + 30, 30 * n);
num = -8;
}
if (y % 30 != 0)
if (data[m][n+1]->getID() == 'K') {
data[m][n+1] = new Ground (30 * m, 30 * n + 30);
num = -8;
}
if (x % 30 != 0 && y % 30 != 0)
if (data[m+1][n+1]->getID() == 'K') {
data[m+1][n+1] = new Ground (30 * m + 30, 30 * n + 30);
num = -8;
}
checkDoors();
checkSwitches(x, y);
return num;
}
void moveEnemies(LoZObject*& link) { //Enemy AI. Rather basic. Also, rather messy.
int q = link->getX();
int w = link->getY();
shot->tick();
if (QRect(q, w, 30, 30).intersects(QRect(shot->getX(), shot->getY(), 10, 10))) {
((Shot*)shot)->repos(0, 0);
((Shot*)shot)->stop();
((Link*)link)->damage(1);
}
if (ens.empty())
return;
for (int i = 0; i < ens.size()-1; i++) {
ens[i]->tick();
int m = ens[i]->getX() / 30;
int n = ens[i]->getY() / 30;
if (((Enemy*)ens[i])->canMove()) {
if (((Enemy*)ens[i])->getType() == 1) {
switch (rand() % 4) {
case 1: if (data[m+1][n]->getID() == 'G') {
data[m+1][n] = ens[i];
data[m][n] = new Ground(30 * m, 30 * n);
((Enemy*)ens[i])->moved(m+1, n);
}
else if (data[m+1][n]->getID() == 'H')
ens.erase(ens.begin()+i--);
break;
case 2: if (data[m][n+1]->getID() == 'G') {
data[m][n+1] = ens[i];
data[m][n] = new Ground(30 * m, 30 * n);
((Enemy*)ens[i])->moved(m, n+1);
}
else if (data[m][n+1]->getID() == 'H')
ens.erase(ens.begin()+i--);
break;
case 3: if (data[m][n-1]->getID() == 'G') {
data[m][n-1] = ens[i];
data[m][n] = new Ground(30 * m, 30 * n);
((Enemy*)ens[i])->moved(m, n-1);
}
else if (data[m][n-1]->getID() == 'H')
ens.erase(ens.begin()+i--);
break;
case 0: if (data[m-1][n]->getID() == 'G') {
data[m-1][n] = ens[i];
data[m][n] = new Ground(30 * m, 30 * n);
((Enemy*)ens[i])->moved(m-1, n);
}
else if (data[m-1][n]->getID() == 'H')
ens.erase(ens.begin()+i--);
break;
}
}
else if (((Enemy*)ens[i])->getType() == 2) {
int up = -1, right = -1;
if (ens[i]->getX() < q)
right = 1;
else if (ens[i]->getX() > q)
right = 0;
if (ens[i]->getY() < w)
up = 2;
else if (ens[i]->getY() > w)
up = 3;
int dir = 0;
if (up == -1)
dir = right;
else if (right == -1)
dir = up;
else {
if (rand() % 2 == 1)
dir = up;
else
dir = right;
}
switch (dir) {
case 1: if (data[m+1][n]->getID() == 'G') {
data[m+1][n] = ens[i];
data[m][n] = new Ground(30 * m, 30 * n);
((Enemy*)ens[i])->moved(m+1, n);
}
else if (data[m+1][n]->getID() == 'H')
ens.erase(ens.begin()+i--);
break;
case 2: if (data[m][n+1]->getID() == 'G') {
data[m][n+1] = ens[i];
data[m][n] = new Ground(30 * m, 30 * n);
((Enemy*)ens[i])->moved(m, n+1);
}
else if (data[m][n+1]->getID() == 'H')
ens.erase(ens.begin()+i--);
break;
case 3: if (data[m][n-1]->getID() == 'G') {
data[m][n-1] = ens[i];
data[m][n] = new Ground(30 * m, 30 * n);
((Enemy*)ens[i])->moved(m, n-1);
}
else if (data[m][n-1]->getID() == 'H')
ens.erase(ens.begin()+i--);
break;
case 0: if (data[m-1][n]->getID() == 'G') {
data[m-1][n] = ens[i];
data[m][n] = new Ground(30 * m, 30 * n);
((Enemy*)ens[i])->moved(m-1, n);
}
else if (data[m-1][n]->getID() == 'H')
ens.erase(ens.begin()+i--);
break;
default: ((Enemy*)ens[i])->moved(m, n);
}
}
else if (((Enemy*)ens[i])->getType() == 3) {
((Shot*)shot)->repos(m * 30 + 15, n * 30 + 15);
((Shot*)shot)->target(q, w);
((Enemy*)ens[i])->moved(m, n);
}
}
}
}
std::vector<LoZObject*>* getEns() {
if (ens.empty())
return NULL;
return &ens;
}
void killEnemy(int i) {
if (ens.empty())
return;
int m = ens[i]->getX() / 30;
int n = ens[i]->getY() / 30;
ens.erase(ens.begin() + i);
data[m][n] = new Ground (30 * m, 30 * n);
}
};
#endif