-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.cpp
197 lines (180 loc) · 4.08 KB
/
manager.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
class Manager {
std::list<Enemy*> enemies;
std::list<Wall*> walls;
std::list<Bullet*> bullets;
Role* role;
int w=0,h=0;
public:
Manager() {}
~Manager() {
clear();
}
void setRole(Role* r) {
role=r;
}
bool isRole(Thing *t) {
return t==role;
}
void setSize(int nw,int nh) {
w=nw;
h=nh;
}
bool outOfMap(Thing *t) {
return t->getX()<0||t->getY()<0||t->getX()>=w||t->getY()>=h;
}
int width() {
return w;
}
int height() {
return h;
}
void clear() {
for(Enemy *e:enemies) {
delete e;
e=NULL;
}
enemies.clear();
for(Wall *w:walls) {
delete w;
w=NULL;
}
walls.clear();
for(Bullet *b:bullets) {
delete b;
b=NULL;
}
bullets.clear();
}
void addEnemy(int x,int y,int a) {
enemies.insert(enemies.end(),new Enemy(x,y,a));
}
void addEnemy(Enemy*e) {
enemies.insert(enemies.end(),e);
}
void addBullet(Tank *t) {
bullets.insert(bullets.end(),new Bullet(t));
}
void addWall(Wall *w) {
walls.insert(walls.end(),w);
}
Enemy* getCollideEnemy(Bullet& t) {
for(Enemy *e:enemies) {
if (e->collide(t)) {
return e;
}
}
return NULL;
}
Enemy* getCollideEnemy(Tank& t) {
for(Enemy *e:enemies) {
if (e->collide(t)) {
return e;
}
}
return NULL;
}
Enemy* randAddEnemy() {
Enemy *e;
while(true) {
int x=random(1,w-1);
int y=random(1,h-1);
int aspect=random(0,3);
e=new Enemy(x,y,aspect);
if (e->cantmove()) {
delete e;
} else {
break;
}
}
enemies.insert(enemies.end(),e);
return e;
}
Bullet* getCollideBullet(Bullet& t) {
for(Bullet *e:bullets) {
if (e!=&t&&e->collide(t)) {
return NULL;
}
}
return NULL;
}
bool collideEnemies(Tank& t) {
for(Enemy *e:enemies) {
if (e->collide(t)) {
return true;
}
}
return false;
}
bool collideTanks(Tank& t) {
if (&t!=role&&role->collide(t)) {
return true;
}
for(Enemy *e:enemies) {
if (&t!=e&&e->collide(t)) {
return true;
}
}
return false;
}
bool collideWalls(Tank t) {
for(Wall *w:walls) {
if (w->collide(t)) {
return true;
}
}
return false;
}
bool collideEnemies(Bullet t) {
for(Enemy *e:enemies) {
if (e->collide(t)) {
return true;
}
}
return false;
}
bool collideWalls(Bullet t) {
for(Wall *w:walls) {
if (w->collide(t)) {
return true;
}
}
return false;
}
void show(Drawable &a) {
for (auto it =bullets.begin(); it!=bullets.end(); ) {
Bullet *e=*it;
if (e->isExist()) {
e->run();
e->show(a);
it++;
} else {
it=bullets.erase(it);
freep(e);
}
}
for (auto it =walls.begin(); it!=walls.end(); ) {
Wall *e=*it;
if (e->isExist()) {
e->run();
e->show(a);
it++;
} else {
it=walls.erase(it);
freep(e);
}
}
for (auto it =enemies.begin(); it!=enemies.end(); ) {
Enemy *e=*it;
if (e->isExist()) {
e->run();
e->show(a);
it++;
} else {
it=enemies.erase(it);
freep(e);
}
}
}
protected:
private:
};