Skip to content

Commit cc79d7d

Browse files
committed
Add object networked functionality
1 parent 5b9a1e8 commit cc79d7d

9 files changed

Lines changed: 549 additions & 164 deletions

File tree

rectangles/engine/Boundary.java

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

3-
import processing.core.PShape;
4-
53
public class Boundary extends GameObj{
64

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);
5+
public Boundary(float objWidth, float objHeight, float x, float y, boolean isFloor) {
6+
super(objWidth, objHeight, 0, x, y, isFloor, false);
107
}
118

129
@Override
1310
public String getType() {
1411
return "boundary";
1512
}
13+
14+
@Override
15+
public String toSerial() {
16+
String serial = "{"
17+
+ "width:" + this.getObjWidth() + ","
18+
+ "height:" + this.getObjHeight() + ","
19+
+ "x:" + this.getPy().getLocation().x + ","
20+
+ "y:" + this.getPy().getLocation().y + ","
21+
+ "isFloor:" + this.isFloor()
22+
+ "}";
23+
return serial;
24+
}
1625

26+
public static Boundary deSerial(String serial) {
27+
float width = 0;
28+
float height = 0;
29+
float x = 0;
30+
float y = 0;
31+
boolean isFloor = false;
32+
33+
serial = serial.replace("{", "").replace("}", "");
34+
String[] data = serial.split(",");
35+
36+
for (String d : data) {
37+
String[] subData = d.split(":");
38+
String key = subData[0];
39+
String value = subData[1];
40+
41+
switch(key) {
42+
case("width"):
43+
width = Float.parseFloat(value);
44+
break;
45+
case("height"):
46+
height = Float.parseFloat(value);
47+
break;
48+
case("x"):
49+
x = Float.parseFloat(value);
50+
break;
51+
case("y"):
52+
y = Float.parseFloat(value);
53+
break;
54+
case("isFloor"):
55+
isFloor = Boolean.parseBoolean(value);
56+
break;
57+
default:
58+
break;
59+
}
60+
}
61+
Boundary res = new Boundary(width, height, x, y, isFloor);
62+
return res;
63+
}
1764
}

rectangles/engine/GameObj.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
package engine;
22

3-
import processing.core.PShape;
43

54
import java.util.UUID;
65

76
import processing.core.PApplet;
87

