99import java .util .concurrent .CopyOnWriteArrayList ;
1010import java .util .concurrent .ExecutorService ;
1111import 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 ;
1317import networking .Client ;
1418import networking .Packet ;
1519import 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}
0 commit comments