-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
352 lines (341 loc) · 20.2 KB
/
Board.java
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
import java.util.ArrayList;
/**
* This class sets up the game environment and implements how Mario is able to interact with his environment aka map
* This class contains various game environments that Mario may interact with that are randomly generated at run time
*/
public class Board {
private char[][] map;
private ArrayList<Character> characters = new ArrayList<Character>();
private ArrayList<Entity> entities = new ArrayList<Entity>();
private int mapType = 0;
private boolean gameOver = false;
private final char[][] DEFAULT_MAP_0 = new char[][] {
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'f', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'c', 'c', 'c', 'x', 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x', 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', 'c', 'c', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'g', ' ', ' ', ' ', ' ', ' ',' ','c',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', 'c', 'c', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', 'c', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', 'd', 'x', ' ', ' ', 'g', 'x', 'x', 'x', 'x', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', ' ', ' ',' ','x','x', 'x', ' ', ' ', ' ', 'd', ' '},
{'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b','b','b','b', 'b', 'b', 'b', 'b', 'b', 'b'},
{'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u','u','u','u', 'u', 'u', 'u', 'u', 'u', 'u'}};
private final char[][] DEFAULT_MAP_1 = new char[][] {
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'c', 'c', 'c', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', 'c', 'c', ' ', ' ', ' ', ' ', ' ', ' ', 'x', ' ', ' ', 'c', 'c', 'x', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x', 'x', ' ', ' ', ' ', 'x', 'x', ' ', 'c', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', ' ', ' ', 'x', 'x', 'x', 'x', ' ', ' ', ' ', ' ',' ',' ','c', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', 'd', 'x', ' ', ' ', 'g', 'x', 'x', 'x', 'x', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'f', ' ', 'x', 'x', 'x', 'x', 'x', ' ', ' ', ' ',' ','x','x', ' ', 'g', 'x', ' ', 'd', ' '},
{'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', ' ','b','b','b', 'b', 'b', 'b', 'b', 'b', 'b'},
{'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', ' ','u','u','u', 'u', 'u', 'u', 'u', 'u', 'u'}};
private final char[][] DEFAULT_MAP_2 = new char[][] {
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'c', ' ', 'c', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'f', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'c', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'c', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x', ' ', ' ', ' ', ' ', ' ', 'x', 'x', 'x', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', ' ', ' ', ' ', ' ', ' ', 'c', 'c', 'c', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', ' ', ' ',' ',' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', 'd', ' ', ' ', ' ', ' ', 'x', 'g', ' ', 'x', 'x', 'x', 'g', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x', 'x', ' ','x','g',' ', ' ', ' ', ' ', 'x', 'd', ' '},
{'b', 'b', 'b', 'b', ' ', ' ', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', ' ','b','b','b', 'b', 'b', 'b', 'b', 'b', 'b'},
{'u', 'u', 'u', 'u', ' ', ' ', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', ' ','u','u','u', 'u', 'u', 'u', 'u', 'u', 'u'}};
/**
* @param map is setup as a character array
*/
public Board(char[][] map) {
this.map = initializeMap(map);
}
public Board() {
this.map = initializeMap(null);
}
/**
* The different characters/letters in the maps' character array correspond to different types of blocks
* i.e. c corresponds to coin and so on to initialise the game environment/map
* @param input if there is no user input the map is set to the default version
* if user input is detected, it returns the updated map to reflect this
* @return returns the map which is a character array
* Each time the game is run from console a different map is displayed from the character arrays above
* with the default map being map_0
*/
public char[][] initializeMap(char[][] input) {
char[][] map;
mapType = (int)(Math.random() * 3);
characters.clear();
entities.clear();
if (input == null) {
switch (mapType){
case 0:
map = DEFAULT_MAP_0;
break;
case 1:
map = DEFAULT_MAP_1;
break;
case 2:
map = DEFAULT_MAP_2;
break;
default:
map = DEFAULT_MAP_0;
}
} else {
map = input;
}
int p = 1;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
switch (map[i][j]) {
case 'd':
entities.add(new Mario(j*32, i*32, p));
if (p == 1){
p = 2;
}
characters.add((Character)entities.get(entities.size()-1));
map[i][j] = ' ';
break;
case 'g':
entities.add(new Goomba(j*32, i*32));
characters.add((Character)entities.get(entities.size()-1));
map[i][j] = ' ';
break;
case 'b':
entities.add(new Block(j*32, i*32, "Grass"));
break;
case 'u':
entities.add(new Block(j*32, i*32, "Dirt"));
break;
case 'c':
entities.add(new Block(j*32, i*32, "Coin"));
break;
case 'x':
entities.add(new Block(j*32, i*32, "Block"));
break;
case 'q':
entities.add(new Block(j*32, i*32, "QuestionBlock"));
break;
case 'f':
entities.add (new Block(j*32, i*32, "Flag"));
case ' ':
break;
default:
entities.add(new Block(j*32, i*32, "Undefined"));
break;
}
}
}
return map;
}
/**
* @return returns mapClone
* which is a copy of the map.
*/
public char[][] getMap() {
char[][] mapClone = new char[map.length][map[0].length];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
mapClone[i][j] = map[i][j];
}
}
return mapClone;
}
public char [][] getTextMap(){
return DEFAULT_MAP_0;
}
public ArrayList<Entity> getEntities() {
// Memory leak here but is needed
return this.entities;
}
public ArrayList<Character> getCharacters() {
return this.characters;
}
public Entity getEntity(int i){
return entities.get(i-1);
}
public Character getCharacter(int i){
return characters.get(i-1);
}
/**
* Getter Method that indicates game status
* @return returns a boolean which is set to a default of false
*/
public boolean getGameStatus(){
return this.gameOver;
}
/**
* This method handles collisions between Mario and other objects such as Goombas
* It uses the side of the character that is touching the other object
* where 'T' - top, 'B' - bottom, 'L' - left, 'R' - right
* It first finds a collision face
* It compare the two entities and do actions based on what face the collision is on
* if Collision is not nothing (not empty) and not T, and it's Mario and Goomba, take health off the Mario
*/
public void handleCollisions() {
int charXPos;
int charYPos;
int entXPos;
int entYPos;
// The side of the character that is touching the other object
char collisionFace = ' '; // 'T' - top, 'B' - bottom, 'L' - left, 'R' - right
for (int i = 0; i < characters.size(); i++) {
characters.get(i).setGrounded(false);
for (int j = 0; j < entities.size(); j++) {
if (characters.size() == i){
break;
}
if (characters.get(i) != entities.get(j)) {
charXPos = characters.get(i).getXPos();
charYPos = characters.get(i).getYPos();
entXPos = entities.get(j).getXPos();
entYPos = entities.get(j).getYPos();
// Find collision face
if ((charYPos <= entYPos && charYPos + 32 >= entYPos) && (charYPos + 32 - entYPos <= charXPos + 32 - entXPos) && (charYPos + 32 - entYPos <= entXPos + 32 - charXPos)) {
collisionFace = 'B';
} else if ((charYPos >= entYPos && charYPos <= entYPos + 32) && (entYPos + 32 - charYPos <= charXPos + 32 - entXPos) && (entYPos + 32 - charYPos <= entXPos + 32 - charXPos)) {
collisionFace = 'T';
} else if ((charXPos <= entXPos && charXPos + 32 >= entXPos) && (charXPos + 32 - entXPos <= charYPos + 32 - entYPos) && (charXPos + 32 - entXPos <= entYPos + 32 - charYPos)) {
collisionFace = 'R';
} else if ((charXPos >= entXPos && charXPos <= entXPos + 32) && (entXPos + 32 - charXPos <= charYPos + 32 - entYPos) && (entXPos + 32 - charXPos <= entYPos + 32 - charYPos)) {
collisionFace = 'L';
} else {
collisionFace = ' ';
}
// Compare the two entities and do actions based on what face the collision is on
if (collisionFace != ' ') {
if (entities.get(j).getType() == "Coin" && characters.get(i).getType() == "Mario") {
entities.remove(j);
((Mario)characters.get(i)).addCoin();
j--; // Without this the loop will skip an object
continue;
}
if (entities.get(j).getType() == "Flag" && characters.get(i).getType() == "Mario"){
checkWin();
}
}
// if Collision is not nothing and not T, and it's Mario and Goomba, take health off the Mario
if (collisionFace != ' ' && collisionFace != 'B' && entities.get(j).getType().equals("Goomba") && characters.get(i).getType().equals("Mario")){
//if (entities.get(j).getType().equals("Goomba") && characters.get(i).getType().equals("Mario")){
((Mario)characters.get(i)).hurt(1);
if (((Mario)characters.get(i)).getHealth() <= 0){
for (int e = 0; e < entities.size(); e++){
if (entities.get(e).equals(characters.get(i))){
characters.remove(i);
entities.remove(e);
break;
}
}
}
// }
} else if (collisionFace == 'T') {
if (entities.get(j).isCollideable()) {
// Move character
characters.get(i).setYPos(entYPos + 32);
}
} else if (collisionFace == 'B') {
if (entities.get(j).isCollideable()) {
if (entities.get(j).getType().equals("Goomba") && characters.get(i).getType().equals("Mario")){
((Goomba)entities.get(j)).kill();
((Mario)characters.get(i)).addCoin();
entities.remove(j);
j--;
}
characters.get(i).setGrounded(true);
characters.get(i).setYVelocity(0);
// Move character
characters.get(i).setYPos(entYPos - 32);
}
} else if (collisionFace == 'L') {
if (entities.get(j).isCollideable()) {
//characters.get(i).setXVelocity(0);
// Move character
if (characters.get(i).getType().equals("Goomba")){
characters.get(i).setXVelocity(characters.get(i).getXVelocity()*-1);
}
characters.get(i).setXPos(entXPos + 32);
}
} else if (collisionFace == 'R') {
if (entities.get(j).isCollideable()) {
//characters.get(i).setXVelocity(0);
// Move character
if (characters.get(i).getType().equals("Goomba") && entities.get(j).getType().equals("Block")){
characters.get(i).setXVelocity(characters.get(i).getXVelocity()*-1);
}
characters.get(i).setXPos(entXPos - 32);
}
}
}
}
}
}
/**
* This method determines if a win is possible once a collision with a flag is made.
* If there are no coins or goombas left on the map, the Mario wins.
* Hence all coins must be collected and goombas defeated when Mario reaches the flag in order to win
* When Mario wins the game is ends
*/
public void checkWin(){
boolean coinCheck = false;
boolean goombaCheck = false;
for (int i = 0; i < entities.size(); i++){
if (entities.get(i).getType() == "Coin"){
coinCheck = true;
}
if (entities.get(i).getType() == "Goomba"){
goombaCheck = true;
}
}
if (coinCheck == false && goombaCheck == false){
gameOver = true;
}
}
/**
* This method deals when either one of the Mario's falls into the pit on the board
* It will first "hurt" the Mario by removing his health (-1) and then resets the
* the Mario who "fell" to their initial positions
* If the Mario's health is too low, the Mario in play will be removed from the board completely.
*/
public void handleFallingOffBoard(){
for (int i = 0; i < characters.size(); i++) {
if (characters.get(i) instanceof Mario){
Mario m = (Mario)characters.get(i);
int charY = characters.get(i).getYPos();
if(charY >= (map.length * 32) - 32){
m.hurt(1);
((Character) m).setXPos(m.getStartPositionX());
((Character) m).setYPos(m.getStartPositionY());
if (((Mario)characters.get(i)).getHealth() <= 0){
for (int e = 0; e < entities.size(); e++){
if (entities.get(e).equals(characters.get(i))){
characters.remove(i);
entities.remove(e);
break;
}
}
}
}
}
}
}
}