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.

SpaceObject.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. SpaceObject.java
  13. Part of the Spacewar system.
  14. */
  15. package spacewar;
  16. /**
  17. * SpaceObjects are objects that float around in space. They support the
  18. * minimal SpaceObject protocol, having to do with position, velocity,
  19. * size and liveness. They are constructed with game, position, velocity
  20. * and size. When constructed, a spaceobject adds itself to the registry.
  21. *
  22. * When it dies, a spaceobject removes itself from the registry. But note
  23. * that it doesn't decide when to die, subclasses do that.
  24. *
  25. * The display aspects actually draw the space object on the screen and say
  26. * how much space it takes up there.
  27. */
  28. abstract class SpaceObject {
  29. private Game game;
  30. private double xPos, yPos, oldXPos, oldYPos, xVel, yVel;
  31. private boolean alive;
  32. SpaceObject (Game theGame, double xP, double yP, double xV, double yV) {
  33. game = theGame;
  34. xPos = xP;
  35. yPos = yP;
  36. oldXPos = xP;
  37. oldYPos = yP;
  38. xVel = xV;
  39. yVel = yV;
  40. alive = true;
  41. getGame().getRegistry().register(this);
  42. }
  43. Game getGame() { return game; }
  44. double getXPos() { return xPos; }
  45. double getYPos() { return yPos; }
  46. double getOldXPos() { return oldXPos; }
  47. double getOldYPos() { return oldYPos; }
  48. double getXVel() { return xVel; }
  49. double getYVel() { return yVel; }
  50. void setXVel (double n) { xVel = n; }
  51. void setYVel (double n) { yVel = n; }
  52. boolean isAlive() { return alive; }
  53. void setIsAlive(boolean n) { alive = n; }
  54. /**
  55. * Move 1 unit of time's worth of distance. I.e. increment xPos by xVel
  56. * and yPos by yVel. If we move off an edge of the screen move us back
  57. * in the opposite edge.
  58. */
  59. void clockTick() {
  60. oldXPos = xPos;
  61. oldYPos = yPos;
  62. xPos = (xPos + xVel) % getGame().getWidth();
  63. if(xPos < 0)
  64. xPos += getGame().getWidth();
  65. yPos = (yPos + yVel) % getGame().getHeight();
  66. if(yPos < 0)
  67. yPos += getGame().getHeight();
  68. }
  69. void accelerate(double dXVel, double dYVel) {
  70. xVel += dXVel;
  71. yVel += dYVel;
  72. }
  73. void die() {
  74. getGame().getRegistry().unregister(this);
  75. }
  76. abstract int getSize();
  77. /** resolve the effects of colliding with a space object.
  78. * @param obj the space object that this object is colliding with.
  79. */
  80. abstract void handleCollision(SpaceObject obj);
  81. }