-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathGame.java
More file actions
89 lines (81 loc) · 3.14 KB
/
Game.java
File metadata and controls
89 lines (81 loc) · 3.14 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
package com.jadventure.game;
import com.jadventure.game.entities.Player;
import com.jadventure.game.monsters.Monster;
import com.jadventure.game.monsters.MonsterFactory;
import com.jadventure.game.repository.LocationRepository;
import com.jadventure.game.prompts.CommandParser;
import java.util.ArrayList;
/**
* This class contains the main loop that takes the input and
* does the according actions.
*/
public class Game {
public ArrayList<Monster> monsterList = new ArrayList<Monster>();
public MonsterFactory monsterFactory = new MonsterFactory();
public CommandParser parser;
public Monster monster;
Player player = null;
public Game(Player player, String playerType, JAdventure jAdventure) throws DeathException {
this.parser = new CommandParser(player);
this.player = player;
switch (playerType) {
case "new":
jAdventure.loadCharacterName(player, this);
break;
case "old":
jAdventure.loadWelcome(player, "old");
//player.getLocation().print();
//gamePrompt(player);
break;
default:
QueueProvider.offer("Invalid player type");
break;
}
}
/**
* Starts a new game.
* It prints the introduction text first and asks for the name of the player's
* character and welcomes him / her. After that, it goes to the normal game prompt.
*/
public void newGameStart(Player player) throws DeathException {
LocationRepository locationRepo = GameBeans.getLocationRepository(player.getName());
this.player.setLocation(locationRepo.getInitialLocation());
player.save();
//QueueProvider.offer("Welcome to Silliya, " + player.getName() + ".");
//player.getLocation().print();
//gamePrompt(player);
}
public void newGameStart_old(Player player) throws DeathException {
QueueProvider.offer(player.getIntro());
String userInput = QueueProvider.take();
player.setName(userInput);
LocationRepository locationRepo = GameBeans.getLocationRepository(player.getName());
this.player.setLocation(locationRepo.getInitialLocation());
player.save();
QueueProvider.offer("Welcome to Silliya, " + player.getName() + ".");
player.getLocation().print();
gamePrompt(player);
}
/**
* This is the main loop for the player-game interaction. It gets input from the
* command line and checks if it is a recognised command.
*
* This keeps looping as long as the player didn't type an exit command.
*/
public void gamePrompt(Player player) throws DeathException {
boolean continuePrompt = true;
try {
while (continuePrompt) {
QueueProvider.offer("\nPrompt:");
String command = QueueProvider.take().toLowerCase();
continuePrompt = parser.parse(player, command);
}
} catch (DeathException e) {
if (e.getLocalisedMessage().equals("replay")) {
return;
} else {
throw e;
}
}
}
}