You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ship.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. Ship.java
  13. Part of the Spacewar system.
  14. */
  15. package spacewar;
  16. class Ship extends SpaceObject {
  17. pointcut helmCommandsCut(Ship ship):
  18. target(ship) && ( call(void rotate(int)) ||
  19. call(void thrust(boolean)) ||
  20. call(void fire()) );
  21. /**
  22. * Energy and Damage are key values in the state of a ship. Energy is
  23. * basically about fuel, and damage is about how bad a shape we are in.
  24. *
  25. * The energy related values are:
  26. * <ul>
  27. * <li>MAX_ENERGY</li>
  28. * <li>BULLET_ENERGY</li>
  29. * <li>ACCELERATION_ENERGY_FACTOR</li>
  30. * <li>energy</li>
  31. * </ul>
  32. * The damage related values are:
  33. * <ul>
  34. * <li>MAX_DAMAGE</li>
  35. * <li>BULLET_DAMAGE</li>
  36. * <li>COLLISION_DAMAGE_FACTOR</li>
  37. * <li>damage</li>
  38. * </ul>
  39. * Finally, REPAIR_RATE is the rate at which energy is consumed to fix
  40. * damage.
  41. *
  42. */
  43. private static final int MAX_ENERGY = 100;
  44. private static final int BULLET_ENERGY= 2;
  45. private static final double ACCELERATION_COST_FACTOR = 0.05;
  46. //XXX was private
  47. static final int MAX_DAMAGE = 100;
  48. private static final int BULLET_DAMAGE = 15;
  49. private static final double COLLISION_DAMAGE_FACTOR = 0.1;
  50. private static final double REPAIR_RATE = 0.08;
  51. private static final int EXPLOSION_LENGTH = 10;
  52. static final int BULLET_SPEED = 10;
  53. static final int CLOCKWISE = 1;
  54. static final int STOP = 0;
  55. static final int COUNTERCLOCKWISE = (-1);
  56. static final double DEFAULT_ANGULAR_VELOCITY = 0.2;
  57. static final double DEFAULT_ACCELERATION = .4;
  58. static private final int SIZE = 30; //Can't be changed for now!!!
  59. private double energy; // range: 0 to MAX_ENERGY
  60. private double damage; // range: 0 to MAX_DAMAGE
  61. private double orientation; // in degrees
  62. private double angularVel; // in ???
  63. private double xAcc, yAcc, rAcc; //
  64. private int countdown; // remaining explosion time
  65. private Pilot pilot;
  66. Ship(Game theGame, double xPos, double yPos, double orientation) {
  67. super(theGame, xPos, yPos, 0, 0);
  68. xAcc = 0;
  69. yAcc = 0;
  70. this.orientation = orientation;
  71. angularVel = 0;
  72. energy = MAX_ENERGY;
  73. damage = 0;
  74. countdown = EXPLOSION_LENGTH;
  75. }
  76. int getSize() { return SIZE; }
  77. double getEnergy() { return energy; }
  78. double getDamage() { return damage; }
  79. double getOrientation() { return orientation; }
  80. double getRAcc() { return rAcc; }
  81. Pilot getPilot() { return pilot; }
  82. void setPilot (Pilot p) { pilot = p; }
  83. float getEnergyLevel() {
  84. return (float)energy / (float)MAX_ENERGY;
  85. }
  86. float getDamageLevel() {
  87. return (float)damage / (float)MAX_DAMAGE;
  88. }
  89. /** returns false if energy is out, otherwise decrements energy by amount
  90. * and returns true
  91. */
  92. boolean expendEnergy(double amount) {
  93. if (amount <= energy) {
  94. energy -= amount;
  95. return true;
  96. }
  97. else
  98. return false;
  99. }
  100. /** increments damage by amount and handles the destruction of a ship if
  101. * damage reaches MAX_DAMAGE.
  102. */
  103. void inflictDamage(double amount) {
  104. if (amount < 0) // shouldn't happen
  105. return;
  106. damage = Math.min(MAX_DAMAGE, damage + amount);
  107. if (damage == MAX_DAMAGE)
  108. setIsAlive(false);
  109. }
  110. /** repairs some damage
  111. */
  112. void repairDamage(double amount) {
  113. if (amount < 0) // shouldn't happen
  114. return;
  115. if (damage == 0)
  116. return;
  117. damage = Math.max(0, damage - amount);
  118. }
  119. public void clockTick() {
  120. if (! isAlive()) {
  121. //
  122. // If we aren't alive, but we are still in the registry, it means
  123. // we are exploding. countdown counts the length of the explosion.
  124. //
  125. if (--countdown == 0)
  126. die();
  127. }
  128. else {
  129. if (angularVel != 0) {
  130. orientation += angularVel;
  131. xAcc = rAcc * Math.cos(orientation);
  132. yAcc = rAcc * Math.sin(orientation);
  133. }
  134. setXVel(getXVel() + xAcc);
  135. setYVel(getYVel() + yAcc);
  136. //expend energy
  137. if (!expendEnergy(rAcc * ACCELERATION_COST_FACTOR))
  138. rAcc = xAcc = yAcc = 0;
  139. // fix damage
  140. if (energy > 10 && damage > REPAIR_RATE) {
  141. expendEnergy(REPAIR_RATE);
  142. repairDamage(REPAIR_RATE);
  143. }
  144. }
  145. super.clockTick();
  146. }
  147. /**
  148. * First check to make sure we have enough energy to accelerate. If
  149. * we do, then go ahead and do so. Acceleration is in the direction
  150. * we are already facing (i.e. orientation).
  151. */
  152. void setAcceleration(double acc) {
  153. if (acc * ACCELERATION_COST_FACTOR <= energy) {
  154. rAcc = acc;
  155. xAcc = rAcc * Math.cos(orientation);
  156. yAcc = rAcc * Math.sin(orientation);
  157. }
  158. }
  159. /**
  160. * First check to make sure we have enough energy to rotate. If
  161. * we do, then go ahead and do so.
  162. */
  163. void setAngularVelocity(double omega) {
  164. // changing direction of rotation takes energy
  165. if (!expendEnergy(Math.abs(omega - angularVel) / 2))
  166. return;
  167. //sets amount of degree rotation per clock tick, in radians;
  168. //clockwise is positive
  169. angularVel = omega;
  170. }
  171. /** affect rotation thrusters. Direction can be one of {@link
  172. * #CLOCKWISE}, {@link #COUNTERCLOCKWISE}, or zero for turning off
  173. * the thrusters.
  174. */
  175. void rotate(int direction) {
  176. setAngularVelocity(
  177. direction == CLOCKWISE ? DEFAULT_ANGULAR_VELOCITY :
  178. direction == COUNTERCLOCKWISE ? -DEFAULT_ANGULAR_VELOCITY :
  179. 0);
  180. }
  181. /** turn on acceleration */
  182. void thrust(boolean onOff) {
  183. setAcceleration(onOff ? DEFAULT_ACCELERATION : 0);
  184. }
  185. /** create a bullet and fire it */
  186. void fire() {
  187. // firing a shot takes energy
  188. if (!expendEnergy(BULLET_ENERGY))
  189. return;
  190. //create a bullet object so it doesn't hit the ship that's firing it
  191. double xV = getXVel() + BULLET_SPEED * (Math.cos(orientation));
  192. double yV = getYVel() + BULLET_SPEED * (Math.sin(orientation));
  193. // create the actual bullet
  194. new Bullet(
  195. getGame(),
  196. (getXPos() + ((getSize()/2 + 2) * (Math.cos(orientation))) + xV),
  197. (getYPos() + ((getSize()/2 + 2) * (Math.sin(orientation))) + yV),
  198. xV,
  199. yV);
  200. }
  201. void handleCollision(SpaceObject obj) {
  202. if (obj instanceof Ship) {
  203. // should never be called. ship - ship collisions are handled in
  204. // Ship.bounce(Ship shipA, Ship shipB)
  205. }
  206. else if (obj instanceof Bullet) {
  207. inflictDamage(BULLET_DAMAGE);
  208. }
  209. else if (obj instanceof EnergyPacket) {
  210. double packetEnergy = ((EnergyPacket)obj).getEnergy();
  211. energy = Math.max(0, Math.min(energy + packetEnergy, MAX_ENERGY));
  212. }
  213. else {
  214. System.err.println("collision with UFO!");
  215. }
  216. }
  217. static void bounce(Ship shipA, Ship shipB) {
  218. double dx, dy, denominator,
  219. xAccA, yAccA, xAccB, yAccB, damage,
  220. xComp, yComp, dvx, dvy;
  221. dx = Math.abs(shipA.getXPos() - shipB.getXPos());
  222. dy = Math.abs(shipA.getYPos() - shipB.getYPos());
  223. denominator = Math.sqrt(dx * dx + dy * dy);
  224. xComp = dx / denominator;
  225. yComp = dy / denominator;
  226. xAccA = shipB.getXVel() * xComp + shipA.getXVel() * (1 - xComp) -
  227. shipA.getXVel();
  228. yAccA = shipB.getYVel() * yComp + shipA.getYVel() * (1 - yComp) -
  229. shipA.getYVel();
  230. xAccB = shipA.getXVel() * xComp + shipB.getXVel() * (1 - xComp) -
  231. shipB.getXVel();
  232. yAccB = shipA.getYVel() * yComp + shipB.getYVel() * (1 - yComp) -
  233. shipB.getYVel();
  234. shipA.accelerate(xAccA, yAccA);
  235. shipB.accelerate(xAccB, yAccB);
  236. dvx = shipA.getXVel() - shipB.getXVel();
  237. dvy = shipA.getYVel() - shipA.getYVel();
  238. damage = COLLISION_DAMAGE_FACTOR * (dvx * dvx + dvy * dvy);
  239. shipA.inflictDamage(damage);
  240. shipB.inflictDamage(damage);
  241. // !!!
  242. // !!! poopers! this does a local time warp. this has to be a
  243. // !!! violation of the clockTick protocol
  244. // !!!
  245. while (Game.isCollision(shipA, shipB)) {
  246. shipA.clockTick();
  247. shipB.clockTick();
  248. }
  249. }
  250. }