Skip to content

Commit 31e5612

Browse files
committed
Boi I hope these events will work
1 parent 071701a commit 31e5612

12 files changed

Lines changed: 321 additions & 65 deletions

File tree

rectangles/engine/Boundary.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package engine;
22

3+
import engine.events.Event;
4+
35
public class Boundary extends GameObj{
46

57
public Boundary(float objWidth, float objHeight, float x, float y, boolean isFloor) {
@@ -61,4 +63,8 @@ public static Boundary deSerial(String serial) {
6163
Boundary res = new Boundary(width, height, x, y, isFloor);
6264
return res;
6365
}
66+
67+
public void handleEvent(Event e) {
68+
// Do nothing
69+
}
6470
}

rectangles/engine/DeathZone.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package engine;
22

3+
import engine.events.Event;
34
import processing.core.PApplet;
45

56
public class DeathZone extends GameObj {
@@ -48,4 +49,9 @@ public static DeathZone deSerial(PApplet inst, String serial) {
4849
DeathZone res = new DeathZone(inst, x, y);
4950
return res;
5051
}
52+
53+
@Override
54+
public void handleEvent(Event e) {
55+
// Do nothing
56+
}
5157
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package engine;
2+
3+
import engine.events.Event;
4+
import processing.core.PApplet;
5+
6+
public abstract class EngineObject extends PApplet {
7+
8+
public abstract void handleEvent(Event e);
9+
}

rectangles/engine/GameObj.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
import java.util.UUID;
55

6+
import engine.events.Event;
67
import processing.core.PApplet;
78

8-
public abstract class GameObj extends PApplet {
9+
public abstract class GameObj extends EngineObject {
910
protected Physics py;
1011
protected Renderable rend;
1112
private float objWidth;

rectangles/engine/Physics.java

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import java.awt.geom.Point2D;
77
import java.awt.geom.Rectangle2D;
88
import java.util.ArrayList;
9+
import java.util.HashMap;
910
import java.util.concurrent.CopyOnWriteArrayList;
1011

12+
import engine.events.Event;
1113
import processing.core.PApplet;
1214
import processing.core.PVector;
1315

@@ -42,40 +44,13 @@ public Physics(float x, float y, float objWidth, float objHeight, float mass, fl
4244
public void update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
4345
//this.acceleration.setMag(0.2);
4446

47+
GameObj collidedWith = null;
48+
4549
for (GameObj obj : objects) {
4650
if (this.intersects(obj.getPy().getBounds2D()) && !obj.getUUID().equals(caller.getUUID())) {
47-
if (caller.getType().equals("platform")) {
48-
if (obj.getType().equals("boundary")) {
49-
this.velocity.mult(-1);
50-
}
51-
} else {
52-
if (obj.isFloor()) {
53-
this.velocity.y = (float) -1;
54-
} else {
55-
switch (obj.getType()) {
56-
case ("player"):
57-
// Do nothing, clip-less
58-
break;
59-
case("platform"):
60-
this.acceleration.mult((float) -1);
61-
if (this.velocity.y > 0) {
62-
this.velocity.y = (float) 0;
63-
} else {
64-
this.velocity.y = (float) -1;
65-
this.velocity.x = (float) 0;
66-
}
67-
break;
68-
case("death-zone"):
69-
Spawn s = Rectangles.spawnPoints[Rectangles.generator.nextInt(2)];
70-
this.location.set(s.getPy().getLocation().x, s.getPy().getLocation().y);
71-
Rectangles.deathPoints++;
72-
break;
73-
default:
74-
this.velocity.mult((float) -1);
75-
break;
76-
}
77-
}
78-
}
51+
collidedWith = obj;
52+
// TODO: Need break here?
53+
break;
7954
}
8055
}
8156

@@ -87,9 +62,28 @@ public void update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
8762
*
8863
* In replays, just worry about movement events! :D
8964
*/
90-
this.velocity.add(this.acceleration);
91-
this.velocity.limit(this.topSpeed);
92-
this.location.add(this.velocity);
65+
66+
if (collidedWith != null) {
67+
HashMap<String, Object> data = new HashMap<>();
68+
data.put("caller", caller.getUUID());
69+
data.put("collidedWith", collidedWith.getUUID());
70+
Event e = new Event(Event.EVENT_COLLISON, Rectangles.globalTimeline.getCurrentTime(), data);
71+
Rectangles.eventManager.raiseEvent(e);
72+
} else {
73+
this.velocity.add(this.acceleration);
74+
this.velocity.limit(this.topSpeed);
75+
if (this.velocity.mag() > 0) {
76+
PVector newLoc = new PVector(this.location.x, this.location.y);
77+
newLoc.add(this.velocity);
78+
HashMap<String, Object> data = new HashMap<>();
79+
data.put("caller", caller.getUUID());
80+
data.put("x", newLoc.x);
81+
data.put("y", newLoc.y);
82+
Event e = new Event(Event.EVENT_MOVEMENT, Rectangles.globalTimeline.getCurrentTime(), data);
83+
Rectangles.eventManager.raiseEvent(e);
84+
}
85+
//this.location.add(this.velocity);
86+
}
9387

9488
// Reset acceleration?
9589
if (this.isGrav) {

rectangles/engine/Platform.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package engine;
22

3+
import java.util.HashMap;
4+
import java.util.UUID;
5+
6+
import engine.events.Event;
37
import processing.core.PApplet;
48
import processing.core.PShape;
9+
import processing.core.PVector;
510

611
public class Platform extends GameObj {
712
private boolean movable;
@@ -86,4 +91,36 @@ public static Platform deSerial(PApplet inst, String serial) {
8691
res.rend.setColor(color);
8792
return res;
8893
}
94+
95+
@Override
96+
public void handleEvent(Event e) {
97+
switch(e.getType()) {
98+
case (Event.EVENT_COLLISON):
99+
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
100+
GameObj collidedWith = Rectangles.objectMap.get((UUID) e.getData().get("collidedWith"));
101+
if (collidedWith.getType().equals("boundary")) {
102+
this.getPy().getVelocity().mult(-1);
103+
if (this.getPy().getVelocity().mag() > 0) {
104+
PVector newLoc = new PVector(this.getPy().getLocation().x, this.getPy().getLocation().y);
105+
newLoc.add(this.getPy().getVelocity());
106+
HashMap<String, Object> data = new HashMap<>();
107+
data.put("caller", this.getUUID());
108+
data.put("x", newLoc.x);
109+
data.put("y", newLoc.y);
110+
Event mov = new Event(Event.EVENT_MOVEMENT, Rectangles.globalTimeline.getCurrentTime(), data);
111+
Rectangles.eventManager.raiseEvent(mov);
112+
}
113+
}
114+
}
115+
break;
116+
case(Event.EVENT_MOVEMENT):
117+
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
118+
PVector newLoc = new PVector((float) (e.getData().get("x")), (float) e.getData().get("y"));
119+
this.getPy().setLocation(newLoc);
120+
}
121+
break;
122+
default:
123+
break;
124+
}
125+
}
89126
}

rectangles/engine/Player.java

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33

44
import java.util.HashMap;
5+
import java.util.UUID;
56

7+
import engine.events.Event;
68
import processing.core.PApplet;
9+
import processing.core.PConstants;
710
import processing.core.PShape;
11+
import processing.core.PVector;
812

913
public class Player extends GameObj {
1014
private float dim;
@@ -15,7 +19,7 @@ public Player(PApplet inst, float dim, float x, float y) {
1519
int[] color = {(int) random(255), (int) random(255), (int) random(255)};
1620
this.rend = new Renderable(inst, color, PShape.RECT, dim, dim);
1721
}
18-
22+
1923
@Override
2024
public String getType() {
2125
return "player";
@@ -78,4 +82,96 @@ public static Player deSerial(PApplet inst, String serial) {
7882
return res;
7983
}
8084

85+
@Override
86+
public void handleEvent(Event e) {
87+
88+
switch(e.getType()) {
89+
case(Event.EVENT_COLLISON):
90+
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
91+
GameObj collidedWith = Rectangles.objectMap.get((UUID) e.getData().get("collidedWith"));
92+
if (collidedWith.isFloor()) {
93+
this.getPy().getVelocity().y = (float) -1;
94+
} else {
95+
switch (collidedWith.getType()) {
96+
case ("player"):
97+
// Do nothing, clip-less
98+
break;
99+
case ("platform"):
100+
this.getPy().getAcceleration().mult((float) -1);
101+
if (this.getPy().getVelocity().y > 0) {
102+
this.getPy().getVelocity().y = (float) 0;
103+
} else {
104+
this.getPy().getVelocity().y = (float) -1;
105+
this.getPy().getVelocity().x = (float) 0;
106+
}
107+
break;
108+
case ("death-zone"):
109+
HashMap<String, Object> data = new HashMap<>();
110+
data.put("caller", this.getUUID());
111+
Event death = new Event(Event.EVENT_DEATH, e.getTime(), data);
112+
Rectangles.eventManager.raiseEvent(death);
113+
114+
default:
115+
this.getPy().getVelocity().mult((float) -1);
116+
break;
117+
}
118+
}
119+
if (this.getPy().getVelocity().mag() > 0) {
120+
PVector newLoc = new PVector(this.getPy().getLocation().x, this.getPy().getLocation().y);
121+
newLoc.add(this.getPy().getVelocity());
122+
HashMap<String, Object> data = new HashMap<>();
123+
data.put("caller", this.getUUID());
124+
data.put("x", newLoc.x);
125+
data.put("y", newLoc.y);
126+
Event mov = new Event(Event.EVENT_MOVEMENT, e.getTime(), data);
127+
Rectangles.eventManager.raiseEvent(mov);
128+
}
129+
}
130+
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;
137+
case(Event.EVENT_DEATH):
138+
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
139+
Rectangles.deathPoints++;
140+
HashMap<String, Object> data = new HashMap<>();
141+
data.put("caller", this.getUUID());
142+
Event spawn = new Event(Event.EVENT_SPAWN, e.getTime(), data);
143+
Rectangles.eventManager.raiseEvent(spawn);
144+
}
145+
case(Event.EVENT_SPAWN):
146+
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
147+
Spawn s = Rectangles.spawnPoints[Rectangles.generator.nextInt(2)];
148+
HashMap<String, Object> data = new HashMap<>();
149+
data.put("caller", this.getUUID());
150+
data.put("x", s.getPy().getLocation().x);
151+
data.put("y", s.getPy().getLocation().y);
152+
Event mov = new Event(Event.EVENT_MOVEMENT, e.getTime(), data);
153+
Rectangles.eventManager.raiseEvent(mov);
154+
}
155+
break;
156+
case(Event.EVENT_INPUT):
157+
if (((UUID) e.getData().get("caller")).equals(this.getUUID())) {
158+
switch((int) e.getData().get("keyCode")) {
159+
case(PConstants.LEFT):
160+
this.getPy().setAccelerationX(-5);
161+
break;
162+
case(PConstants.RIGHT):
163+
this.getPy().setAccelerationX(5);
164+
break;
165+
case(' '):
166+
this.getPy().setAccelerationY(-20);
167+
break;
168+
}
169+
}
170+
break;
171+
default:
172+
break;
173+
}
174+
175+
}
176+
81177
}

rectangles/engine/Rectangles.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.net.Socket;
55
import java.util.ArrayList;
6+
import java.util.HashMap;
67
import java.util.Random;
78
import java.util.UUID;
89
import java.util.concurrent.ConcurrentHashMap;
@@ -11,6 +12,8 @@
1112
import java.util.concurrent.Executors;
1213
import java.util.concurrent.TimeUnit;
1314

15+
import engine.events.Event;
16+
import engine.events.EventManager;
1417
import engine.time.GlobalTimeline;
1518
import engine.time.LocalTimeline;
1619
import engine.time.Timeline;
@@ -36,6 +39,7 @@ public class Rectangles extends PApplet {
3639
public static Timeline physicsTimeline = new LocalTimeline(globalTimeline, 1);
3740
public static Timeline networkTimeline = new LocalTimeline(globalTimeline, 1);
3841
public static Timeline renderTimeline = new LocalTimeline(globalTimeline, 1);
42+
public static EventManager eventManager = new EventManager();
3943

4044

4145
public static Player player;
@@ -267,38 +271,24 @@ public void dispose() {
267271
}
268272

269273
public void keyPressed() {
270-
if (key == CODED) {
271-
if (keyCode == LEFT) {
272-
if (this.isServer) {
273-
player.getPy().setAccelerationX(-5);
274-
} else {
275-
Packet p = new Packet(keyCode, player.getUUID());
276-
this.localClient.write(p);
277-
}
278-
}
279-
if (keyCode == RIGHT) {
280-
if (this.isServer) {
281-
player.getPy().setAccelerationX(5);
282-
} else {
283-
Packet p = new Packet(keyCode, player.getUUID());
284-
this.localClient.write(p);
285-
}
286-
}
287-
}
288-
if (key == ' ') {
274+
if (keyCode == LEFT || keyCode == RIGHT || key == ' ') {
289275
if (this.isServer) {
290-
player.getPy().setAccelerationY(-20);
276+
HashMap<String, Object> data = new HashMap<>();
277+
Event e = new Event(Event.EVENT_INPUT, globalTimeline.getCurrentTime(), data);
278+
data.put("keyCode", keyCode);
279+
data.put("caller", player.getUUID());
280+
eventManager.raiseEvent(e);
291281
} else {
292282
Packet p = new Packet(keyCode, player.getUUID());
293283
this.localClient.write(p);
294284
}
295285
}
296-
286+
297287
if (key == 'p') {
298288
globalTimeline.pause();
299289
System.out.println("Paused");
300290
}
301-
291+
302292
if (key == 'u') {
303293
globalTimeline.unpause();
304294
System.out.println("Un-Paused");

0 commit comments

Comments
 (0)