Skip to content

Commit 875e9ac

Browse files
committed
Broken on boundaries
1 parent 31e5612 commit 875e9ac

13 files changed

Lines changed: 153 additions & 54 deletions

rectangles/engine/EngineObject.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,21 @@
66
public abstract class EngineObject extends PApplet {
77

88
public abstract void handleEvent(Event e);
9+
10+
public EventRead getReader (Event e) {
11+
return new EventRead(e);
12+
}
13+
14+
private class EventRead implements Runnable {
15+
private Event e;
16+
17+
public EventRead(Event e) {
18+
this.e = e;
19+
}
20+
21+
@Override
22+
public void run() {
23+
handleEvent(e);
24+
}
25+
}
926
}

rectangles/engine/Physics.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ public void update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
6767
HashMap<String, Object> data = new HashMap<>();
6868
data.put("caller", caller.getUUID());
6969
data.put("collidedWith", collidedWith.getUUID());
70-
Event e = new Event(Event.EVENT_COLLISON, Rectangles.globalTimeline.getCurrentTime(), data);
70+
Event e = new Event(Event.EVENT_COLLISION, Rectangles.globalTimeline.getCurrentTime(), data);
7171
Rectangles.eventManager.raiseEvent(e);
72-
} else {
72+
}
73+
7374
this.velocity.add(this.acceleration);
7475
this.velocity.limit(this.topSpeed);
7576
if (this.velocity.mag() > 0) {
@@ -81,7 +82,9 @@ public void update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
8182
data.put("y", newLoc.y);
8283
Event e = new Event(Event.EVENT_MOVEMENT, Rectangles.globalTimeline.getCurrentTime(), data);
8384
Rectangles.eventManager.raiseEvent(e);
84-
}
85+
// Actually move
86+
//this.setLocation(newLoc);
87+
8588
//this.location.add(this.velocity);
8689
}
8790

rectangles/engine/Platform.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public static Platform deSerial(PApplet inst, String serial) {
9494

9595
@Override
9696
public void handleEvent(Event e) {
97-
switch(e.getType()) {
98-
case (Event.EVENT_COLLISON):
97+
switch (e.getType()) {
98+
case (Event.EVENT_COLLISION):
9999
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
100100
GameObj collidedWith = Rectangles.objectMap.get((UUID) e.getData().get("collidedWith"));
101101
if (collidedWith.getType().equals("boundary")) {
@@ -109,6 +109,8 @@ public void handleEvent(Event e) {
109109
data.put("y", newLoc.y);
110110
Event mov = new Event(Event.EVENT_MOVEMENT, Rectangles.globalTimeline.getCurrentTime(), data);
111111
Rectangles.eventManager.raiseEvent(mov);
112+
// Actually move
113+
//this.getPy().setLocation(newLoc);
112114
}
113115
}
114116
}
@@ -119,6 +121,15 @@ public void handleEvent(Event e) {
119121
this.getPy().setLocation(newLoc);
120122
}
121123
break;
124+
case (Event.EVENT_PHYSICS):
125+
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
126+
this.getPy().update(this, Rectangles.objects);
127+
HashMap<String, Object> data = new HashMap<>();
128+
data.put("caller", this.getUUID());
129+
Event py = new Event(Event.EVENT_PHYSICS, e.getTime() + Rectangles.physicsTimeline.getTickSize(), data);
130+
Rectangles.eventManager.raiseEvent(py);
131+
}
132+
break;
122133
default:
123134
break;
124135
}

rectangles/engine/Player.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static Player deSerial(PApplet inst, String serial) {
8686
public void handleEvent(Event e) {
8787

8888
switch(e.getType()) {
89-
case(Event.EVENT_COLLISON):
89+
case(Event.EVENT_COLLISION):
9090
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
9191
GameObj collidedWith = Rectangles.objectMap.get((UUID) e.getData().get("collidedWith"));
9292
if (collidedWith.isFloor()) {
@@ -124,16 +124,12 @@ public void handleEvent(Event e) {
124124
data.put("x", newLoc.x);
125125
data.put("y", newLoc.y);
126126
Event mov = new Event(Event.EVENT_MOVEMENT, e.getTime(), data);
127-
Rectangles.eventManager.raiseEvent(mov);
127+
//Rectangles.eventManager.raiseEvent(mov);
128+
// Actually move
129+
this.getPy().setLocation(newLoc);
128130
}
129131
}
130132
break;
131-
case(Event.EVENT_MOVEMENT):
132-
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
133-
PVector newLoc = new PVector((float) (e.getData().get("x")), (float) e.getData().get("y"));
134-
this.getPy().setLocation(newLoc);
135-
}
136-
break;
137133
case(Event.EVENT_DEATH):
138134
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
139135
Rectangles.deathPoints++;
@@ -142,6 +138,7 @@ public void handleEvent(Event e) {
142138
Event spawn = new Event(Event.EVENT_SPAWN, e.getTime(), data);
143139
Rectangles.eventManager.raiseEvent(spawn);
144140
}
141+
break;
145142
case(Event.EVENT_SPAWN):
146143
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
147144
Spawn s = Rectangles.spawnPoints[Rectangles.generator.nextInt(2)];
@@ -168,6 +165,21 @@ public void handleEvent(Event e) {
168165
}
169166
}
170167
break;
168+
case(Event.EVENT_MOVEMENT):
169+
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
170+
PVector newLoc = new PVector((float) (e.getData().get("x")), (float) e.getData().get("y"));
171+
this.getPy().setLocation(newLoc);
172+
}
173+
break;
174+
case(Event.EVENT_PHYSICS):
175+
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
176+
this.getPy().update(this, Rectangles.objects);
177+
HashMap<String, Object> data = new HashMap<>();
178+
data.put("caller", this.getUUID());
179+
Event py = new Event(Event.EVENT_PHYSICS, e.getTime() + Rectangles.physicsTimeline.getTickSize(), data);
180+
Rectangles.eventManager.raiseEvent(py);
181+
}
182+
break;
171183
default:
172184
break;
173185
}

