Vorlagecode für die einzelnen HAs in Prog2.
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.

125 lines
2.8 KiB

  1. package collider;
  2. import gameobjects.GameObject;
  3. import playground.Playground;
  4. import controller.ObjectController;
  5. /**
  6. * abstract base class for all Colliders to detect collisions between GameObjects
  7. *
  8. *
  9. */
  10. public abstract class Collider {
  11. /** unique internal name for Collider */
  12. public String id = null;
  13. /** GameObject it belongs to */
  14. protected GameObject gameobject = null;
  15. /** PlayGround instance it belongs to */
  16. protected Playground playground = null;
  17. /** the ObjectController to the corresponding GameObject (can be null) */
  18. protected ObjectController controller = null;
  19. protected double dx = 0.;
  20. double dy = 0.;
  21. /**
  22. *
  23. * @param id unique name for Collider (internally)
  24. * @param o GameObject instance it belongs to
  25. */
  26. public Collider(String id, GameObject o) {
  27. this.gameobject = o;
  28. this.id = id;
  29. this.controller = o.getObjectController();
  30. this.playground = o.getPlayground();
  31. }
  32. /**
  33. * setter for offset values to be used relative to GameObject center. default is zero.
  34. *
  35. * @param dx offset in X direction (default 0)
  36. * @param dy offset in Y direction (default 0)
  37. * @return this instance of Collider
  38. */
  39. public Collider setOffsets(double dx, double dy) {
  40. this.dx = dx;
  41. this.dy = dy;
  42. return this;
  43. }
  44. public String toString() {
  45. return "baseColl";
  46. }
  47. /**
  48. * returns the corresponding game objects X coordinate (center) plus this colliders offset in X
  49. * (probably zero).
  50. *
  51. * @return X value
  52. */
  53. public double getX() {
  54. return this.gameobject.getX() + this.dx;
  55. }
  56. /**
  57. * returns the corresponding game objects Y coordinate (center) plus this colliders offset in Y
  58. * (probably zero).
  59. *
  60. * @return Y value
  61. */
  62. public double getY() {
  63. return this.gameobject.getY() + this.dy;
  64. }
  65. /**
  66. * returns the internal unique name
  67. *
  68. * @return the String with the name
  69. */
  70. public String getId() {
  71. return id;
  72. }
  73. /**
  74. * setter for corresponding GameObject
  75. *
  76. * @param gameObject to be saved in attribute
  77. */
  78. public void setObject(GameObject gameObject) {
  79. this.gameobject = gameObject;
  80. }
  81. /**
  82. * setter for GameController
  83. *
  84. * @param controller to be saved in attribute
  85. */
  86. public void setController(ObjectController controller) {
  87. this.controller = controller;
  88. }
  89. /**
  90. * setter for Playground instance this collider belongs to
  91. *
  92. * @param playground instance to be stored in attribute
  93. */
  94. public void setPlayground(Playground playground) {
  95. this.playground = playground;
  96. }
  97. /**
  98. * checks the collission with another collider instance.
  99. *
  100. * @param other the instance to compare to
  101. * @return true if the colliders collide (touch or overlap)
  102. */
  103. abstract public boolean collidesWith(Collider other);
  104. }