001 package components; 002 import java.rmi.RemoteException; 003 import java.util.HashSet; 004 import java.util.List; 005 import java.util.Map; 006 import java.util.Set; 007 008 import physics3d.Angle; 009 import physics3d.GamePhysics; 010 import physics3d.OldGamePhysics; 011 import physics3d.Shape; 012 import physics3d.Vect3; 013 014 /** 015 * A Ball is a mutable object that bounces around the GameSpace 016 */ 017 018 /* 019 * Abstraction Function 020 * AF(r) = a Ball, b 021 */ 022 023 /* 024 * Representation Invariant 025 * 026 * Ball's position is inside the GameSpace of the Ball, 027 * as specified by the Walls of the GameSpace 028 */ 029 030 public class Ball extends GameObject { 031 032 //Constructors 033 /** 034 * @effects Contructs new Ball 035 */ 036 public Ball(Vect3 tlf, Vect3 velocity, Vect3 ov, Angle oa, String name, GameSpace g) { 037 super(tlf, velocity, ov,oa,name,g); 038 this.shape = Shape.getBallShape(); 039 center = tlf; 040 } 041 042 /** 043 * @effects Constructs a new Ball 044 */ 045 public Ball(Vect3 tlf, Vect3 velocity, String name, GameSpace g) { 046 this(tlf, velocity, Vect3.Z_HAT, Angle.ZERO, name, g); 047 } 048 049 /** 050 * @effects Constructs a new Ball 051 */ 052 public Ball(Vect3 tlf, String name, GameSpace g) { 053 this(tlf, Vect3.ZERO, name, g); 054 } 055 056 //methods 057 058 public Ball(Map<String, String> p, GameSpace g) { 059 super(p, g); 060 } 061 062 /** in order for the ball to be in the middle of a grid, the center is offset by .5 from the tlf on the z axis**/ 063 @Override 064 public Vect3 getDiff() { 065 return Vect3.ZERO; 066 } 067 068 /** balls have the same center and tlf, so don't do that thar rounding thing **/ 069 @Override 070 public Vect3 getTLF() { 071 return center.minus(getDiff()); 072 } 073 074 @Override 075 protected Map<String, String> defaults() { 076 Map<String, String> smap = super.defaults(); 077 smap.put("frozen", "false"); 078 return smap; 079 } 080 081 @Override 082 /** 083 * @effects finds what the Ball collides with and tells what the Ball hits to 084 * respond appropriately 085 */ 086 public void stepFrame() { 087 List<GameObject> collisions = GamePhysics.moveBall(this); 088 089 for (GameObject o : collisions) 090 o.onCollision(this); 091 } 092 093 /** 094 * @return the GameObjectClassification of this object 095 */ 096 public GameObjectClassification getGOClassification() { 097 return GameObjectClassification.BALL; 098 } 099 100 //See parent class 101 public void getBasicPropertyMap(Map<String,String> m) { 102 super.getBasicPropertyMap(m); 103 GameObject.putInVect3(velocity, m, "Velocity",false); 104 GameObject.putInVect3(getCenter(), m, "",false); 105 System.err.println("Got ball properties...center=" + getCenter().toString()); 106 System.err.println("Got ball map=" + m.toString()); 107 } 108 109 @Override 110 protected Shape shape() { 111 return Shape.getBallShape(); 112 } 113 114 /** 115 * @return the top left front corners of the grid locations occupied by the Ball, 116 * where the x grid positions go from 0-20, y from 0-20, and z from 0-10 117 */ 118 public Set<Vect3> getOccupiedPositions() { 119 Set<Vect3> ops = new HashSet<Vect3>(); 120 Vect3 tlf = getNonRoundedTLF(); 121 int x = (int) tlf.x(); 122 int y = (int) tlf.y(); 123 int z = (int) tlf.z(); 124 Vect3 newtlf = new Vect3(x, y, z); 125 ops.add(newtlf); 126 return ops; 127 } 128 }