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.

173 lines
4.9 KiB

  1. package playground;
  2. import gameobjects.*;
  3. import java.util.LinkedList;
  4. import org.apache.logging.log4j.LogManager;
  5. import org.apache.logging.log4j.Logger;
  6. import java.awt.Graphics2D;
  7. import controller.*;
  8. public abstract class BreakoutLevelBase extends Playground {
  9. /**
  10. * instance of the ball, needs to be set by {@link #prepareLevel(String) }
  11. *
  12. */
  13. protected GameObject ball = null,
  14. /**
  15. * instance of the ego objects, needs to be set by {@link #prepareLevel(String) }
  16. *
  17. */
  18. ego = null;
  19. private static Logger logger = LogManager.getLogger(BreakoutLevelBase.class);
  20. public BreakoutLevelBase() {
  21. super();
  22. this.canvasX = this.preferredSizeX();
  23. this.canvasY = this.preferredSizeY();
  24. }
  25. /**
  26. * signals to game engine that the game has finished by game over. called every game loop. default
  27. * implementation is always false.
  28. *
  29. * @return false
  30. */
  31. public boolean gameOver() {
  32. return false;
  33. }
  34. /**
  35. * signals to game engine that the game has finished by success. called every game loop. default
  36. * implementation is always false.
  37. *
  38. * @return false
  39. */
  40. public boolean levelFinished() {
  41. return false;
  42. }
  43. /**
  44. * signals to game engine that the game has been requested to be reseted (restart). called every
  45. * game loop. default implementation is always false.
  46. *
  47. * @return false
  48. */
  49. public boolean resetRequested() {
  50. return false;
  51. }
  52. /**
  53. * unimplemented empty method called by game engine every loop.
  54. *
  55. */
  56. public void redrawLevel(Graphics2D g) {
  57. }
  58. /**
  59. * Signal that the level has a size of 700x700 pixels.
  60. *
  61. * @return x size of level 700
  62. */
  63. @Override
  64. public int preferredSizeX() {
  65. return 700;
  66. }
  67. /**
  68. * Signal that the level has a size of 700x700 pixels.
  69. *
  70. * @return y size of level 700
  71. */
  72. @Override
  73. public int preferredSizeY() {
  74. return 700;
  75. }
  76. /**
  77. * Method that gets called by applyGameLogic() whenever the ball collides with a brick.
  78. *
  79. *
  80. * @param ball A reference to the current ball object
  81. * @param brick A reference to the ego object
  82. */
  83. protected abstract void actionIfBallHitsBrick(GameObject ball, GameObject brick);
  84. /**
  85. * Method that gets called by applyGameLogic() whenever the ball collides with the ego object.
  86. *
  87. * @param ball A reference to the current ball object
  88. * @param ego A reference to the ego object
  89. */
  90. protected abstract void actionIfBallHitsEgo(GameObject ball, GameObject ego);
  91. /**
  92. * checks for interactions between GameObjects; notably ball with ego and ball with brick.
  93. * In case of detected collisions, it calls either {@link #actionIfBallHitsBrick(GameObject, GameObject)}
  94. * or {@link #actionIfBallHitsEgo(GameObject, GameObject)}.
  95. * Called every game loop.
  96. */
  97. @Override
  98. public void applyGameLogic() {
  99. LinkedList<GameObject> bricks = collectObjects("brick", false);
  100. for (GameObject brick : bricks) {
  101. if (this.ball.collisionDetection(brick)) {
  102. logger.trace("Collision detected of ball and brick " + brick.getId());
  103. this.actionIfBallHitsBrick(this.ball, brick);
  104. }
  105. }
  106. if (this.ego.collisionDetection(ball)) {
  107. logger.trace("Collision detected of ball and ego");
  108. this.actionIfBallHitsEgo(this.ball, this.ego);
  109. }
  110. }
  111. /**
  112. * Creates the ego object and returns it, called by {@link #prepareLevel}. Does NOT add the ego
  113. * object to the playground, but returns it.
  114. *
  115. * @return The created ego object instance (of class {@link RectObject} with
  116. * {@link EgoController}.
  117. */
  118. protected abstract GameObject createEgoObject();
  119. /**
  120. * Creates the ball object and returns it, called by #prepareLevel. Does NOT add the ball object
  121. * to the playground, but returns it.
  122. *
  123. * @return The created ball object instance (of class {@link FallingStar})
  124. */
  125. protected abstract GameObject createBall();
  126. /**
  127. * Creates the GameObject (RectObject) instance representing a single brick at a certain grid
  128. * position. The brick is NOT added here, but returned.
  129. *
  130. * @param row row position in the grid, ranges from 0 to calcNrBricksY()-1
  131. * @param column column position in the grid of bricks, ranges from 0 to calcNrBricksX()-1
  132. * @return The GameObject instance (really a RectObject) representing the created brick.
  133. */
  134. protected abstract GameObject createBrick(int row, int column);
  135. /**
  136. * Prepares a generic Breakout-Type level. This method relies on the methods {@link #createEgoObject()},
  137. * {@link #createBall} and {@link #createBrick}, among others, which are meant to be overwritten
  138. * in subclasses. <br>
  139. * Attention: the attributes {@link #ball} and {@link #ego} need to be set properly to GameObject
  140. * instances when implementing this method {@link #prepareLevel(String)}.
  141. *
  142. * @param level String passes by the game engine (not used currently and can be ignored).
  143. */
  144. @Override
  145. abstract public void prepareLevel(String level);
  146. }