-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameEngine.java
254 lines (222 loc) · 6.84 KB
/
GameEngine.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
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* This class includes the Rogue-Expanded game engine and high level logic for
* instatiating
* important objects and calling high level methods to fascilate gameplay.
*
* @author Ross Petridis | [email protected] | 1080249
*/
public class GameEngine {
private Player player = new Player();
private Monster monster = new Monster();
private boolean defaultMap;
/** scanner object for the game to read from System.in */
private static Scanner scanner = new Scanner(System.in);
private Scanner inputStream;
private String fileName;
public static final String FILE_EXTENSION = ".dat";
public static final String CMD_PROMPT = "> ";
public static void main(String[] args) {
GameEngine gameEngine = new GameEngine();
gameEngine.runGame();
scanner.close();
}
/*
* Logic for running the main game loop. Mostly Fascilitates the main menu in
* side of helper function "parseCommand"
*/
private void runGame() {
displayMenu();
String[] cmd_args = null;
// Do main menu.
do {
if (scanner.hasNextLine()) {
cmd_args = scanner.nextLine().trim().split(" ");
}
if (cmd_args[0] != "") {
parseCommand(cmd_args);
}
} while (!cmd_args[0].equals("exit"));
}
/**
* Logic for handling different input commands and actioning the commands by
* calling appriopriate methods
*
* @param cmd_args The inputted command line args from stdin.
*/
private void parseCommand(String[] cmd_args) {
String base_cmd = cmd_args[0];
switch (base_cmd) {
case "player":
player.createPlayer();
returnToMain();
break;
case "help":
System.out.println("Type 'commands' to list all available commands");
System.out.println("Type 'start' to start a new game");
System.out.print("Create a character, battle monsters, and find treasure!\n\n" + CMD_PROMPT);
break;
case "commands":
System.out.print("help\nplayer\nmonster\nstart\nexit\n\n" + CMD_PROMPT);
break;
case "exit":
System.out.println("Thank you for playing Rogue!");
break;
case "monster":
monster.createMonster();
returnToMain();
break;
case "load":
player.load();
System.out.print("\n\n" + CMD_PROMPT);
break;
case "save":
try { // try save player
player.save();
System.out.println("Player data saved.");
} catch (NoPlayerException e) {
System.out.println("No player data to save.");
} catch (FileNotFoundException e) {
System.out.println("File could not be created. Error: " + e.getMessage());
}
System.out.print("\n\n" + CMD_PROMPT);
//returnToMain();
break;
case "start":
if (player.getName() == null) {
System.out.println("No player found, please create a player with 'player' first.\n");
returnToMain();
break;
}
if (startingDefualt(cmd_args)) { //
// start default game.
if (monster.getName() == null) {
System.out.println("No monster found, please create a monster with 'monster' first.\n");
returnToMain();
break;
} else { // start default game!
player.heal();
monster.heal();
defaultMap = true; // do we actually use this variable inside of world?
startGame();
break;
}
} else {
// have player and loading rest from file.
try {
startFromFile(cmd_args[1] + FILE_EXTENSION);
} catch (GameLevelNotFoundException e) {
//System.out.println("Map not found.\n");
System.out.println(e.getMessage());
returnToMain();
}
break;
}
default:
System.out.print(
"Invalid command entered. Type 'commands' to see valid commands and try again.\n" + CMD_PROMPT);
break;
}
}
/**
* Start game fro input file name
*
* @param fileName The file name containing the level
* @throws GameLevelNotFoundException if the Game file cannot be found.
*/
private void startFromFile(String fileName) throws GameLevelNotFoundException {
try {
inputStream = new Scanner(new FileInputStream(fileName));
} catch (FileNotFoundException e) { // update to try again with new file name.
throw new GameLevelNotFoundException();
}
player.heal();
defaultMap = false;
startGame();
}
/**
* Engine logic for starting the game. Creates a world and begins the search
* scene until the player and monster have encounterd each other. At this
* point, the battle scene is instatiated and run.
*/
private void startGame() {
// Create world according to word type. (default world or from file)
World world = defaultMap ? new World(player, monster) : new World(player, inputStream);
boolean returnedHome = world.runGameLoop();
if (returnedHome) {
System.out.println("Returning home...\n");
}
world.reset(); // dont heal player until re starting. But update the level here (and new maxHealth)
returnToMain();
// game over!
}
/**
* Called when its time to return to main. Awaits for enter input before
* calling a method to display Menu graphics.
*/
private void returnToMain() {
System.out.print("(Press enter key to return to main menu)\n");
scanner.nextLine();
// scanner.nextLine();
displayMenu();
}
/**
* Method for diplsaying Menu graphics including health and names.
*/
private void displayMenu() {
displayTitleText();
player.displayNameAndHealth();
System.out.print(" | ");
monster.displayNameAndHealth();
System.out.println("Please enter a command to continue.");
System.out.print("Type 'help' to learn how to get started.\n\n" + CMD_PROMPT);
}
/*
* Displays the title text.
*/
private void displayTitleText() {
String titleText = " ____ \n" +
"| _ \\ ___ __ _ _ _ ___ \n" +
"| |_) / _ \\ / _` | | | |/ _ \\\n" +
"| _ < (_) | (_| | |_| | __/\n" +
"|_| \\_\\___/ \\__, |\\__,_|\\___|\n" +
"COMP90041 |___/ Assignment ";
System.out.println(titleText);
System.out.println();
}
public String getFileName() {
return fileName;
}
public static Scanner getStdInScanner() {
return scanner;
}
public Scanner getInputStreamScanner() {
return inputStream;
}
/**
* Returns only once valid integer input has been recieved
*/
public static void validIntegerInput() {
while (!scanner.hasNextInt()) {
System.out.println("Please enter an integer number");
scanner.next();
}
}
/**
*
* @return true if game mode is default
*/
public boolean getGameMode() {
return defaultMap;
}
private boolean startingDefualt(String[] cmd_args) {
return (cmd_args.length <= 1);
}
}
/*
* else if (scanner.hasNext()){
* cmd_args = scanner.next().trim().split(" ");
* }
*/