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.

123 lines
3.4 KiB

  1. package playground;
  2. import java.awt.Color;
  3. import org.apache.logging.log4j.LogManager;
  4. import org.apache.logging.log4j.Logger;
  5. import collider.RectCollider;
  6. import controller.EgoController;
  7. import controller.ReboundController;
  8. import gameobjects.FallingStar;
  9. import gameobjects.GameObject;
  10. import gameobjects.RectObject;
  11. /**
  12. * solution example for Breakout game
  13. * <ul>
  14. * <li>colored bricks
  15. * <li>rebound depends on position where ball hits ego object
  16. * <li>rebound on brick hit
  17. *
  18. * not implemented: penalty if balls hits ground, no score for removed bricks, no bonus items, no
  19. * lives.
  20. * </ul>
  21. *
  22. */
  23. public class LevelBreakout0 extends LevelBreakoutBase {
  24. private static Logger logger = LogManager.getLogger(LevelBreakoutBase.class);
  25. protected GameObject createEgoObject() {
  26. RectObject ro = new RectObject("ego", this, 350, 550, 0, 0, 80, 10, Color.BLUE);
  27. ro.generateColliders();
  28. EgoController ec = new EgoController(80,10);
  29. ro.addController(ec);
  30. logger.info("ego created.");
  31. return ro;
  32. }
  33. protected GameObject createBall() {
  34. GameObject co = new FallingStar("ball1", this, 350, 350, 100, 100, Color.RED, 5);
  35. co.addController(new ReboundController());
  36. logger.info("ball created.");
  37. return co;
  38. }
  39. /**
  40. * creates bricks. For collision {@link RectCollider} is used.
  41. */
  42. @Override
  43. protected GameObject createBrick(int row, int column) {
  44. double xSize = 60;
  45. double ySize = 30;
  46. double xStart = 40;
  47. double yStart = 40;
  48. double space = 5;
  49. Color c = Color.BLUE;
  50. RectObject ro = new RectObject("brick" + row + "/" + column, this, xStart + column * (xSize + space),
  51. yStart + row * (ySize + space), 0, 0, xSize - 4, ySize - 4, c);
  52. RectCollider rc = new RectCollider("egal", ro, xSize - 4, ySize - 4);
  53. ro.addCollider(rc);
  54. return ro;
  55. }
  56. /**
  57. * lets ball bounce in Y direction, deletes brick and creates a red/blue colored explosion.
  58. */
  59. @Override
  60. protected void actionIfBallHitsBrick(GameObject ball, GameObject brick) {
  61. ball.setVY(ball.getVY() * -1); // bounce effect for ball
  62. this.deleteObject(brick.getId()); // remove brick from field
  63. logger.debug("deleted brick " + brick.getId());
  64. }
  65. /**
  66. * Let the ball bounce off in Y direction.
  67. *
  68. */
  69. @Override
  70. protected void actionIfBallHitsEgo(GameObject ball, GameObject ego) {
  71. double ballY = ball.getY();
  72. double egoY = ego.getY();
  73. ball.setY(ballY < egoY ? ballY - 10 : ballY + 10); // radius 5 hard coded.
  74. ball.setVY(ball.getVY() * -1);
  75. logger.debug("ball bounces of ego object.");
  76. }
  77. /**
  78. * Prepares a Breakout level with a 3 x 3 matrix of blocks on top. This method relies on the
  79. * methods {@link #createEgoObject()}, {@link #createBall()} and {@link #createBrick(int, int)}, among others, which
  80. * are meant to be overwritten in subclasses.
  81. *
  82. * @param level String passes by the game engine (not used currently and can be ignored).
  83. */
  84. @Override
  85. public void prepareLevel(String level) {
  86. for (int y = 3; y < 6; y++) {
  87. for (int x = 0; x < 3; x++) {
  88. logger.trace("trying to create brick X, Y (" + x + "," + y + ")");
  89. GameObject brick = this.createBrick(x, y);
  90. this.addObject(brick);
  91. }
  92. }
  93. this.ego = this.createEgoObject();
  94. this.ball = this.createBall();
  95. this.addObject(this.ego);
  96. this.addObject(this.ball);
  97. logger.info("level preperation succeeded.");
  98. }
  99. }