-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
172 lines (129 loc) · 3.64 KB
/
Game.java
File metadata and controls
172 lines (129 loc) · 3.64 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
// Game.java
// 11/27/17
// Jake Maro
//
// This file contains all game info.
// a thread is a miniprogram
//
//
// game loop updates variables, positions, then renders (draws everything), then repeats
package mainPackage;
import java.awt.*;
import java.awt.image.*;
public class Game implements Runnable { // allows to be ran in a thread
private Display display;
private int width, height;
public String title;
private boolean running = false;
private Thread thread;
private BufferStrategy bs; // way for computer to draw to screen with hidden screen (buffer)
private Graphics g;
//states
private State gameState;
private State menuState;
private State settingsState;
//input
private KeyManager keyManager;
//camera
private GameCamera gameCamera;
//handler
private Handler handler;
public Game(String title, int width, int height) {
this.width = width;
this.height = height;
this.title = title;
keyManager = new KeyManager();
}
private void tick() { // updates things
keyManager.tick();
if(State.getState() != null) {
State.getState().tick();
}
}
private void render() { //draw things to the screen
bs = display.getCanvas().getBufferStrategy(); // sets bs to current bs of game
if(bs == null) {
display.getCanvas().createBufferStrategy(3); // if it doesnt already have a buffer
return;
}
g = bs.getDrawGraphics();
// clear screen
g.clearRect(0, 0, width, height);
// draw here
if(State.getState() != null) {
State.getState().render(g);
}
// end drawing
bs.show(); // okay im done now
g.dispose(); // toss the canvas
}
private void init() { //initializes graphics of game, called once
display = new Display(title, width, height);
display.getFrame().addKeyListener(keyManager);
Assets.init();
handler = new Handler(this);
gameCamera = new GameCamera(handler, 0, 0);
//initialize all states
gameState = new GameState(handler);
menuState = new MenuState(handler);
settingsState = new SettingsState(handler);
State.setState(gameState);
}
public void run() { // needed for implementing Runnable, will contain most of code
init();
int fps = 60; //frames (ticks) per second
double timePerTick = 1000000000 / fps; //1 billion nanoseconds in one second
double delta = 0;
long now;
long lastTime = System.nanoTime(); //returns amount of current time in nanoseconds
long timer = 0;
int ticks = 0;
while(running) {
now = System.nanoTime();
delta += (now - lastTime) / timePerTick; // tells computer when to call tick&render
timer += now - lastTime; // adds amt of time since block was last called
lastTime = now;
if(delta >= 1) {
tick();
render();
ticks++;
delta--;
}
if(timer >= 1000000000) {
System.out.println("Ticks and Frames: " + ticks);
ticks = 0;
timer = 0;
}
}
stop();
}
public KeyManager getKeyManager() {
return keyManager;
}
public GameCamera getGameCamera() {
return gameCamera;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public synchronized void start() { //synchronized used when working with threads directly
if(running) // just incase
return;
running = true;
thread = new Thread(this);
thread.start(); //calls run method
}
public synchronized void stop() {
if(!running) //just incase
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}