-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardManager.cpp
More file actions
256 lines (242 loc) · 9.38 KB
/
BoardManager.cpp
File metadata and controls
256 lines (242 loc) · 9.38 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
#include "BoardManager.h"
#include <sstream>
/*==============================================================================
==============================================================================*/
BoardManager::BoardManager(int s){
this->size = s;
tiles.resize(s);
/* Resizing the table to fit to the argument given. */
for (auto &tile : tiles) tile.resize(s);
for (int i = 0; i < s; i++) {
for (int j = 0; j < s; j++ ) {
items[i][j] = NULL;
}
}
for (int i = 0; i < s; i++) {
for (int j = 0; j < s; j++) {
std::stringstream ss;
ss << i << j;
map[i][j] = Gamedata::getInstance().getXmlInt("map/t" + ss.str());
}
}
/* Will ONLY work with perfect square. */
for ( int row = 0; row < s; row++ ) {
for ( int col = 0; col < s; col++ ) {
std::stringstream ss;
ss << "Tile" << map[row][col];
tiles[row][col] = new BackgroundTile(ss.str(), (row * 100), (col * 100), map[row][col]);
}
}
for ( int row = 0; row < s; row++ ) {
for ( int col = 0; col < s; col++ ) {
/* If item placeholder detected. */
if (map[row][col] == 4) {
std::stringstream ss;
ss << "Crate";
items[row][col] = new Item(ss.str(), row, col, 100);
}
}
}
humanHasBow = false;
AIHasBow = false;
this->human = new Player("human", 0, 0);
this->ai = new Player("AI", 8, 9);
endgame = false;
}
/*==============================================================================
==============================================================================*/
bool BoardManager::playerHasBow(int p) const{
if (p == 0) {
return human->bow;
} else {
return ai->bow;
}
}
/*==============================================================================
==============================================================================*/
void BoardManager::createProjectile(int row, int col, int dir) {
switch(dir) {
case 0:
if (map[row-1][col] == 3 || map[row-1][col] == 4)
projectiles.push_back(new Projectile("Left_Arrow", row-1, col));
break;
case 1:
if (map[row+1][col] == 3 || map[row+1][col] == 4)
projectiles.push_back(new Projectile("Right_Arrow", row+1, col));
break;
case 2:
if (map[row][col-1] == 3 || map[row][col-1] == 4)
projectiles.push_back(new Projectile("Up_Arrow", row, col-1));
break;
case 3:
if (map[row][col+1] == 3 || map[row][col+1] == 4)
projectiles.push_back(new Projectile("Down_Arrow", row, col+1));
break;
}
}
/*==============================================================================
==============================================================================*/
void BoardManager::drawTiles() const {
for (unsigned int i = 0; i < this->size; i++) {
for (unsigned int j = 0; j < this->size; j++) {
tiles[i][j]->draw();
if (map[i][j] == 4) {
items[i][j]->draw();
}
}
}
human->draw();
ai->draw();
for (auto &projectile : projectiles) projectile->draw();
}
/*==============================================================================
==============================================================================*/
void BoardManager::update(Uint32 ticks) {
human->update(ticks);
ai->update(ticks);
}
/*==============================================================================
==============================================================================*/
BoardManager::~BoardManager() {
for (auto& row : tiles)
for (auto& tile : row) delete tile;
}
/*==============================================================================
==============================================================================*/
Drawable* BoardManager::getTile() {
return tiles[0][0];
}
/*==============================================================================
==============================================================================*/
int BoardManager::getPlayerX(int player) {
if (player == 0)
return human->getX();
else return ai->getX();
}
/*==============================================================================
==============================================================================*/
int BoardManager::getPlayerY(int player) {
if (player == 0)
return human->getY();
else return ai->getY();
}
int BoardManager::getObjects() const {
int total_objs = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (items[i][j] != NULL) total_objs++;
}
}
total_objs += 2; //Player objects.
total_objs += projectiles.size();
return total_objs;
}
/*==============================================================================
==============================================================================*/
void BoardManager::godMode() {
if (!human->godmode) human->godmode = true;
else human->godmode = false;
}
/*==============================================================================
==============================================================================*/
int BoardManager::movePlayer(int player, int dir, int t) {
/* Tiles 3 and 4 are walkable. */
/* HUMAN */
if (player == 0) {
// Move Left
if (dir == 0 && (human->getX()-t) >= 0 && ((ai->getX() != human->getX() - t) || (ai->getY() != human->getY()))
&& tiles[(human->getX() - 1) / 100][human->getY() / 100]->isWalkable){
human->moveDirection(dir, t);
}
// Move Right
else if (dir == 1 && (human->getX()-t ) < (size-2) * t && ((ai->getX() != human->getX() + t) || (ai->getY() != human->getY()))
&& tiles[((human->getX() + 1) / 100) + 1][human->getY() / 100]->isWalkable){
human->moveDirection(dir, t);
}
//Move Up
else if (dir == 2 && (human->getY()-t) >= 0 && ((ai->getY() != human->getY() - t) || (ai->getX() != human->getX()))
&& tiles[human->getX() / 100][(human->getY() - 1) / 100]->isWalkable){
human->moveDirection(dir, t);
}
//Move Down
else if (dir == 3 && (human->getY()-t) < (size-2) * t && ((ai->getY() != human->getY() + t) || (ai->getX() != human->getX()))
&& tiles[human->getX() / 100][((human->getY() + 1) / 100) + 1]->isWalkable){
human->moveDirection(dir, t);
}
/* AI */
} else {
if (dir == 0 && (ai->getX()-t) >= 0
&& ((human->getX() != ai->getX() - t)
|| (human->getY() != ai->getY()))
&& tiles[(ai->getX() - 1) / 100][ai->getY() / 100]->isWalkable)
ai->moveDirection(dir, t);
else if (dir == 1 && (ai->getX()-t ) < (size-2) * t
&& ((human->getX() != ai->getX() + t)
|| (human->getY() != ai->getY()))
&& tiles[((ai->getX() + 1) / 100) + 1][ai->getY() / 100]->isWalkable)
ai->moveDirection(dir, t);
else if (dir == 2 && (ai->getY()-t) >= 0
&& ((human->getY() != ai->getY() - t)
|| (human->getX() != ai->getX()))
&& tiles[ai->getX() / 100][(ai->getY() - 1) / 100]->isWalkable)
ai->moveDirection(dir, t);
else if (dir == 3 && (ai->getY()-t) < (size-2) * t
&& ((human->getY() != ai->getY() + t)
|| (human->getX() != ai->getX()))
&& tiles[ai->getX() / 100][((ai->getY() + 1) / 100) + 1]->isWalkable)
ai->moveDirection(dir, t);
}
/* ITEM DETECTION ========================================================= */
if (items[human->cur_row][human->cur_col] != NULL) {
items[human->cur_row][human->cur_col] = new Item("Crate", -1, -1, 100);
human->bow = true;
}
if (items[ai->cur_row][ai->cur_col] != NULL) {
items[ai->cur_row][ai->cur_col] = new Item("Crate", -1, -1, 100);
ai->bow = true;
}
/* Setting the Human/AI that they have a bow to work with */
if (human->bow == true) humanHasBow = true;
if (ai->bow == true) AIHasBow = true;
/* PROJECTILE MAPPING & MOVEMENT ========================================= */
bool player_hit = false;
bool ai_hit = false;
for (auto &projectile: projectiles) {
projectile->moveDirection(projectile->getID(), 50);
/* If the arrow collides with a wall, delete it. */
if (map[projectile->getCurRow()][projectile->getCurCol()] != 3 &&
(map[projectile->getCurRow()][projectile->getCurCol()] != 4)) {
projectile = new Projectile("Up_Arrow", -1, -1);
/* If the arrow collides with the human, that human dies. */
} else if (human->getCurRow() == projectile->getCurRow() &&
human->getCurCol() == projectile->getCurCol()){
std::cout << "Hit on human detected!\n";
if (!human->godmode) {
projectile = new Projectile("Up_Arrow", -1, -1);
player_hit = true;
endgame = true;
return 1;
}
else std::cout << "Ignored hit!\n";
/* If the arrow collides with the AI, the AI dies. */
} else if (ai->getCurRow() == projectile->getCurRow() &&
ai->getCurCol() == projectile->getCurCol()){
std::cout << "Hit on AI detected!\n";
projectile = new Projectile("Up_Arrow", -1, -1);
ai_hit = true;
endgame = true;
return 2;
/* Otherwise, the arrow continues forward. */
}
// else {
// projectile->moveDirection(projectile->getID(), 50);
// }
}
/* Necessary for some reason. Can't return in the above if chain. */
if (ai_hit) return 2;
else if (player_hit) return 1;
else return 0;
}
void BoardManager::updateStatus(){
endgame = false;
}