-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpriteDemo.java
290 lines (252 loc) · 9 KB
/
SpriteDemo.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
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import framework.*;
import sound.MusicManager;
import sound.SoundEffectManager;
import sprite.*;
import world.*;
public class SpriteDemo extends SimpleFramework {
// viewport matrix
private Matrix3x3f view;
// game sprites
private TorchSprite heart;
private BGSprite bg;
private PlayerSprite deku;
private MenuSprite startMenu;
private MenuSprite winMenu;
private MenuSprite loseMenu;
// gamestate
private boolean renderBounds;
private int musicSelect; // for selecting what music to play, 1 when the player is chased, 2 for title, 3 for game over, 4 for victory
private int lastPlayed;
private int screenType;
private static final int startScreen = 0;
private static final int gameScreen = 1;
private static final int winScreen = 2;
private static final int loseScreen = 3;
private boolean playerChased;
// sound
private SoundEffectManager soundEffectManager;
private MusicManager musicManager;
public SpriteDemo() {
appTitle = "Maze Game";
appBorderScale = 0.99f;
// app is a square
appWidth = appHeight = 1250;
appWorldWidth = appWorldHeight = 2.0f;
}
@Override
protected void initialize() {
super.initialize();
// essential initializations
renderBounds = false;
view = getViewportTransform();
// load spritesheets
heart = new TorchSprite(getClass().getResource("/res/img/torch_9x1.png"));
bg = new BGSprite(getClass().getResource("/res/img/background_1x1.png"));
deku = new PlayerSprite(getClass().getResource("/res/img/girlsprite_4x4.png"));
startMenu = new MenuSprite(getClass().getResource("/res/img/startscreen_1x1.png"));
loseMenu = new MenuSprite(getClass().getResource("/res/img/deathscreen_1x1.png"));
winMenu = new MenuSprite(getClass().getResource("/res/img/victoryscreen_1x1.png"));
// move deku up and to the right a bit
deku.setPos(new Vector2f(0.75f, -.72f));
heart.setPos(bg.getTorchPos());
// set the viewport for the sprites
bg.setViewsForBounds(view);
heart.setViewsForBounds(view);
deku.setViewsForBounds(view);
for (Sprite sprite : GameStates.enemies){
sprite.setViewsForBounds(view);
}
// music and sound
soundEffectManager = new SoundEffectManager();
musicManager = new MusicManager();
musicSelect = 2;
lastPlayed = -1;
musicManager.playMusic("start");
setResizable(false);
playerChased = false;
// Set up game state
screenType = startScreen;
createTimer();
}
private void createTimer(){
GameStates.timer = new Timer();
GameStates.timer.schedule(new TimerTask() {
public void run() {
if(--GameStates.gameTime == 0 && screenType != startScreen){
screenType = loseScreen;
GameStates.gameTime = 60;
}
}
}, 0, 1000);
}
private void renderTimer(Graphics g){
Font z = new Font("ZapfDingbats", Font.PLAIN, 30);
g.setColor(Color.red);
g.setFont(z);
g.drawString("Time Left: " + GameStates.gameTime, 80, 80);
}
@Override
protected void processInput(float delta) {
super.processInput(delta);
}
@Override
protected void updateObjects(float delta) {
super.updateObjects(delta);
playerChased = false;
if (screenType == startScreen) {
if (keyboard.keyDownOnce(KeyEvent.VK_SPACE)) {
screenType = gameScreen;
GameStates.gameTime = 60;
}
}
if (screenType == gameScreen) {
if (heart.wonGame) {
screenType = winScreen;
}
// toggle rendering of bounding shapes
if (keyboard.keyDownOnce(KeyEvent.VK_B)) {
renderBounds = !renderBounds;
}
heart.setPos(bg.getTorchPos());
bg.setViewsForBounds(getViewportTransform());
heart.setViewsForBounds(getViewportTransform());
deku.setViewsForBounds(getViewportTransform());
for (Sprite sprite : GameStates.enemies){
sprite.setViewsForBounds(getViewportTransform());
}
// update game sprites
heart.update(deku, bg);
deku.update(keyboard, bg);
for (Sprite sprite : GameStates.enemies){
if (sprite instanceof GhostSprite &&
((GhostSprite) sprite).isChasingPlayer()) {
playerChased = true;
}
if (sprite instanceof GhostSprite || sprite instanceof JelloSprite) {
if (sprite.isCollidingWith(deku)) {
screenType = loseScreen;
}
}
sprite.update(deku, bg);
}
// apply words to vectorobject bounds
heart.updateWorldsForBounds();
bg.updateWorldsForBounds();
deku.updateWorldsForBounds();
for (Sprite sprite : GameStates.enemies) {
sprite.updateWorldsForBounds();
}
if(playerChased)
musicSelect = 1;
else
musicSelect = 0;
playMusic();
}
if (screenType == winScreen) {
musicSelect = 4;
playMusic();
if (keyboard.keyDownOnce(KeyEvent.VK_SPACE)) {
GameStates.enemies.clear();
GameStates.timer.cancel();
musicManager.stopMusic();
initialize();
}
}
if (screenType == loseScreen) {
musicSelect = 3;
playMusic();
if (keyboard.keyDownOnce(KeyEvent.VK_SPACE)) {
GameStates.enemies.clear();
GameStates.timer.cancel();
musicManager.stopMusic();
initialize();
}
}
}
private void playMusic() {
if(musicSelect == 0 && lastPlayed != musicSelect) {
musicManager.stopMusic();
musicManager.playMusic("level");
lastPlayed = musicSelect;
}
if(musicSelect == 1 && lastPlayed != musicSelect) {
musicManager.stopMusic();
musicManager.playMusic("chase");
lastPlayed = musicSelect;
}
if(musicSelect == 2 && lastPlayed != musicSelect) {
musicManager.stopMusic();
musicManager.playMusic("start");
lastPlayed = musicSelect;
}
if(musicSelect == 3 && lastPlayed != musicSelect) {
musicManager.stopMusic();
musicManager.playMusic("gameover");
lastPlayed = musicSelect;
}
if(musicSelect == 4 && lastPlayed != musicSelect) {
musicManager.stopMusic();
musicManager.playMusic("victory");
lastPlayed = musicSelect;
}
}
@Override
protected void render(Graphics g) {
super.render(g);
// create a graphics2d object for
// drawing BufferedImage instances
Graphics2D g2d = (Graphics2D) g;
if (screenType == startScreen) {
startMenu.render(g2d, getViewportTransform());
}
if (screenType == gameScreen) {
// call render function for each sprite
bg.render(g2d, getViewportTransform());
heart.render(g2d, getViewportTransform());
deku.render(g2d, getViewportTransform());
for (Sprite sprite : GameStates.enemies){
sprite.render(g2d, getViewportTransform());
}
renderTimer(g);
// render the bounding shapes only
// if bounds rendering is enabled
if (renderBounds) {
bg.renderBoundingShapes(g);
heart.renderBoundingShapes(g);
deku.renderBoundingShapes(g);
for (Sprite sprite : GameStates.enemies){
if (!sprite.bounds.isEmpty()) {
sprite.renderBoundingShapes(g);
}
}
}
if(bg.isNewLevel()){
bg.renderBoundingShapes(g);
deku.renderBoundingShapes(g);
for (Sprite sprite : GameStates.enemies){
if (!sprite.bounds.isEmpty()) {
sprite.renderBoundingShapes(g);
}
}
heart.renderBoundingShapes(g);
}
}
if (screenType == winScreen) {
winMenu.render(g2d, getViewportTransform());
}
if (screenType == loseScreen) {
loseMenu.render(g2d, getViewportTransform());
}
}
@Override
protected void terminate() {
super.terminate();
}
public static void main(String[] args) {
launchApp(new SpriteDemo());
}
}