Skip to content

Commit 071701a

Browse files
committed
Pausing working
1 parent f99272f commit 071701a

6 files changed

Lines changed: 212 additions & 31 deletions

File tree

rectangles/engine/Physics.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Physics(float x, float y, float objWidth, float objHeight, float mass, fl
3939
this.mass = mass;
4040
}
4141

42-
public PVector update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
42+
public void update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
4343
//this.acceleration.setMag(0.2);
4444

4545
for (GameObj obj : objects) {
@@ -79,6 +79,14 @@ public PVector update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
7979
}
8080
}
8181

82+
/**
83+
* TODO: Implement here so that if collision occurs, no movement below, let collision event handle
84+
* raising a new movement event
85+
*
86+
* else, just raise movement event here if object moved
87+
*
88+
* In replays, just worry about movement events! :D
89+
*/
8290
this.velocity.add(this.acceleration);
8391
this.velocity.limit(this.topSpeed);
8492
this.location.add(this.velocity);
@@ -87,7 +95,6 @@ public PVector update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
8795
if (this.isGrav) {
8896
this.acceleration = new PVector(0, GRAV);
8997
}
90-
return this.location;
9198
}
9299

93100
// From processing tutorial forces with vectors

rectangles/engine/Rectangles.java

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import java.util.concurrent.CopyOnWriteArrayList;
1010
import java.util.concurrent.ExecutorService;
1111
import java.util.concurrent.Executors;
12+
import java.util.concurrent.TimeUnit;
1213

14+
import engine.time.GlobalTimeline;
15+
import engine.time.LocalTimeline;
16+
import engine.time.Timeline;
1317
import networking.Client;
1418
import networking.Packet;
1519
import networking.Server;
@@ -28,6 +32,10 @@ public class Rectangles extends PApplet {
2832
public static Spawn[] spawnPoints = new Spawn[2];
2933
public static Random generator = new Random();
3034
public static int deathPoints = 0;
35+
public static Timeline globalTimeline = new GlobalTimeline(1000/60);
36+
public static Timeline physicsTimeline = new LocalTimeline(globalTimeline, 1);
37+
public static Timeline networkTimeline = new LocalTimeline(globalTimeline, 1);
38+
public static Timeline renderTimeline = new LocalTimeline(globalTimeline, 1);
3139

3240

3341
public static Player player;
@@ -41,21 +49,76 @@ public class Rectangles extends PApplet {
4149
private GameObj rightWall;
4250

4351
private ExecutorService threadPool = Executors.newFixedThreadPool(NUM_THREADS);
52+
private boolean setup = false;
4453

4554

4655
public Rectangles(boolean isServer) {
4756
this.isServer = isServer;
4857
System.out.println("Server: " + this.isServer);
58+
59+
// Start timelines
60+
globalTimeline.start();
61+
physicsTimeline.start();
62+
networkTimeline.start();
63+
renderTimeline.start();
64+
}
65+
66+
/**
67+
* Just runs the game loop infinitely
68+
*/
69+
public void runLoop() {
70+
while(true) {
71+
this.gameLoop(globalTimeline.getAndResetDelta());
72+
}
4973
}
5074

75+
/**
76+
* Single iteration of the game loop
77+
* @param delta local time since last iteration
78+
*/
79+
private void gameLoop(long delta) {
80+
if (delta > 0) {
81+
this.updatePhysics(physicsTimeline.getAndResetDelta());
82+
this.updateNetwork(networkTimeline.getAndResetDelta());
83+
this.updateRender(renderTimeline.getAndResetDelta());
84+
}
85+
}
86+
87+
/* Update methods with deltas */
88+
// TODO: Determine if this is event the best way to do it...
89+
90+
private void updateRender(long delta) {
91+
if (delta > 0) {
92+
this.redraw();
93+
}
94+
}
95+
96+
private void updateNetwork(long delta) {
97+
if (delta > 0 && this.isServer) {
98+
this.server.updateClients();
99+
}
100+
}
101+
102+
private void updatePhysics(long delta) {
103+
// Dummy Renderer?
104+
if (delta > 0 && this.isServer) {
105+
// Update physics
106+
for (GameObj obj : movObjects) {
107+
obj.getPy().update(obj, objects);
108+
}
109+
}
110+
}
111+
51112
public void settings() {
52113
size(640, 360);
53114
}
54115

55116
public void setup() {
56117
background(0);
57-
frameRate(60);
118+
// Just set to be unreasonably high
119+
frameRate(1000);
58120
textSize(32);
121+
noLoop();
59122

60123

61124
// Setup Server
@@ -169,33 +232,17 @@ public void setup() {
169232
}
170233
new Thread(this.localClient).start();
171234
}
172-
235+
236+
this.setup = true;
173237
}
174238

175239
public void draw() {
176240
background(0);
177241
if (this.isServer) {
178242
text("Deaths: " + deathPoints , 110, 40);
179243
}
180-
181-
this.renderAll(objects);
182-
183-
184-
// Dummy Renderer?
185-
if (isServer) {
186-
// Update physics
187-
for (GameObj obj : movObjects) {
188-
obj.getPy().update(obj, objects);
189-
}
190-
}
191-
192-
// Only run update to clients 30fps?
193-
if (frameCount % 1 == 0) {
194-
if (this.isServer) {
195-
this.server.updateClients();
196-
}
197-
}
198244

245+
this.renderAll(objects);
199246
}
200247

201248
private void renderAll(CopyOnWriteArrayList<GameObj> objects) {
@@ -246,8 +293,22 @@ public void keyPressed() {
246293
this.localClient.write(p);
247294
}
248295
}
296+
297+
if (key == 'p') {
298+
globalTimeline.pause();
299+
System.out.println("Paused");
300+
}
301+
302+
if (key == 'u') {
303+
globalTimeline.unpause();
304+
System.out.println("Un-Paused");
305+
}
249306
}
250-
307+
308+
public boolean isSetup() {
309+
return this.setup;
310+
}
311+
251312
public static void setPlayer(Player p) {
252313
player = p;
253314
objectMap.put(player.getUUID(), player);
@@ -266,6 +327,10 @@ public static void main(String[] args) {
266327
sketch = new Rectangles(false);
267328
}
268329
PApplet.runSketch(processingArgs, sketch);
330+
while (!sketch.isSetup()) {
331+
System.out.println("Waiting...");
332+
}
333+
sketch.runLoop();
269334
}
270335

