Skip to content
Browse files

ok

  • Loading branch information...
1 parent 0cd4bfd commit 34561ea4b367c70e764b2353c5a76e087e36b3a4 @daveheitzman committed Feb 18, 2011
Showing with 4,859 additions and 0 deletions.
  1. BIN bin/com/aptifuge/thingworld/Collision$CollidedThingSet.class
  2. BIN bin/com/aptifuge/thingworld/Collision.class
  3. BIN bin/com/aptifuge/thingworld/Constance.class
  4. BIN bin/com/aptifuge/thingworld/Fourint.class
  5. BIN bin/com/aptifuge/thingworld/KB.class
  6. BIN bin/com/aptifuge/thingworld/Line.class
  7. BIN bin/com/aptifuge/thingworld/Main.class
  8. BIN bin/com/aptifuge/thingworld/MainMenuHandler.class
  9. BIN bin/com/aptifuge/thingworld/MainRenderingPanel.class
  10. BIN bin/com/aptifuge/thingworld/PopupMenuHandler.class
  11. BIN bin/com/aptifuge/thingworld/ShapeControls$1.class
  12. BIN bin/com/aptifuge/thingworld/ShapeControls$2.class
  13. BIN bin/com/aptifuge/thingworld/ShapeControls$3.class
  14. BIN bin/com/aptifuge/thingworld/ShapeControls$4.class
  15. BIN bin/com/aptifuge/thingworld/ShapeControls$5.class
  16. BIN bin/com/aptifuge/thingworld/ShapeControls.class
  17. BIN bin/com/aptifuge/thingworld/ThMan.class
  18. BIN bin/com/aptifuge/thingworld/ThMissile.class
  19. BIN bin/com/aptifuge/thingworld/ThSpoke.class
  20. BIN bin/com/aptifuge/thingworld/Thing.class
  21. BIN bin/com/aptifuge/thingworld/ThingWorldControlPanel$1.class
  22. BIN bin/com/aptifuge/thingworld/ThingWorldControlPanel$js_gravlistener.class
  23. BIN bin/com/aptifuge/thingworld/ThingWorldControlPanel.class
  24. BIN bin/com/aptifuge/thingworld/ThingWorldController.class
  25. BIN bin/com/aptifuge/thingworld/ThingWorldModel.class
  26. BIN bin/com/aptifuge/thingworld/ThingWorldView.class
  27. BIN bin/com/aptifuge/thingworld/Trig.class
  28. BIN bin/com/aptifuge/thingworld/ViewSquare.class
  29. BIN bin/com/aptifuge/thingworld/testspoke.class
  30. BIN bin/ec/util/MersenneTwisterFast.class
  31. +280 −0 src/com/aptifuge/thingworld/Collision.java
  32. +101 −0 src/com/aptifuge/thingworld/Constance.java
  33. +37 −0 src/com/aptifuge/thingworld/Fourint.java
  34. +115 −0 src/com/aptifuge/thingworld/KB.java
  35. +98 −0 src/com/aptifuge/thingworld/Line.java
  36. +40 −0 src/com/aptifuge/thingworld/Main.java
  37. +46 −0 src/com/aptifuge/thingworld/MainMenuHandler.java
  38. +37 −0 src/com/aptifuge/thingworld/MainRenderingPanel.java
  39. +196 −0 src/com/aptifuge/thingworld/PopupMenuHandler.java
  40. +141 −0 src/com/aptifuge/thingworld/ShapeControls.java
  41. +317 −0 src/com/aptifuge/thingworld/ThMan.java
  42. +61 −0 src/com/aptifuge/thingworld/ThMissile.java
  43. +55 −0 src/com/aptifuge/thingworld/ThSpoke.java
  44. +545 −0 src/com/aptifuge/thingworld/Thing.java
  45. +146 −0 src/com/aptifuge/thingworld/ThingWorldControlPanel.java
  46. +368 −0 src/com/aptifuge/thingworld/ThingWorldController.java
  47. +309 −0 src/com/aptifuge/thingworld/ThingWorldModel.java
  48. +421 −0 src/com/aptifuge/thingworld/ThingWorldView.java
  49. +56 −0 src/com/aptifuge/thingworld/Trig.java
  50. +102 −0 src/com/aptifuge/thingworld/ViewSquare.java
  51. +42 −0 src/com/aptifuge/thingworld/testspoke.java
  52. +1,346 −0 src/ec/util/MersenneTwisterFast.java
