Skip to content

Commit 9896c35

Browse files
committed
Platfroms
1 parent 2d36b32 commit 9896c35

6 files changed

Lines changed: 135 additions & 54 deletions

File tree

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package engine;
2+
13
import processing.core.PShape;
24

35

@@ -11,9 +13,11 @@ public class GameObj extends PApplet {
1113
private boolean isFloor;
1214

1315
public GameObj(float objWidth, float objHeight, float mass, float x, float y, PShape shape,
14-
boolean isFloor) {
16+
boolean isFloor, boolean isGrav) {
1517
this.isFloor = isFloor;
16-
this.py = new Physics(x, y, objWidth, objHeight, mass, 20);
18+
this.objHeight = height;
19+
this.objWidth = width;
20+
this.py = new Physics(x, y, objWidth, objHeight, mass, 20, isGrav);
1721
try {
1822
this.shape = shape;
1923
} catch (NullPointerException e) {
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
package engine;
12
import java.awt.Rectangle;
23
import java.awt.Shape;
34
import java.awt.geom.AffineTransform;
45
import java.awt.geom.PathIterator;
56
import java.awt.geom.Point2D;
67
import java.awt.geom.Rectangle2D;
78
import java.util.ArrayList;
9+
import java.util.concurrent.CopyOnWriteArrayList;
810

911
import processing.core.PApplet;
1012
import processing.core.PVector;
@@ -20,18 +22,24 @@ public class Physics extends PApplet implements Shape {
2022
private float objWidth;
2123
private float objHeight;
2224
private float mass;
25+
private boolean isGrav;
2326

24-
public Physics(float x, float y, float objWidth, float objHeight, float mass, float topSpeed) {
27+
public Physics(float x, float y, float objWidth, float objHeight, float mass, float topSpeed, boolean isGrav) {
28+
this.isGrav = isGrav;
2529
this.location = new PVector(x, y);
2630
this.velocity = new PVector(0,0);
27-
this.acceleration = new PVector(0, GRAV);
31+
if (this.isGrav) {
32+
this.acceleration = new PVector(0, GRAV);
33+
} else {
34+
this.acceleration = new PVector(0, 0);
35+
}
2836
this.topSpeed = topSpeed;
2937
this.objWidth = objWidth;
3038
this.objHeight = objHeight;
3139
this.mass = mass;
3240
}
3341

34-
public PVector update(ArrayList<GameObj> objects) {
42+
public PVector update(CopyOnWriteArrayList<GameObj> objects) {
3543
//this.acceleration.setMag(0.2);
3644

3745
for (GameObj obj : objects) {
@@ -61,6 +69,10 @@ public void applyForce(PVector force) {
6169
PVector f = PVector.div(force, this.mass);
6270
acceleration.add(f);
6371
}
72+
73+
public float getMass() {
74+
return this.mass;
75+
}
6476

6577
public PVector getAcceleration() {
6678
return acceleration;

rectangles/engine/Platform.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package engine;
2+
3+
import processing.core.PShape;
4+
5+
public class Platform extends GameObj {
6+
7+
public Platform(PShape shape, boolean movable, float x, float y) {
8+
super(shape.getWidth(), shape.getHeight(), 0, x, y, shape, false, false);
9+
}
10+
11+
}
Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1+
package engine;
12

23
import java.io.IOException;
34
import java.net.Socket;
45
import java.util.ArrayList;
6+
import java.util.concurrent.CopyOnWriteArrayList;
57
import java.util.concurrent.ExecutorService;
68
import java.util.concurrent.Executors;
79

810
import networking.Client;
911
import networking.Server;
1012
import processing.core.PApplet;
1113
import processing.core.PShape;
14+
import processing.core.PVector;
15+
16+
1217

1318
public class Rectangles extends PApplet {
1419

20+
public static final int NUM_THREADS = 5;
21+
1522
private boolean isServer;
1623
private Server server;
1724
private Client localClient;
1825
private GameObj floor;
1926
private GameObj ceiling;
2027
private GameObj leftWall;
2128
private GameObj rightWall;
22-
private GameObj square;
23-
private GameObj rectangle;
24-
25-
private ExecutorService threadPool = Executors.newFixedThreadPool(5);
26-
private ArrayList<GameObj> objects = new ArrayList<GameObj>();
29+
private GameObj player;
2730

31+
private ExecutorService threadPool = Executors.newFixedThreadPool(NUM_THREADS);
32+
private CopyOnWriteArrayList<GameObj> objects = new CopyOnWriteArrayList<GameObj>();
33+
private CopyOnWriteArrayList<GameObj> movObjects = new CopyOnWriteArrayList<GameObj>();
34+
2835
public Rectangles(boolean isServer) {
2936
this.isServer = isServer;
3037
System.out.println("Server: " + this.isServer);
@@ -38,45 +45,72 @@ public void setup() {
3845
background(0);
3946
frameRate(60);
4047
textSize(32);
41-
4248

43-
this.floor = new GameObj(width, (float) 100, 0, 0, height, null, true);
44-
this.ceiling = new GameObj(width, (float) 100, 0, 0, -100, null, false);
45-
this.leftWall = new GameObj((float) 100, height, 0, -100, 0, null, false);
46-
this.rightWall = new GameObj((float) 100, height, width, width, 0, null, false);
49+
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);
4755

4856
this.objects.add(this.floor);
4957
this.objects.add(this.ceiling);
5058
this.objects.add(this.leftWall);
5159
this.objects.add(this.rightWall);
5260

53-
// Place square and rectangle in bottom corners of screen
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);
65+
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+
}
77+
78+
Platform static_1 = new Platform(platformStatic, false, width - platformStatic.getWidth(), 100);
79+
Platform static_2 = new Platform(platformStatic, false, platformStatic.getWidth(), 100);
80+
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);
86+
87+
movPlatforms.add(mov_1);
88+
movPlatforms.add(mov_2);
89+
90+
this.objects.addAll(staticPlatforms);
91+
this.objects.addAll(movPlatforms);
92+
93+
this.movObjects.addAll(movPlatforms);
94+
95+
96+
// Player
5497
float sqrDim = 50;
55-
float rectWidth = 100;
56-
float rectHeight = 50;
5798
PShape sqr = createShape(RECT, 0, 0, sqrDim, sqrDim);
5899
sqr.setFill(color(random(255), random(255), random(255)));
59100
sqr.setStroke(false);
60-
61-
PShape rect = createShape(RECT, 0, 0, rectWidth, rectHeight);
62-
rect.setFill(color(random(255), random(255), random(255)));
63-
rect.setStroke(false);
64-
65-
this.square = new GameObj(sqrDim, sqrDim, 0, height - sqrDim - 2, 1, sqr, false);
66-
this.rectangle = new GameObj(rectWidth, rectHeight, 2, width - rectWidth, height - rectHeight, rect, false);
67-
68-
// TODO: This will need to be reworked for server-client
69-
this.objects.add(this.rectangle);
101+
this.player = new GameObj(sqrDim, sqrDim, 0, height - sqrDim - 2, 1, sqr, false, true);
102+
103+
this.movObjects.add(this.player);
70104

71105

72106
// Setup Server
73107
if (this.isServer) {
74-
this.server = new Server(9200, this.threadPool);
108+
this.server = new Server(9200, this.threadPool, this.player);
75109
this.localClient = this.server.getLocalClient();
76110
new Thread(this.server).start();
77111
} else {
78112
try {
79-
this.localClient = new Client(new Socket("127.0.0.1", 9200), null, threadPool);
113+
this.localClient = new Client(new Socket("127.0.0.1", 9200), this.threadPool, this.player);
80114
} catch (IOException e) {
81115
System.out.println("Error opening local client socket");
82116
e.printStackTrace();
@@ -100,13 +134,13 @@ public void draw() {
100134
(float) this.rightWall.getPy().getBounds2D().getWidth(), (float) this.rightWall.getPy().getBounds2D().getHeight());
101135

102136
// Update physics
103-
this.square.getPy().update(objects);
104-
137+
for (GameObj obj : movObjects) {
138+
obj.getPy().update(this.objects);
139+
}
105140
// Render
106-
shape(this.square.getShape(), this.square.getPy().getLocation().x,
107-
this.square.getPy().getLocation().y);
108-
shape(this.rectangle.getShape(), this.rectangle.getPy().getLocation().x,
109-
this.rectangle.getPy().getLocation().y);
141+
shape(this.localClient.getPlayer().getShape(), this.localClient.getPlayer().getPy().getLocation().x,
142+
this.localClient.getPlayer().getPy().getLocation().y);
143+
110144

111145
// Only run update to clients 3fps?
112146
if (this.isServer && (frameCount % 20 == 0)) {
@@ -127,14 +161,14 @@ public void dispose() {
127161
public void keyPressed() {
128162
if (key == CODED) {
129163
if (keyCode == LEFT) {
130-
this.square.getPy().setAccelerationX(-5);
164+
this.localClient.getPlayer().getPy().setAccelerationX(-5);
131165
}
132166
if (keyCode == RIGHT) {
133-
this.square.getPy().setAccelerationX(5);
167+
this.localClient.getPlayer().getPy().setAccelerationX(5);
134168
}
135169
}
136170
if (key == ' ') {
137-
this.square.getPy().setAccelerationY(-20);
171+
this.localClient.getPlayer().getPy().setAccelerationY(-20);
138172
}
139173
if (key == 'q') {
140174
this.localClient.iterNumIter();

rectangles/networking/Client.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@
66
import java.io.IOException;
77
import java.net.Socket;
88
import java.net.SocketException;
9+
import java.util.concurrent.CopyOnWriteArrayList;
910
import java.util.concurrent.ExecutorService;
11+
import engine.GameObj;
1012

1113
public class Client implements Runnable {
1214
private Socket socket;
1315
private DataInputStream input;
1416
private DataOutputStream output;
1517
private boolean isStopped;
1618
private ExecutorService threadPool;
17-
private Client trackClient;
18-
19+
private CopyOnWriteArrayList<GameObj> state;
20+
private GameObj player;
21+
1922
private int numIter = 0;
2023

21-
public Client(ExecutorService threadPool) {
24+
public Client(ExecutorService threadPool, GameObj player) {
2225
this.threadPool = threadPool;
23-
this.trackClient = this;
26+
this.player = player;
27+
this.state.add(this.player);
2428
}
2529

26-
public Client(Socket s, Client trackClient, ExecutorService threadPool) {
30+
public Client(Socket s, ExecutorService threadPool, GameObj player) {
2731
this.socket = s;
28-
if (trackClient != null) {
29-
this.trackClient = trackClient;
30-
} else {
31-
this.trackClient = this;
32-
}
32+
this.player = player;
33+
this.state = new CopyOnWriteArrayList<GameObj>();
3334
this.threadPool = threadPool;
3435
try {
3536
this.input = new DataInputStream(this.getSocket().getInputStream());
@@ -53,6 +54,10 @@ public int getNumIter() {
5354
return this.numIter;
5455
}
5556

57+
public GameObj getPlayer() {
58+
return this.player;
59+
}
60+
5661
public synchronized void setNumIter(int n) {
5762
this.numIter = n;
5863
}
@@ -61,6 +66,10 @@ public synchronized void iterNumIter() {
6166
this.numIter++;
6267
}
6368

69+
public synchronized CopyOnWriteArrayList<GameObj> getState() {
70+
return this.state;
71+
}
72+
6473
@Override
6574
/*
6675
* Listening socket thread for each client (non-Javadoc) ExecutorService
@@ -74,7 +83,7 @@ public void run() {
7483
// TODO: Just keeping track of a number for now
7584
synchronized (this.input) {
7685
try {
77-
this.threadPool.execute(new ClientRead(this.input.readInt(), this.trackClient));
86+
this.threadPool.execute(new ClientRead(this.input.readInt(), this));
7887
} catch (EOFException | SocketException e) {
7988
// Ignore, client has just disconnected
8089
}

rectangles/networking/Server.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
import java.io.IOException;
33
import java.net.ServerSocket;
44
import java.net.Socket;
5+
import java.util.Random;
56
import java.util.concurrent.CopyOnWriteArrayList;
67
import java.util.concurrent.ExecutorService;
78

9+
import engine.GameObj;
10+
import processing.core.PApplet;
11+
812
// From http://tutorials.jenkov.com/java-multithreaded-servers/thread-pooled-server.html
913

10-
public class Server implements Runnable {
14+
public class Server extends PApplet implements Runnable {
1115

1216
protected ServerSocket serverSocket;
1317
protected Thread runningThread;
@@ -18,11 +22,11 @@ public class Server implements Runnable {
1822
protected boolean isStopped = false;
1923
private ExecutorService threadPool;
2024

21-
public Server(int port, ExecutorService threadPool) {
25+
public Server(int port, ExecutorService threadPool, GameObj player) {
2226
this.serverPort = port;
2327
this.threadPool = threadPool;
2428
this.clients = new CopyOnWriteArrayList<Client>();
25-
this.localClient = new Client(threadPool);
29+
this.localClient = new Client(threadPool, player);
2630
}
2731

2832
@Override
@@ -41,7 +45,14 @@ public void run() {
4145
}
4246
throw new RuntimeException("Error accepting client connection" + e);
4347
}
44-
Client client = new Client(clientSocket, this.localClient, this.threadPool);
48+
GameObj playerCopy = this.localClient.getPlayer();
49+
Random r = new Random();
50+
playerCopy.getShape().setFill(color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
51+
52+
Client client = new Client(clientSocket, this.threadPool, new GameObj(playerCopy.getObjWidth(),
53+
playerCopy.getObjHeight(), playerCopy.getPy().getMass(), playerCopy.getPy().getLocation().x,
54+
playerCopy.getPy().getLocation().y, playerCopy.getShape(), false, false));
55+
4556
synchronized (this.clients) {
4657
this.clients.add(client);
4758
}

0 commit comments

Comments
 (0)