271336
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package engine.events;
2+
3+
public class Event {
4+
5+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package engine.time;
2+
3+
public class GlobalTimeline implements Timeline {
4+
5+
private int tickSize;
6+
private long startTime;
7+
8+
9+
/* Is it paused? */
10+
private boolean isPaused = false;
11+
12+
/* Accumulates paused time */
13+
private long pausedTotalTime = 0;
14+
15+
/* Anchor for when timeline was paused */
16+
private long pausedTime = 0;
17+
18+
/* Last time delta was calculated */
19+
private long lastTime = 0;
20+
21+
/* Lock for pausing */
22+
private Object lock = new Object();
23+
24+
25+
public GlobalTimeline(int tickSize) {
26+
this.tickSize = tickSize;
27+
}
28+
29+
@Override
30+
public void start() {
31+
this.startTime = System.currentTimeMillis();
32+
}
33+
34+
@Override
35+
public void pause() {
36+
synchronized (this.lock) {
37+
if (!this.isPaused) {
38+
this.pausedTime = System.currentTimeMillis();
39+
this.isPaused = true;
40+
}
41+
}
42+
}
43+
44+
@Override
45+
public void unpause() {
46+
synchronized (this.lock) {
47+
this.pausedTotalTime += (System.currentTimeMillis() - this.pausedTime);
48+
this.pausedTime = 0;
49+
this.isPaused = false;
50+
}
51+
}
52+
53+
@Override
54+
public long getCurrentTime() {
55+
if (this.isPaused ) {
56+
return this.pausedTime;
57+
} else {
58+
long elapsedTime = (System.currentTimeMillis() - this.startTime) - this.pausedTotalTime;
59+
return elapsedTime / this.tickSize;
60+
}
61+
}
62+
63+
@Override
64+
public int getTickSize() {
65+
return this.tickSize;
66+
}
67+
68+
@Override
69+
public void setTickSize(int tickSize) {
70+
this.tickSize = tickSize;
71+
}
72+
73+
@Override
74+
public long getAndResetDelta() {
75+
long currentTime = this.getCurrentTime();
76+
long delta = currentTime - this.lastTime;
77+
this.lastTime = currentTime;
78+
return delta;
79+
}
80+
}

rectangles/engine/time/LocalTimeline.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package engine.time;
22

3+
import java.util.concurrent.atomic.AtomicBoolean;
4+
35
public class LocalTimeline implements Timeline {
46

57
private int tickSize;
@@ -11,11 +13,17 @@ public class LocalTimeline implements Timeline {
1113
private boolean isPaused = false;
1214

1315
/* Accumulates paused time */
14-
private long pauseTotalTime = 0;
16+
private long pausedTotalTime = 0;
1517

1618
/* Anchor for when timeline was paused */
1719
private long pausedTime = 0;
1820

21+
/* Last time delta was calculated */
22+
private long lastTime = 0;
23+
24+
/* Lock for pausing */
25+
private Object lock = new Object();
26+
1927

2028
public LocalTimeline(Timeline anchor, int tickSize) {
2129
this.anchor = anchor;
@@ -29,26 +37,31 @@ public void start() {
2937

3038
@Override
3139
public void pause() {
32-
synchronized (this) {
40+
synchronized (this.lock) {
3341
if (!this.isPaused) {
3442
this.pausedTime = anchor.getCurrentTime();
3543
this.isPaused = true;
3644
}
3745
}
38-
3946
}
4047

4148
@Override
4249
public void unpause() {
43-
this.pausedTime += (anchor.getCurrentTime() - this.pausedTime);
44-
this.pausedTime = 0;
45-
this.isPaused = true;
50+
synchronized (this.lock) {
51+
this.pausedTotalTime += (anchor.getCurrentTime() - this.pausedTime);
52+
this.pausedTime = 0;
53+
this.isPaused = false;
54+
}
4655
}
4756

4857
@Override
4958
public long getCurrentTime() {
50-
long elapsedTime = (this.anchor.getCurrentTime() - this.startTime) - this.pauseTotalTime;
51-
return elapsedTime / this.tickSize;
59+
if (this.isPaused) {
60+
return this.pausedTime;
61+
} else {
62+
long elapsedTime = (this.anchor.getCurrentTime() - this.startTime) - this.pausedTotalTime;
63+
return elapsedTime / this.tickSize;
64+
}
5265
}
5366

5467
@Override
@@ -61,4 +74,12 @@ public void setTickSize(int tickSize) {
6174
this.tickSize = tickSize;
6275
}
6376

77+
@Override
78+
public long getAndResetDelta() {
79+
long currentTime = this.getCurrentTime();
80+
long delta = currentTime - this.lastTime;
81+
this.lastTime = currentTime;
82+
return delta;
83+
}
84+
6485
}

rectangles/engine/time/Timeline.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public interface Timeline {
1313

1414
/* Get Current time */
1515
public long getCurrentTime();
16+
17+
/* Get delta time since last time serviced */
18+
public long getAndResetDelta();
1619

1720
/* Get tick size */
1821
public int getTickSize();

0 commit comments

Comments
 (0)