View
BIN bin/com/aptifuge/thingworld/Collision$CollidedThingSet.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/Collision.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/Constance.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/Fourint.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/KB.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/Line.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/Main.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/MainMenuHandler.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/MainRenderingPanel.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/PopupMenuHandler.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ShapeControls$1.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ShapeControls$2.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ShapeControls$3.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ShapeControls$4.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ShapeControls$5.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ShapeControls.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ThMan.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ThMissile.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ThSpoke.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/Thing.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ThingWorldControlPanel$1.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ThingWorldControlPanel$js_gravlistener.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ThingWorldControlPanel.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ThingWorldController.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ThingWorldModel.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ThingWorldView.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/Trig.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/ViewSquare.class
Binary file not shown.
View
BIN bin/com/aptifuge/thingworld/testspoke.class
Binary file not shown.
View
BIN bin/ec/util/MersenneTwisterFast.class
Binary file not shown.
View
280 src/com/aptifuge/thingworld/Collision.java
@@ -0,0 +1,280 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+package com.aptifuge.thingworld;
+
+import java.awt.Color;
+import java.awt.Point;
+import java.util.ArrayList;
+
+
+public class Collision implements Constance{
+ public boolean simplecollision=false; //just measures distance of max radiuses
+
+ ThingWorldModel tw;
+ public class CollidedThingSet {
+ //these variables can just be passed to doCollision(..) as is
+ //two Things and some related variables.
+ //adds these Things to the queue so that collisions can be performed on them
+ //t1,t1 are the two Things colliding
+ //d1 and d2 are the respective distances between the center of the thing and the point on the line that connects
+ //the ends of the two spokes that are facing the collision where that line intersects the line that
+ //intersects the centers of both objects .
+ //dist is the distance between the two objects' centers.
+ public CollidedThingSet(Thing t1, Thing t2,double d1, double d2, double dist){
+ this.t1=t1;this.t2=t2;this.d1=d1;this.d2=d2;this.dist=dist;
+ }
+ public Thing t1,t2;
+ public double d1,d2,dist;
+
+ }
+
+ public ArrayList<CollidedThingSet> collisionqueue = new ArrayList<CollidedThingSet>(0);
+
+ public Collision(ThingWorldModel tw){
+ this.tw=tw;
+ }
+
+ public Collision () {}
+
+ public Point linesintersectpoint(Line l1,Line l2) {
+ Point p = new Point();
+ // Given two lines, it returns the point at which they meet. If this point is
+ //beyond the extent of either line, it p is set to null;
+
+ double ymeet,xmeet,m1,m2,b1,b2,slp1 = l1.rot, slp2=l2.rot;
+
+ m1 = Math.tan(slp1);
+ m2 = Math.tan(slp2);
+
+ b1=l1.y1-m1*l1.x1;
+ b2=l2.y1-m2*l2.x1;
+ xmeet=(b2-b1)/(m1-m2);
+ ymeet = m1*xmeet+b1;
+ if ( (xmeet > l1.lbound)&&(xmeet < l1.rbound)
+ &&(xmeet > l2.lbound)&&(xmeet < l2.rbound)
+ &&(ymeet > l1.topbound)&&(ymeet < l1.botbound)
+ &&(ymeet > l2.topbound)&&(ymeet < l2.botbound))
+ {
+ p.x =(int)xmeet; p.y=(int)ymeet;
+ } else
+ {
+ p=null;
+ }
+ return p;
+ }
+
+ public boolean linesintersect(Line l1, Line l2){
+ //determines if two lines intersect. returns 0 if not. returns the x value of the crossover point if so.
+ //watch out for vertical and horizontal lines!
+ boolean collisiondetected=false;
+ double ymeet,xmeet,m1,m2,b1,b2,slp1 = l1.rot, slp2=l2.rot;
+
+ m1 = Math.tan(slp1);
+ m2 = Math.tan(slp2);
+
+//lines with similar slopes are slipping through
+// if (Math.rint((slp1%pi)*10)==Math.rint((slp2%pi)*10)) return true;
+
+ b1=l1.y1-m1*l1.x1;
+ b2=l2.y1-m2*l2.x1;
+ xmeet=(b2-b1)/(m1-m2);
+ ymeet = m1*xmeet+b1;
+ if ( (xmeet > l1.lbound)&&(xmeet < l1.rbound)
+ &&(xmeet > l2.lbound)&&(xmeet < l2.rbound)
+ &&(ymeet > l1.topbound)&&(ymeet < l1.botbound)
+ &&(ymeet > l2.topbound)&&(ymeet < l2.botbound)) {
+
+ collisiondetected=true;
+// tw.af.pauseresume();
+// System.out.println("Collision!");
+// System.out.println("x:"+xmeet+" y:"+ymeet);
+// System.out.println(Math.PI+" "+Math.PI/2);
+
+ /* System.out.println("m1 "+m1);
+ System.out.println("m2 "+m2);
+ System.out.println("b1 "+b1);
+ System.out.println("b2 "+b2);
+ System.out.println("lbound "+l1.lbound);
+ System.out.println("rbound "+l1.rbound);
+ System.out.println(xmeet);
+*/
+// tw.af.pauseresume();
+// System.out.println("collision: "+xmeet+","+ymeet);
+ }
+ return collisiondetected;
+ }
+
+ public void collisions(ArrayList<Thing> thingz, ThingWorldModel tw){
+ Thing t1,t2;
+ double t1t2ang,t2t1ang,mindist,dist;
+
+
+ //go through and analyze the Thingz for collisions
+ //the queue will become filled up with collided objects.
+ if (thingz.size()>1) {
+ for (int f4=0;f4 < (thingz.size()-1);f4++){
+ for (int f6=f4+1;f6<thingz.size();f6++){
+ t1=thingz.get(f4);
+ t2=thingz.get(f6);
+ mindist = t1.radmax + t2.radmax;
+ dist = Math.hypot(t1.ox-t2.ox, t1.oy-t2.oy);
+ t1t2ang = Trig.norm(Math.atan2(t2.oy-t1.oy, t2.ox-t1.ox));
+ t2t1ang = Trig.norm(Math.atan2(t1.oy-t2.oy, t1.ox-t2.ox));
+
+ if( (t1.hiatus <=0) && (t2.hiatus <= 0) && (mindist>dist*1.1) ){
+// if( (mindist>dist*1.1) ){
+ this.assessexactcollision(t1, t2);
+
+ double d1=getradiusbetweenspokes(t1,t1t2ang);
+ double d2=getradiusbetweenspokes(t2,t2t1ang);
+ if((d1+d2)>dist){
+ queuecollision(t1,t2,d1,d2,dist);
+ //docollision(t1,t2,d1,d2,dist);
+ System.out.println("collision level 1");
+ System.out.println("t1-t2: "+d1+", t2-t1: "+d2+
+ ". Distance from centers: "+dist);
+ }else {
+ assessexactcollision(t1,t2);
+
+ Line l1 = new Line( t1.colspoke0.getx2(),
+ t1.colspoke0.gety2(),
+ t1.colspoke1.getx2(),
+ t1.colspoke1.gety2());
+ Line l2 = new Line( t2.colspoke0.getx2(),
+ t2.colspoke0.gety2(),
+ t2.colspoke1.getx2(),
+ t2.colspoke1.gety2());
+
+ if (linesintersect(l1,l2))
+ {
+ queuecollision(t1,t2,d1,d2,dist);
+ //docollision(t1,t2,d1,d2,dist);
+ System.out.println("collision level 3");
+ }
+ }
+ } //end are both objects not on "hiatus"
+ } //end inner for loop
+ }// end main for loop
+ }// end if (thingz.size()>1)
+
+ //the collisionqueue will be completely emptied by the following routine
+ //First process collision queue -- these are objects already collided that need to
+ //have their travel angle and velocity adjusted
+ while (collisionqueue.size() > 0) {
+ CollidedThingSet ctp = collisionqueue.remove(collisionqueue. size()-1);
+ docollision(ctp.t1,ctp.t2,ctp.d1,ctp.d2,ctp.dist);
+
+ }
+
+ }//end collisions
+
+ public void queuecollision(Thing t1, Thing t2, double d1, double d2, double dist){
+ //adds these Things to the queue so that collisions can be performed on them
+ //t1,t1 are the two Things colliding
+ //d1 and d2 are the respective distances between the center of the thing and the point on the line that connects
+ //the ends of the two spokes that are facing the collision where that line intersects the line that
+ //intersects the centers of both objects .
+ //dist is the distance between the two objects' centers.
+
+ collisionqueue.add(new CollidedThingSet(t1,t2,d1,d2,dist));
+
+ }
+
+ public void assessexactcollision(Thing t1, Thing t2){
+ //note: this method will change t1 and t2 so that their fields colspoke0 and colspoke1
+ //will be set to contain the end points (x2,y2) of the two spokes that are the closest
+ //to the line connecting the centers of t1 and t2.
+
+ double t1t2ang; //angle from the center of t1 to the center of t2
+ double t2t1ang; //angle from center of t2 to the center of t1
+ t1t2ang = Math.atan2(t2.oy-t1.oy, t2.ox-t1.ox);
+ t2t1ang = Math.atan2(t1.oy-t2.oy, t1.ox-t2.ox);
+
+ t1.closest(t1t2ang); //this resets colspoke0 and colspoke1
+ t2.closest(t2t1ang); //this resets colspoke0 and colspoke1
+ }
+
+ public void docollision(Thing t1, Thing t2, double d1, double d2, double dist){
+ double t1t2ang, t2t1ang,t1newang,t2newang,t1newvelocity,t2newvelocity;
+ double temp = t1.getrealdirection();
+ double midang = temp + ( temp-t2.getrealdirection() / 2 );
+ int t1newposx,t1newposy,t2newposx,t2newposy;
+ t1t2ang = Math.atan2(t2.oy-t1.oy, t2.ox-t1.ox);
+ t2t1ang = Math.atan2(t1.oy-t2.oy, t1.ox-t2.ox);
+
+ t1newang = (t1t2ang+pi)-(t1.getrealdirection()-t1t2ang);
+ t2newang = (t2t1ang+pi)-(t2.getrealdirection()-t2t1ang);
+
+
+
+ t1newvelocity=t1.getvelocity();
+ t2newvelocity=t2.getvelocity();
+
+ t1.notifycollision(t2,t1newang,t1newvelocity);
+ t2.notifycollision(t1,t2newang,t2newvelocity);
+ }
+
+ public double getradiusbetweenspokes(Thing t1, double midang){
+ double rval, raddist,lendif,dc01,dc10;
+ double eda,fer,fed,red,nr,er,ed,fd,nd,rd,fde,fdr,rde,rda,ef;
+ ed=t1.colspoke0.len;
+ fd=t1.colspoke1.len;
+ ef=Math.hypot(t1.colspoke0.x2-t1.colspoke1.x2, t1.colspoke0.y2-t1.colspoke1.y2);
+ fed=Math.acos((fd*fd-ed*ed-ef*ef)/(-2*ed*ef));
+ fde = Trig.distanceclockwise(t1.colspoke0.ang, t1.colspoke1.ang);
+
+ if (fde < pidiv2)
+ {
+ //this is when the angle between the two spokes is < pi/2 (45 deg)
+ eda=Trig.distanceclockwise(t1.colspoke0.ang,midang)%(pi);
+ er=Math.abs(Math.sin(eda)*ed);
+ red=(pidiv2)-(eda%(pidiv2));
+ fer = Math.abs(fed-red);
+ nr=Math.abs(Math.tan(fer)*er);
+ rd=Math.abs(er/Math.tan(eda));
+ nd=rd+nr;
+ }
+ else
+ {
+ //this is when the angle between the two spokes is > 2pi
+ eda=Trig.distanceclockwise(t1.colspoke0.ang,midang)%(pi);
+ rd = Math.abs(ed*Math.sin(fed));
+ rde = pidiv2-fed;
+ rda = rde-eda;
+ nd = Math.abs(rd/Math.cos(rda));
+ }
+ rval = nd;
+// System.out.println("radius dist: shapenum "+t1.shapenum+",rad = "+eda+". Dist= "+nd+" t1.radmax="+t1.radmax);
+ return rval;
+ }
+
+}
View
101 src/com/aptifuge/thingworld/Constance.java
@@ -0,0 +1,101 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+package com.aptifuge.thingworld;
+
+import java.awt.Color;
+import java.awt.*;
+import ec.util.*;
+
+public interface Constance {
+ public static final int tickspersecond = 30;
+ public static final int maxspokes =20;
+ public static final double nanospertick = 1000000000/tickspersecond;
+
+ public static final int winx=1200,winy=800; //size of display window for game
+ public static final int framegutterx=3,frameguttery=27; //size of title bar and other unusable parts of the JFrame
+
+ public static final int WORLDSIZEX=1100,WORLDSIZEY=600; //size of the "world" of the game
+ public static final int worldpanelx=winx,worldpanely=winy;
+ public static final String mainwintitle = "Shapefun" ;
+ public static final int ovalwidth=10,ovalheight = 10;
+ public static final int KEY_UP=1, KEY_DOWN=3;
+ public static final int KEY_LEFT=2, KEY_RIGHT=4;
+ public static final int KEY_JUMP=5, KEY_ACT=6, KEY_HOOK=7;
+
+ //COLORS:
+ public static final Color colorbackground= new Color (0,40,64);
+ public static final Color polin = new Color (154,130,55);
+ public static final Color polout = new Color (15,15,15);
+ public static final Color triin = new Color (167,90,90);
+ public static final Color triout = new Color (230,230,230);
+ public static final Color squin = new Color (155,244,88);
+ public static final Color squout = new Color (230,230,230);
+ public static final Color missin = new Color (200,188,99);
+ public static final Color missout = new Color (230,230,230);
+ public static final Color worldbordout = new Color (0,0,0);
+ public static final Color worldbordin = new Color (188,210,205);
+
+
+ public static final double accelcap=2;
+ public static final double speedcap=2;
+ public static final double keylowthresh=60000000;//60 ms
+ public static final double keyhighthresh=1500000000;//1500 ms
+
+ public static final double horzlimit = 4;
+ public static final double vertuplimit = -8;
+ public static final double vertdownlimit = 12;
+ public static final double friction = 0.95; //this is multiplied directly to xt -- the x incrementer
+ public static final double yacceldecay=0.08;
+ public static final double xacceldecay=0.03;
+ public static final double gravity=0.02;//0.3; //this gets multiplied by acceleration every cycle
+ public static final double defaultmissilespeed=6;
+
+ //shape templates:
+ public static final int RANDOM=0;
+ public static final int POLYGON=1;
+ public static final int TRIANGLE = 2;
+ public static final int SQUARE = 3;
+ public static final int RECTANGLE = 4;
+ public static final int SPIKY = 5;
+ public static final int SHAPE_MISSILE = 32001;
+ public static final int missiletimetolive = 100;
+
+
+ //trigonometry stuff
+ public static final int piint = 3141;
+ public static final int pifac = 1000;
+ public static final double pi = 3.141592653589793;
+ public static final double twopi = 2.0*3.141592653589793;
+ public static final double pidiv2 = 1.5707963267948966;
+
+ public static final MersenneTwisterFast rand = new MersenneTwisterFast();
+
+}
View
37 src/com/aptifuge/thingworld/Fourint.java
@@ -0,0 +1,37 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+package com.aptifuge.thingworld;
+
+public class Fourint {
+ int x1,x2,y1,y2;
+ public Fourint(){};
+}
View
115 src/com/aptifuge/thingworld/KB.java
@@ -0,0 +1,115 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+package com.aptifuge.thingworld;
+
+import javax.swing.*;
+import java.awt.event.*;
+
+public class KB extends KeyAdapter implements Constance{
+ public static double keytime[]=new double[16];
+ //keytime returns the length of the time key has been held down, or if the key is up, it stores
+ //the length of the last time when it was held down.
+ public static boolean keydown[]=new boolean[16];
+ public static boolean jumpexpired=false; //it's when the jump button is held down but the jump
+ // has been executed already
+ //or when the jump has been executed already -- reset on
+ //next call in other words, if key is not pressed.
+ public static double jumpstarttime=0;
+
+ public KB(){
+
+ }
+
+ public static void keypressed(int kn){
+ if (kn == KEY_JUMP){
+ if (keydown[kn]){
+ keytime[kn]= System.nanoTime()-jumpstarttime;
+ }else{
+ keytime[kn]=0;
+ jumpstarttime=System.nanoTime();
+ jumpexpired=false;
+ keydown[kn]=true;
+ }
+ }else {
+ if (kn<=16){
+ if (!keydown[kn]) {
+ keydown[kn]=true;
+ keytime[kn]=System.nanoTime();
+ }
+// printkeystates();
+ }
+ }
+// System.out.println("Key "+kn+" keypressed.");
+ }
+ public static void keyreleased(int kn) {
+ if (kn == KEY_JUMP){
+ if (jumpexpired){
+ keytime[kn]=0;
+ jumpexpired=false;
+ }else{
+ keydown[kn]=false;
+ keytime[kn]=System.nanoTime()-keytime[kn];
+ }
+// printkeystates();
+ }else {
+ if (kn<=16) {
+ keydown[kn]=false;
+ keytime[kn]=System.nanoTime()-keytime[kn];
+ }
+// printkeystates();
+ }
+ // System.out.println(System.nanoTime()-keytime[kn]);
+ }
+ public static double getkeytime(int kn){
+ //returns the length in nanoseconds that a key has been held down
+ if (kn<=16) {
+/*
+ if (keydown[kn]) {
+ double time =System.nanoTime()-keytime[kn];
+ //keytime[kn]=0;
+ //keydown[kn]=false;
+ return time;
+ }else
+ {return 0.0;}
+*/
+ return keytime[kn];
+ }
+ else return 0.0;
+ }
+ public static void printkeystates(){
+ for (int t5 = 0;t5<16; t5++){
+// System.out.println(getkeytime(t5));
+ }
+ }
+ public static void makejumpexpired () {
+ jumpexpired=true;
+ }
+}
View
98 src/com/aptifuge/thingworld/Line.java
@@ -0,0 +1,98 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+package com.aptifuge.thingworld;
+
+import java.awt.Rectangle;
+import java.awt.Shape;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Line2D;
+import java.awt.geom.PathIterator;
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+
+public class Line implements Constance {
+ Trig trig=new Trig();
+ public int posx,posy; //xinc and yinc are how much the line moves each frame
+ public int x1,y1,x2,y2,lbound,rbound, botbound, topbound;
+ public double accumx,accumy,len;
+ public double rotinc=0.0;
+ public double rot=0.0; // eventually, hopefully in radians/sec. Negative is counter clockwise
+ public double xinc,yinc;
+ public long begintime,time1, time2;
+
+ public void update(){
+ rot = Trig.norm(rot + rotinc);
+ accumx=accumx+xinc;
+ accumy=accumy+yinc;
+ posx=(int)accumx;
+ posy=(int)accumy;
+// int xi=(int)(Trig.costable[(int)(rot*pifac)]*len/2);
+// int yi=(int)(Trig.sintable[(int)(rot*pifac)]*len/2);
+ int xi=(int)Math.round((Math.cos(rot)*len));
+ int yi=(int)Math.round((Math.sin(rot)*len));
+ x1=posx;
+ y1=posy;
+ x2=posx+xi;
+ y2=posy+yi;
+
+ lbound = x1 > x2 ? x2:x1;
+ rbound = x1 > x2 ? x1:x2;
+ botbound = y1 > y2 ? y1:y2;
+ topbound = y1 > y2 ? y2:y1;
+ }
+ public Line(){
+ //(Math.random()*0.6)-0.3
+ this(100,100,(int)(Math.random()*30+20),0,(Math.random()*0.6)-0.3,(Math.random()*4)-2,(Math.random()*4)-2);
+ }
+ public Line(int x1, int y1, int len, double rot, double rotinc, double xinc, double yinc){
+ begintime=System.nanoTime();
+ accumx=posx=x1;
+ accumy=posy=y1;
+ this.xinc=xinc;
+ this.yinc=yinc;
+ this.rot = rot;
+ this.len = len;
+ this.rotinc=rotinc;
+ this.update();
+ }
+ public Line(int x1,int y1,int x2, int y2){
+ begintime=System.nanoTime();
+ accumx=posx=x1;
+ accumy=posy=y1;
+ this.xinc=0;
+ this.yinc=0;
+ this.rot = Trig.norm(Math.atan2(y2-y1, x2-x1));
+ this.len = Math.hypot(x2-x1, y2-y1);
+ this.rotinc=0;
+ this.update();
+ }
+
+}
View
40 src/com/aptifuge/thingworld/Main.java
@@ -0,0 +1,40 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+package com.aptifuge.thingworld;
+
+public class Main {
+ public static void main(String[] args)
+ {
+ System.out.println("ThingWorld 0.3 !");
+ ThingWorldController twController = new ThingWorldController();
+ System.out.println("ThingWorld 0.3 ! -- terminated normally Main.main ");
+ }
+}
View
46 src/com/aptifuge/thingworld/MainMenuHandler.java
@@ -0,0 +1,46 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+package com.aptifuge.thingworld;
+
+import java.awt.*;
+import javax.swing.*;
+
+public class MainMenuHandler extends JMenu {
+ private JMenuItem jsubmenu1;
+ private JCheckBoxMenuItem jcbmi1;
+ private MenuItem mi1;
+
+ public MainMenuHandler(){
+
+ }
+
+
+}
View
37 src/com/aptifuge/thingworld/MainRenderingPanel.java
@@ -0,0 +1,37 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+package com.aptifuge.thingworld;
+
+import javax.swing.JPanel;
+
+public class MainRenderingPanel extends JPanel implements Constance {
+
+}
View
196 src/com/aptifuge/thingworld/PopupMenuHandler.java
@@ -0,0 +1,196 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+package com.aptifuge.thingworld;
+
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.MenuItem;
+import java.awt.PopupMenu;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.swing.event.PopupMenuEvent;
+import javax.swing.event.PopupMenuListener;
+import java.awt.MenuItem;
+import javax.swing.*;
+
+public class PopupMenuHandler extends JPopupMenu implements ActionListener, MouseListener,PopupMenuListener {
+ public ThingWorldController twC;
+ public ThingWorldView twV;
+ public ThingWorldModel twM;
+ private int numberofmenuitems = 7;
+ private JMenuItem popupmenuitems[] = new JMenuItem[numberofmenuitems];
+
+ public PopupMenuHandler(ThingWorldController twC, ThingWorldView twV,ThingWorldModel twM) {
+ this.twC = twC;
+ this.twV = twV;
+ this.twM = twM;
+ this.setOpaque(true);
+ for (int c4 = 0;c4<numberofmenuitems;c4++){
+ popupmenuitems[c4] = new JCheckBoxMenuItem();
+ popupmenuitems[c4].addActionListener(this);
+ }
+ popupmenuitems[0].setText("Numbers On");
+ popupmenuitems[0].setActionCommand("numbers");
+ popupmenuitems[0].setSelected(twV.isNumberson());
+ popupmenuitems[1].setText("Direction Flags On");
+ popupmenuitems[1].setActionCommand("directionflags");
+ popupmenuitems[1].setSelected(twV.isDirectionflags());
+ popupmenuitems[2].setText("Surround Box On");
+ popupmenuitems[2].setActionCommand("surroundbox");
+ popupmenuitems[2].setSelected(twV.isSurroundbox());
+ popupmenuitems[3].setText("Show Collision Span");
+ popupmenuitems[3].setActionCommand("showcollisionspan");
+ popupmenuitems[3].setSelected(twV.isShowcollisionspan());
+ popupmenuitems[4].setText("Status Dump");
+ popupmenuitems[4].setActionCommand("statusdump");
+ popupmenuitems[4].setSelected(twV.isStatusdump());
+ popupmenuitems[5].setText("Collisions On");
+ popupmenuitems[5].setActionCommand("collisions");
+ popupmenuitems[5].setSelected(twM.isCollisionson());
+
+ popupmenuitems[6] = new JMenuItem();
+ popupmenuitems[6].setText("Follow This Thing");
+ popupmenuitems[6].setActionCommand("followthis");
+ popupmenuitems[6].addActionListener(this);
+
+ for (int c4 = 0;c4<numberofmenuitems;c4++){
+ this.add(popupmenuitems[c4]);
+ }
+
+ this.setInvoker(twC);
+ this.pack();
+ this.revalidate();
+ }
+
+
+ @Override
+ public void popupMenuCanceled(PopupMenuEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+
+ @Override
+ public void actionPerformed(ActionEvent arg0) {
+ // TODO Auto-generated method stub
+ System.out.println("Popupmenu clicked; Action command == "+arg0.getActionCommand());
+ if(arg0.getActionCommand() == "numbers") {
+ twV.setNumberson(!twV.isNumberson() );
+ } else
+ if(arg0.getActionCommand() == "directionflags") {
+ twV.setDirectionflags(!twV.isDirectionflags() );
+ } else
+ if(arg0.getActionCommand() == "surroundbox") {
+ twV.setSurroundbox(!twV.isSurroundbox() );
+ } else
+ if(arg0.getActionCommand() == "showcollisionspan") {
+ twV.setShowcollisionspan(!twV.isShowcollisionspan() );
+ } else
+ if(arg0.getActionCommand() == "statusdump") {
+ twV.setStatusdump(!twV.isStatusdump() );
+ } else
+ if(arg0.getActionCommand() == "collisions") {
+ twM.setCollisionson(!twM.isCollisionson() );
+ } else
+ if (arg0.getActionCommand() == "followthis"){
+ if (twC.getlastclickedthing() != null) {
+ twV.viewsquare.follownew(twC.getlastclickedthing());
+ twC.setlastclickedthing(null);
+ twC.pauseresume();
+ }
+ System.out.println("clicked followthis");
+ }
+
+ }
+
+
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ popupmenuitems[0].setSelected(twV.isNumberson());
+ popupmenuitems[1].setSelected(twV.isDirectionflags());
+ popupmenuitems[2].setSelected(twV.isSurroundbox());
+ popupmenuitems[3].setSelected(twV.isShowcollisionspan());
+ popupmenuitems[4].setSelected(twV.isStatusdump());
+ popupmenuitems[5].setSelected(twM.isCollisionson());
+
+ if (e.getButton()==MouseEvent.BUTTON3){
+ this.show(e.getComponent(), e.getX(), e.getY());
+ System.out.println("popup e.getcomponent() "+e.getComponent().toString());
+ }
+ }
+
+
+ @Override
+ public void mouseEntered(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+
+ @Override
+ public void mouseExited(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+
+ @Override
+ public void mousePressed(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+
+ @Override
+ public void mouseReleased(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+
+
+}
View
141 src/com/aptifuge/thingworld/ShapeControls.java
@@ -0,0 +1,141 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+package com.aptifuge.thingworld;
+
+import java.awt.*;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.swing.*;
+import javax.swing.event.*;
+
+public class ShapeControls extends JPanel implements Constance, KeyListener {
+ ThingWorldController twC;
+ ThingWorldView twV;
+ JPanel jp1 = new JPanel();
+ JButton button1 = new JButton(" Pause ");
+ JButton button2 = new JButton("Add Line");
+ JButton button3 = new JButton("Add Thing");
+ JButton button4 = new JButton("Reset");
+ JButton button5 = new JButton("Control Panel");
+
+ public ShapeControls(ThingWorldController af, ThingWorldView sarea){
+ //constructor
+ twC = af;
+ this.twV=sarea;
+ this.setLayout(new GridLayout());
+ this.add(jp1);
+ jp1.setBackground(new Color(30,50,30));
+ if (twC.getpaused() ) {
+ button1.setText("Resume");
+ } else {
+ button1.setText(" Pause ");
+ }
+
+ jp1.add(button1);
+ jp1.add(button2);
+ jp1.add(button3);
+ jp1.add(button4);
+ jp1.add(button5);
+ this.setBackground(Color.green);
+ this.setVisible(true);
+
+ addKeyListener(twC);
+
+ button1.addMouseListener(new MouseAdapter(){
+ public void mouseReleased(MouseEvent me){
+ pauseresumeclick();
+
+ if (button1.getText()==" Pause ") {button1.setText("Resume");}
+ else {button1.setText(" Pause ");}
+ twC.requestFocus();
+ }
+ });
+
+ button2.addMouseListener(new MouseAdapter(){
+ public void mouseReleased(MouseEvent me){
+ addshape();
+ twC.requestFocus();
+ }
+ });
+
+ button3.addMouseListener(new MouseAdapter(){
+ public void mouseReleased(MouseEvent me){
+ twC.addshape();
+ twC.requestFocus();
+ }
+ });
+ button4.addMouseListener(new MouseAdapter(){
+ public void mouseReleased(MouseEvent me){
+ twC.resetself();
+ twC.requestFocus();
+ }
+ });
+ button5.addMouseListener(new MouseAdapter(){
+ public void mouseReleased(MouseEvent me){
+ ThingWorldControlPanel twcp= new ThingWorldControlPanel(twC,twV,twC.twModel);
+ twcp.setVisible(true);
+ }
+ });
+ }
+
+ public void pauseresumeclick(){
+
+ twC.pauseresume();
+ }
+ public void addshape(){
+ twV.addshape();
+ }
+
+ @Override
+ public void keyPressed(KeyEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void keyReleased(KeyEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void keyTyped(KeyEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
View
317 src/com/aptifuge/thingworld/ThMan.java
@@ -0,0 +1,317 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+package com.aptifuge.thingworld;
+
+import java.awt.*;
+
+public class ThMan extends Thing implements Constance{
+ public double facing=0;//direction in radians of direction he is facing, usually 0 or pi
+ public Thing onsurfaceof;
+
+
+ private Line bbline = new Line(); //the line on thMan's bottom bound
+ private Line lineon = new Line(); // the line on the object the man is standing on
+ private Collision col = new Collision();
+ private boolean onsurface=false;
+
+
+ public ThMan(ThingWorldModel tw){
+ super(tw);
+ setMystroke(new BasicStroke(1));
+ setonsurface(false);
+ onsurfaceof = null;
+
+ this.addspoke(new ThSpoke(0,0,0,-5));
+ this.addspoke(new ThSpoke(0,0,1,-5));
+ this.addspoke(new ThSpoke(0,0,1,0));
+
+ this.addspoke(new ThSpoke(0,0,5,0));
+ this.addspoke(new ThSpoke(0,0,5,1));
+ this.addspoke(new ThSpoke(0,0,0,1));
+
+ this.addspoke(new ThSpoke(0,0,4,7));
+// this.addspoke(new ThSpoke(0,0,0,0));
+ this.addspoke(new ThSpoke(0,0,2,8));
+ this.addspoke(new ThSpoke(0,0,0,1));
+ this.addspoke(new ThSpoke(0,0,-4,7));
+// this.addspoke(new ThSpoke(0,0,0,0));
+ this.addspoke(new ThSpoke(0,0,-2,8));
+ this.addspoke(new ThSpoke(0,0,0,0));
+// this.addspoke(new ThSpoke(0,0,-5,0));
+// this.addspoke(new ThSpoke(0,0,0,0));
+
+ //triangle legs
+/*
+ this.addspoke(new ThSpoke(0,0,5,0));
+ this.addspoke(new ThSpoke(0,0,0,0));
+ this.addspoke(new ThSpoke(0,0,0,-5));
+ this.addspoke(new ThSpoke(0,0,0,0));
+ this.addspoke(new ThSpoke(0,0,4,8));
+ this.addspoke(new ThSpoke(0,0,-4,8));
+ this.addspoke(new ThSpoke(0,0,0,0));
+*/
+// this.addspoke(new ThSpoke(0,0,-5,0));
+// this.addspoke(new ThSpoke(0,0,0,0));
+
+
+ //square box guy
+/* this.addspoke(new ThSpoke(0,0,-4,-4));
+ this.addspoke(new ThSpoke(0,0,4,-4));
+ this.addspoke(new ThSpoke(0,0,4,4));
+ this.addspoke(new ThSpoke(0,0,-4,4));
+*/
+ this.putatpos(winx/2, winy-80);
+ statusdump=true;
+ this.twM =tw;
+ gravityfactor=4;
+ }
+
+// @Override
+// public void drawself(Graphics2D g2d){
+// g2d.drawOval(xi-3-twM.dispx1, yi-8-twM.dispy1, 8, 6);
+// super.drawself(g2d);
+ //g2d.drawOval(xi-3, yi-8, 6, 5);
+//
+// }
+ @Override
+ public void update(ThingWorldModel tw){
+// System.out.println("manticks "+ticks);
+ //keyboard
+ dokeys();
+ double ticks = tickspassed();
+ if ( getonsurface() ){
+ ya=0.0;yt=0.0;
+ }
+ super.update(tw);
+ friction(ticks);
+ limitspeed();
+ updatesurfacelines();
+ if ( getonsurfaceof()!=null ){
+ //our man is standing on some line segment.
+ Point p = col.linesintersectpoint(lineon, bbline);
+ if ( p != null) {
+ this.putatpos( p.x, p.y-(int)((this.gettb()-this.getbb())/2) ) ;
+ }
+ else {
+ this.setonsurfaceof(null);
+ }
+ } else {
+ //recalculate the angle of the edge of the thing I am on.
+ //do sliding, depending on the steepness of the surface
+
+
+ }
+ }
+
+ @Override
+ public void notifycollision(Thing thThing, double newangle, double newvelocity)
+ {
+ //check the velocity and location of theMan and
+ //the object he collided with.
+ //if (this.getrealdirection() )
+ System.out.println("Man/Object collision");
+ //System.exit(0);
+ //hitbot();
+ updatesurfacelines();
+ setonsurfaceof(thThing);
+
+ }
+
+ @Override
+ public void hitbot(){
+ //if ( getonsurface() ) return;
+ System.out.println("MAN: yt = "+yt);
+ //dont' mess with this stuff. it makes the guy bounce a certain way upon hitting
+ //the ground. it takes a lot of effort to get it to look decent.
+ if (Math.abs(yt) < 1.10) {
+ this.putatpos(ox, this.twM.worldbord.getbb() - (5+ this.getbb() - this.gettb())/2 );
+ ya=0.0; yt=0;
+ setonsurfaceof(this.twM.worldbord);
+ } else {
+ yt = -0.4*Math.abs(yt);
+ this.putatpos(ox, this.twM.worldbord.getbb() - (5+ this.getbb() - this.gettb())/2 );
+// setonsurfaceof(null);
+ }
+ }
+
+ public void dokeys(){
+ //keyboard
+ //System.out.println("dokeys()");
+ double kh;
+ if (KB.keydown[KEY_UP]){
+ facing = pi*1.5;
+ }
+
+ if(KB.keydown[KEY_LEFT]){
+ if (getonsurface()){
+ this.xa=-0.2;
+ } else
+ { if (xt > 0) this.xa=-0.07;}
+ facing = pi;
+ } else
+ if(KB.keydown[KEY_RIGHT]){
+ if (getonsurface()){
+ this.xa = 0.2;
+ } else
+ { if (xt < 0 ) this.xa = 0.07;}
+ facing = 0;
+ } else
+ {
+ this.xa = 0;
+ }
+
+ if ((kh=KB.getkeytime(KEY_DOWN))>keylowthresh) {
+ //this.ya=0.2;
+ }
+ if (KB.keydown[KEY_ACT]){
+ double dir = facing < (pi/2)?1.75*pi:1.25*pi;
+ if (facing == 1.5*pi) dir = 1.5*pi;
+ twM.addmissile(xi, yi, dir);
+ }
+ //JUMP ROUTINE:
+ if (getonsurface()) {
+ if (KB.keydown[KEY_JUMP]){
+ this.ya=-3.8;
+ this.yt=-1.5;
+ System.out.println("JUMP!!"+kh);
+ this.adjustpos(0, -2);
+ setonsurface(false);
+
+ }
+ }
+ }
+ public void limitspeed(){
+ if (xt > horzlimit) {
+ xt=horzlimit;
+ }
+ if (xt < (-horzlimit)){
+ xt = -horzlimit;
+ }
+ if (yt < vertuplimit){
+ yt=vertuplimit;
+ }
+ if (yt > vertdownlimit){
+ yt= vertdownlimit;
+ }
+ }
+ public void friction(double ticks){
+ if (getonsurface()){
+ if (Math.abs(xt)>0.001) {xt=xt*Math.pow(friction, ticks);}
+ else {xt = 0.0;}
+ }
+ }
+
+ public void updatesurfacelines() {
+ //updates the line at thman's bottom edge, and the line of the
+ // thing he is standing on.
+ Thing th = getonsurfaceof();
+ if (th != null )
+ {
+ this.bbline.x1 = this.getlb();
+ this.bbline.x2=this.getrb();
+ this.bbline.y1 = this.getbb();
+ this.bbline.y2 = this.bbline.y1;
+
+ this.lineon.x1 = th.colspoke0.getx2();
+ this.lineon.y1 = th.colspoke0.gety2();
+ this.lineon.x2 = th.colspoke1.getx2();
+ this.lineon.y2 = th.colspoke1.gety2();
+ }
+ }
+
+ public Thing getonsurfaceof(){
+ return onsurfaceof;
+ }
+ public void setonsurface(boolean b){onsurface = b;}
+ public boolean getonsurface () {return onsurface;}
+
+ public void setonsurfaceof(Thing th){
+ setonsurface(th != null );
+ onsurfaceof = th;
+ }
+
+ @Override
+ public void gravity(double ticks){
+ Thing th = getonsurfaceof();
+ double ang;
+ if (!onsurface){
+ //ya=ya+(gravityfactor*gravity);
+ //ya=ya+Math.pow(gravityfactor*gravity,ticks);
+ ya=ya*(1+Math.pow((gravityfactor*gravity),ticks));
+ ya=ya+(gravityfactor*gravity);
+ } else {
+// new code
+ th = getonsurfaceof();
+// ang = this.lineon.rot%pi;
+// this.setdirection(this.lineon.rot%pi);
+
+// old code
+// ya=0;
+ }
+ if (getonsurface()) {
+ ya = th.ya;
+ xa = th.xa;
+ }
+ }
+
+}
+
+//JUMP ROUTINE THAT DOESN't quite work
+/*
+if (onsurface) {
+ kh=KB.getkeytime(KEY_JUMP);
+ if (kh > keyhighthresh){
+ kh = keyhighthresh+100;
+ }
+ if (!KB.jumpexpired){
+ if (KB.keydown[KEY_JUMP]){
+ if(kh>=keyhighthresh){
+ KB.makejumpexpired();
+ //do jump:
+ this.ya=-1.3-0.5*(kh/keyhighthresh);
+ System.out.println("JUMP!!"+kh);
+ onsurface=false;
+ }
+ } else {
+ if (kh>keylowthresh)
+ {
+ //do jump:
+ KB.makejumpexpired();
+ this.ya=-1.3-0.5*(kh/keyhighthresh);
+ System.out.println("JUMP!!"+kh);
+ onsurface=false;
+ }
+ }
+ }
+}
+*/
View
61 src/com/aptifuge/thingworld/ThMissile.java
@@ -0,0 +1,61 @@
+package com.aptifuge.thingworld;
+
+public class ThMissile extends Thing implements Constance{
+
+ public ThMissile(ThingWorldModel tw, int x, int y){
+ super(tw);
+ spawnlocx=x;
+ spawnlocy=y;
+ init();
+ }
+
+
+ public void init(){
+ ttl=missiletimetolive;
+ this.xt=defaultmissilespeed;this.yt=defaultmissilespeed;
+ this.shapenum=SHAPE_MISSILE;
+ this.rt = Math.random()*0.035-0.0075;//.4-0.2;
+ ThSpoke thspoke;// = new ThSpoke(375,250,Math.random()*lenmax,0);
+ double len=4;
+ thspoke = new ThSpoke(0,0,len,0);
+ addspoke(thspoke);
+ thspoke = new ThSpoke(0,0,len,pi/2.0);
+ addspoke(thspoke);
+ thspoke = new ThSpoke(0,0,len,pi);
+ addspoke(thspoke);
+ thspoke = new ThSpoke(0,0,len,3*pi/2.0);
+ addspoke(thspoke);
+ this.putatpos(spawnlocx,spawnlocy);
+ }
+
+ @Override
+ public void hitrt() {
+ // TODO Auto-generated method stub
+ if (ttl < (missiletimetolive-2)){
+ this.xt=-(Math.abs(xt));
+ adjustpos(-2.0,0.0);
+ }
+ }
+ @Override
+ public void hitlf() {
+ //hit something to the left
+ // TODO Auto-generated method stub
+ if (ttl < (missiletimetolive-2)){
+ this.xt=(Math.abs(xt));
+ adjustpos(-2.0,0.0);
+ }
+ }
+/* @Override
+ public void hittop() {
+ // TODO Auto-generated method stub
+ //collisionchangetopbottom();
+ tw.thingz.remove(this);
+ }
+ @Override
+ public void hitbot() {
+ // TODO Auto-generated method stub
+ //collisionchangetopbottom();
+ tw.thingz.remove(this);
+ }
+*/
+}
View
55 src/com/aptifuge/thingworld/ThSpoke.java
@@ -0,0 +1,55 @@
+package com.aptifuge.thingworld;
+
+//ThSpoke is a simple thing: a line defined as an origin, a length, and an angle from 0-2pi radians
+// x increases to the right, y increases down
+public class ThSpoke implements Constance{
+
+ public double ox,oy,x2,y2;
+ public double ang;
+ public double len;
+
+ public ThSpoke(double ox, double oy, double len, double ang){
+ this.ox = ox; this.oy = oy;
+ this.ang = Trig.norm(ang);
+ this.len = len;
+ }
+
+ public ThSpoke(int ox,int oy,int x2, int y2) {
+ this.ox = ox; this.oy = oy;
+ this.x2 = x2; this.y2 = y2;
+ ang = Trig.norm( Math.atan2((y2-oy),(x2-ox)) );
+ len = Math.sqrt(Math.pow((ox-x2),2)+Math.pow((oy-y2),2));
+ }
+
+ public void incox(double iox){
+ this.ox += iox;
+ }
+
+ public void incoy(double ioy){
+ this.oy += ioy;
+ }
+
+ public void incang(double i){
+ //give me an i to increment/decrement the angle;
+ ang += i;
+ ang = Trig.norm(ang);
+ }
+
+
+ public int getox(){
+ return (int)Math.round(ox);
+ }
+ public int getoy(){
+ return (int)Math.round(oy);
+ }
+ public int getx2(){
+ //x2=ox+(Trig.costable[(int)(ang)]*len);
+ x2=this.ox+((Math.cos(ang)*len));
+ return (int)Math.round(x2);
+ }
+ public int gety2(){
+ //y2=oy+(Trig.sintable[(int)(ang)]*len);
+ y2=this.oy+((Math.sin(ang)*len));
+ return (int)Math.round(y2);
+ }
+}
View
545 src/com/aptifuge/thingworld/Thing.java
@@ -0,0 +1,545 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+
+
+package com.aptifuge.thingworld;
+
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.Point;
+import java.awt.Polygon;
+import java.awt.Rectangle;
+import java.awt.Shape;
+import java.awt.Stroke;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.PathIterator;
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+import ec.util.*;
+public class Thing implements Constance {
+ //this is the interface for all "things" in the world.
+ /*
+ * 1. have a "per tick" modifier for x and y (position), and r, rotation (in radians: positive = counterclockwise)
+ * 2. have a per tick modifier for the modifiers
+ * 3. double: ox,oy (position) -- this is the "center" of the object.
+ * 4. double: xt,yt (per tick modifier) (speed)
+ * 5. double: xa,ya (per tick modifier for the modifier) (acceleration)
+ * double: r = current rot, rt = rot modifier, ra = modifies the modifier
+ * 6. int: xi,yi -- integer screen position variable -- updated each call to update().
+ *
+ *
+ * Therefore:
+ * 7. Each call to "update" results in a check to system.nanotime()
+ * 8. the number of "ticks" is calculated, but capped at tickspersecond (typically 30).
+ * 9. xp = xp + xt*ticks, etc.
+ * 10. xt = xt + xa*ticks..
+ * 11. xa (acceleration) can be altered by weather or other factors. Be mindful to set to zero if not
+ * needed for something
+ * 12. lbound,rbound,tbound,bbound: left, right,top,bottom - these are methods, calculated when needed,
+ * not each update.
+ * 13. each object has a "span" from center ls = left span, the distance from center (xp,yp) to its leftmost bound.
+ *
+ * 14. all angles are in RADIANS, from 0 - 2pi. no negative pi radians - screws up calculations.
+ *
+ * */
+ public Polygon polygon = new Polygon(); //used for drawing in colored
+ private Color polyout = new Color(0,0,0);
+ private Color polyin = new Color(0,0,0);
+ private Point centerpoint = new Point(0,0);
+
+ //rendering things
+ private BasicStroke mystroke = new BasicStroke(1);
+
+
+ public ThingWorldModel twM;
+
+ public double ox, oy, xt, yt, xa, ya, r, rt, ra, radmin=Double.MAX_VALUE, radmax=Double.MIN_VALUE;
+
+ public double lastox=0,lastoy=0;
+
+ public double realdirection = 0;
+
+ public int xi=0,yi=0, shapenum=-99;
+ public double lastupdate;
+ public ThSpoke colspoke0=new ThSpoke(0,0,0,0), colspoke1= new ThSpoke(0,0,0,0);
+ // the two "collision" spokes updated by calls to closest()
+ public int numspokes=0;
+ public ThSpoke[] spokes = new ThSpoke[maxspokes];
+ public double width, height;
+ private double maxxseen, minxseen,maxyseen,minyseen;
+ public int hiatus=0; //if hiatus > 0 the collision system will not collide this. This is useful
+ // for making sure recently collided objects do not "recollide" before they
+ // have a chance to move far enough away from each other. this value is
+ // decremented each call to update()
+
+ protected int spawnlocx,spawnlocy;
+
+
+ //helpful output and markups
+ public boolean numberson=true;
+ public boolean surroundbox =false;
+ public boolean identnumberson=false;
+ public boolean statusdump=false;
+ public boolean directionflags=false;
+ public boolean showcollisionspan=false;
+
+ private boolean translatetowindow = true;
+
+ //physics
+
+ protected double gravityfactor=1.0;
+
+ //these are immutable, and cause the acceleration to automatically decay to 0 at this rate
+ private final double xad=xacceldecay,yad=yacceldecay;
+
+ //time to live
+ protected int ttl = 0x7fffffff;
+
+ //display
+ private boolean iamvisible = true;
+
+ //interacts with world
+ private boolean icollide = true;
+ private boolean iamgravitational = true;
+ private boolean iamscenery = false;
+
+ double highestValueEver = Double.MAX_VALUE-1; // I subtract 1 in case there is something f'd about their edge-case handling
+
+
+ public Thing(ThingWorldModel tw,int n){
+ this.twM=tw;
+ init(n);
+ }
+
+ public Thing(ThingWorldModel tw){
+ this.twM=tw;
+ //init();
+ }
+
+ public void init(int n){
+ //make a random shape
+ lastupdate=System.nanoTime();
+ setMystroke(new BasicStroke(2));
+ if (n == RANDOM) { n = rand.nextInt(5)+1;}
+ switch(n){
+ case RANDOM:
+ break;
+
+ case POLYGON:{
+ polyout = polout;
+ //polyin = polin;
+ polyin = new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
+ int lenmax=20;
+ this.xt = rand.nextDouble()*2.7-1.35;
+ this.yt = rand.nextDouble()*2.9-1.45;
+ this.rt = rand.nextDouble()*0.017-0.0085;//.4-0.2;
+ ThSpoke thspoke;// = new ThSpoke(375,250,Math.random()*lenmax,0);
+ double deg = 0, numspokes=rand.nextDouble()*8+5;
+ double spokesadded=0;
+ while ((deg < 2*pi) && (spokesadded <= numspokes)){
+ deg = deg + (2*pi/numspokes) + (rand.nextDouble()*pi/10-pi/20);
+ thspoke = new ThSpoke(375,250,lenmax*2+rand.nextDouble()*4-2,deg);
+ addspoke(thspoke);
+ spokesadded++;
+ }
+ this.putatpos(0,0);
+ this.xt=0;this.yt=0;
+
+ }
+ break;
+
+ case SQUARE:{
+ polyout = squout;
+// polyin = squin;
+ polyin = new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
+ int lenmax=50;
+ this.yt = rand.nextDouble()*3-1.5;
+ this.rt = rand.nextDouble()*0.015-0.0075;//0.4-0.2;
+ this.r=0;
+ this.ra=0;
+ //this.rt=0;
+ ThSpoke thspoke;// = new ThSpoke(375,250,Math.random()*lenmax,0);
+ double deg = 0, numspokes=2,len;
+ len = rand.nextDouble()*lenmax+3;
+ thspoke = new ThSpoke(250,250,len,0);
+ addspoke(thspoke);
+ thspoke = new ThSpoke(250,250,len,pi/2.0);
+ addspoke(thspoke);
+ thspoke = new ThSpoke(250,250,len,pi);
+ addspoke(thspoke);
+ thspoke = new ThSpoke(250,250,len,3*pi/2.0);
+ addspoke(thspoke);
+ } break;
+ case RECTANGLE:{
+ polyout = squout;
+// polyin = squin;
+ polyin = new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
+// polyin = new Color(rand.nextInt(55),rand.nextInt(55),255);
+ int lenmax=50;
+ this.yt = rand.nextDouble()*3-1.5;
+ this.rt = rand.nextDouble()*0.015-0.0075;//0.4-0.2;
+ this.r=0;
+ this.ra=0;
+
+ double deg1 = pi- rand.nextDouble()*(0.8*pi);
+// deg1 = pi/2;
+ double len = rand.nextDouble()*lenmax+8;
+ addspoke(new ThSpoke(250,250,len,0));
+ addspoke(new ThSpoke(250,250,len,deg1));
+ addspoke(new ThSpoke(250,250,len,pi));
+ addspoke(new ThSpoke(250,250,len,pi+deg1));
+
+ } break;
+ case TRIANGLE:{
+ polyout = squout;
+ polyin = new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
+ int lenmax=50;
+ this.yt = rand.nextDouble()*3-1.5;
+ this.rt = rand.nextDouble()*0.015-0.0075;//0.4-0.2;
+ this.r=0;
+ this.ra=0;
+
+ double deg1 = pi- rand.nextDouble()*(0.7*pi);;
+ double deg2 = pi- rand.nextDouble()*(0.7*pi);;
+
+ double len = rand.nextDouble()*lenmax+3;
+ addspoke(new ThSpoke(250,250,len,0));
+ len = rand.nextDouble()*lenmax+3;
+ addspoke(new ThSpoke(250,250,len,deg1));
+ len = rand.nextDouble()*lenmax+3;
+ addspoke(new ThSpoke(250,250,len,deg1+deg2));
+
+ } break;
+ case SPIKY:{
+ polyout = polout;
+ //polyin = polin;
+ polyin = new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
+ int lenmin=10;
+ this.xt = rand.nextDouble()*2.7-1.35;
+ this.yt = rand.nextDouble()*2.9-1.45;
+ this.rt = rand.nextDouble()*0.217-0.0085;//.4-0.2;
+ ThSpoke thspoke;// = new ThSpoke(375,250,Math.random()*lenmax,0);
+ double deg = 0, numspokes=rand.nextDouble()*8+5;
+ double spokesadded=0;
+ while ((deg < 2*pi) && (spokesadded <= numspokes)){
+ deg = deg + (2*pi/numspokes) + (rand.nextDouble()*pi/10-pi/20);
+ thspoke = new ThSpoke(375,250,lenmin+rand.nextDouble()*20,deg);
+ addspoke(thspoke);
+ spokesadded++;
+ }
+ this.putatpos(0,0);
+ this.xt=0;this.yt=0;
+ } break;
+ } //end switch
+ };
+ public int getrb(){
+ maxxseen=-highestValueEver;
+ for (int cw=0;cw<numspokes;cw++){
+ maxxseen = Math.max(spokes[cw].x2,maxxseen);
+// System.out.println(spokes[cw].getx2()+" "+spokes[cw].gety2());
+ }
+ return (int)Math.round( maxxseen);};//rightbound
+ public int getlb(){
+ minxseen=highestValueEver;
+ for (int cw=0;cw<numspokes;cw++){
+ minxseen = Math.min(spokes[cw].x2,minxseen);
+ }
+ return (int)Math.round(minxseen);
+ };//leftbound
+ public int gettb(){
+ minyseen=highestValueEver;
+ for (int cw=0;cw<numspokes;cw++){
+ minyseen = Math.min(spokes[cw].y2,minyseen);
+ }
+ return (int)Math.round(minyseen);
+ };//topbound
+ public int getbb(){
+ maxyseen=-highestValueEver;
+ for (int cw=0;cw<numspokes;cw++){
+ maxyseen = Math.max(spokes[cw].y2,maxyseen);
+ }
+ return (int)Math.round(maxyseen);
+ };//bottombound
+
+ public void update(ThingWorldModel tw){
+ double ticks= tickspassed();
+
+ ttl--; //time to live is decremented
+ //1: basic movement
+ lastox = ox;
+ lastoy = oy;
+ ox=ox+(xt*ticks);
+ oy=oy+(yt*ticks);
+ yt=yt+(ya*ticks);
+ xt=xt+(xa*ticks);
+
+ xi = (int)ox;
+ yi = (int)oy;
+
+ for (int cw=0;cw<numspokes;cw++){
+ spokes[cw].incang(rt*ticks);
+ spokes[cw].ox=ox;
+ spokes[cw].oy=oy;
+ }
+
+ //2: "physics"
+ if (!iamscenery) {
+ acceldecay(ticks);
+ gravity(ticks);
+ }
+ lastupdate=System.nanoTime();
+ hiatus=0;
+ };
+
+
+
+ public double tickspassed(){
+ double t = ( (System.nanoTime()-lastupdate)/(nanospertick) );
+ if (t > tickspersecond) {t = tickspersecond;}
+ if (t > 5 ){t=1;}
+ return t;
+ }
+
+ public void addspoke(ThSpoke spk){
+ if (numspokes<maxspokes){
+ spokes[numspokes]=spk;
+ numspokes++;
+ if (spk.len > radmax) {radmax = spk.len;}
+ if (spk.len < radmin) {radmin = spk.len;}
+ ox = spk.ox;
+ oy = spk.oy;
+ }
+ }
+
+ public void setrot(double r){
+ for (int cw=0;cw<numspokes;cw++){
+ spokes[cw].ang=Trig.norm(r+spokes[cw].ang);
+ }
+ }
+
+ public void adjustpos(double xadj, double yadj){
+ this.ox = this.ox+xadj;
+ this.oy = this.oy+yadj;
+ for (int cw=0;cw<numspokes;cw++){
+ spokes[cw].incox(xadj);
+ spokes[cw].incoy(yadj);
+ }
+ }
+
+ public void adjustposrads(double theta, double distance){
+ double th = Trig.norm(theta);
+ double xadj,yadj;
+ xadj = Math.cos(th)*distance;
+ yadj = Math.sin(th)*distance;
+ adjustpos(xadj,yadj);
+ }
+
+ public void putatpos(double x,double y){
+ this.ox = x;
+ this.oy = y;
+ for (int cw=0;cw<numspokes;cw++){
+ spokes[cw].ox=x;
+ spokes[cw].oy=y;
+ // System.out.println(spokes[cw].getx2()+" "+spokes[cw].gety2());
+ }
+ }
+
+ public void hitrt() {
+ this.xt=-(Math.abs(xt));
+ adjustpos(-2.0,0.0);
+ }
+
+ public void hitlf() {
+ //hit something to the left
+ this.xt=Math.abs(xt);
+ adjustpos(2.0,0.0);
+ }
+
+ public void hittop() {
+ this.yt=(Math.abs(yt));
+ adjustpos(0.0,2.0);
+ }
+
+ public void hitbot() {
+ this.yt=-(Math.abs(yt));
+ adjustpos(0.0,-2.0);
+ }
+
+ public Fourint closest(double angle){
+ //this returns the closest two points in x,y space to the radius from ox,oy at the angle given
+ if (numspokes < 2) return (new Fourint());
+ boolean done =false;
+ double dif;
+ angle = Trig.norm(angle) ;
+ Fourint fi= new Fourint();
+ for (int g2=0;g2<numspokes-1;g2++){
+ if (spokes[g2].ang > spokes[g2+1].ang){
+ //we are crossing over from 1.999pi to 0.01 pi
+ //for comparison, we drop everyone below twopi and see how far they fall
+ double a =Trig.norm(pi+spokes[g2].ang);
+ double b =Trig.norm(pi+spokes[g2+1].ang);
+ double between = Trig.norm(pi+angle);
+ ;
+ if ((between>a)&&(between<b)) {
+ done=true;
+ colspoke0 = spokes[g2];colspoke1=spokes[g2+1];
+ fi.x1=spokes[g2].getx2();fi.y1=spokes[g2].gety2();
+ fi.x2=spokes[g2+1].getx2();fi.y2=spokes[g2+1].gety2();
+ }
+ }else
+ if ((angle > spokes[g2].ang)&&(angle < spokes[g2+1].ang)){
+ done=true;
+ colspoke0 = spokes[g2];colspoke1=spokes[g2+1];
+ fi.x1=spokes[g2].getx2();fi.y1=spokes[g2].gety2();
+ fi.x2=spokes[g2+1].getx2();fi.y2=spokes[g2+1].gety2();
+ }
+ }
+ //this bottom part is for the situation where the two angles of interest
+ //are the last elements of the array the the first: spokes[numspokes-1].ang, spokes[0].ang
+ if (!done) {
+ if (spokes[numspokes-1].ang > spokes[0].ang){
+ double a =Trig.norm(pi+spokes[numspokes-1].ang);
+ double b =Trig.norm(pi+spokes[0].ang);
+ double between = Trig.norm(pi+angle);
+ if ((between>a)&&(between<b)) {
+ done=true;
+ colspoke0 = spokes[numspokes-1];colspoke1=spokes[0];
+ fi.x1=spokes[numspokes-1].getx2();fi.y1=spokes[numspokes-1].gety2();
+ fi.x2=spokes[0].getx2();fi.y2=spokes[0].gety2();
+ }
+ } else
+ if ((angle < spokes[0].ang)&&(angle > spokes[numspokes-1].ang )){
+ done=true;
+ colspoke0 = spokes[numspokes-1];colspoke1=spokes[0];
+ fi.x1=spokes[numspokes-1].getx2();fi.y1=spokes[numspokes-1].gety2();
+ fi.x2=spokes[0].getx2();fi.y2=spokes[0].gety2();
+
+ //System.out.println("colspokes:"+colspoke0.ang+","+colspoke1.ang);
+ }
+ }
+ if (!done) {fi.x1=-9999999;}
+ return fi;
+ }
+
+ public double getvelocity(){
+ return Math.hypot(xt, yt);
+ }
+ public double getdirection(){
+ //returns the direction it is theoretically traveling, not counting on gravity or other factors
+ return Trig.norm(Math.atan2(yt,xt));
+ }
+ public double getrealdirection(){
+ //this returns the actual direction it is traveling, based on its last position and its current position
+ return Trig.norm(Math.atan2(oy-lastoy,ox-lastox));
+ }
+
+ public void setdirection(double theta){
+ //direction goes clockwise starting from 3 o'clock which == 0, to 9 o'clock which
+ //== pi, to 12:00 which is 1.5 pi, etc.
+ double speed = getvelocity();
+ xt = Math.cos(theta)*speed;
+ yt = Math.sin(theta)*speed;
+ }
+
+ public void acceldecay(double ticks){
+ //it must be done this way using exponents in order to capture
+ //"multiplying" by a factor over a different number of frames, due to sizing
+ //everything to the number of "ticks" that have clicked since last update (often < 1)
+ if (xa>0.001){xa = xa*Math.pow(xad, ticks);} else {xa=0.0;}
+ if (ya>0.001){ya = ya*Math.pow(yad, ticks);} else {ya=0.0;}
+
+ }
+ public void gravity(double ticks){
+ ya=ya*(1+Math.pow((gravityfactor*twM.getGravity_current()),ticks));
+ ya=ya+(gravityfactor*twM.getGravity_current());
+ }
+
+ public Color getPolyout() { return polyout;}
+
+ public void setPolyout(Color polyout) { this.polyout = polyout; }
+
+ public Color getPolyin() { return polyin; }
+
+ public void setPolyin(Color polyin) { this.polyin = polyin; }
+
+ public void setiamscenery(boolean b) {
+ xa=0;xt=0;ya=0;yt=0;
+ iamscenery=b;
+ }
+
+ public Polygon getPolygon()
+ {
+ polygon.reset();
+ for (int c4=0;c4<numspokes;c4++)
+ {
+ polygon.addPoint(spokes[c4].getx2(),spokes[c4].gety2());
+ }
+ return polygon;
+ }
+
+ public Point getCenter()
+ {
+ centerpoint.setLocation(ox,oy);
+ return centerpoint;
+ }
+
+
+
+ public void setpolyin(Color color) {
+ // TODO Auto-generated method stub
+ if (color != null) { this.polyin = color;}
+ }
+
+ public void setvelocity(double newvelocity){
+ double currentvelocity = this.getvelocity();
+ xt = xt*(newvelocity/currentvelocity);
+ yt = yt*(newvelocity/currentvelocity);
+ }
+
+ public void notifycollision(Thing t2, double newangle, double newvelocity) {
+ // TODO Auto-generated method stub
+ //note that colspoke0 and colspoke1 should be set correctly as the collided spoke
+ this.setdirection(newangle);
+ this.setvelocity(newvelocity);
+ // this.hiatus=4;
+ }
+ public BasicStroke getMystroke() {
+ return mystroke;
+ }
+
+ public void setMystroke(BasicStroke mystroke) {
+ this.mystroke = mystroke;
+ }
+}
+
View
146 src/com/aptifuge/thingworld/ThingWorldControlPanel.java
@@ -0,0 +1,146 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) <2010,2011> <David R. Heitzman >
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+
+
+package com.aptifuge.thingworld;
+
+import java.awt.*;
+import java.awt.event.ComponentAdapter;
+import java.awt.event.ComponentEvent;
+import java.awt.event.ComponentListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+
+import javax.swing.*;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import javax.swing.event.MouseInputListener;
+
+
+public class ThingWorldControlPanel extends JPanel implements MouseInputListener {
+
+ JFrame twcpframe = new JFrame("ThingWorld Control Panel");
+ JButton jb1 = new JButton("space button");
+ JButton jbok = new JButton("Okay");
+ JButton jbcancel = new JButton ("Cancel");
+ JPanel jpphysics = new JPanel();
+ JPanel jpokcancel = new JPanel();
+ JSlider js_grav = new JSlider(JSlider.HORIZONTAL,0,100,1 );
+ ThingWorldController twC;ThingWorldView twV; ThingWorldModel twM;
+
+
+ public ThingWorldControlPanel(ThingWorldController twC,ThingWorldView twV,ThingWorldModel twM)
+ {
+ this.twC=twC; this.twV = twV; this.twM=twM;
<