-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLemmings.java
388 lines (320 loc) · 11.3 KB
/
Lemmings.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import java.util.ArrayList;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics2D;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public abstract class Lemmings extends Thing{ //Classe des Lemmings (elle sera abstraite)
//==================== ATTRIBUTS ========================
protected int id; //identifiant du Lemming
public static World w;
protected int direction; //sens de mouvement : -1 si a gauche, +1 si a droite
public static int nbLemmings = 0; //compteur static des lemmings.
protected int job; //si en pleine action
protected boolean action; //quelle action : 0 si aucune plus tard classe Action
protected boolean alive; //dead or alive
protected boolean spawned;
protected boolean inWorld; //si il est entre dans le terrain
protected boolean outside; //si il a reussi a sortir
protected int iDeath = 0;
protected boolean bombDeath = false;
protected int height; //Taille de l'image d'un Lemming standard
protected int width; //Largeur de l'image d'un Lemming standard
protected boolean inAir = false; //Boolean pour savoir si le lemmingsest en train de tomber
protected int iWalk = 0; //iteration qui debute lanimation de bouger
protected int iFall = 0;
protected static int maxFall; //hauteur max avant la mort
protected static BufferedImage imageRight; //Image du Walker avancant sur la droite
protected static BufferedImage imageRightStep; //Image du Walker avancant sur la droite en marchant
protected static BufferedImage imageLeft; //Image du Walker avancant sur la gauche
protected static BufferedImage imageLeftStep; //Image du Walker avancant sur la gauche en marchant
//Image du Walker avancant sur la gauche en marchant
//Image du Walker avancant sur la gauche en marchant
protected static BufferedImage deathFirst;
protected static BufferedImage deathSecond;
protected int bombCountdown=-1;
protected static BufferedImage boom1;
protected static BufferedImage boom2;
protected static BufferedImage boom3;
protected static BufferedImage boom4;
protected static BufferedImage boom5;
protected static BufferedImage imageExplosion;
public static final int bombRadius = 25;
//================== CONSTRUCTEURS ======================
public Lemmings(int posX, int posY){
super(posX,posY);
nbLemmings++; //Incrementation de nombre total de lemmings
this.id = nbLemmings;
direction = 1; //initialement, ils se deplacent a droite
id = nbLemmings; //Iniatialise l identifiant a la
outside = false; //boolean pour savoir si il s'est refugie initialement faux
inWorld = false; //initialement, le lemming nest pas dans le monde jusqu au spawn
alive = true; //initialement, il est en vie
action = false; //classe Action
spawned = false;
job = World.WALKER;
this.width = imageRight.getWidth(); //recupere la largeur et hauteur du lemming
this.height = imageRight.getHeight();
maxFall = 10*height;
}
public static void loadAssets(){
try{
imageRight = ImageIO.read(new File("lemmings/lemmings1.png")); //recupere les images des Walker a differents etats
imageRightStep = ImageIO.read(new File("lemmings/lemmings1step.png"));
imageLeft = ImageIO.read(new File("lemmings/lemmings2.png"));
imageLeftStep = ImageIO.read(new File("lemmings/lemmings2step.png"));
imageExplosion = ImageIO.read(new File("lemmings/explosion.png"));
deathFirst = ImageIO.read(new File("lemmings/death1.png"));
deathSecond = ImageIO.read(new File("lemmings/death2.png"));
boom1 = ImageIO.read(new File("lemmings/bombEffect1.png"));
boom2 = ImageIO.read(new File("lemmings/bombEffect2.png"));
boom3 = ImageIO.read(new File("lemmings/bombEffect3.png"));
boom4 = ImageIO.read(new File("lemmings/bombEffect4.png"));
boom5 = ImageIO.read(new File("lemmings/bombEffect5.png"));
}catch(Exception e){e.printStackTrace();}
}
public Lemmings(Lemmings l){
this(l.posX,l.posY);
this.id = l.id;
iFall = l.iFall;
direction = l.direction;
inWorld = l.inWorld;
iDeath = l.iDeath;
inAir = l.inAir;
iWalk = l.iWalk;
alive = l.alive;
spawned = l.spawned;
bombCountdown = l.bombCountdown;
}
//===================== METHODES =========================
public abstract void move();
public String toString(){
return "Lemmings number "+id;
}
public void draw(Graphics2D g){
if(drawDeath(g)) return;
if(!inWorld) return;
if(!alive) return;
drawMove(g);
if (action) drawAction(g);
drawBomb(g);
}
public abstract void drawAction(Graphics2D g);
public void drawBomb(Graphics2D g){
if(bombCountdown>0){
g.setColor(Color.white);
g.setFont(new Font("default", Font.BOLD, 14));
if (Window.getTps()-bombCountdown<60){
g.drawString(""+5,posX-width/2,posY-2*height);
g.drawImage(boom1, posX-imageRight.getWidth(),posY-imageRight.getHeight(),null);
}
else if (Window.getTps()-bombCountdown<120){
g.drawString(""+4,posX-width/2,posY-2*height);
g.drawImage(boom2, posX-imageRight.getWidth(),posY-imageRight.getHeight(),null);
}
else if (Window.getTps()-bombCountdown<180){
g.drawString(""+3,posX-width/2,posY-2*height);
g.drawImage(boom3, posX-imageRight.getWidth(),posY-imageRight.getHeight(),null);
}
else if (Window.getTps()-bombCountdown<240){
g.drawString(""+2,posX-width/2,posY-2*height);
g.drawImage(boom4, posX-imageRight.getWidth(),posY-imageRight.getHeight(),null);
}
else if (Window.getTps()-bombCountdown<300){
g.drawString(""+1,posX-width/2,posY-2*height);
g.drawImage(boom5, posX-imageRight.getWidth(),posY-imageRight.getHeight(),null);
}
}
}
public void drawMove(Graphics2D g){
if(action) return;
if (direction == 1){
if((Window.getTps()-iWalk)%10 > 5 && !inAir){
g.drawImage(getImageRightStep(),posX-imageRightStep.getWidth()/2,posY-imageRightStep.getHeight(),null);
}
else g.drawImage(getImageRight(),posX-imageRight.getWidth()/2,posY-imageRightStep.getHeight(),null);
}
else {
if((Window.getTps()-iWalk)%10 > 5 && !inAir){
g.drawImage(getImageLeftStep(),posX-imageLeftStep.getWidth()/2,posY-imageRightStep.getHeight(),null);
}
else g.drawImage(getImageLeft(),posX-imageLeft.getWidth()/2,posY-imageRightStep.getHeight(),null);
}
}
public boolean drawDeath(Graphics2D g){
if (iDeath != 0){
if (bombDeath){
g.drawImage(imageExplosion,posX-imageExplosion.getWidth()/2,posY-imageExplosion.getHeight()/2,null);
}
else{
//if(getClass().toString().contains("class Stopper")) System.out.println("death");
if (iDeath >= 20) g.drawImage(deathFirst,posX-imageRight.getWidth()/2,posY-imageRight.getHeight(),null);
else g.drawImage(deathSecond,posX-imageRight.getWidth()/2,posY-imageRight.getHeight(),null);
}
iDeath--;
return true;
}
return false;
}
public void update(){
if (!alive) return;
bomb();
move();
}
public boolean fall(){
for (int i=0;i<(imageRight.getWidth()/2);i++){
if(w.getPos(posX-i,posY+1)!=World.AIR_CST || w.getPos(posX+i,posY+1)!=World.AIR_CST){
if (iFall<maxFall && posY+2<=w.getHeight()) {
iFall = 0;
}
else kill();
inAir = false;
return false;
}
}
posY++;
inAir = true;
iWalk = Window.getTps();
iFall++;
action = false;
return true;
}
public boolean walk(){
//Fonction qui fait marcher le lemmings
for (int i =0;i<(height);i++){
if(w.getPos(posX+direction*(imageRight.getWidth()/2),posY-i)!=0
&& w.getPos(posX+direction*(imageRight.getWidth()/2),posY-i)!=direction+3
&& w.getPos(posX+direction*(imageRight.getWidth()/2),posY-i)!=direction+4){ //Verifie qur toute la hauteur du corps passe pour marcher
//direction + 3 car w.WALL_RIGHT_CST = 4; et w.WALL_LEFT_CST = 2;
return false;
}
}
posX+=direction;
return true; //avance car aucun obstacle
}
public boolean climbDown(){
//Fonction qui tente de descendre le lemming
int i;
for (i=1;i<imageRight.getHeight()/2;i++){ //descend si le leming n'a pas a se baisser trop
if(w.getPos(posX+direction*(imageRight.getWidth()/2),posY+i)==0 && w.getPos(posX+direction*(imageRight.getWidth()/2)+(direction*3*(imageRight.getWidth()/2)),i+posY-imageRight.getHeight()+1)==0){
for (int j =1;j<i;j++){
if(w.getPos(posX+direction*(imageRight.getWidth()/2),posY-i)!=0){
//On verifie que il n y a pas d obstacle trop haut sinon on retourne false
return false;
}
}
//Le if verifie que il peut descendre et que quand il descend il peut renterr en entier
posX+=direction;
posY+=i;
return true;
}
}
return false;
}
public boolean climbUp(){
int i,j;
for (i =(imageRight.getHeight()/2);i<imageRight.getHeight();i++){
if(w.getPos(posX+direction*(imageRight.getWidth()/2),posY-i)!=0 && w.getPos(posX+direction*(imageRight.getWidth()/2),posY-i)!=direction+3){
//On verifie que il n y a pas d obstacle trop haut sinon on retourne false
return false;
}
}
for (i =(imageRight.getHeight())/2+1;i>=0;i--){
if(w.getPos(posX+direction*(imageRight.getWidth()/2),posY-i)!=0 && w.getPos(posX+direction*(imageRight.getWidth()/2),posY-i)!=direction+3){
//On regarde la taille de la marche et on la climb
for (j =i+1;j<i+imageRight.getHeight();j++){
if(w.getPos(posX+direction*(imageRight.getWidth()/2),posY-j)!=0 && w.getPos(posX+direction*(imageRight.getWidth()/2),posY-j)!=direction+3){
//On verifie que il n y a pas d obstacle trop haut sinon on retourne false
return false;
}
}
posX+=direction;
posY-=i+1;
return true;
}
}
return false;
}
public boolean bomb(){
if(bombCountdown == -1) return false;
int time = Window.getTps()-bombCountdown;
if(time>300 && inWorld){
System.out.println("direction : "+direction);
for (int i = posX-bombRadius;i<posX+bombRadius;i++){
for (int j = posY-bombRadius;j<posY+bombRadius;j++){
if (w.getPos(i,j)>=1
&& w.getPos(i,j)!=3
&& w.getPos(i,j)!=5
&& (i-posX)*(i-posX)+(j-posY)*(j-posY)<=bombRadius*bombRadius){
w.setMapTypeAtPos(i,j,w.AIR_CST);
w.setMapPixelColor(i,j,w.AIR_LIST.get(w.airIndex));
}
}
}
if (this instanceof Affecter) ((Affecter)this).resetMap();
bombDeath = true;
kill();
bombCountdown =-1;
}
return true;
}
public void startBomb(){
bombCountdown = Window.getTps();
}
public void spawn(){
inWorld = true;
spawned = true;
}
public void win(){
if (this instanceof Affecter){
((Affecter)this).resetMap();
}
inWorld = false;
}
public void kill(){
alive = false;
inWorld = false;
iDeath = 40;
if (this instanceof Affecter){
((Affecter)this).resetMap();
}
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public int getDirection(){
return direction;
}
public void setPosX(int posX){
this.posX = posX;
}
public void setPosY(int posY){
this.posY = posY;
}
public boolean getInWorld(){
return inWorld;
}
public int getJob(){
return job;
}
public int getId(){
return id;
}
public boolean getAlive(){
return alive;
}
public boolean getSpawned(){
return spawned;
}
public long getBombCountdown(){
return bombCountdown;
}
public abstract BufferedImage getImageRight();
public abstract BufferedImage getImageRightStep();
public abstract BufferedImage getImageLeft();
public abstract BufferedImage getImageLeftStep();
}