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.

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