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
152 lines
4.2 KiB
package playground;
|
|
|
|
import java.awt.Color;
|
|
import java.util.Random;
|
|
|
|
import controller.BreakoutController;
|
|
import controller.LimitedTimeController;
|
|
import controller.ReboundController;
|
|
import gameobjects.GameObject;
|
|
import gameobjects.RectObject;
|
|
|
|
public class BreakoutLevel2 extends BreakoutLevelBaseAdvanced {
|
|
|
|
@Override
|
|
protected int calcNrBricksX() {
|
|
return 6;
|
|
}
|
|
|
|
@Override
|
|
protected int calcNrBricksY() {
|
|
return 5;
|
|
}
|
|
|
|
@Override
|
|
protected double getBrickSizeX() {
|
|
return 60.0;
|
|
}
|
|
|
|
@Override
|
|
protected double getBrickSizeY() {
|
|
return 30.0;
|
|
}
|
|
|
|
@Override
|
|
protected double getBrickStartX() {
|
|
return 90.0;
|
|
}
|
|
|
|
@Override
|
|
protected double getBrickStartY() {
|
|
return 60.0;
|
|
}
|
|
|
|
|
|
/** Create Explosion effect with random Colors that lasts 2 Seconds.
|
|
*
|
|
* @param ball GameObject
|
|
* @param brick GameObject
|
|
*/
|
|
@Override
|
|
protected void actionIfBallHitsBrick(GameObject ball, GameObject brick) {
|
|
//logger.info(brick.getId()+" is hitted and deleted!");
|
|
ball.setVY(ball.getVY() * -1.0);
|
|
int max = 240;
|
|
int min = -240;
|
|
Random rand = new Random();
|
|
|
|
for(int i = 0; i < 20; i++) {
|
|
Color randColor = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
|
|
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);
|
|
explosion.addController(new LimitedTimeController(gameTime, 2));
|
|
this.addObjectNow(explosion);
|
|
|
|
this.deleteObject(brick.getId());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Lets Ball bounce of from Ego.
|
|
* If the right or left end part is hitted, the ball bounces of in the end part direction doesn't matter from which site
|
|
*
|
|
* @param ball Gameobject
|
|
* @param ego GameObject
|
|
*/
|
|
@Override
|
|
protected void actionIfBallHitsEgo(GameObject ball, GameObject ego) {
|
|
RectObject egoCopyCastedInRectObject = (RectObject) ego;
|
|
double egoMiddleX = ego.getX();
|
|
double ballX = ball.getX();
|
|
double egoWidth = egoCopyCastedInRectObject.getWidth();// Maybe an easier and cleaner way instead of casting an new
|
|
double egoHalfWidth = egoWidth / 2.0;
|
|
|
|
double egoEndPerc = 45.0;
|
|
double egoDistEndPartFromMiddle = (egoEndPerc * egoHalfWidth) / 100;
|
|
double egoEndPartStartLeft = egoMiddleX - egoDistEndPartFromMiddle;
|
|
double egoEndPartStartRight = egoMiddleX + egoDistEndPartFromMiddle;
|
|
|
|
if ((ballX < egoEndPartStartLeft && (ball.getVX() > 0.0)) || (ballX > egoEndPartStartRight && (ball.getVX() < 0.0))) {
|
|
ball.setVX(ball.getVX() * -1.0);
|
|
}
|
|
|
|
ball.setVY(ball.getVY() * -1.0);
|
|
}
|
|
|
|
/** Creates Ego object with an extra Controller for Breakout.
|
|
*
|
|
* @return ego
|
|
*/
|
|
@Override
|
|
protected GameObject createEgoObject() {
|
|
RectObject ego = new RectObject("ego", this, 350, 550, 0, 0, 60, 10, Color.blue);
|
|
ego.addController(new BreakoutController(ego.getWidth(), ego.getHeight()));
|
|
ego.addCollider(new collider.RectCollider(ego.getId(), ego, ego.getWidth(), ego.getHeight()));
|
|
return ego;
|
|
}
|
|
|
|
/** Creates Ball.
|
|
*
|
|
* @return ball
|
|
*/
|
|
@Override
|
|
protected GameObject createBall() {
|
|
gameobjects.FallingStar ball = new gameobjects.FallingStar("ball", this, 350, 350, 130, 130, Color.red, 5);
|
|
ball.addController(new ReboundController());
|
|
return ball;
|
|
}
|
|
|
|
/** Creates Brick with a random color.
|
|
*
|
|
* @param row int
|
|
* @param column int
|
|
*
|
|
* @return brick
|
|
*/
|
|
@Override
|
|
protected GameObject createBrick(int row, int column) {
|
|
|
|
double rnd = Math.random();
|
|
Color clr;
|
|
if (rnd <= .25) {
|
|
clr = Color.red;
|
|
} else {
|
|
if (rnd <= .5) {
|
|
clr = Color.blue;
|
|
} else {
|
|
if (rnd <= .75) {
|
|
clr = Color.yellow;
|
|
} else {
|
|
clr = Color.green;
|
|
}
|
|
}
|
|
}
|
|
|
|
RectObject brick = new RectObject("brick" + row + column, this, getBrickStartX() + (row * (getBrickSizeX() * 2)), getBrickStartY() + (column * (getBrickSizeY() * 2)), 0, 0, getBrickSizeX(), getBrickSizeY(), clr);
|
|
brick.addCollider(new collider.RectCollider(brick.getId(), brick, brick.getWidth(), brick.getHeight()));
|
|
|
|
return brick;
|
|
}
|
|
|
|
}
|