Skip to content

Commit bdbff7e

Browse files
committed
Checkpoint rendering re-factor
1 parent 9896c35 commit bdbff7e

9 files changed

Lines changed: 379 additions & 101 deletions

File tree

rectangles/engine/Boundary.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package engine;
2+
3+
import processing.core.PShape;
4+
5+
public class Boundary extends GameObj{
6+
7+
public Boundary(float objWidth, float objHeight, float mass, float x, float y, PShape shape, boolean isFloor,
8+
boolean isGrav) {
9+
super(objWidth, objHeight, mass, x, y, shape, isFloor, isGrav);
10+
}
11+
12+
@Override
13+
public String getType() {
14+
return "boundary";
15+
}
16+
17+
}

rectangles/engine/GameObj.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,39 @@
22

33
import processing.core.PShape;
44

5+
import java.util.UUID;
56

67
import processing.core.PApplet;
78

8-
public class GameObj extends PApplet {
9+
public abstract class GameObj extends PApplet {
910
private Physics py;
1011
private PShape shape;
1112
private float objWidth;
1213
private float objHeight;
1314
private boolean isFloor;
15+
private UUID uuid;
1416

1517
public GameObj(float objWidth, float objHeight, float mass, float x, float y, PShape shape,
1618
boolean isFloor, boolean isGrav) {
1719
this.isFloor = isFloor;
1820
this.objHeight = height;
1921
this.objWidth = width;
2022
this.py = new Physics(x, y, objWidth, objHeight, mass, 20, isGrav);
23+
this.uuid = UUID.randomUUID();
2124
try {
2225
this.shape = shape;
2326
} catch (NullPointerException e) {
2427
// Do nothing, its probably screen limits
2528
}
29+
2630
}
31+
32+
public abstract String getType();
2733

34+
public UUID getUUID() {
35+
return this.uuid;
36+
}
37+
2838
public Physics getPy() {
2939
return py;
3040
}

rectangles/engine/Physics.java

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

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

4545
for (GameObj obj : objects) {
46-
if (this.intersects(obj.getPy().getBounds2D())) {
46+
if (this.intersects(obj.getPy().getBounds2D()) && !obj.getUUID().equals(caller.getUUID())) {
4747
if (obj.isFloor()) {
4848
this.velocity.y = (float) -1;
4949
this.acceleration.mult(0);

rectangles/engine/Platform.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
import processing.core.PShape;
44

55
public class Platform extends GameObj {
6+
private boolean movable;
67

78
public Platform(PShape shape, boolean movable, float x, float y) {
89
super(shape.getWidth(), shape.getHeight(), 0, x, y, shape, false, false);
10+
11+
this.movable = movable;
12+
}
13+
14+
@Override
15+
public String getType() {
16+
return "platform";
17+
}
18+
19+
public boolean isMovable() {
20+
return this.movable;
921
}
1022

1123
}

rectangles/engine/Player.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package engine;
2+
3+
import processing.core.PShape;
4+
5+
public class Player extends GameObj {
6+
public Player(PShape shape, float x, float y) {
7+
super(shape.getWidth(), shape.getHeight(), 0, x, y, shape, false, false);
8+
}
9+
10+
@Override
11+
public String getType() {
12+
return "player";
13+
}
14+
}

rectangles/engine/Rectangles.java

Lines changed: 103 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import java.io.IOException;
44
import java.net.Socket;
55
import java.util.ArrayList;
6+
import java.util.UUID;
7+
import java.util.concurrent.ConcurrentHashMap;
68
import java.util.concurrent.CopyOnWriteArrayList;
79
import java.util.concurrent.ExecutorService;
810
import java.util.concurrent.Executors;
911

1012
import networking.Client;
13+
import networking.Packet;
1114
import networking.Server;
1215
import processing.core.PApplet;
1316
import processing.core.PShape;
@@ -26,11 +29,12 @@ public class Rectangles extends PApplet {
2629
private GameObj ceiling;
2730
private GameObj leftWall;
2831
private GameObj rightWall;
29-
private GameObj player;
32+
private Player player;
3033

3134
private ExecutorService threadPool = Executors.newFixedThreadPool(NUM_THREADS);
3235
private CopyOnWriteArrayList<GameObj> objects = new CopyOnWriteArrayList<GameObj>();
3336
private CopyOnWriteArrayList<GameObj> movObjects = new CopyOnWriteArrayList<GameObj>();
37+
private ConcurrentHashMap<UUID, GameObj> objectMap = new ConcurrentHashMap<UUID, GameObj>();
3438

3539
public Rectangles(boolean isServer) {
3640
this.isServer = isServer;
@@ -46,71 +50,95 @@ public void setup() {
4650
frameRate(60);
4751
textSize(32);
4852

53+
54+
// Setup Server
55+
if (this.isServer) {
56+
this.server = new Server(9200, this.threadPool, this.player);
57+
this.localClient = this.server.getLocalClient();
58+
new Thread(this.server).start();
4959

50-
// Add screen boundaries
51-
this.floor = new GameObj(width, (float) 100, 0, 0, height, null, true, false);
52-
this.ceiling = new GameObj(width, (float) 100, 0, 0, -100, null, false, false);
53-
this.leftWall = new GameObj((float) 100, height, 0, -100, 0, null, false, false);
54-
this.rightWall = new GameObj((float) 100, height, width, width, 0, null, false, false);
5560

56-
this.objects.add(this.floor);
57-
this.objects.add(this.ceiling);
58-
this.objects.add(this.leftWall);
59-
this.objects.add(this.rightWall);
61+
// Add screen boundaries
62+
this.floor = new Boundary(width, (float) 100, 0, 0, height, null, true, false);
63+
this.ceiling = new Boundary(width, (float) 100, 0, 0, -100, null, false, false);
64+
this.leftWall = new Boundary((float) 100, height, 0, -100, 0, null, false, false);
65+
this.rightWall = new Boundary((float) 100, height, width, width, 0, null, false, false);
6066

61-
// Platforms
62-
PShape platformStatic = createShape(RECT, 0, 0, width/5, 25);
63-
platformStatic.setFill(color(random(255), random(255), random(255)));
64-
platformStatic.setStroke(false);
67+
this.objects.add(this.floor);
68+
this.objectMap.put(this.floor.getUUID(), this.floor);
69+
this.server.newPacket(Packet.PACKET_CREATE, this.floor);
70+
71+
this.objects.add(this.ceiling);
72+
this.objectMap.put(this.ceiling.getUUID(), this.ceiling);
73+
this.server.newPacket(Packet.PACKET_CREATE, this.ceiling);
6574

66-
PShape platformMov = createShape(RECT, 0, 0, width/5, 25);
67-
platformMov.setFill(color(random(255), random(255), random(255)));
68-
platformMov.setStroke(false);
69-
70-
ArrayList<Platform> staticPlatforms = new ArrayList<Platform>();
71-
ArrayList<Platform> movPlatforms = new ArrayList<Platform>();
72-
73-
for (Platform p : movPlatforms) {
74-
p.getPy().setTopSpeed(5);
75-
p.getPy().setVelocity(new PVector(5,0));
76-
}
75+
this.objects.add(this.leftWall);
76+
this.objectMap.put(this.leftWall.getUUID(), this.leftWall);
77+
this.server.newPacket(Packet.PACKET_CREATE, this.leftWall);
7778

78-
Platform static_1 = new Platform(platformStatic, false, width - platformStatic.getWidth(), 100);
79-
Platform static_2 = new Platform(platformStatic, false, platformStatic.getWidth(), 100);
79+
this.objects.add(this.rightWall);
80+
this.objectMap.put(this.rightWall.getUUID(), this.rightWall);
81+
this.server.newPacket(Packet.PACKET_CREATE, this.rightWall);
8082

81-
staticPlatforms.add(static_1);
82-
staticPlatforms.add(static_2);
83-
84-
Platform mov_1 = new Platform(platformStatic, false, width - platformStatic.getWidth(), 300);
85-
Platform mov_2 = new Platform(platformStatic, false, platformStatic.getWidth(), 300);
8683

87-
movPlatforms.add(mov_1);
88-
movPlatforms.add(mov_2);
8984

90-
this.objects.addAll(staticPlatforms);
91-
this.objects.addAll(movPlatforms);
92-
93-
this.movObjects.addAll(movPlatforms);
9485

86+
// Platforms
87+
PShape platformStatic = createShape(RECT, 0, 0, width / 5, 25);
88+
platformStatic.setFill(color(random(255), random(255), random(255)));
89+
platformStatic.setStroke(false);
9590

96-
// Player
97-
float sqrDim = 50;
98-
PShape sqr = createShape(RECT, 0, 0, sqrDim, sqrDim);
99-
sqr.setFill(color(random(255), random(255), random(255)));
100-
sqr.setStroke(false);
101-
this.player = new GameObj(sqrDim, sqrDim, 0, height - sqrDim - 2, 1, sqr, false, true);
102-
103-
this.movObjects.add(this.player);
91+
PShape platformMov = createShape(RECT, 0, 0, width / 5, 25);
92+
platformMov.setFill(color(random(255), random(255), random(255)));
93+
platformMov.setStroke(false);
10494

95+
ArrayList<Platform> staticPlatforms = new ArrayList<Platform>();
96+
ArrayList<Platform> movPlatforms = new ArrayList<Platform>();
10597

106-
// Setup Server
107-
if (this.isServer) {
108-
this.server = new Server(9200, this.threadPool, this.player);
109-
this.localClient = this.server.getLocalClient();
110-
new Thread(this.server).start();
98+
for (Platform p : movPlatforms) {
99+
p.getPy().setTopSpeed(5);
100+
p.getPy().setVelocity(new PVector(5, 0));
101+
}
102+
103+
Platform static_1 = new Platform(platformStatic, false, width - platformStatic.getWidth(), 100);
104+
Platform static_2 = new Platform(platformStatic, false, platformStatic.getWidth(), 100);
105+
106+
staticPlatforms.add(static_1);
107+
staticPlatforms.add(static_2);
108+
109+
Platform mov_1 = new Platform(platformStatic, false, width - platformStatic.getWidth(), 300);
110+
Platform mov_2 = new Platform(platformStatic, false, platformStatic.getWidth(), 300);
111+
112+
movPlatforms.add(mov_1);
113+
movPlatforms.add(mov_2);
114+
115+
for (Platform p : staticPlatforms) {
116+
this.objects.add(p);
117+
this.objectMap.put(p.getUUID(), p);
118+
this.server.newPacket(Packet.PACKET_CREATE, p);
119+
120+
}
121+
122+
for (Platform p : movPlatforms) {
123+
this.objects.add(p);
124+
this.objectMap.put(p.getUUID(), p);
125+
this.movObjects.add(p);
126+
this.server.newPacket(Packet.PACKET_CREATE, p);
127+
}
128+
129+
// Player
130+
float sqrDim = 50;
131+
PShape sqr = createShape(RECT, 0, 0, sqrDim, sqrDim);
132+
sqr.setFill(color(random(255), random(255), random(255)));
133+
sqr.setStroke(false);
134+
this.player = new Player(sqr, height - sqrDim - 2, 1);
135+
this.objectMap.put(this.player.getUUID(), this.player);
136+
this.movObjects.add(this.player);
137+
this.server.newPacket(Packet.PACKET_CREATE, this.player);
138+
111139
} else {
112140
try {
113-
this.localClient = new Client(new Socket("127.0.0.1", 9200), this.threadPool, this.player);
141+
this.localClient = new Client(new Socket("127.0.0.1", 9200), this.threadPool, null);
114142
} catch (IOException e) {
115143
System.out.println("Error opening local client socket");
116144
e.printStackTrace();
@@ -122,7 +150,8 @@ public void setup() {
122150

123151
public void draw() {
124152
background(0);
125-
text(this.localClient.getNumIter(), 10, 40);
153+
154+
this.renderAll(objects);
126155
// Walls
127156
rect((float) this.floor.getPy().getBounds2D().getX(), (float) this.floor.getPy().getBounds2D().getY(),
128157
(float) this.floor.getPy().getBounds2D().getWidth(), (float) this.floor.getPy().getBounds2D().getHeight());
@@ -133,9 +162,12 @@ public void draw() {
133162
rect((float) this.rightWall.getPy().getBounds2D().getX(), (float) this.rightWall.getPy().getBounds2D().getY(),
134163
(float) this.rightWall.getPy().getBounds2D().getWidth(), (float) this.rightWall.getPy().getBounds2D().getHeight());
135164

136-
// Update physics
137-
for (GameObj obj : movObjects) {
138-
obj.getPy().update(this.objects);
165+
// Dummy Renderer?
166+
if (isServer) {
167+
// Update physics
168+
for (GameObj obj : movObjects) {
169+
obj.getPy().update(obj, this.objects);
170+
}
139171
}
140172
// Render
141173
shape(this.localClient.getPlayer().getShape(), this.localClient.getPlayer().getPy().getLocation().x,
@@ -149,6 +181,21 @@ public void draw() {
149181

150182
}
151183

184+
private void renderAll(CopyOnWriteArrayList<GameObj> objects) {
185+
for (GameObj obj : objects) {
186+
this.render(obj);;
187+
}
188+
}
189+
190+
public void render(GameObj obj) {
191+
if (obj.getType().equals("boundary")) {
192+
rect((float) obj.getPy().getBounds2D().getX(), (float) obj.getPy().getBounds2D().getY(),
193+
(float) obj.getPy().getBounds2D().getWidth(), (float) obj.getPy().getBounds2D().getHeight());
194+
} else {
195+
shape(obj.getShape(), obj.getPy().getLocation().x, obj.getPy().getLocation().y);
196+
}
197+
}
198+
152199
public void dispose() {
153200
if (this.isServer) {
154201
System.out.println("Stopping Server");
@@ -170,12 +217,6 @@ public void keyPressed() {
170217
if (key == ' ') {
171218
this.localClient.getPlayer().getPy().setAccelerationY(-20);
172219
}
173-
if (key == 'q') {
174-
this.localClient.iterNumIter();
175-
if (!this.isServer) {
176-
this.localClient.write(this.localClient.getNumIter());
177-
}
178-
}
179220
}
180221

181222
// API stuff from https://happycoding.io/tutorials/java/processing-in-java

0 commit comments

Comments
 (0)