4 Commits
5be60e1dd0
...
64624f4273
Author | SHA1 | Message | Date |
---|---|---|---|
Nico B | 64624f4273 |
HA07 sucessfully finished
|
3 years ago |
Nico B | 23bffafb1e |
new files for HA07
|
3 years ago |
jkonert | 2b56d72aa2 |
fixed small syntax issue with missing this.
|
3 years ago |
jkonert | f593d61f5b |
base files for HA07 added: BreakoutGame.java and BreakoutLevelBase.java
|
3 years ago |
4 changed files with 329 additions and 1 deletions
-
38GameProject/src/base/BreakoutGame.java
-
2GameProject/src/log4j2.xml
-
117GameProject/src/playground/BreakoutLevel1.java
-
173GameProject/src/playground/BreakoutLevelBase.java
@ -0,0 +1,38 @@ |
|||
package base; |
|||
import java.io.IOException; |
|||
import org.apache.logging.log4j.LogManager; |
|||
import org.apache.logging.log4j.Logger; |
|||
import playground.*; |
|||
|
|||
|
|||
/** |
|||
* main class to start a game with only one level. |
|||
* |
|||
*/ |
|||
public class BreakoutGame extends GameLoop { |
|||
|
|||
private static Logger logger = LogManager.getLogger(BreakoutGame.class); |
|||
|
|||
/** |
|||
* adds only one level to play ({@link playground.LevelBreakout1}). |
|||
*/ |
|||
@Override |
|||
public void defineLevels() { |
|||
this.resetLevels(); // removes Level1 added by superclass constructor |
|||
this.addLevel(new BreakoutLevel1()); // FIXME add this as soon as your level exists |
|||
} |
|||
|
|||
/** |
|||
* starts this game. |
|||
* |
|||
* @param args command line parameters (forwarded to {@link GameLoop#runGame(String[])}). |
|||
* @throws IOException if highscore.txt file cannot be written or accessed, the exception is |
|||
* thrown (and game ends). |
|||
*/ |
|||
public static void main(String[] args) throws IOException { |
|||
GameLoop myGame = new BreakoutGame(); |
|||
logger.info("BreakoutGame program started."); |
|||
myGame.runGame(args); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,117 @@ |
|||
package playground; |
|||
|
|||
import java.awt.Color; |
|||
|
|||
import org.apache.logging.log4j.LogManager; |
|||
import org.apache.logging.log4j.Logger; |
|||
|
|||
import collider.RectCollider; |
|||
import controller.EgoController; |
|||
import controller.ReboundController2; |
|||
import gameobjects.FallingStar; |
|||
import gameobjects.GameObject; |
|||
import gameobjects.RectObject; |
|||
|
|||
/** |
|||
* Class that generates a level with one egoObject, ball and ten bricks which need to be destroyed with the ball. |
|||
*/ |
|||
|
|||
|
|||
public class BreakoutLevel1 extends BreakoutLevelBase { |
|||
|
|||
|
|||
private static Logger logger2 = LogManager.getLogger(BreakoutLevel1.class); |
|||
|
|||
/** |
|||
* This method defines the behavior when the ball hits a brick. It allows the ball to destroy the brick and move on in opposite direction. |
|||
* |
|||
* @param ball GameObject the one ball of the game. |
|||
* @param brick GameObject the brick that is hit. |
|||
*/ |
|||
@Override |
|||
protected void actionIfBallHitsBrick(GameObject ball, GameObject brick) { |
|||
ball.setVY(ball.getVY() * -1); |
|||
this.deleteObject(brick.id); |
|||
} |
|||
|
|||
/** |
|||
* This method defines the behavior when the ball hits the EgoObject. |
|||
* |
|||
* @param ball GameObject the one ball in the game. |
|||
* @param ego GameObject the object the player controls. |
|||
* |
|||
*/ |
|||
@Override |
|||
protected void actionIfBallHitsEgo(GameObject ball, GameObject ego) { |
|||
ball.setVY(ball.getVY() * -1); |
|||
} |
|||
|
|||
/** |
|||
* This method creates a new RectObject. |
|||
* |
|||
* @return ego object. |
|||
*/ |
|||
@Override |
|||
protected GameObject createEgoObject() { |
|||
|
|||
RectObject ego = new RectObject("ego", this, 350d, 550d, 0d, 0d, 80d, 10d, Color.BLUE); |
|||
EgoController eController = new EgoController(80d, 10d); |
|||
ego.addController(eController); |
|||
RectCollider collider1 = new RectCollider("collider1", ego, 80d, 10d); |
|||
ego.addCollider(collider1); |
|||
logger2.info("sucessfully created ego object with controller and collider"); |
|||
return ego; |
|||
} |
|||
|
|||
/** |
|||
* This method creates a new ball. |
|||
* |
|||
* @return ball |
|||
*/ |
|||
@Override |
|||
protected GameObject createBall() { |
|||
FallingStar ball = new FallingStar("ball", this, 350d, 350d, 120d, 120d, Color.RED, 5d); |
|||
ReboundController2 bbound = new ReboundController2(); |
|||
ball.addController(bbound); |
|||
logger2.info("sucessfully created ball object with controller"); |
|||
return ball; |
|||
} |
|||
|
|||
/** |
|||
* This method creates a new brick. |
|||
* |
|||
* @param row int row for position of the brick. |
|||
* @param column int column for the position of the brick. |
|||
* |
|||
* @return brick. |
|||
*/ |
|||
@Override |
|||
protected GameObject createBrick(int row, int column) { |
|||
RectObject brick = new RectObject("brick" + row + column, this, 40d+ column * 65, 40d + row * 35, 0d, 0d, 60d, 30d, Color.GREEN); |
|||
RectCollider greenCollider = new RectCollider("greenCollider", brick, 60d, 30d); |
|||
brick.addCollider(greenCollider); |
|||
logger2.info("sucessfully created a brick object and collider"); |
|||
return brick; |
|||
} |
|||
|
|||
/** |
|||
* This method creates 3 rows and 10 columns with bricks. It also adds objects of ball and ego to Playground. |
|||
*/ |
|||
@Override |
|||
public void prepareLevel(String level) { |
|||
this.ego = createEgoObject(); |
|||
this.ball = createBall(); |
|||
this.addObject(this.ego); |
|||
this.addObject(this.ball); |
|||
logger2.info("added ego and ball object to the current level"); |
|||
|
|||
for (int i = 0; i < 3; i++) { |
|||
for (int j = 0; j < 10; j++) { |
|||
this.addObject(createBrick(i ,j)); |
|||
} |
|||
} |
|||
logger2.info("created all 30 bricks"); |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,173 @@ |
|||
package playground; |
|||
|
|||
import gameobjects.*; |
|||
import java.util.LinkedList; |
|||
import org.apache.logging.log4j.LogManager; |
|||
import org.apache.logging.log4j.Logger; |
|||
import java.awt.Graphics2D; |
|||
import controller.*; |
|||
|
|||
public abstract class BreakoutLevelBase extends Playground { |
|||
|
|||
/** |
|||
* instance of the ball, needs to be set by {@link #prepareLevel(String) } |
|||
* |
|||
*/ |
|||
protected GameObject ball = null, |
|||
/** |
|||
* instance of the ego objects, needs to be set by {@link #prepareLevel(String) } |
|||
* |
|||
*/ |
|||
ego = null; |
|||
|
|||
private static Logger logger = LogManager.getLogger(BreakoutLevelBase.class); |
|||
|
|||
public BreakoutLevelBase() { |
|||
super(); |
|||
this.canvasX = this.preferredSizeX(); |
|||
this.canvasY = this.preferredSizeY(); |
|||
} |
|||
|
|||
/** |
|||
* signals to game engine that the game has finished by game over. called every game loop. default |
|||
* implementation is always false. |
|||
* |
|||
* @return false |
|||
*/ |
|||
public boolean gameOver() { |
|||
return false; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* signals to game engine that the game has finished by success. called every game loop. default |
|||
* implementation is always false. |
|||
* |
|||
* @return false |
|||
*/ |
|||
public boolean levelFinished() { |
|||
return false; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* signals to game engine that the game has been requested to be reseted (restart). called every |
|||
* game loop. default implementation is always false. |
|||
* |
|||
* @return false |
|||
*/ |
|||
public boolean resetRequested() { |
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* unimplemented empty method called by game engine every loop. |
|||
* |
|||
*/ |
|||
public void redrawLevel(Graphics2D g) { |
|||
|
|||
} |
|||
|
|||
|
|||
|
|||
/** |
|||
* Signal that the level has a size of 700x700 pixels. |
|||
* |
|||
* @return x size of level 700 |
|||
*/ |
|||
@Override |
|||
public int preferredSizeX() { |
|||
return 700; |
|||
} |
|||
|
|||
/** |
|||
* Signal that the level has a size of 700x700 pixels. |
|||
* |
|||
* @return y size of level 700 |
|||
*/ |
|||
@Override |
|||
public int preferredSizeY() { |
|||
return 700; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 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 |
|||
*/ |
|||
protected abstract void actionIfBallHitsBrick(GameObject ball, GameObject brick); |
|||
|
|||
/** |
|||
* 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 |
|||
*/ |
|||
protected abstract void actionIfBallHitsEgo(GameObject ball, GameObject ego); |
|||
|
|||
/** |
|||
* checks for interactions between GameObjects; notably ball with ego and ball with brick. |
|||
* In case of detected collisions, it calls either {@link #actionIfBallHitsBrick(GameObject, GameObject)} |
|||
* or {@link #actionIfBallHitsEgo(GameObject, GameObject)}. |
|||
* Called every game loop. |
|||
*/ |
|||
@Override |
|||
public void applyGameLogic() { |
|||
LinkedList<GameObject> bricks = collectObjects("brick", false); |
|||
|
|||
for (GameObject brick : bricks) { |
|||
if (this.ball.collisionDetection(brick)) { |
|||
logger.trace("Collision detected of ball and brick " + brick.getId()); |
|||
this.actionIfBallHitsBrick(this.ball, brick); |
|||
} |
|||
} |
|||
|
|||
if (this.ego.collisionDetection(ball)) { |
|||
logger.trace("Collision detected of ball and ego"); |
|||
this.actionIfBallHitsEgo(this.ball, this.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}. |
|||
*/ |
|||
protected abstract GameObject createEgoObject(); |
|||
|
|||
/** |
|||
* 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}) |
|||
*/ |
|||
protected abstract GameObject createBall(); |
|||
|
|||
/** |
|||
* 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. |
|||
*/ |
|||
protected abstract GameObject createBrick(int row, int column); |
|||
|
|||
/** |
|||
* Prepares a generic Breakout-Type level. This method relies on the methods {@link #createEgoObject()}, |
|||
* {@link #createBall} and {@link #createBrick}, among others, which are meant to be overwritten |
|||
* in subclasses. <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 |
|||
abstract public void prepareLevel(String level); |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue