Browse Source

Solved HA 07. Added BreakoutLevel and Game.

master
fdai7303 2 years ago
parent
commit
28aaafb5df
  1. 2
      GameProject/src/base/BreakoutGame.java
  2. 5
      GameProject/src/log4j2.xml
  3. 121
      GameProject/src/playground/BreakoutLevel1.java

2
GameProject/src/base/BreakoutGame.java

@ -19,7 +19,7 @@ public class BreakoutGame extends GameLoop {
@Override @Override
public void defineLevels() { public void defineLevels() {
this.resetLevels(); // removes Level1 added by superclass constructor this.resetLevels(); // removes Level1 added by superclass constructor
// this.addLevel(new BreakoutLevel1()); // FIXME add this as soon as your level exists
this.addLevel(new BreakoutLevel1()); // FIXME add this as soon as your level exists
} }
/** /**

5
GameProject/src/log4j2.xml

@ -28,6 +28,11 @@
<Logger name="playground.LevelMovingHitObjects" level="debug"> <Logger name="playground.LevelMovingHitObjects" level="debug">
</Logger> </Logger>
<Logger name="playground.BreakoutLevel1" level="debug">
</Logger>
<Logger name="playground.BreakoutLevel1" level="trace">
</Logger>
</loggers> </loggers>
</configuration> </configuration>

121
GameProject/src/playground/BreakoutLevel1.java

@ -0,0 +1,121 @@
package playground;
import java.awt.Color;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import gameobjects.FallingStar;
import gameobjects.GameObject;
import gameobjects.RectObject;
import collider.RectCollider;
import controller.EgoController;
public class BreakoutLevel1 extends BreakoutLevelBase {
private static Logger logger = LogManager.getLogger(BreakoutLevel1.class);
/**
* Method that gets called by applyGameLogic() whenever the ball collides with a brick.
*
*
* @param ball A reference to the current ball object
* @param brick A reference to the ego object
*/
@Override
protected void actionIfBallHitsBrick(GameObject ball, GameObject brick) {
// TODO Auto-generated method stub
this.ball.setVY(this.ball.getVY()*-1);
this.deleteObject(brick.getId());
logger.trace("Collision detected of ball and brick " + brick.getId());
}
/**
* Method that gets called by applyGameLogic() whenever the ball collides with the ego object.
*
* @param ball A reference to the current ball object
* @param ego A reference to the ego object
*/
@Override
protected void actionIfBallHitsEgo(GameObject ball, GameObject ego) {
// TODO Auto-generated method stub
this.ball.setVY(this.ball.getVY()*-1);
logger.trace("Collision detected of ball and ego ");
}
/**
* Creates the ego object and returns it, called by {@link #prepareLevel}. Does NOT add the ego
* object to the playground, but returns it.
*
* @return The created ego object instance (of class {@link RectObject} with
* {@link EgoController}.
*/
@Override
protected GameObject createEgoObject() {
// TODO Auto-generated method stub
RectObject egoObject_BL1 = new RectObject("egoObject_BL1", this, 350, 550, 0, 0, 80, 10, Color.BLUE);
egoObject_BL1.addController(new controller.EgoController(80, 10));
egoObject_BL1.addCollider(new RectCollider("egoObject_BL1_Col", egoObject_BL1, 80, 10));
logger.debug("Ego created ");
return egoObject_BL1;
}
/**
* Creates the ball object and returns it, called by #prepareLevel. Does NOT add the ball object
* to the playground, but returns it.
*
* @return The created ball object instance (of class {@link FallingStar})
*/
@Override
protected GameObject createBall() {
// TODO Auto-generated method stub
gameobjects.FallingStar BallObject_BL1 = new gameobjects.FallingStar ("BallObject_BL1", this, 350, 350, 120, 120, Color.RED, 5);
BallObject_BL1.addController(new controller.ReboundController());
logger.debug("Ball created ");
return BallObject_BL1;
}
/**
* Creates the GameObject (RectObject) instance representing a single brick at a certain grid
* position. The brick is NOT added here, but returned.
*
* @param row row position in the grid, ranges from 0 to calcNrBricksY()-1
* @param column column position in the grid of bricks, ranges from 0 to calcNrBricksX()-1
* @return The GameObject instance (really a RectObject) representing the created brick.
*/
@Override
protected GameObject createBrick(int row, int column) {
// TODO Auto-generated method stub
RectObject brick = new RectObject("brick" + row + column, this, column * 70 - 30, row * 40, 0, 0, 60, 30, Color.GREEN);
brick.addCollider(new RectCollider("brick" + row + column + "Col", brick, 60, 30));
logger.debug("Brick created: " + brick.getId());
return brick;
}
/**
* Prepares a generic Breakout-Type level. This method relies on the methods {@link #createEgoObject()},
* {@link #createBall} and {@link #createBrick}, among others. <br>
* Attention: the attributes {@link #ball} and {@link #ego} need to be set properly to GameObject
* instances when implementing this method {@link #prepareLevel(String)}.
*
* @param level String passes by the game engine (not used currently and can be ignored).
*/
@Override
public void prepareLevel(String level) {
this.ego = this.createEgoObject();
this.addObject(ego);
this.ball = this.createBall();
this.addObject(ball);
for (int i = 1; i <= 10; i++) {
for (int k = 1; k <= 3; k++) {
this.addObject(this.createBrick(k, i));
}
}
// TODO Auto-generated method stub
}
}
Loading…
Cancel
Save