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.

110 lines
3.2 KiB

  1. package controller;
  2. import gameobjects.GameObject;
  3. import playground.Playground;
  4. import org.apache.logging.log4j.Logger;
  5. import org.apache.logging.log4j.LogManager;
  6. /**
  7. * Class that controls the LOGICAL behavior of an object independently of how it is displayed or
  8. * drawn. The most important method here is {@link #updateObject}: this method is, by various
  9. * indirections, called exactly once per game time step for every object that is on the playground.
  10. * It has, by virtue of the member variables {@link #gameObject} full access to
  11. * <ul>
  12. * <li>the object it is controlling
  13. * <li>the playground this object belongs to
  14. * </ul>
  15. * Typically, updateObject would check whether an object leaves the screen to react appropriately.
  16. * In that case the object can be marked for deletion (by adding it to the flag "deleted" that is
  17. * always defined for any playground), but of course other reactions are possible like rebounding,
  18. * emerging on the other side, ...
  19. */
  20. public abstract class ObjectController {
  21. protected GameObject gameObject = null;
  22. protected String dummy = "";
  23. private static Logger logger = LogManager.getLogger(ObjectController.class);
  24. public void setObject(GameObject gameObject) {
  25. this.gameObject = gameObject;
  26. }
  27. public void setDummy(String x) {
  28. logger.debug("DUMMY called!!");
  29. this.dummy = x;
  30. logger.debug("DUMMY is now:" + dummy);
  31. }
  32. /**
  33. * Is called once every game time step by the game itself. NEVER call this directly, not
  34. * necessary!<br>
  35. * The method can do whatever it likes, including nothing. The attribute {@link #gameObject}
  36. * contains a reference to the controlled object, which allows access to the Playground the object
  37. * belongs to (useful for getting the pixel size in x and y of the playing field.<br>
  38. * <strong>Recommended:</strong> when implementing this method, call at the end
  39. * {@link #applySpeedVector() } method. This is a helper method that sets the new x,y coordinates
  40. * for the {@link #gameObject} correctly.
  41. */
  42. public abstract void updateObject();
  43. /**
  44. * Convenience method: simply moves the object forward one step from its present position, using
  45. * its present speed.
  46. */
  47. public void applySpeedVector() {
  48. double ts = this.getPlayground().getTimestep();
  49. this.setX(this.getX() + this.getVX() * ts);
  50. gameObject.setY(this.getY() + this.getVY() * ts);
  51. }
  52. public double getTimestep() {
  53. return this.gameObject.getPlayground().getTimestep();
  54. }
  55. public double getX() {
  56. return this.gameObject.getX();
  57. }
  58. public double getY() {
  59. return this.gameObject.getY();
  60. }
  61. public double getVX() {
  62. return this.gameObject.getVX();
  63. }
  64. public double getVY() {
  65. return this.gameObject.getVY();
  66. }
  67. public void setX(double x) {
  68. this.gameObject.setX(x);
  69. }
  70. public void setY(double y) {
  71. this.gameObject.setY(y);
  72. }
  73. public void setVX(double vx) {
  74. this.gameObject.setVX(vx);
  75. }
  76. public void setVY(double vy) {
  77. this.gameObject.setVY(vy);
  78. }
  79. public Playground getPlayground() {
  80. return this.gameObject.getPlayground();
  81. }
  82. public void setPlayground(Playground playground) {
  83. this.gameObject.setPlayground(playground);
  84. }
  85. }