-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cpp
75 lines (66 loc) · 1.93 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
#include "GameManager.h"
GameManager::GameManager(SnakeMap &m, Snake& s):
map(m), snake(s), frame(0), seconds(0) {
for (int i=0; i<map.row; i++) {
for (int j=0; j<map.col; j++) {
if (map.mat[i][j] == EMPTY_SPACE)
emptySpace.push_back(Point(i, j));
else if (map.mat[i][j] == WALL)
wall.push_back(Point(i, j));
}
}
maxLengthRecord = 3;
growthItemRecord = 0;
poisonItemRecord = 0;
gateUsageRecord = 0;
seconds = 0;
}
void GameManager::randomItemGenerate() {
if (frame % 20 != 0)
return;
Item newItem;
int idx = 0;
int lifespan = 0;
do {
idx = rand() % emptySpace.size();
lifespan = rand() % 20 + 30;
newItem = emptySpace[idx];
newItem.lifespan = lifespan;
} while(snake.isSnake(newItem));
if (rand() % 2) {
map[newItem] = GROWTH_ITEM;
growthItems.push_back(Item(newItem));
}
else {
newItem.lifespan = newItem.lifespan + 50;
map[newItem] = POISON_ITEM;
poisonItems.push_back(Item(newItem));
}
emptySpace.erase(emptySpace.begin() + idx);
}
void GameManager::randomGateGenerate() {
for (int i=0; i<2; i++) {
int idx = rand() % wall.size();
gates[i] = wall[idx];
map[gates[i]] = GATE;
wall.erase(wall.begin() + idx);
}
}
void GameManager::itemStatusChange() {
for (auto it=poisonItems.begin(); it != poisonItems.end(); it++) {
it->lifespan--;
if (it->lifespan == 0) {
map.mat[it->row][it->col] = EMPTY_SPACE;
emptySpace.push_back(*it);
it = poisonItems.erase(it);
}
}
for (auto it=growthItems.begin(); it != growthItems.end(); it++) {
it->lifespan--;
if (it->lifespan == 0) {
map.mat[it->row][it->col] = EMPTY_SPACE;
emptySpace.push_back(*it);
it = growthItems.erase(it);
}
}
}