Skip to content

Commit f6a81a6

Browse files
committed
Everything looks to be working
1 parent cc79d7d commit f6a81a6

9 files changed

Lines changed: 212 additions & 32 deletions

File tree

rectangles/engine/DeathZone.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package engine;
2+
3+
import processing.core.PApplet;
4+
5+
public class DeathZone extends GameObj {
6+
7+
public DeathZone(PApplet inst, float x, float y) {
8+
super(100, 100, 0, x, y, false, false);
9+
}
10+
11+
@Override
12+
public String getType() {
13+
return "death-zone";
14+
}
15+
16+
@Override
17+
public String toSerial() {
18+
String serial = "{"
19+
+ "x:" + this.getPy().getLocation().x + ","
20+
+ "y:" + this.getPy().getLocation().y + ","
21+
+ "}";
22+
return serial;
23+
}
24+
25+
public static DeathZone deSerial(PApplet inst, String serial) {
26+
float x = 0;
27+
float y = 0;
28+
29+
serial = serial.replace("{", "").replace("}", "");
30+
String[] data = serial.split(",");
31+
32+
for (String d : data) {
33+
String[] subData = d.split(":");
34+
String key = subData[0];
35+
String value = subData[1];
36+
37+
switch(key) {
38+
case("x"):
39+
x = Float.parseFloat(value);
40+
break;
41+
case("y"):
42+
y = Float.parseFloat(value);
43+
break;
44+
default:
45+
break;
46+
}
47+
}
48+
DeathZone res = new DeathZone(inst, x, y);
49+
return res;
50+
}
51+
}

