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.

127 lines
2.8 KiB

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