-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
336 lines (301 loc) · 10.2 KB
/
Player.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
public class Player extends DynamicActor
{
private int numberBombs;
private int numberBombsActive;
private int bombLevel;
private boolean kick;
private boolean transparency;
private int countTransp;
private int changeTransp;
private int saveSpeed;
private boolean slow;
private int timeSlow;
private String [] moveKeys;
private String bombKey;
public Player(int player, String upKey, String downKey, String leftKey, String rightKey, String bombKey)
{
super("p", player, 3);
lifes = 3;
createPlayerImages(imgsDown, NUMBER_OF_IMAGES, actorNumber, "d");
createPlayerImages(imgsUp, NUMBER_OF_IMAGES, actorNumber, "u");
createPlayerImages(imgsLeft, NUMBER_OF_IMAGES, actorNumber, "l");
createPlayerImages(imgsRight, NUMBER_OF_IMAGES, actorNumber, "r");
createPlayerImages(imgsDeath, NUMBER_OF_IMAGES+1, actorNumber, "m");
imgsDeath[4] = new GreenfootImage(actor + actorNumber + "m3.png");
if(player == 2)
setImage(imgsDown[1]);
numberBombs = 1;
numberBombsActive = 0;
bombLevel = 1;
kick = false;
transparency = false;
countTransp = 0;
changeTransp = 100;
saveSpeed = 1;
slow = false;
timeSlow = 0;
moveKeys = new String[4]; //[0]->upKey, [1]->downKey, [2]->leftKey, [3]->rightKey
moveKeys[0] = upKey;
moveKeys[1] = downKey;
moveKeys[2] = leftKey;
moveKeys[3] = rightKey;
this.bombKey = bombKey;
}
public void act()
{
if(!die)
{
if(transparency)
transparency();
underBomb();
move();
powerTouched();
slow();
}
else
death();
}
public void move()
{
if(timeMove++ == 1)
{
if(Greenfoot.isKeyDown(moveKeys[0])) //up
{
setImage("u");
setLocation(getX(), getY() - speed);
Actor staticActor = getOneIntersectingObject(StaticActor.class);
Bomb bomb = (Bomb) getOneIntersectingObject(Bomb.class);
if(staticActor != null || (bomb != null && !underBomb))
setLocation(getX(), getY() + speed);
if(bomb != null && !underBomb && kick)
{
Greenfoot.playSound("Kick.wav");
bomb.move("u");
}
}
if(Greenfoot.isKeyDown(moveKeys[1])) //down
{
setImage("d");
setLocation(getX(), getY() + speed);
Actor staticActor = getOneIntersectingObject(StaticActor.class);
Bomb bomb = (Bomb) getOneIntersectingObject(Bomb.class);
if(staticActor != null || (bomb != null && !underBomb))
setLocation(getX(), getY() - speed);
if(bomb != null && !underBomb && kick)
{
Greenfoot.playSound("Kick.wav");
bomb.move("d");
}
}
if(Greenfoot.isKeyDown(moveKeys[2])) //left
{
setImage("l");
setLocation(getX() - speed, getY());
Actor staticActor = getOneIntersectingObject(StaticActor.class);
Bomb bomb = (Bomb) getOneIntersectingObject(Bomb.class);
if(staticActor != null || (bomb != null && !underBomb))
setLocation(getX() + speed, getY());
if(bomb != null && !underBomb && kick)
{
Greenfoot.playSound("Kick.wav");
bomb.move("l");
}
}
if(Greenfoot.isKeyDown(moveKeys[3])) //right
{
setImage("r");
setLocation(getX() + speed, getY());
Actor staticActor = getOneIntersectingObject(StaticActor.class);
Bomb bomb = (Bomb) getOneIntersectingObject(Bomb.class);
if(staticActor != null || (bomb != null && !underBomb))
setLocation(getX() - speed, getY());
if(bomb != null && !underBomb && kick)
{
Greenfoot.playSound("Kick.wav");
bomb.move("r");
}
}
if(Greenfoot.isKeyDown(bombKey) && numberBombsActive < numberBombs) //bomb
{
Bomb bomb = (Bomb) getOneIntersectingObject(Bomb.class);
Player player = (Player) getOneIntersectingObject(Player.class);
Enemie enemie = (Enemie) getOneIntersectingObject(Enemie.class);
if(bomb == null)
{
getWorld().addObject(new Bomb(droppingObjectCoords(getX()), droppingObjectCoords(getY()), bombLevel, this), droppingObjectCoords(getX()), droppingObjectCoords(getY()));
numberBombsActive++;
underBomb = true;
if(player != null)
player.setUnderBomb();
if(enemie != null)
enemie.setUnderBomb();
}
}
timeMove = 1;
if(slow) timeMove = 0;
}
}
private void createPlayerImages(GreenfootImage [] images, int numberOfImages, int player, String direction)
{
for(int i = 0; i < numberOfImages; i++)
{
images[i] = new GreenfootImage("p" + player + direction + i + ".png");
}
}
//polimorfismo <--------------------------------------------------------------------------
private void powerTouched()
{
Power power = (Power) getOneIntersectingObject(Power.class);
if(power != null)
power.touchPower(this);
}
public void decNumberBombsActive()
{
numberBombsActive--;
}
//função que incrementa o atributo numberBombs
public void setNumberBombs(int n)
{
numberBombs = n;
}
//função que retorna o atributo numberBombs
public int getNumberBombs()
{
return numberBombs;
}
//função que incrementa o atributo bombLevel
public void setBombLevel(int n)
{
bombLevel = n;
}
//funcção que retorna o atributo bombLevel
public int getBombLevel()
{
return bombLevel;
}
//função que mete a true o atributo kick
public void setKick()
{
kick = true;
}
public boolean getKick()
{
return kick;
}
//função que retorna o player
public int getPlayer()
{
return actorNumber;
}
//função que retorna o atributo lifes
public int getLifes()
{
return lifes;
}
//função que altera o atributo lifes
public void setLifes(int n)
{
lifes = n;
}
public void slow()
{
Actor poop = getOneIntersectingObject(Poop.class);
if(poop != null && speed != 1)
{
saveSpeed = speed;
speed = 1;
timeMove = 0;
slow = true;
}
else if(poop != null && speed == 1 && !slow)
{
timeMove = 0;
slow = true;
}
else if(slow)
{
if(timeSlow++ == 100)
{
speed = saveSpeed;
slow = false;
timeSlow = 0;
}
}
}
public boolean getSlow()
{
return slow;
}
public int getSaveSpeed()
{
return saveSpeed;
}
public void death()
{
die = true;
if(changeImageDeath++ == 20)
{
if(currentImageDeath == 0) Greenfoot.playSound("Death.wav");
setImage(imgsDeath[currentImageDeath]);
currentImageDeath++;
changeImageDeath = 0;
if(currentImageDeath == 5)
{
lifes--;
if(((BomberWorld)getWorld()).getLevel() == 10 && lifes != 0)
((BomberWorld)getWorld()).restartMultiplayer();
else
{
if(lifes == 0)
((BomberWorld)getWorld()).endGame(actorNumber);
else
{
if(actorNumber == 1)
setLocation(48, 80);
else //if(player == "2")
setLocation(432, 464);
setImage(imgsDown[1]);
currentImageDeath = 0;
((BomberWorld)getWorld()).setMsgOnBox(actorNumber, 1, lifes);
die = false;
transparency = true;
speed = 1;
((BomberWorld)getWorld()).setMsgOnBox(actorNumber, 3, 1);
numberBombs = 1;
((BomberWorld)getWorld()).setMsgOnBox(actorNumber, 2, 1);
bombLevel = 1;
((BomberWorld)getWorld()).setMsgOnBox(actorNumber, 4, 1);
kick = false;
((BomberWorld)getWorld()).removeKickImage(actorNumber);
}
}
}
}
}
private void transparency()
{
transparency = true;
if(countTransp++ <= 240)
{
getImage().setTransparency(150);
if(countTransp == 240)
{
for(int i = 0; i < 3; i++)
{
imgsDown[i].setTransparency(255);
imgsUp[i].setTransparency(255);
imgsLeft[i].setTransparency(255);
imgsRight[i].setTransparency(255);
}
transparency = false;
countTransp = 0;
}
}
}
public boolean getTransparency()
{
return transparency;
}
}