-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePanel.java
More file actions
303 lines (235 loc) · 7.34 KB
/
Copy pathGamePanel.java
File metadata and controls
303 lines (235 loc) · 7.34 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
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
import java.awt.*;
import java.io.IOException;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GamePanel extends JPanel implements Runnable{
// Screen variables
private final int originalSize = 18;
private final int scale = 3;
private final int maxScreenCol = 20;
private final int maxScreenRow = 14;
private final int tileSize = originalSize * scale;
private final int screenWidth = 1000; //maxScreenCol * tileSize;
private final int screenHeight = 700; //maxScreenRow * tileSize;
private final KeyHandler key = new KeyHandler(this);
Thread gameThread;
// World variables
private final int worldCol = 74;
private final int worldRow = 55;
private final int worldWidth = worldCol * tileSize;
private final int worldHeight = worldRow * tileSize;
//private final int maxMap = 500;
// Sound variables
private SoundManager sm;
// Image variables
private ImageManager2 im = new ImageManager2();
// Player variables
public Hunt player;
public AssertObjects ao = new AssertObjects(this);
public CollisionChecker cc = new CollisionChecker(this);
public TileMapManagerHelp tmm = new TileMapManagerHelp(this);
public Objects obj[] = new Objects[10];
public Entities hostile[] = new Entities[15];
public Entities neutral[] = new Entities[15];
// System variables
public UI ui = new UI(this);
public int gameState;
private final int menuState = 0;
private final int playState = 1;
private final int pauseState = 2;
public final int gameOverState = 3;
public final int dialoueState = 4;
//FPS
private int FPS = 60;
public GamePanel( GameWindow window){
this.setPreferredSize(new Dimension(getScreenWidth(), getScreenHeight()));
System.out.println("Screen Width: " + screenWidth + " Screen Height: " + screenHeight);
this.setDoubleBuffered(true);
this.addKeyListener(key);
this.setFocusable(true);
System.out.println("Get Screen Width: " + getScreenWidth() + " Get Screen Height: " + getScreenHeight());
System.out.println("Get World Width: " + getWorldWidth() + " Get World Height: " + getWorldHeight());
System.out.println("Get World Col: " + getWorldCol() + " Get World Row: " + getWorldRow());
sm = SoundManager.getInstance();
player = new Hunt(this, key, window);
}
public void startGameThread(){
gameThread = new Thread(this);
gameThread.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
requestFocusInWindow();
}
});
}
public void endGameThread(){
sm.stopClip("level1_loop");
gameThread = new Thread(this);
gameThread = null;
return;
}
public void run(){
double drawInterval = 1000000000 / FPS;
double delta = 0;
long lastTime = System.nanoTime();
long currentTime;
long timer = 0;
int frames = 0;
while(gameThread != null){
currentTime = System.nanoTime();
delta += (currentTime - lastTime) / drawInterval;
timer += (currentTime - lastTime);
lastTime = currentTime;
if(delta >= 1){
update();
repaint();
delta--;
frames++;
}
if(timer >= 1000000000){
//System.out.println("FPS: " + frames);
frames = 0;
timer = 0;
}
}
}
public void gameSetup(){
ao.setObjects();
ao.setHostile();
ao.setNeutral();
sm.setVolume("level1_loop", 0.2f);
sm.playClip("level1_loop", true);
gameState = 0;
}
public void update(){
if(gameState == playState){
player.update();
for(int i = 0; i < hostile.length; i++){
if(hostile[i] != null){
hostile[i].update();
//cc.checkEntity(hostile[i], hostile);
//cc.checkEntity(hostile[i], neutral);
//cc.checkTile(hostile[i]);
}
}
for(int i = 0; i < neutral.length; i++){
if(neutral[i] != null){
neutral[i].update();
//cc.checkEntity(neutral[i], hostile);
//cc.checkEntity(neutral[i], neutral);
//cc.checkTile(neutral[i]);
}
}
}else
if(gameState == pauseState){
// Pause
}else
if(gameState == gameOverState){
// Game Over
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
long start = 0;
if(key.debugger == true){
start = System.nanoTime();
}
if(gameState == menuState){
ui.draw(g2);
}
else{
tmm.draw(g2);
for(int i = 0; i < obj.length; i++){
if(obj[i] != null){
obj[i].draw(g2, this);
}
}
for(int i = 0; i < hostile.length; i++){
if(hostile[i] != null){
hostile[i].draw(g2);
}
}
for(int i = 0; i < neutral.length; i++){
if(neutral[i] != null){
neutral[i].draw(g2);
}
}
player.draw(g2);
ui.draw(g2);
}
if(key.debugger == true){
long elapsed = System.nanoTime() - start;
g2.setColor(Color.WHITE);
g2.drawString("FPS: " + (1000000000 / elapsed), 10, 10);
System.out.println("FPS: " + (1000000000 / elapsed));
}
g2.dispose();
}
public int getTileSize(){
return tileSize;
}
public int getScreenWidth(){
return screenWidth;
}
public int getScreenHeight(){
return screenHeight;
}
public int getWorldWidth(){
return worldWidth;
}
public int getWorldHeight(){
return worldHeight;
}
public int getWorldCol(){
return worldCol;
}
public int getWorldRow(){
return worldRow;
}
public int getScreenCol(){
return maxScreenCol;
}
public int getScreenRow(){
return maxScreenRow;
}
public KeyHandler getKeyHandler(){
return key;
}
public Hunt getPlayer(){
return player;
}
public int getOriginalSize(){
return originalSize;
}
public SoundManager getSoundManager(){
return sm;
}
public ImageManager2 getImageManager(){
return im;
}
public int getGameState(){
return gameState;
}
public int getMenuState(){
return menuState;
}
public int getPlayState(){
return playState;
}
public int getPauseState(){
return pauseState;
}
public void togglePauseState(){
if(gameState == pauseState){
gameState = playState;
}else{
gameState = pauseState;
}
return;
}
public void die(){
gameState = gameOverState;
sm.stopClip("level1_loop");
}
}