-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathCore.java
More file actions
278 lines (242 loc) · 7.72 KB
/
Core.java
File metadata and controls
278 lines (242 loc) · 7.72 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
package engine;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import screen.*;
/**
* Implements core game logic.
*
* @author <a href="mailto:RobertoIA1987@gmail.com">Roberto Izquierdo Amo</a>
*
*/
public final class Core {
/** Width of current screen. */
private static int WIDTH = 448;
/** Height of current screen. */
private static int HEIGHT = 520;
/** Max fps of current screen. */
private static final int FPS = 60;
/** Max lives. */
private static final int MAX_LIVES = 3;
/** Levels between extra life. */
private static final int EXTRA_LIFE_FRECUENCY = 3;
/** Total number of levels. */
private static final int NUM_LEVELS = 7;
/** Difficulty settings for level 1. */
private static final GameSettings SETTINGS_LEVEL_1 =
new GameSettings(5, 4, 60, 2000);
/** Difficulty settings for level 2. */
private static final GameSettings SETTINGS_LEVEL_2 =
new GameSettings(5, 5, 50, 2500);
/** Difficulty settings for level 3. */
private static final GameSettings SETTINGS_LEVEL_3 =
new GameSettings(6, 5, 40, 1500);
/** Difficulty settings for level 4. */
private static final GameSettings SETTINGS_LEVEL_4 =
new GameSettings(6, 6, 30, 1500);
/** Difficulty settings for level 5. */
private static final GameSettings SETTINGS_LEVEL_5 =
new GameSettings(7, 6, 20, 1000);
/** Difficulty settings for level 6. */
private static final GameSettings SETTINGS_LEVEL_6 =
new GameSettings(7, 7, 10, 1000);
/** Difficulty settings for level 7. */
private static final GameSettings SETTINGS_LEVEL_7 =
new GameSettings(8, 7, 2, 500);
/** Frame to draw the screen on. */
public static Frame frame;
/** Screen currently shown. */
private static Screen currentScreen;
/** Difficulty settings list. */
private static List<GameSettings> gameSettings;
/** Application logger. */
private static final Logger LOGGER = Logger.getLogger(Core.class
.getSimpleName());
/** Logger handler for printing to disk. */
private static Handler fileHandler;
/** Logger handler for printing to console. */
private static ConsoleHandler consoleHandler;
/**
* Test implementation.
*
* @param args
* Program args, ignored.
*/
public static void main(final String[] args) {
try {
LOGGER.setUseParentHandlers(false);
fileHandler = new FileHandler("log");
fileHandler.setFormatter(new MinimalFormatter());
consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(new MinimalFormatter());
LOGGER.addHandler(fileHandler);
LOGGER.addHandler(consoleHandler);
LOGGER.setLevel(Level.ALL);
} catch (Exception e) {
// TODO handle exception
e.printStackTrace();
}
frame = new Frame(WIDTH, HEIGHT);
DrawManager.getInstance().setFrame(frame);
int width = frame.getWidth();
int height = frame.getHeight();
gameSettings = new ArrayList<GameSettings>();
gameSettings.add(SETTINGS_LEVEL_1);
gameSettings.add(SETTINGS_LEVEL_2);
gameSettings.add(SETTINGS_LEVEL_3);
gameSettings.add(SETTINGS_LEVEL_4);
gameSettings.add(SETTINGS_LEVEL_5);
gameSettings.add(SETTINGS_LEVEL_6);
gameSettings.add(SETTINGS_LEVEL_7);
GameState gameState;
Sound.playMusic();
int returnCode = 1;
do {
gameState = new GameState(1, 0, MAX_LIVES, 0, 0);
switch (returnCode) {
case 1:
// Main menu.
frame.setVisible(false); // This makes the old window disappear
frame = new Frame(WIDTH, HEIGHT); // This creates a new window with new width & height values
DrawManager.getInstance().setFrame(frame);
width = frame.getWidth();
height = frame.getHeight();
currentScreen = new TitleScreen(width, height, FPS);
LOGGER.info("Starting " + WIDTH + "x" + HEIGHT
+ " title screen at " + FPS + " fps.");
returnCode = frame.setScreen(currentScreen);
LOGGER.info("Closing title screen.");
break;
case 2:
// Game & score.
do {
// One extra live every few levels.
boolean bonusLife = gameState.getLevel()
% EXTRA_LIFE_FRECUENCY == 0
&& gameState.getLivesRemaining() < MAX_LIVES;
currentScreen = new GameScreen(gameState,
gameSettings.get(gameState.getLevel() - 1),
bonusLife, width, height, FPS);
LOGGER.info("Starting " + WIDTH + "x" + HEIGHT
+ " game screen at " + FPS + " fps.");
frame.setScreen(currentScreen);
LOGGER.info("Closing game screen.");
gameState = ((GameScreen) currentScreen).getGameState();
gameState = new GameState(gameState.getLevel() + 1,
gameState.getScore(),
gameState.getLivesRemaining(),
gameState.getBulletsShot(),
gameState.getShipsDestroyed());
} while (gameState.getLivesRemaining() > 0
&& gameState.getLevel() <= NUM_LEVELS);
LOGGER.info("Starting " + WIDTH + "x" + HEIGHT
+ " score screen at " + FPS + " fps, with a score of "
+ gameState.getScore() + ", "
+ gameState.getLivesRemaining() + " lives remaining, "
+ gameState.getBulletsShot() + " bullets shot and "
+ gameState.getShipsDestroyed() + " ships destroyed.");
currentScreen = new ScoreScreen(width, height, FPS, gameState);
returnCode = frame.setScreen(currentScreen);
LOGGER.info("Closing score screen.");
break;
case 3:
// High scores.
currentScreen = new HighScoreScreen(width, height, FPS);
LOGGER.info("Starting " + WIDTH + "x" + HEIGHT
+ " high score screen at " + FPS + " fps.");
returnCode = frame.setScreen(currentScreen);
LOGGER.info("Closing high score screen.");
break;
case 4:
// Help.
currentScreen = new HelpScreen(width, height, FPS);
LOGGER.info("Starting " + WIDTH + "x" + HEIGHT
+ " help screen at " + FPS + " fps.");
returnCode = frame.setScreen(currentScreen);
LOGGER.info("Closing help screen.");
break;
case 5:
// Settings.
currentScreen = new SettingsScreen(width, height, FPS);
LOGGER.info("Starting " + WIDTH + "x" + HEIGHT
+ " settings screen at " + FPS + " fps.");
returnCode = frame.setScreen(currentScreen);
LOGGER.info("Closing settings screen.");
break;
default:
break;
}
} while (returnCode != 0);
fileHandler.flush();
fileHandler.close();
System.exit(0);
}
/**
* Constructor, not called.
*/
private Core() {
}
/**
* Controls access to the logger.
*
* @return Application logger.
*/
public static Logger getLogger() {
return LOGGER;
}
/**
* Controls access to the drawing manager.
*
* @return Application draw manager.
*/
public static DrawManager getDrawManager() {
return DrawManager.getInstance();
}
/**
* Controls access to the input manager.
*
* @return Application input manager.
*/
public static InputManager getInputManager() {
return InputManager.getInstance();
}
/**
* Controls access to the file manager.
*
* @return Application file manager.
*/
public static FileManager getFileManager() {
return FileManager.getInstance();
}
/**
* Controls creation of new cooldowns.
*
* @param milliseconds
* Duration of the cooldown.
* @return A new cooldown.
*/
public static Cooldown getCooldown(final int milliseconds) {
return new Cooldown(milliseconds);
}
/**
* Controls creation of new cooldowns with variance.
*
* @param milliseconds
* Duration of the cooldown.
* @param variance
* Variation in the cooldown duration.
* @return A new cooldown with variance.
*/
public static Cooldown getVariableCooldown(final int milliseconds,
final int variance) {
return new Cooldown(milliseconds, variance);
}
public static void setSize(int width, int height) {
WIDTH = width;
HEIGHT = height;
}
}