98
public abstract class GameObj extends PApplet {
10-
private Physics py;
11-
private PShape shape;
9+
protected Physics py;
10+
protected Renderable rend;
1211
private float objWidth;
1312
private float objHeight;
1413
private boolean isFloor;
1514

1615
private UUID uuid = UUID.randomUUID();
1716

18-
public GameObj(float objWidth, float objHeight, float mass, float x, float y, PShape shape,
17+
public GameObj(float objWidth, float objHeight, float mass, float x, float y,
1918
boolean isFloor, boolean isGrav) {
2019
this.isFloor = isFloor;
21-
this.objHeight = height;
22-
this.objWidth = width;
20+
this.objHeight = objHeight;
21+
this.objWidth = objWidth;
2322
this.py = new Physics(x, y, objWidth, objHeight, mass, 20, isGrav);
24-
25-
try {
26-
this.shape = shape;
27-
} catch (NullPointerException e) {
28-
// Do nothing, its probably screen limits
29-
}
3023

3124
}
3225

3326
public abstract String getType();
27+
28+
public abstract String toSerial();
29+
30+
public Renderable getRend() {
31+
return this.rend;
32+
}
3433

3534
public UUID getUUID() {
3635
return this.uuid;
3736
}
3837

38+
public void setUUID(UUID id) {
39+
this.uuid = id;
40+
}
41+
3942
public Physics getPy() {
4043
return py;
4144
}
@@ -44,14 +47,6 @@ public void setPy(Physics py) {
4447
this.py = py;
4548
}
4649

47-
public PShape getShape() {
48-
return shape;
49-
}
50-
51-
public void setShape(PShape shape) {
52-
this.shape = shape;
53-
}
54-
5550
public float getObjWidth() {
5651
return objWidth;
5752
}

rectangles/engine/Platform.java

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package engine;
22

3+
import processing.core.PApplet;
34
import processing.core.PShape;
45

56
public class Platform extends GameObj {
67
private boolean movable;
8+
private PApplet inst;
79

8-
public Platform(PShape shape, boolean movable, float x, float y) {
9-
super(shape.getWidth(), shape.getHeight(), 0, x, y, shape, false, false);
10-
10+
public Platform(PApplet inst, float width, float height, float x, float y, boolean movable) {
11+
super(width, height, 0, x, y, false, false);
1112
this.movable = movable;
13+
14+
int[] color = {(int) random(255), (int) random(255), (int) random(255)};
15+
this.rend = new Renderable(inst, color, PShape.RECT, width, height);
1216
}
1317

1418
@Override
@@ -19,5 +23,67 @@ public String getType() {
1923
public boolean isMovable() {
2024
return this.movable;
2125
}
26+
27+
@Override
28+
public String toSerial() {
29+
String serial = "{"
30+
+ "width:" + this.getObjWidth() + ","
31+
+ "height:" + this.getObjHeight() + ","
32+
+ "x:" + this.getPy().getLocation().x + ","
33+
+ "y:" + this.getPy().getLocation().y + ","
34+
+ "movable:" + this.movable + ","
35+
+ "color:" + this.rend.getColorToString()
36+
+ "}";
2237

38+
return serial;
39+
}
40+
41+
public static Platform deSerial(PApplet inst, String serial) {
42+
float width = 0;
43+
float height = 0;
44+
float x = 0;
45+
float y = 0;
46+
boolean movable = false;
47+
int[] color = new int[3];
48+
49+
serial = serial.replace("{", "").replace("}", "");
50+
String[] data = serial.split(",");
51+
52+
for (String d : data) {
53+
String[] subData = d.split(":");
54+
String key = subData[0];
55+
String value = subData[1];
56+
57+
switch(key) {
58+
case("width"):
59+
width = Float.parseFloat(value);
60+
break;
61+
case("height"):
62+
height = Float.parseFloat(value);
63+
break;
64+
case("x"):
65+
x = Float.parseFloat(value);
66+
break;
67+
case("y"):
68+
y = Float.parseFloat(value);
69+
break;
70+
case("movable"):
71+
movable = Boolean.parseBoolean(value);
72+
break;
73+
case("color"):
74+
value = value.replace("[", "").replace("]", "");
75+
int i = 0;
76+
for(String c : value.split(" ")) {
77+
color[i] = Integer.parseInt(c);
78+
i++;
79+
}
80+
break;
81+
default:
82+
break;
83+
}
84+
}
85+
Platform res = new Platform(inst, width, height, x, y, movable);
86+
res.rend.setColor(color);
87+
return res;
88+
}
2389
}

rectangles/engine/Player.java

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,81 @@
11
package engine;
22

3+
4+
import java.util.HashMap;
5+
6+
import processing.core.PApplet;
37
import processing.core.PShape;
48

59
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);
10+
private float dim;
11+
12+
public Player(PApplet inst, float dim, float x, float y) {
13+
super(dim, dim, 0, x, y, false, false);
14+
this.dim = dim;
15+
int[] color = {(int) random(255), (int) random(255), (int) random(255)};
16+
this.rend = new Renderable(inst, color, PShape.RECT, dim, dim);
817
}
9-
18+
1019
@Override
1120
public String getType() {
1221
return "player";
1322
}
23+
24+
public float getDim() {
25+
return this.dim;
26+
}
27+
28+
@Override
29+
public String toSerial() {
30+
String serial = "{"
31+
+ "dim:" + this.dim + ","
32+
+ "x:" + this.getPy().getLocation().x + ","
33+
+ "y:" + this.getPy().getLocation().y + ","
34+
+ "color:" + this.rend.getColorToString()
35+
+ "}";
36+
return serial;
37+
}
38+
39+
40+
public static Player deSerial(PApplet inst, String serial) {
41+
float dim = 0;
42+
float x = 0;
43+
float y = 0;
44+
int[] color = new int[3];
45+
46+
serial = serial.replace("{", "").replace("}", "");
47+
String[] data = serial.split(",");
48+
49+
for (String d : data) {
50+
String[] subData = d.split(":");
51+
String key = subData[0];
52+
String value = subData[1];
53+
54+
switch(key) {
55+
case("dim"):
56+
dim = Float.parseFloat(value);
57+
break;
58+
case("x"):
59+
x = Float.parseFloat(value);
60+
break;
61+
case("y"):
62+
y = Float.parseFloat(value);
63+
break;
64+
case("color"):
65+
value = value.replace("[", "").replace("]", "");
66+
int i = 0;
67+
for(String c : value.split(" ")) {
68+
color[i] = Integer.parseInt(c);
69+
i++;
70+
}
71+
break;
72+
default:
73+
break;
74+
}
75+
}
76+
Player res = new Player(inst, dim, x, y);
77+
res.rend.setColor(color);
78+
return res;
79+
}
80+
1481
}

0 commit comments

Comments
 (0)