-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.java
More file actions
executable file
·637 lines (535 loc) · 16.8 KB
/
GameEngine.java
File metadata and controls
executable file
·637 lines (535 loc) · 16.8 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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
import java.util.HashMap;
import java.util.Stack;
import java.net.URL;
import java.util.Scanner;
import java.io.IOException;
import java.lang.NullPointerException;
/**
* La classe gerant le jeu.
* @author Clément Chomicki
*/
public class GameEngine
{
private Parser aParser;
private UserInterface aGui;
private HashMap<String, Room> aRooms;
private Player aPlayer;
private int aTimeLimit;
private int aTimeSpent;
/**
* Constructeur de la classe
*/
public GameEngine()
{
this.aParser = new Parser();
this.aRooms = new HashMap<String, Room>();
this.aPlayer = new Player("Anon");
this.aTimeLimit = 200;
this.aTimeSpent = 0;
createRooms();
} //GameEngine()
/**
* Initie aGui
* @param userInterface: l'interface du jeu
*/
public void setGUI(final UserInterface userInterface)
{
this.aGui = userInterface;
this.printWelcome();
} //setGUI()
/**
* Procedure affichant la description de la piece et les sorties.
*/
private void printLocationInfos()
{
this.aGui.println ( this.aPlayer.getLocation().getLongDescription() );
} //printLocationInfos()
/**
* Print out the opening message for the player.
*/
private void printWelcome()
{
this.aGui.println ( "Welcome to “Within the Woods”" );
this.aGui.println ( "Type 'help' if you want help (you won't get any)." );
this.printLocationInfos();
this.aGui.showImage(aPlayer.getLocation().getImageName());
} //printWelcome()
/**
* Procedure creant les pieces et les reliant entre elles.
*/
private void createRooms ()
{
String vFolder = "Ressources/Images";
String vFormat = "png";
String vImgForest = vFolder+"/"+"dryads_res"+"."+vFormat;
String vImgDryads = vFolder+"/"+"dryads_res"+"."+vFormat;
String vImgNaiads = vFolder+"/"+"naiads_res"+"."+vFormat;
String vImgFaun = vFolder+"/"+"faun_res"+"."+vFormat;
String vImgFlowers = vFolder+"/"+"flowers_res"+"."+vFormat;
String vImgCave = vFolder+"/"+"sanctuary_res"+"."+vFormat;
String vImgShrooms = vFolder+"/"+"shrooms_res"+"."+vFormat;
String vImgSquirel = vFolder+"/"+"squirel_res"+"."+vFormat;
String vImgWitch = vFolder+"/"+"witch_res"+"."+vFormat;
String vImgCorgy = vFolder+"/"+"corgy.jpg";
Room vSpirit = new Room ("in a strange forest. You feel observed.", vImgForest);
Room vForest1 = new Room ("lost in the forest.", vImgForest);
Room vForest2 = new Room ("lost in the forest.", vImgForest);
Room vForest3 = new Room ("lost in the forest.", vImgForest);
Room vWitch = new Room ("at the wicked witch's house.", vImgWitch);
Room vNaiads = new Room ("in a warm, enlighted and clean cave with some kinds of clear ponds.", vImgNaiads);
Room vDryads = new Room ("in a dense and dark part of the forest.", vImgDryads);
Room vCave = new Room ("surrounded by calm darknesses.\nThe water is clean and warm and the walls are veined by a phosphorescent mineral.\nYou feel comfortable.", vImgCave);
Room vFaun = new Room ("near a convenient house-shaped tree.", vImgFaun);
Room vShrooms = new Room ("surrounded by mushrooms. They are absolutly everywhere.", vImgShrooms);
Room vSquirel = new Room ("at some big tree with an hole in it.", vImgSquirel);
Room vFlower = new Room ("in a warm and windy glade, covered by nice and odorant flowers of all sort.", vImgFlowers);
HashMap<String, Room> vForestNeighborhood = new HashMap<String, Room>();
vForestNeighborhood.put("forest1", vForest1);
vForestNeighborhood.put("forest2", vForest2);
vForestNeighborhood.put("forest3", vForest3);
vForestNeighborhood.put("faun", vFaun);
vForestNeighborhood.put("flower", vFlower);
vForestNeighborhood.put("squirel", vSquirel);
vForestNeighborhood.put("shrooms", vShrooms);
Room vForest = new TransporterRoom ("lost in the forest.", vImgForest, vForestNeighborhood);
aRooms.put("spirit", vSpirit);
vSpirit.setExit("north", vForest);
vSpirit.setExit("south", vForest);
vSpirit.setExit("east", vForest);
vSpirit.setExit("west", vForest);
aRooms.put ( "forest" , vForest );
aRooms.put ( "forest1", vForest1 );
aRooms.put ( "forest2", vForest2 );
aRooms.put ( "forest3", vForest3 );
aRooms.put ( "witch" , vWitch );
aRooms.put ( "naiads" , vNaiads );
aRooms.put ( "dryads" , vDryads );
aRooms.put ( "cave" , vCave );
aRooms.put ( "faun" , vFaun );
aRooms.put ( "shrooms", vShrooms );
aRooms.put ( "squirel", vSquirel );
aRooms.put ( "flower" , vFlower );
vWitch.setExit ( "south", vDryads );
vWitch.setExit ( "north", vForest );
vWitch.setExit ( "east" , vForest );
vWitch.setExit ( "west" , vForest );
vWitch.setExit ( "south-east" , vFlower );
vNaiads.setExit ( "south", vForest );
vNaiads.setExit ( "north", vDryads );
vNaiads.setExit ( "east" , vFaun );
vNaiads.setExit ( "west" , vForest );
vNaiads.setExit ( "down" , vCave );
vCave.setExit ("up", vNaiads );
vDryads.setExit ( "south", vNaiads );
vDryads.setExit ( "north", vWitch );
vDryads.setExit ( "east" , vFlower );
vDryads.setExit ( "west" , vForest );
vFaun.setExit ( "south", vForest );
vFaun.setExit ( "north", vFlower );
vFaun.setExit ( "east" , vForest );
vFaun.setExit ( "west" , vNaiads );
vShrooms.setExit ( "south", vForest );
vShrooms.setExit ( "north", vSquirel);
vShrooms.setExit ( "east" , vForest );
vShrooms.setExit ( "west" , vFlower );
vSquirel.setExit ( "north", vForest );
vSquirel.setExit ( "south", vShrooms);
vSquirel.setExit ( "east" , vForest );
vSquirel.setExit ( "west" , vForest );
vFlower.setExit ( "south", vFaun );
vFlower.setExit ( "north", vForest );
vFlower.setExit ( "east" , vShrooms);
vFlower.setExit ( "west" , vDryads );
vFlower.setExit ( "north-west" , vWitch );
vForest.setExit ( "south", vForest1 );
vForest.setExit ( "north", vForest );
vForest.setExit ( "east" , vForest2 );
vForest.setExit ( "west" , vForest1 );
vForest1.setExit ( "south", vForest2 );
vForest1.setExit ( "north", vForest3 );
vForest1.setExit ( "east" , vForest );
vForest1.setExit ( "west" , vForest1 );
vForest2.setExit ( "south", vForest );
vForest2.setExit ( "north", vForest3 );
vForest2.setExit ( "east" , vForest1 );
vForest2.setExit ( "west" , vForest2 );
vForest3.setExit ( "south", vFaun );
vForest3.setExit ( "north", vFlower );
vForest3.setExit ( "east" , vDryads );
vForest3.setExit ( "west" , vForest );
Item vDague = new Item("dagger", 8);
Item vTheiere = new Item("teapot", 1);
Item vFleur = new Item("flower", 0);
Item vNoix = new Item("nut", 2);
Item vCailloux = new Beamer("stone", 1);
Item vPantalon = new Item("trouser", 1);
vDryads.addItem ( vTheiere );
vSquirel.addItem ( vDague );
vFlower.addItem ( vFleur );
vFlower.addItem ( vFleur );
vShrooms.addItem ( vNoix, 666 );
vForest.addItem ( vCailloux );
vForest.addItem ( vPantalon );
this.aPlayer.setMaxWeight(2);
this.aPlayer.setLocation(vSpirit);
} //createRooms()
/**
* Methode traitant une commande.
* @param pCommandLine: String entree
*/
public void interpretCommand (final String pCommandLine)
{
this.aGui.println(pCommandLine);
Command vCommand = this.aParser.getCommand(pCommandLine);
if (vCommand.isUnknown())
{
this.aGui.println ( "I don't know what you mean..." );
}
else
switch (vCommand.getCommandWord())
{
case GO :
this.goRoom(vCommand);
break;
case HELP :
this.printHelp();
break;
case QUIT :
if ( vCommand.hasSecondWord() )
aGui.println("Quit what?");
else
this.endGame();
break;
case LOOK :
this.look();
break;
case EAT :
this.eat(vCommand);
break;
case BACK :
this.back( vCommand );
break;
case TEST :
this.test(vCommand);
break;
case TAKE :
this.take( vCommand );
break;
case DROP :
this.drop( vCommand );
break;
case ITEMS:
this.items( vCommand );
break;
case STONE:
this.stone( vCommand );
break;
case ALEA:
this.alea( vCommand );
break;
default :
this.aGui.println ( "Looks like the dev forgot to implement that command but yet whitelisted it." );
}
} // interpretCommand()
/**
* procedure appellee par la commande alea
* @param pCommand la commande entree
*/
public void alea(final Command pCommand)
{
Room vRoom = this.aPlayer.getLocation();
if ( vRoom.getClass().equals(TransporterRoom.class ))
{
TransporterRoom vTRoom = (TransporterRoom) vRoom;
if (pCommand.hasSecondWord() )
{
String vWord = pCommand.getSecondWord();
if (vTRoom.doesExist(vWord) )
{
vTRoom.setNext(vWord);
this.aGui.println("Destiny have been altered, you will arrive to "+vWord);
}
else
this.aGui.println("This isn't an available room");
}
else
{
vTRoom.setNext(null);
this.aGui.println("Destiny have been altered, you will arrive ... somewhere");
}
}
else
this.aGui.println("You are not in a Transporter Room");
} //alea()
/**
* procedure appellee par la commande stone
* @param pCommand la commande entree
*/
public void stone(final Command pCommand)
{
Beamer vStone = (Beamer) this.aPlayer.getItemByName("stone");
if ( vStone == null)
this.aGui.println("You don't have any stone.");
else if ( pCommand.hasSecondWord() )
{
String vWord = pCommand.getSecondWord();
if (vWord.equals("infuse")) {
vStone.charge(this.aPlayer.getLocation());
this.aGui.println("The stone takes the colour of the ambiant light.");
}
else if (vWord.equals("jump") )
{
if ( vStone.isCharged() )
{
this.aGui.println("You look into the stone, the stone look into you. Your surroundings changed without you noticing it.");
Room vDestination = vStone.discharge();
this.aPlayer.resetStack();
this.changeRoom( vDestination );
}
else
this.aGui.println("Your look into the stone. Your mind get lost in its plain gray color. You suddendly realize you're getting nowhere.");
}
else
this.aGui.println("Stop doing that to the stone!");
}
else
this.aGui.println("You put the stone on your head. It has no effect.");
} //stone()
/**
* Procedure appelee par la commande 'items'
* @param pCommand une commande dont on sait que le mot-commande est 'items'
*/
private void items( final Command pCommand )
{
if ( pCommand.hasSecondWord() )
this.aGui.println("That doesn't make any sense");
else
this.aGui.println(this.aPlayer.makeItemsDescription());
} //items()
/**
* Procedure appelee par la commande 'take'
* @param pCommand une commande dont on sait que le mot-commande est 'take'
*/
private void take( final Command pCommand )
{
if ( pCommand.hasSecondWord() )
{
String vSec = pCommand.getSecondWord();
Room vLocation = this.aPlayer.getLocation();
Item vItem = vLocation.getItemByName( vSec );
if (vItem == null)
this.aGui.println( "This isn't something you can take." );
else if (this.aPlayer.canCarryItem( vItem) )
{
this.aPlayer.addItem( vItem );
vLocation.removeItem( vItem );
this.aGui.println( "You took a " + vSec );
}
else
this.aGui.println("You cannot carry more weight.");
}
else
{
this.aGui.println ( "Take what?" ); }
} //take()
/**
* Procedure appelee par la commande 'drop'
* @param pCommand une commande dont on sait que le mot-commande est 'drop'
*/
private void drop( final Command pCommand )
{
if ( pCommand.hasSecondWord() )
{
String vSec = pCommand.getSecondWord();
Room vLocation = this.aPlayer.getLocation();
Item vItem = this.aPlayer.getItemByName( vSec );
if (vItem == null)
this.aGui.println( "You don't have this." );
else {
this.aPlayer.removeItem( vItem );
vLocation.addItem( vItem );
this.aGui.println( "You dropped a " + vSec );
}
}
else
{
this.aGui.println ( "Drop what?" ); }
} //drop()
/**
* Procedure appelee par la commande 'eat'
* @param pCommand une commande dont on sait que le mot-commande est 'eat'
*/
private void eat ( final Command pCommand )
{
if ( pCommand.hasSecondWord() )
{
if ( pCommand.getSecondWord().equals("nut") )
{
Item vNut = this.aPlayer.getItemByName("nut");
if (vNut == null)
this.aGui.println( "You don't have anything to eat." );
else
{
this.aPlayer.removeItem( vNut );
this.aPlayer.setMaxWeight(this.aPlayer.getMaxWeight()+1);
this.aGui.println( "You just ate a nut.\nYou feel good, stronger than ever." );
}
}
else
this.aGui.println("You can't eat that.");
}
else
this.aGui.println("You try to eat thin air. You kind of like it.");
} //eat()
/**
* Procedure appelee par la commande 'look'
*/
private void look ()
{
this.printLocationInfos();
} // look()
/**
* Procedure affichant l'aide.
*/
private void printHelp ()
{
this.aGui.println( "You are lost in this mysterious forest.\nYour command words are:" );
this.aGui.println( this.aParser.getCommandList() );
} //printHelp()
/**
* Methode changeant la piece courrante et appellant l'affichage de description de la nouvelle piece.
* @param pNextRoom: Room dans laquelle on se deplace
*/
private void changeRoom( final Room pNextRoom )
{
this.aPlayer.setLocation( pNextRoom );
this.printLocationInfos();
if (this.aPlayer.getLocation().getImageName() != null)
this.aGui.showImage(aPlayer.getLocation().getImageName());
} //changeRoom()
/**
* Procedure changeant la piece courante.
* @param pCommand objet Command dont on sait que le premier mot est 'go'
*/
private void goRoom (final Command pCommand)
{
if ( ! pCommand.hasSecondWord() )
{
this.aGui.println("Go where?");
return;
}
Room vNextRoom = this.aPlayer.getLocation().getExit ( pCommand.getSecondWord() );
if (vNextRoom == null) {
this.aGui.println ( "There is no door!" );
} else {
if ( vNextRoom.isExit(this.aPlayer.getLocation()) )
{
this.aPlayer.pushPreviousLocation( vNextRoom );
}
else
{
this.aPlayer.resetStack();
}
this.changeRoom( vNextRoom );
this.addTime();
}
} //goRoom()
/**
* incremente le temps passe de 1
*/
private void addTime()
{
this.aTimeSpent += 1;
if (this.aTimeSpent >= this.aTimeLimit)
looseGame();
} //addTime()
/**
* Methode permettant de revenir en arriere
* @param pCommand: commande entree
*/
private void back( final Command pCommand )
{
if ( pCommand.hasSecondWord() )
{
this.aGui.println("Backstreet's back alright!");
} else
{
if ( this.aPlayer.hasNoPreviousLocations() )
this.aGui.println ( "There is no going back." );
else
this.changeRoom( this.aPlayer.goToPreviousLocation() );
}
} //back()
/**
* Methode appellee par la commande test
* @param pCommand: commande entree
*/
private void test( final Command pCommand )
{
if ( pCommand.hasSecondWord() )
{
String vName = pCommand.getSecondWord();
if (! vName.matches(".*.txt") )
vName += ".txt";
Scanner vScript = RessourcesFetcher.openScript( vName );
if (! (vScript == null) )
{
while ( vScript.hasNextLine() )
{
this.interpretCommand( vScript.nextLine() );
}
}
} else {
this.aGui.println ( "Please specify the script to use." );
}
} //test()
/**
* @return la description de la piece courante
*/
private String getCurrentRoomName()
{
return this.aPlayer.getLocation().getDescription();
} //getCurrentRoomName()
/**
* @param pRoomName la description d'une piece
* @return un booleen
*/
private boolean estDansPiece( final String pRoomName )
{
return this.aPlayer.getLocation().getDescription().equals( pRoomName );
} //estDansPiece()
/**
* Procedure de meurtre
*/
private void kill()
{
if ( this.aPlayer.getLocation() == this.aRooms.get("witch") )
winGame();
else
looseGame();
} //kill()
/**
* Procedure de victoire
*/
private void winGame()
{
this.aGui.println("You win!");
endGame();
} //winGame()
/**
* Procedure de defaite
*/
private void looseGame()
{
this.aGui.println("You lost!");
endGame();
} //looseGame()
/**
* Procedure terminant le jeu.
*/
private void endGame()
{
this.aGui.println("Thank you for playing. Good bye.");
this.aGui.enable(false);
} //endGame()
}