55import java .awt .geom .PathIterator ;
66import java .awt .geom .Point2D ;
77import java .awt .geom .Rectangle2D ;
8+ import java .io .File ;
9+ import java .io .FileNotFoundException ;
10+ import java .io .FileReader ;
811import java .util .ArrayList ;
912import java .util .HashMap ;
1013import java .util .concurrent .CopyOnWriteArrayList ;
1114
15+ import org .w3c .dom .css .Rect ;
16+
1217import engine .events .Event ;
18+ import engine .scripting .ScriptManager ;
1319import processing .core .PApplet ;
1420import processing .core .PVector ;
1521
@@ -41,56 +47,25 @@ public Physics(float x, float y, float objWidth, float objHeight, float mass, fl
4147 this .mass = mass ;
4248 }
4349
44- public void update (GameObj caller , CopyOnWriteArrayList <GameObj > objects ) {
45- //this.acceleration.setMag(0.2);
46-
47- GameObj collidedWith = null ;
48-
49- for (GameObj obj : objects ) {
50- if (this .intersects (obj .getPy ().getBounds2D ()) && !obj .getUUID ().equals (caller .getUUID ()) && !obj .getType ().equals ("player" )) {
51- collidedWith = obj ;
52- // TODO: Need break here?
53- break ;
54- }
55- }
56-
57- /**
58- * TODO: Implement here so that if collision occurs, no movement below, let collision event handle
59- * raising a new movement event
60- *
61- * else, just raise movement event here if object moved
62- *
63- * In replays, just worry about movement events! :D
64- */
65-
66- if (collidedWith != null ) {
67- HashMap <String , Object > data = new HashMap <>();
68- data .put ("caller" , caller .getUUID ());
69- data .put ("collidedWith" , collidedWith .getUUID ());
70- // TODO: Could just make the collision event also be movement?
71- Event e = new Event (Event .EVENT_COLLISION , Rectangles .globalTimeline .getCurrentTime (), data );
72- Rectangles .eventManager .raiseEvent (e );
73- } else {
74- this .velocity .add (this .acceleration );
75- this .velocity .limit (this .topSpeed );
76- if (this .velocity .mag () > 0 ) {
77- PVector newLoc = new PVector (this .location .x , this .location .y );
78- newLoc .add (this .velocity );
79- HashMap <String , Object > data = new HashMap <>();
80- data .put ("caller" , caller .getUUID ());
81- data .put ("x" , newLoc .x );
82- data .put ("y" , newLoc .y );
83- Event e = new Event (Event .EVENT_MOVEMENT , Rectangles .globalTimeline .getCurrentTime (), data );
84- Rectangles .eventManager .raiseEvent (e );
85- // Actually move
86- //this.location.add(this.velocity);
50+ public void update (GameObj caller ) {
51+ String file ;
52+ FileReader script = null ;
53+ try {
54+ file = new File ("scripts/" + Rectangles .game + "/" + caller .getType () + "/physics.js" ).getAbsolutePath ();
55+ script = new FileReader (file );
56+ } catch (FileNotFoundException e1 ) {
57+ file = new File ("scripts/" + Rectangles .game + "/physics.js" ).getAbsolutePath ();
58+ try {
59+ script = new FileReader (file );
60+ } catch (FileNotFoundException e2 ) {
61+ e1 .printStackTrace ();
8762 }
8863 }
89-
90- // Reset acceleration?
91- if ( this . isGrav ) {
92- this . acceleration = new PVector ( 0 , GRAV );
93- }
64+ ScriptManager . bindArgument ( "objects" , Rectangles . objects );
65+ ScriptManager . bindArgument ( "GRAV" , GRAV );
66+ ScriptManager . bindArgument ( "globalTimeline" , Rectangles . globalTimeline );
67+ ScriptManager . loadScript ( script );
68+ ScriptManager . executeScript ( "update" , this , caller );
9469 }
9570
9671 // From processing tutorial forces with vectors
@@ -114,11 +89,15 @@ public void setAcceleration(PVector acceleration) {
11489 public void setAccelerationX (float x ) {
11590 this .acceleration .x = x ;
11691 }
117-
92+
11893 public void setAccelerationY (float y ) {
11994 this .acceleration .y = (y + GRAV );
12095 }
12196
97+ public void resetAcceleration () {
98+ this .acceleration = new PVector (0 , GRAV );
99+ }
100+
122101 public PVector getLocation () {
123102 return location ;
124103 }
@@ -203,4 +182,21 @@ public boolean intersects(double x, double y, double w, double h) {
203182 // TODO Auto-generated method stub
204183 return false ;
205184 }
185+
186+ /*
187+ * Helper function for copying location in script
188+ */
189+ public PVector copyLoc () {
190+ return new PVector (this .getLocation ().x , this .getLocation ().y );
191+ }
192+ /*
193+ * Helper function for getting new location in script
194+ */
195+ public PVector newLoc (float x , float y ) {
196+ return new PVector (x , y );
197+ }
198+
199+ public boolean isGrav () {
200+ return isGrav ;
201+ }
206202}
0 commit comments