33import java .io .IOException ;
44import java .net .Socket ;
55import java .util .ArrayList ;
6+ import java .util .UUID ;
7+ import java .util .concurrent .ConcurrentHashMap ;
68import java .util .concurrent .CopyOnWriteArrayList ;
79import java .util .concurrent .ExecutorService ;
810import java .util .concurrent .Executors ;
911
1012import networking .Client ;
13+ import networking .Packet ;
1114import networking .Server ;
1215import processing .core .PApplet ;
1316import 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