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.

152 lines
4.2 KiB

2 years ago
  1. package playground;
  2. import java.awt.Color;
  3. import java.util.Random;
  4. import controller.BreakoutController;
  5. import controller.LimitedTimeController;
  6. import controller.ReboundController;
  7. import gameobjects.GameObject;
  8. import gameobjects.RectObject;
  9. public class BreakoutLevel2 extends BreakoutLevelBaseAdvanced {
  10. @Override
  11. protected int calcNrBricksX() {
  12. return 6;
  13. }
  14. @Override
  15. protected int calcNrBricksY() {
  16. return 5;
  17. }
  18. @Override
  19. protected double getBrickSizeX() {
  20. return 60.0;
  21. }
  22. @Override
  23. protected double getBrickSizeY() {
  24. return 30.0;
  25. }
  26. @Override
  27. protected double getBrickStartX() {
  28. return 90.0;
  29. }
  30. @Override
  31. protected double getBrickStartY() {
  32. return 60.0;
  33. }
  34. /** Create Explosion effect with random Colors that lasts 2 Seconds.
  35. *
  36. * @param ball GameObject
  37. * @param brick GameObject
  38. */
  39. @Override
  40. protected void actionIfBallHitsBrick(GameObject ball, GameObject brick) {
  41. //logger.info(brick.getId()+" is hitted and deleted!");
  42. ball.setVY(ball.getVY() * -1.0);
  43. int max = 240;
  44. int min = -240;
  45. Random rand = new Random();
  46. for(int i = 0; i < 20; i++) {
  47. Color randColor = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
  48. gameobjects.FallingStar explosion = new gameobjects.FallingStar("explosion"+gameTime+i, this, brick.getX(), brick.getY(), rand.nextInt(max - min + 1) + min, rand.nextInt(max - min + 1) + min, randColor, 2);
  49. explosion.addController(new LimitedTimeController(gameTime, 2));
  50. this.addObjectNow(explosion);
  51. this.deleteObject(brick.getId());
  52. }
  53. }
  54. /** Lets Ball bounce of from Ego.
  55. * If the right or left end part is hitted, the ball bounces of in the end part direction doesn't matter from which site
  56. *
  57. * @param ball Gameobject
  58. * @param ego GameObject
  59. */
  60. @Override
  61. protected void actionIfBallHitsEgo(GameObject ball, GameObject ego) {
  62. RectObject egoCopyCastedInRectObject = (RectObject) ego;
  63. double egoMiddleX = ego.getX();
  64. double ballX = ball.getX();
  65. double egoWidth = egoCopyCastedInRectObject.getWidth();// Maybe an easier and cleaner way instead of casting an new
  66. double egoHalfWidth = egoWidth / 2.0;
  67. double egoEndPerc = 45.0;
  68. double egoDistEndPartFromMiddle = (egoEndPerc * egoHalfWidth) / 100;
  69. double egoEndPartStartLeft = egoMiddleX - egoDistEndPartFromMiddle;
  70. double egoEndPartStartRight = egoMiddleX + egoDistEndPartFromMiddle;
  71. if ((ballX < egoEndPartStartLeft && (ball.getVX() > 0.0)) || (ballX > egoEndPartStartRight && (ball.getVX() < 0.0))) {
  72. ball.setVX(ball.getVX() * -1.0);
  73. }
  74. ball.setVY(ball.getVY() * -1.0);
  75. }
  76. /** Creates Ego object with an extra Controller for Breakout.
  77. *
  78. * @return ego
  79. */
  80. @Override
  81. protected GameObject createEgoObject() {
  82. RectObject ego = new RectObject("ego", this, 350, 550, 0, 0, 60, 10, Color.blue);
  83. ego.addController(new BreakoutController(ego.getWidth(), ego.getHeight()));
  84. ego.addCollider(new collider.RectCollider(ego.getId(), ego, ego.getWidth(), ego.getHeight()));
  85. return ego;
  86. }
  87. /** Creates Ball.
  88. *
  89. * @return ball
  90. */
  91. @Override
  92. protected GameObject createBall() {
  93. gameobjects.FallingStar ball = new gameobjects.FallingStar("ball", this, 350, 350, 130, 130, Color.red, 5);
  94. ball.addController(new ReboundController());
  95. return ball;
  96. }
  97. /** Creates Brick with a random color.
  98. *
  99. * @param row int
  100. * @param column int
  101. *
  102. * @return brick
  103. */
  104. @Override
  105. protected GameObject createBrick(int row, int column) {
  106. double rnd = Math.random();
  107. Color clr;
  108. if (rnd <= .25) {
  109. clr = Color.red;
  110. } else {
  111. if (rnd <= .5) {
  112. clr = Color.blue;
  113. } else {
  114. if (rnd <= .75) {
  115. clr = Color.yellow;
  116. } else {
  117. clr = Color.green;
  118. }
  119. }
  120. }
  121. RectObject brick = new RectObject("brick" + row + column, this, getBrickStartX() + (row * (getBrickSizeX() * 2)), getBrickStartY() + (column * (getBrickSizeY() * 2)), 0, 0, getBrickSizeX(), getBrickSizeY(), clr);
  122. brick.addCollider(new collider.RectCollider(brick.getId(), brick, brick.getWidth(), brick.getHeight()));
  123. return brick;
  124. }
  125. }