-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.java
324 lines (287 loc) · 11.5 KB
/
Game.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
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Game extends Canvas implements Runnable{
static final int WIDTH = 800, HEIGHT = 500;
private Thread thread;
private boolean running = false;
private Handler handler;
private Window window;
private Menu menu;
//Spawns objects during the scrolling phase
private Spawner obstacleSpawner, powerupSpawner, decorSpawner;
private GameObjectSpawner gameObjectSpawner;
private Player player;
//Loads Images
private BufferedImage bg, powerups, obstacles, decor, charSprites, managerSprites, mopBucket, counterLeft, counterRight, menuImage, gameOverImage, lobbyBg, creditsImage, infoImage,
exitMat, startMat, soupImage, washroomMat, soupTurn, soupBg, mopTiles, infoPage, soupIntro,
mopIntro, creditsPage, brownBG;
private BufferedImageLoader loader;
//Instances for the minigames
private Minigame1 mg1;
private Minigame2 mg2;
//Used to animate the risk bar change
private int changeRisk = 0;
//Controls flow of our game
public static State gameState = State.Menu;
//Increases over time to speed up object movements
public static int objectSpeed = 4;
//False for instructions page, true for gameplay (for the minigames)
public static boolean startOne = false, startTwo = false;
//Constructor, initializes all the objects and assets
public Game() {
loader = new BufferedImageLoader();
bg = loader.loadImage("/background.png");
powerups = loader.loadImage("/powerup_sprite_sheet.png");
obstacles = loader.loadImage("/obstacle_sprite_sheet.png");
decor = loader.loadImage("/wall_decor_sprite_sheet.png");
charSprites = loader.loadImage("/character_sprite_sheet.png");
managerSprites = loader.loadImage("/manager_sprite_sheet.png");
mopBucket = loader.loadImage("/mop_bucket.png");
counterLeft = loader.loadImage("/counter_left.png");
counterRight = loader.loadImage("/counter_right.png");
menuImage = loader.loadImage("/menu.png");
gameOverImage = loader.loadImage("/game_over.png");
lobbyBg = loader.loadImage("/lobby_background.png");
creditsImage = loader.loadImage("/credits.png");
infoImage = loader.loadImage("/info.png");
soupImage = loader.loadImage("/soup.png");
exitMat = loader.loadImage("/exit_mat.png");
startMat = loader.loadImage("/start_mat.png");
washroomMat = loader.loadImage("/washroom_mat.png");
soupTurn = loader.loadImage("/soup_turning.png");
soupBg = loader.loadImage("/soup_bg.png");
mopTiles = loader.loadImage("/mop_tiles.png");
infoPage = loader.loadImage("/info_page.png");
soupIntro = loader.loadImage("/soup_intro.png");
mopIntro = loader.loadImage("/mop_intro.png");
creditsPage = loader.loadImage("/credits_page.png");
brownBG = loader.loadImage("/brown_bg.png");
mg1 = new Minigame1(soupTurn);
mg2 = new Minigame2(mopTiles);
try {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font wonder = Font.createFont(Font.TRUETYPE_FONT, getClass().getClassLoader().getResourceAsStream("wonder.ttf"));
ge.registerFont(wonder);
} catch (IOException|FontFormatException e) {
e.printStackTrace();
}
handler = new Handler();
menu = new Menu(this, handler);
this.addKeyListener(new KeyInput(handler, mg1, mg2));
this.addMouseListener(menu);
window = new Window(WIDTH, HEIGHT, "AT THE COUNTER", this);
}
public void close() {
window.frame.dispatchEvent(new WindowEvent(window.frame, WindowEvent.WINDOW_CLOSING));
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
}
catch(Exception e) {
e.printStackTrace();
}
}
//Game engine, all of the processing is called from inside this method
public void run() {
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0; //ticks per second
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >=1){ //Calls tick() based on elapsed time, this accounts for lag
tick();
delta--;
}
if (running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
System.out.println("FPS: "+ frames);
frames = 0;
}
}
stop();
}
//Performs processing for each tick
private void tick() {
if (gameState == State.Menu) {
menu.tick();
}
else if (gameState == State.Tutorial) {
handler.tick();
}
else if (gameState == State.Lobby) {
handler.tick();
if(changeRisk == 0){ //After the animation, allow the player to move again
player.setCanMove(true);
}
else{ //Animate risk bar change after minigame
player.setCanMove(false);
player.incrementRisk(1 * (changeRisk > 0 ? 1 : -1));
changeRisk += (changeRisk > 0 ? -1 : 1);
}
}
else if (gameState == State.Minigame1) { // Soup
if(startOne == true){
mg1.tick();
if(Math.floor(mg1.getTime()) <= 0){ //When the minigame is over, reload the lobby, and prepare to show risk bar animation
gameState = State.Lobby;
loadLobby();
changeRisk = clamp((mg1.getScore()-1400)/7, -200, 200);
mg1.reset();
startOne = false;
player.setCanMove(false);
}
}
}
else if (gameState == State.Minigame2) { // Mop
if(startTwo == true){
mg2.tick();
if(Math.floor(mg2.getTime()) <= 0){
gameState = State.Lobby;
loadLobby();
changeRisk = clamp((mg2.getScore()-1400)/7, -200, 200);
mg2.reset();
startTwo = false;
player.setCanMove(false);
}
}
}
else if (gameState == State.Game) {
handler.tick();
gameObjectSpawner.tick();
decorSpawner.tick();
}
else if (gameState == State.GameOver) {
menu.setScore(player.getScore());
menu.tick();
}
}
//Renders images
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
if (gameState == State.Menu) {
g.drawImage(menuImage, 0, 0, null);
menu.render(g);
}
else if (gameState == State.Tutorial) {
g.drawImage(bg,0,0,null);
handler.render(g);
}
else if (gameState == State.Lobby) {
g.drawImage(lobbyBg,0,0,null);
handler.render(g);
}
else if (gameState == State.Minigame1) {
if(startOne == true){ //Main gameplay
g.drawImage(soupBg, 0, 0, null);
mg1.render(g);
}
else{ //Instructions page
g.drawImage(soupIntro, 0, 0, null);
}
}
else if (gameState == State.Minigame2) {
if(startTwo == true){
g.drawImage(brownBG, 0, 0, null);
mg2.render(g);
}
else{
g.drawImage(mopIntro, 0, 0, null);
}
}
else if (gameState == State.Info) {
g.drawImage(infoPage, 0, 0, null);
}
else if (gameState == State.Credits) {
g.drawImage(creditsPage, 0, 0, null);
}
else if (gameState == State.Game) {
g.drawImage(bg,0,0,null);
handler.render(g);
}
else if (gameState == State.GameOver) {
g.drawImage(gameOverImage,0,0,null);
menu.render(g);
}
g.dispose();
bs.show();
}
public void startLevel() {
player = new Player(400,350,64,12,-40,-104, charSprites, managerSprites, new HUD(), ID.Player, handler, this);
obstacleSpawner = new Spawner(2, 80, 4, 200, 5, 60, 60, new ObjectID[] {ObjectID.Crowd, ObjectID.Garbage},
obstacles, handler);
powerupSpawner = new Spawner(3, 1800, 1, 200, 5, 60, 60, new ObjectID[] {ObjectID.Mask, ObjectID.Sanitizer, ObjectID.Gloves},
powerups, handler);
gameObjectSpawner = new GameObjectSpawner(new Spawner[] {obstacleSpawner, powerupSpawner}, 200, 5, 60, 60, handler);
decorSpawner = new Spawner(5, 240, 1, 0, 1, 200, 200, new ObjectID[] {ObjectID.Decor, ObjectID.Decor, ObjectID.Decor, ObjectID.Decor, ObjectID.Decor},
decor, handler);
}
public void loadLobby() {
new Object(140, 235, 50, 27, 0, 0,
soupImage,
ObjectID.Mini1, ID.CollidableObject, handler);
new Object(400, 390, 82, 94, 0, 0,
mopBucket,
ObjectID.Mini2, ID.CollidableObject, handler);
new Object(0, 200, 46, 90, 0, 0,
counterLeft,
ObjectID.Misc, ID.CollidableObject, handler);
new Object(46, 234, 244, 56, -46, -34,
counterRight,
ObjectID.Misc, ID.CollidableObject, handler);
new Object(300, 106, 123, 94, 0, -64,
infoImage,
ObjectID.Info, ID.CollidableObject, handler);
new Object(472, 83, 73, 117, 0, -52,
creditsImage,
ObjectID.Credits, ID.CollidableObject, handler);
new Object(6, 320, 44, 109, 0, 0,
startMat,
ObjectID.StartMat, ID.NonCollidableObject, handler);
new Object(753, 218, 41, 113, 0, 0,
exitMat,
ObjectID.ExitMat, ID.NonCollidableObject, handler);
new Object(753, 360, 41, 113, 0, 0,
washroomMat,
ObjectID.WashroomMat, ID.NonCollidableObject, handler);
}
//Helper method that squishes values into a specified range
public static int clamp(int val, int min, int max) {
return Math.min(max, Math.max(val, min));
}
public static double clamp(double val, double min, double max) {
return Math.min(max, Math.max(val, min));
}
public static void main(String[] args) {
new Game();
}
}