rectangles/engine/Rectangles.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ public class Rectangles extends PApplet {
3535
public static Spawn[] spawnPoints = new Spawn[2];
3636
public static Random generator = new Random();
3737
public static int deathPoints = 0;
38-
public static Timeline globalTimeline = new GlobalTimeline(1000/60);
38+
public static Timeline globalTimeline = new GlobalTimeline(1000/144);
3939
public static Timeline physicsTimeline = new LocalTimeline(globalTimeline, 1);
40-
public static Timeline networkTimeline = new LocalTimeline(globalTimeline, 1);
41-
public static Timeline renderTimeline = new LocalTimeline(globalTimeline, 1);
40+
public static Timeline networkTimeline = new LocalTimeline(globalTimeline, 3);
41+
public static Timeline renderTimeline = new LocalTimeline(globalTimeline, 3);
4242
public static EventManager eventManager = new EventManager();
43+
public static ExecutorService threadPool = Executors.newFixedThreadPool(NUM_THREADS);
44+
4345

44-
4546
public static Player player;
4647

4748
private boolean isServer;
@@ -52,7 +53,6 @@ public class Rectangles extends PApplet {
5253
private GameObj leftWall;
5354
private GameObj rightWall;
5455

55-
private ExecutorService threadPool = Executors.newFixedThreadPool(NUM_THREADS);
5656
private boolean setup = false;
5757

5858

@@ -71,41 +71,60 @@ public Rectangles(boolean isServer) {
7171
* Just runs the game loop infinitely
7272
*/
7373
public void runLoop() {
74+
for (GameObj obj : movObjects) {
75+
eventManager.registerHandler(obj, Event.EVENT_COLLISION);
76+
eventManager.registerHandler(obj, Event.EVENT_MOVEMENT);
77+
eventManager.registerHandler(obj, Event.EVENT_PHYSICS);
78+
if (obj.getType() == "player") {
79+
eventManager.registerHandler(obj, Event.EVENT_INPUT);
80+
eventManager.registerHandler(obj, Event.EVENT_DEATH);
81+
eventManager.registerHandler(obj, Event.EVENT_SPAWN);
82+
}
83+
HashMap<String, Object> data = new HashMap<>();
84+
data.put("caller", obj.getUUID());
85+
Event e = new Event(Event.EVENT_PHYSICS, globalTimeline.getCurrentTime()
86+
+ physicsTimeline.getTickSize(), data);
87+
eventManager.raiseEvent(e);
88+
89+
90+
91+
}
7492
while(true) {
75-
this.gameLoop(globalTimeline.getAndResetDelta());
93+
this.gameLoop(globalTimeline.resetDelta());
7694
}
7795
}
7896

7997
/**
8098
* Single iteration of the game loop
8199
* @param delta local time since last iteration
82100
*/
83-
private void gameLoop(long delta) {
84-
if (delta > 0) {
85-
this.updatePhysics(physicsTimeline.getAndResetDelta());
86-
this.updateNetwork(networkTimeline.getAndResetDelta());
87-
this.updateRender(renderTimeline.getAndResetDelta());
101+
private void gameLoop(boolean delta) {
102+
if (delta) {
103+
threadPool.execute(eventManager);
104+
//this.updatePhysics(physicsTimeline.getAndResetDelta());
105+
//this.updateNetwork(networkTimeline.resetDelta());
106+
this.updateRender(renderTimeline.resetDelta());
88107
}
89108
}
90109

91110
/* Update methods with deltas */
92111
// TODO: Determine if this is event the best way to do it...
93112

94-
private void updateRender(long delta) {
95-
if (delta > 0) {
113+
private void updateRender(boolean delta) {
114+
if (delta) {
96115
this.redraw();
97116
}
98117
}
99118

100-
private void updateNetwork(long delta) {
101-
if (delta > 0 && this.isServer) {
119+
private void updateNetwork(boolean delta) {
120+
if (delta && this.isServer) {
102121
this.server.updateClients();
103122
}
104123
}
105124

106-
private void updatePhysics(long delta) {
125+
private void updatePhysics(boolean delta) {
107126
// Dummy Renderer?
108-
if (delta > 0 && this.isServer) {
127+
if (delta && this.isServer) {
109128
// Update physics
110129
for (GameObj obj : movObjects) {
111130
obj.getPy().update(obj, objects);

rectangles/engine/events/Event.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
public class Event {
77
public static final int EVENT_INPUT = 0;
8-
public static final int EVENT_COLLISON = 1;
9-
public static final int EVENT_MOVEMENT = 2;
10-
public static final int EVENT_DEATH = 3;
11-
public static final int EVENT_SPAWN = 4;
8+
public static final int EVENT_DEATH = 1;
9+
public static final int EVENT_SPAWN = 2;
10+
public static final int EVENT_MOVEMENT = 3;
11+
public static final int EVENT_COLLISION = 4;
12+
public static final int EVENT_PHYSICS = 5;
1213

1314
private int type = -1;
1415

rectangles/engine/events/EventManager.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import engine.EngineObject;
99
import engine.GameObj;
10+
import engine.Rectangles;
1011

1112
public class EventManager implements Runnable {
1213

@@ -15,10 +16,10 @@ public class EventManager implements Runnable {
1516

1617
@Override
1718
public int compare(Event e1, Event e2) {
18-
if (e1.getTime() < e2.getTime()) {
19+
if (e1.getType() < e2.getType()) {
1920
return -1;
20-
} else if (e1.getTime() == e2.getTime()) {
21-
return (e1.getType() - e2.getType());
21+
} else if (e1.getType() == e2.getType()) {
22+
return (int) (e1.getTime() - e2.getTime());
2223
} else {
2324
return 1;
2425
}
@@ -31,19 +32,28 @@ public int compare(Event e1, Event e2) {
3132

3233

3334
public void registerHandler(GameObj handler, int type) {
35+
if (!this.registrar.containsKey(type)) {
36+
this.registrar.put(type, new ArrayList<EngineObject>());
37+
}
3438
this.registrar.get(type).add(handler);
3539
}
3640

3741
public void raiseEvent(Event e) {
42+
System.out.println(Rectangles.eventManager.getEventQueue().size());
43+
//System.out.println(e.getType() + " : " + Rectangles.objectMap.get(e.getData().get("caller")).getType());
3844
this.eventQueue.add(e);
3945
}
4046

4147
@Override
4248
public void run() {
4349
try {
4450
Event next = this.eventQueue.take();
45-
for (EngineObject handler : this.registrar.get(next.getType())) {
46-
handler.handleEvent(next);
51+
//System.out.println(next.getType() + " : " + Rectangles.objectMap.get(next.getData().get("caller")).getType());
52+
if (next != null) {
53+
for (EngineObject handler : this.registrar.get(next.getType())) {
54+
handler.handleEvent(next);
55+
//Rectangles.threadPool.execute(handler.getReader(next));
56+
}
4757
}
4858
} catch (InterruptedException e) {
4959
System.out.println("Event removal Interrupted");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package engine.events;
2+
3+
import engine.GameObj;
4+
5+
public class PhysicsSubsystem implements Runnable {
6+
7+
@Override
8+
public void run() {
9+
// TODO Auto-generated method stub
10+
11+
}
12+
13+
}

rectangles/engine/time/GlobalTimeline.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ public void setTickSize(int tickSize) {
7171
}
7272

7373
@Override
74-
public long getAndResetDelta() {
74+
public boolean resetDelta() {
7575
long currentTime = this.getCurrentTime();
7676
long delta = currentTime - this.lastTime;
77-
this.lastTime = currentTime;
78-
return delta;
77+
if (delta > 0) {
78+
this.lastTime = currentTime;
79+
return true;
80+
}
81+
return false;
7982
}
8083
}

rectangles/engine/time/LocalTimeline.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ public void setTickSize(int tickSize) {
7575
}
7676

7777
@Override
78-
public long getAndResetDelta() {
78+
public boolean resetDelta() {
7979
long currentTime = this.getCurrentTime();
8080
long delta = currentTime - this.lastTime;
81-
this.lastTime = currentTime;
82-
return delta;
81+
if (delta > 0) {
82+
this.lastTime = currentTime;
83+
return true;
84+
}
85+
return false;
8386
}
8487

8588
}

0 commit comments

Comments
 (0)