rectangles/engine/Physics.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,41 @@ public Physics(float x, float y, float objWidth, float objHeight, float mass, fl
4141

4242
public PVector update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
4343
//this.acceleration.setMag(0.2);
44-
44+
4545
for (GameObj obj : objects) {
4646
if (this.intersects(obj.getPy().getBounds2D()) && !obj.getUUID().equals(caller.getUUID())) {
47-
if (obj.isFloor()) {
48-
this.velocity.y = (float) -1;
49-
this.acceleration.mult(0);
47+
if (caller.getType().equals("platform")) {
48+
if (obj.getType().equals("boundary")) {
49+
this.velocity.mult(-1);
50+
}
5051
} else {
51-
this.velocity.mult(-1);
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+
}
5278
}
53-
break;
5479
}
5580
}
5681

@@ -59,8 +84,9 @@ public PVector update(GameObj caller, CopyOnWriteArrayList<GameObj> objects) {
5984
this.location.add(this.velocity);
6085

6186
// Reset acceleration?
62-
this.acceleration = new PVector(0, GRAV);
63-
87+
if (this.isGrav) {
88+
this.acceleration = new PVector(0, GRAV);
89+
}
6490
return this.location;
6591
}
6692

rectangles/engine/Player.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Player extends GameObj {
1010
private float dim;
1111

1212
public Player(PApplet inst, float dim, float x, float y) {
13-
super(dim, dim, 0, x, y, false, false);
13+
super(dim, dim, 0, x, y, false, true);
1414
this.dim = dim;
1515
int[] color = {(int) random(255), (int) random(255), (int) random(255)};
1616
this.rend = new Renderable(inst, color, PShape.RECT, dim, dim);

rectangles/engine/Rectangles.java

Lines changed: 47 additions & 16 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.Random;
67
import java.util.UUID;
78
import java.util.concurrent.ConcurrentHashMap;
89
import java.util.concurrent.CopyOnWriteArrayList;
@@ -24,6 +25,11 @@ public class Rectangles extends PApplet {
2425
public static CopyOnWriteArrayList<GameObj> objects = new CopyOnWriteArrayList<GameObj>();
2526
public static CopyOnWriteArrayList<GameObj> movObjects = new CopyOnWriteArrayList<GameObj>();
2627
public static ConcurrentHashMap<UUID, GameObj> objectMap = new ConcurrentHashMap<UUID, GameObj>();
28+
public static Spawn[] spawnPoints = new Spawn[2];
29+
public static Random generator = new Random();
30+
public static int deathPoints = 0;
31+
32+
2733
public static Player player;
2834

2935
private boolean isServer;
@@ -54,9 +60,27 @@ public void setup() {
5460

5561
// Setup Server
5662
if (this.isServer) {
57-
// Player
63+
5864
float sqrDim = 50;
59-
player = new Player(this, sqrDim, height - sqrDim - 2, 1);
65+
66+
// Spawn Points
67+
spawnPoints[0] = new Spawn(this, width - sqrDim, 0 + sqrDim);
68+
spawnPoints[1] = new Spawn(this, width - sqrDim, height - sqrDim);
69+
70+
for (Spawn s : spawnPoints) {
71+
objects.add(s);
72+
objectMap.put(s.getUUID(), s);
73+
}
74+
75+
// Death Zone
76+
DeathZone dz_1 = new DeathZone(this, 0, 0);
77+
objects.add(dz_1);
78+
objectMap.put(dz_1.getUUID(), dz_1);
79+
80+
// Player
81+
Spawn rand = spawnPoints[generator.nextInt(2)];
82+
player = new Player(this, sqrDim, rand.getPy().getLocation().x,
83+
rand.getPy().getLocation().y);
6084

6185
this.server = new Server(this, 9200, this.threadPool, player);
6286
this.localClient = this.server.getLocalClient();
@@ -97,23 +121,30 @@ public void setup() {
97121
ArrayList<Platform> staticPlatforms = new ArrayList<Platform>();
98122
ArrayList<Platform> movPlatforms = new ArrayList<Platform>();
99123

100-
for (Platform p : movPlatforms) {
101-
p.getPy().setTopSpeed(5);
102-
p.getPy().setVelocity(new PVector(5, 0));
103-
}
104-
124+
//Platform static_1 = new Platform(this, pWidth, pHeight, width - 4*pWidth, 500, false);
105125
Platform static_1 = new Platform(this, pWidth, pHeight, width - pWidth, 100, false);
106-
Platform static_2 = new Platform(this, pWidth, pHeight, pWidth, 100, false);
126+
107127

108128
staticPlatforms.add(static_1);
109-
staticPlatforms.add(static_2);
110129

111-
Platform mov_1 = new Platform(this, pWidth, pHeight, width - pWidth, 300, false);
112-
Platform mov_2 = new Platform(this, pWidth, pHeight, pWidth, 300, false);
130+
Platform mov_1 = new Platform(this, pWidth, pHeight, width - 3*pWidth, 150, false);
131+
Platform mov_2 = new Platform(this, pWidth, pHeight, width - 5*pWidth, 250, false);
132+
113133

114134
movPlatforms.add(mov_1);
115135
movPlatforms.add(mov_2);
116136

137+
for (Platform p : movPlatforms) {
138+
p.getPy().setTopSpeed(2);
139+
p.getPy().setVelocity(new PVector(2, 0));
140+
}
141+
142+
Platform mov_3 = new Platform(this, pWidth, pHeight, 0, 100, false);
143+
mov_3.getPy().setTopSpeed(2);
144+
mov_3.getPy().setVelocity(new PVector(0, 2));
145+
movPlatforms.add(mov_3);
146+
147+
117148
for (Platform p : staticPlatforms) {
118149
objects.add(p);
119150
objectMap.put(p.getUUID(), p);
@@ -143,7 +174,10 @@ public void setup() {
143174

144175
public void draw() {
145176
background(0);
146-
177+
if (this.isServer) {
178+
text("Deaths: " + deathPoints , 110, 40);
179+
}
180+
147181
this.renderAll(objects);
148182

149183

@@ -171,10 +205,7 @@ private void renderAll(CopyOnWriteArrayList<GameObj> objects) {
171205
}
172206

173207
public void render(GameObj obj) {
174-
if (obj.getType().equals("boundary")) {
175-
rect((float) obj.getPy().getBounds2D().getX(), (float) obj.getPy().getBounds2D().getY(),
176-
(float) obj.getPy().getBounds2D().getWidth(), (float) obj.getPy().getBounds2D().getHeight());
177-
} else {
208+
if (obj.getRend() != null) {
178209
shape(obj.getRend().getShape(), obj.getPy().getLocation().x, obj.getPy().getLocation().y);
179210
}
180211
}

rectangles/engine/Renderable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Renderable(PApplet inst, int[] color, int type, float width, float height
1414
this.inst = inst;
1515
this.color = color;
1616
this.type = type;
17-
this.shape = inst.createShape(PConstants.RECT, 0, 0, width, height);
17+
this.shape = inst.createShape(type, 0, 0, width, height);
1818
this.shape.setFill(inst.color(color[0], color[1], color[2]));
1919
this.shape.setStroke(false);
2020
}

rectangles/engine/Spawn.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package engine;
2+
3+
import processing.core.PApplet;
4+
import processing.core.PShape;
5+
6+
public class Spawn extends GameObj {
7+
8+
public Spawn(PApplet inst, float x, float y) {
9+
super(0, 0, 0, x, y, false, false);
10+
}
11+
12+
@Override
13+
public String getType() {
14+
// TODO Auto-generated method stub
15+
return "spawn";
16+
}
17+
18+
@Override
19+
public String toSerial() {
20+
String serial = "{"
21+
+ "x:" + this.getPy().getLocation().x + ","
22+
+ "y:" + this.getPy().getLocation().y + ","
23+
+ "}";
24+
return serial;
25+
}
26+
27+
public static Spawn deSerial(PApplet inst, String serial) {
28+
float x = 0;
29+
float y = 0;
30+
31+
serial = serial.replace("{", "").replace("}", "");
32+
String[] data = serial.split(",");
33+
34+
for (String d : data) {
35+
String[] subData = d.split(":");
36+
String key = subData[0];
37+
String value = subData[1];
38+
39+
switch(key) {
40+
case("x"):
41+
x = Float.parseFloat(value);
42+
break;
43+
case("y"):
44+
y = Float.parseFloat(value);
45+
break;
46+
default:
47+
break;
48+
}
49+
}
50+
Spawn res = new Spawn(inst, x, y);
51+
return res;
52+
}
53+
}

rectangles/networking/Client.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ public void run() {
121121
public synchronized void stop() {
122122
this.isStopped = true;
123123
try {
124+
Packet p = new Packet(Packet.PACKET_DESTROY, Rectangles.player);
125+
synchronized (this.output) {
126+
try {
127+
//System.out.println("Sent: " + p.getSerialData());
128+
this.output.writeUTF(p.getSerialData());
129+
} catch (SocketException e) {
130+
// Ignore client has just disconnected
131+
} catch (IOException e) {
132+
System.out.println("Error writing to socket: " + this.socket.toString());
133+
e.printStackTrace();
134+
}
135+
}
124136
this.socket.close();
125137
} catch (IOException e) {
126138
throw new RuntimeException("Error closing client socket", e);
@@ -154,7 +166,7 @@ public ClientRead(String recv, Client client) {
154166
@Override
155167
public void run() {
156168
// Just force processing
157-
this.p = new Packet(this.client.inst, this.recv);
169+
this.p = new Packet(this.client.inst, this.recv);
158170
}
159171
}
160172

rectangles/networking/Packet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import java.util.UUID;
55

66
import engine.Boundary;
7+
import engine.DeathZone;
78
import engine.GameObj;
89
import engine.Platform;
910
import engine.Player;
1011
import engine.Rectangles;
12+
import engine.Spawn;
1113
import processing.core.PApplet;
1214
import processing.core.PConstants;
1315

@@ -192,6 +194,10 @@ public Packet(PApplet inst, String serial) {
192194
case ("boundary"):
193195
this.obj = Boundary.deSerial(objectSerial);
194196
break;
197+
case("spawn"):
198+
this.obj = Spawn.deSerial(inst, objectSerial);
199+
case("death-zone"):
200+
this.obj = DeathZone.deSerial(inst, objectSerial);
195201
default:
196202
break;
197203
}

rectangles/networking/Server.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import engine.GameObj;
1313
import engine.Player;
1414
import engine.Rectangles;
15+
import engine.Spawn;
1516
import processing.core.PApplet;
1617

1718
// From http://tutorials.jenkov.com/java-multithreaded-servers/thread-pooled-server.html
@@ -54,12 +55,12 @@ public void run() {
5455
}
5556
throw new RuntimeException("Error accepting client connection" + e);
5657
}
57-
Player playerCopy = this.localClient.getPlayer();
58-
Random r = new Random();
59-
playerCopy.getRend().getShape().setFill(color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
58+
Random r = Rectangles.generator;
6059

61-
Client client = new Client(this.inst, clientSocket, this.threadPool, new Player(inst, playerCopy.getDim(),
62-
playerCopy.getPy().getLocation().x, playerCopy.getPy().getLocation().y));
60+
Spawn s = Rectangles.spawnPoints[r.nextInt(2)];
61+
62+
Client client = new Client(this.inst, clientSocket, this.threadPool, new Player(inst, Rectangles.player.getDim(),
63+
s.getPy().getLocation().x, s.getPy().getLocation().y));
6364

6465
for (GameObj obj : Rectangles.objects) {
6566
Packet p = new Packet(Packet.PACKET_CREATE, obj);

0 commit comments

Comments
 (0)