-
Notifications
You must be signed in to change notification settings - Fork 1
/
World.java
523 lines (439 loc) · 12.5 KB
/
World.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import java.util.ArrayList;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.io.File;
import java.awt.Color;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
//import javafx.scene.media.Media;
//import javafx.scene.media.MediaPlayer;
public class World implements Renderable{
//classe qui represente le monde actuel dans la fenetre principale
//==================== ATTRIBUTS ========================
private int[][] map; //carte colore du monde a mettre a jour au tout debut
private int height; //hauteur de la carte
private int width; //largeur
private int id;
private Lemmings[] list; //identifiant
private Item[] itemList;
private BufferedImage mapImage; //Image en .png de la carte a charger
public static final ArrayList<Color> AIR_LIST = new ArrayList<Color>(); //liste des constantes d'air
public static final int INVALID_POS_CST = -1;
public static final int AIR_CST = 0; //constantes pour mieux lire
public static final int GROUND_CST = 1;
public static final int WALL_RIGHT_CST = 4; //constantes pour mieux lire
public static final int WALL_LEFT_CST = 2;
public static final int STOPPER_WALL_RIGHT_CST = 5; //constantes pour mieux lire
public static final int STOPPER_WALL_LEFT_CST = 3;
public int airIndex;
public static final int settingsLines = 19;
private Spawner spawn;
private Outside end;
private int spawnX;
private int spawnY;
private SpitFire spitFire;
private int spitFireX;
private int spitFireY;
private int outsideX;
private int outsideY;
private int outsideType;
private int stopperLimit;
private int bomberLimit;
private int builderLimit;
private int basherLimit;
private int minerLimit;
private int excavaterLimit;
private int minerDirection = 1;
private boolean finished = false;
private boolean started = false;
private int victoryCondition;
public static final int WALKER = -1;
public static final int STOPPER = 4;
public static final int BOMBER = 0;
public static final int BUILDER = 3;
public static final int BASHER = 1;
public static final int MINER = 2;
public static final int EXCAVATER = 5;
Stats stats;
//================== CONSTRUCTEURS ======================
public World(int id){
this.id = id;
loadWorld();
}
//===================== METHODES =========================
public void loadWorld(){
try{
mapImage = ImageIO.read(new File("world/world"+id+".png")); //lit l'image de la carte et la stocke en fonction de l'identifiant
}catch(Exception e){e.printStackTrace();}
this.width = mapImage.getWidth();
this.height = mapImage.getHeight();
fillMap();
initAirCst();
setSettings();
for (int i=0;i<width;i++){
for(int j=0;j<height;j++){
Color mapXY = getColor(i,j,mapImage);
if(AIR_LIST.contains(mapXY)){
map[i][j] = AIR_CST;
}
else {
map[i][j] = GROUND_CST;
}
}
}
stats = new Stats(this);
}
public void startWorld(){
for (Item i:itemList){
i.startItem();
}
started = true;
}
public void setSettings(){
BufferedReader br = null;
FileReader fr = null;
int[] settings = new int[settingsLines];
try{
fr = new FileReader("world/world"+id+"settings.txt");
br = new BufferedReader(fr);
String currentLine;
for (int i=0;i<settingsLines;i++){
currentLine = br.readLine();
settings[i] = Integer.parseInt(currentLine);
}
spawnX = settings[0];
spawnY = settings[1];
spawn = new Spawner(spawnX,spawnY,settings[5]);
outsideType = settings[2];
outsideX = settings[3];
outsideY = settings[4];
loadLemmings(settings[6]);
end = new Outside(outsideX,outsideY,list,this,outsideType);
airIndex = settings[7];
victoryCondition = settings[8];
stopperLimit = settings[9];
bomberLimit = settings[10];
builderLimit = settings[11];
basherLimit = settings[12];
minerLimit = settings[13];
excavaterLimit = settings[14];
if( settings[15]>=1){
spitFireX = settings[17];
spitFireY = settings[18];
spitFire = new SpitFire(spitFireX, spitFireY, this, settings[16]);
itemList = new Item[3];
itemList[0] = spawn;
itemList[1] = end;
itemList[2] = spitFire;
}
else{
itemList = new Item[2];
itemList[0] = spawn;
itemList[1] = end;
}
//provisoire
}catch (IOException e){e.printStackTrace();}
finally{
try{
if (br != null)
br.close();
if (fr != null)
fr.close();
}catch (IOException e2) {e2.printStackTrace();}
}
}
public void loadLemmings(int nb){
Lemmings.w = this;
list = new Lemmings[nb];
for (int i=0;i<nb;i++){
addLemmings(i,new Walker(spawnX,spawnY));
}
/*for (int j=0;j<nb;j++){
if ((j%2)==1) {
int id = list[j].id;
Lemmings[] tab = new Lemmings[1];
//end.removeLemmingFromList(id);
list[j] = list[j].changeJob(STOPPER);
tab[0] = list[j];
//end.addLemmings(tab);
}
}*/
}
/*public void son(){
try{
Media song = new Media(new File("world/LemmingsMusic.mp3").toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(song);
mediaPlayer.play();
}catch(Exception e){e.printStackTrace();}
}*/
public void initAirCst(){
//initialise les constantes d'air pour avoir plus de choix (background et tout)
AIR_LIST.add(new Color(97,172,191));
AIR_LIST.add(new Color(0,0,0));
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public int getID(){
return id;
}
public void fillMap(){
// remplit la map d'air
map = new int[width][height];
for (int i=0;i<width;i++){
for(int j=0;j<height;j++){
map[i][j] = 0;
}
}
}
public int getPos(int posX, int posY){
//Fonction qui en fonction de x et y renvoie 1 si la couleur sur la case x,y est considéré comme de l'aire
//le truc des couleur va etre dans la construction du monde pour l'initialisation de map[][] (pour ne pas recalculer a chaque fois)
if (onBounds(posX,posY)) return map[posX][posY];
return INVALID_POS_CST;
}
public void setMapTypeAtPos(int posX, int posY, int TYPE_CST){
//cette fonction sert plus bas
map[posX][posY] = TYPE_CST;
}
public void setMapPixelColor(int posX, int posY, Color c){
//cette fonction servira pour modifier un pixel - ex replaceGroundWithBackground
mapImage.setRGB(posX,posY,c.getRGB());
}
public void replaceGroundWithBackground(int posX, int posY){
//cette fonction servira pour la destruction de terrains
//il faudra select un AIR random ? pas sur que ca marche encore
setMapPixelColor(posX,posY,AIR_LIST.get((int)(Math.random()*(AIR_LIST.size()+1))));
}
public Color getColor(int posX, int posY, BufferedImage image){
//retourne la couleur de la map en posX, posY
int color;
int red;
int green;
int blue;
color = image.getRGB(posX,posY);
red = (color & 0x00ff0000) >> 16;
green = (color & 0x0000ff00) >> 8;
blue = color & 0x000000ff;
return new Color(red,green,blue);
}
public boolean addObjectToWorld(int posX, int posY, int type_CST, BufferedImage image, int direction){
//pas sur encore
if (posX>=width || posX<0 || posY<0 || posY >=height || posX+image.getWidth()>=width || posY+image.getHeight()>=height) return false;
if (direction == 1){
for(int i = posX;i<posX+image.getWidth();i++){
for(int j = posY;j<posY+image.getHeight();j++){
if (getPos(i,j)==1) return false;
else setMapTypeAtPos(i,j,type_CST);
setMapPixelColor(i,j,getColor(i-posX,j-posY,image));
}
}
}
else{
for(int i = posX+image.getWidth()-1;i>=posX;i--){
for(int j = posY+image.getHeight()-1;j>=posY;j--){
if (getPos(i,j)==1) return false;
else setMapTypeAtPos(i,j,type_CST);
setMapPixelColor(i,j,getColor(i-posX,j-posY,image));
}
}
}
return true;
}
public void draw(Graphics2D g){
//Dessine l'image avec l'image .png choisi au debut
g.drawImage(mapImage,0,0,null);
}
public int getSpawnX(){
return spawnX;
}
public int getSpawnY(){
return spawnY;
}
public int getOutsideX(){
return outsideX;
}
public int getOutsideY(){
return outsideY;
}
public void spawnLemmings(){
spawn.addLemmings(list);
}
public void spiteFireLemmingsFilling(){
spitFire.addLemmings(list);
}
public Lemmings[] getLemmingsList(){
return list;
}
public Spawner getSpawner(){
return spawn;
}
public SpitFire getSpitFire(){
return spitFire;
}
public Outside getOutside(){
return end;
}
public boolean getFinished(){
return finished;
}
public boolean getStarted(){
return started;
}
public void setMinerDirection(int directionY){
minerDirection = directionY;
}
public int getVictoryCondition(){
return victoryCondition;
}
public void setFinished(boolean finished){
this.finished = finished;
}
public Stats getStats(){
return stats;
}
public String getLemmingsJob(int state){
if (state == BOMBER) return "Bomber";
else if (state == BUILDER) return "Builder";
else if (state == BASHER) return "Basher";
else if (state == STOPPER) return "Stopper";
else if (state == MINER) return "Miner";
else if (state == EXCAVATER) return "Excavater";
else return "Walker";
}
public int getLemmingsLimit(int state){
if (state == BOMBER) return bomberLimit;
else if (state == BUILDER) return builderLimit;
else if (state == BASHER) return basherLimit;
else if (state == STOPPER) return stopperLimit;
else if (state == MINER) return minerLimit;
else if (state == EXCAVATER) return excavaterLimit;
else return -20;
}
public boolean canDestructPixel(int posX, int posY){
if (!onBounds(posX,posY)) return false;
if (getPos(posX,posY)==INVALID_POS_CST || getPos(posX,posY)==3 || getPos(posX,posY)==5) return false;
return true;
}
public boolean onBounds(int posX, int posY){
if (posX<width && posX>=0 && posY<height && posY >=0) return true;
return false;
}
//=========================PRIORITY==========================
public void addLemmings(int i,Lemmings l){
list[i] = l;
sortLemmings();
}
public void sortLemmings(){
if (list[list.length-1] == null) return;
int index = 0;
for (int i=0;i<list.length;i++){
Lemmings l = list[i];
if (list[index].getJob()>l.getJob()){
reverseLemmings(i,index);
System.out.println("reverse "+i+" et "+index);
index = i;
}
}
}
public void reverseLemmings(int i, int j){
Lemmings lTemp = list[j];
list[j] = list[i];
list[i] = lTemp;
}
public void replaceLemmings(Lemmings l, Lemmings l2){
for (int i=0;i<list.length;i++){
if(l.getId()==list[i].getId()){
addLemmings(i,l2);
return;
}
}
}
public void printList(){
for (int i=0;i<list.length;i++){
if(list[i]!=null){
System.out.println("i : "+i+" | "+list[i].toString());
}
}
}
//======================= END PRIORITY =========================
public void changeJob(Lemmings l,int state){
Lemmings newLemming = null;
if(state == WALKER){
newLemming = new Walker(l);
System.out.println("changeJob to WALKER");
}
else if(state == STOPPER){
if(stopperLimit>0){
stopperLimit--;
newLemming = new Stopper(l);
System.out.println("changeJob to STOPPER");
}
}
else if(state == BOMBER){
if(bomberLimit>0){
bomberLimit--;
l.startBomb();
}
return;
}
else if(state == BUILDER){
if(builderLimit>0){
builderLimit--;
newLemming = new Builder(l);
System.out.println("changeJob to BUILDER");
}
}
else if(state == BASHER){
if(basherLimit>0){
basherLimit--;
newLemming = new Basher(l);
System.out.println("changeJob to BASHER");
}
}
else if(state == MINER){
if(minerLimit>0){
minerLimit--;
newLemming = new Miner(l,minerDirection);
System.out.println("changeJob to MINER");
}
}
else if(state == EXCAVATER){
if(excavaterLimit>0){
excavaterLimit--;
newLemming = new Excavater(l);
System.out.println("changeJob to EXCAVATER");
}
}
else{
System.out.println("Erreur : job non crée.");
}
if(l instanceof Affecter){
System.out.println(l.toString()+" | reset la map");
((Affecter)l).resetMap();
}
int index = INVALID_POS_CST;
for(int i=0;i<list.length;i++){
if (list[i].getId()==l.getId()){
index = i;
break;
}
}
if((index == INVALID_POS_CST) || (newLemming==null)){
System.out.println("Out of lemmings of this type");
return;
}
replaceLemmings(list[index],newLemming);
Lemmings[] tab = new Lemmings[1];
tab[0] = newLemming;
spawn.removeLemmingFromList(l.getId());
spawn.addLemmings(tab);
end.removeLemmingFromList(l.getId());
end.addLemmings(tab);
}
}