1+ package engine ;
12
23import java .io .IOException ;
34import java .net .Socket ;
45import java .util .ArrayList ;
6+ import java .util .concurrent .CopyOnWriteArrayList ;
57import java .util .concurrent .ExecutorService ;
68import java .util .concurrent .Executors ;
79
810import networking .Client ;
911import networking .Server ;
1012import processing .core .PApplet ;
1113import processing .core .PShape ;
14+ import processing .core .PVector ;
15+
16+
1217
1318public 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 ();
0 commit comments