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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. Game.java
  13. Part of the Spacewar system.
  14. */
  15. package spacewar;
  16. import java.awt.Dimension;
  17. /**
  18. * The Game class is the root of the spacewar game. To start a spacewar
  19. * game, you can either call the main method, or instantiate this class
  20. * directly.
  21. *
  22. * Synchronization is done by the GameSynchronization aspect.
  23. */
  24. public class Game extends Thread {
  25. /**
  26. * To run the game from top level, simply say Java Game, as usual. Passing
  27. * an argument makes the game run in demo mode. Without an argument it runs
  28. * in the normal player mode.
  29. */
  30. public static void main(String[] args) {
  31. if ( args.length == 0 )
  32. new Game("1").run();
  33. new Game(args[0]).run();
  34. }
  35. private Timer timer;
  36. private EnergyPacketProducer ePP;
  37. private Registry registry;
  38. private Pilot pilot1, pilot2;
  39. private Dimension screenSize = new Dimension(500, 500);
  40. Registry getRegistry() { return registry; }
  41. Pilot getPilot1() { return pilot1; }
  42. Pilot getPilot2() { return pilot2; }
  43. /** returns the width of the screen, delegating to screenSize */
  44. int getWidth() { return screenSize.width; }
  45. /** returns the height of the screen, delegating to screenSize */
  46. int getHeight() { return screenSize.height; }
  47. /**
  48. * To run the game, simply instantiate this class. It runs in its own
  49. * thread. You can instantiate multiple games at once. For the time being
  50. * the only way to end the game is to exit from the Java VM.
  51. *
  52. * @param mode Controls whether the game runs in demo mode or not. True
  53. * means it is a demo, false means it runs in normal 2 player mode.
  54. */
  55. public Game(String mode) {
  56. timer = new Timer(this);
  57. ePP = new EnergyPacketProducer(this);
  58. registry = new Registry(this);
  59. }
  60. public void run() {
  61. timer.start();
  62. ePP.start();
  63. while(true) {
  64. try {
  65. newRobot(3);
  66. Thread.sleep(15000);
  67. }
  68. catch (InterruptedException e) {}
  69. }
  70. }
  71. /**
  72. * add a robot to the game. This is a menu command.
  73. */
  74. void addRobot() {
  75. newRobot(3);
  76. }
  77. /**
  78. * resurrect the ships in the game. This is a menu command.
  79. */
  80. void resetShips() {
  81. Ship[] ships = registry.getShips();
  82. for (int i = 0; i < ships.length; i++) {
  83. Ship ship = ships[i];
  84. Pilot pilot = ship.getPilot();
  85. newShip(pilot);
  86. }
  87. }
  88. /**
  89. * leave the game. This is a menu command.
  90. */
  91. void quit() {
  92. System.exit(0);
  93. }
  94. void error(Object o) {
  95. System.err.println(o);
  96. }
  97. /**
  98. * returns a new player. With {@link #newRobot} and {@link
  99. * #newShip}, the only ways to make a Player, a Robot, or a Ship.
  100. * The structural invariant is that there should be no calls to
  101. * new of one of these three classes outside these three methods.
  102. */
  103. Player newPlayer(int number) {
  104. Player player = new Player(this, number);
  105. newShip(player);
  106. return player;
  107. }
  108. /**
  109. * returns a new robot. With {@link #newPlayer} and {@link
  110. * #newShip}, the only ways to make a Player, a Robot, or a Ship.
  111. * The structural invariant is that there should be no calls to
  112. * new of one of these three classes outside these three methods.
  113. */
  114. Robot newRobot(int number) {
  115. Robot robot = new Robot(this, number);
  116. newShip(robot);
  117. robot.start();
  118. return robot;
  119. }
  120. /**
  121. * returns a new ship. With {@link #newRobot} and {@link
  122. * #newPlayer}, the only ways to make a Player, a Robot, or a
  123. * Ship. The structural invariant is that there should be no
  124. * calls to new of one of these three classes outside these three
  125. * methods.
  126. */
  127. Ship newShip(Pilot pilot) {
  128. //
  129. // If there is an old ship (we're doing a reset), then remove it from
  130. // the registry.
  131. //
  132. Ship oldShip = pilot.getShip();
  133. if (! (oldShip == null))
  134. oldShip.die();
  135. Ship newShip = new Ship(this,
  136. Math.random() * getWidth(),
  137. Math.random() * getHeight(),
  138. Math.random() * Math.PI * 2);
  139. pilot.setShip(newShip);
  140. newShip.setPilot(pilot);
  141. return newShip;
  142. }
  143. void clockTick() {
  144. registry.clockTick();
  145. handleCollisions();
  146. }
  147. // collision detection
  148. void handleCollisions() {
  149. SpaceObject[] objects = registry.getObjects();
  150. SpaceObject objI, objJ;
  151. for (int i = 0; i < objects.length; i++) {
  152. objI = objects[i];
  153. for (int j = i + 1; j < objects.length; j++) {
  154. objJ = objects[j];
  155. if (objI instanceof Bullet && objJ instanceof Bullet)
  156. continue;
  157. if (isCollision(objI, objJ)) {
  158. if (objI instanceof Ship && objJ instanceof Ship)
  159. Ship.bounce((Ship)(objI), (Ship)(objJ));
  160. else {
  161. objI.handleCollision(objJ);
  162. objJ.handleCollision(objI);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. /*
  169. * Is the distance between the two centers less than the sum of the two
  170. * radii. This is a cheap and dirty (i.e. wrong) implementation of this.
  171. */
  172. static boolean isCollision(SpaceObject a, SpaceObject b) {
  173. return (Math.abs(a.getXPos() - b.getXPos()) +
  174. Math.abs(a.getYPos() - b.getYPos())) <
  175. (a.getSize()/2 + b.getSize()/2);
  176. }
  177. }