From e8be11bfcd09e6588a2f84bd9559aa7795648f0d Mon Sep 17 00:00:00 2001 From: fdai7222 Date: Fri, 27 May 2022 21:42:54 +0200 Subject: [PATCH] aktualisiert konertversion auf ha7 --- .../GameProject/src/base/BreakoutGame.java | 38 ++++ .../src/base/MovingObjectsGame.java | 8 +- .../src/controller/ReboundController2.java | 35 ++++ spaceinvaders/GameProject/src/log4j2.xml | 2 +- .../src/playground/BreakoutLevelBase.java | 173 ++++++++++++++++++ .../src/playground/LevelMovingHitObjects.java | 28 +++ .../src/playground/package-info.java | 2 +- 7 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 spaceinvaders/GameProject/src/base/BreakoutGame.java create mode 100644 spaceinvaders/GameProject/src/controller/ReboundController2.java create mode 100644 spaceinvaders/GameProject/src/playground/BreakoutLevelBase.java create mode 100644 spaceinvaders/GameProject/src/playground/LevelMovingHitObjects.java diff --git a/spaceinvaders/GameProject/src/base/BreakoutGame.java b/spaceinvaders/GameProject/src/base/BreakoutGame.java new file mode 100644 index 0000000..2c636ff --- /dev/null +++ b/spaceinvaders/GameProject/src/base/BreakoutGame.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); + } + +} diff --git a/spaceinvaders/GameProject/src/base/MovingObjectsGame.java b/spaceinvaders/GameProject/src/base/MovingObjectsGame.java index ab52fec..6086e9f 100644 --- a/spaceinvaders/GameProject/src/base/MovingObjectsGame.java +++ b/spaceinvaders/GameProject/src/base/MovingObjectsGame.java @@ -1,13 +1,17 @@ package base; import java.io.IOException; -import playground.LevelMovingObjects; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import playground.*; /** * main class to start a game with only one level {@link playground.LevelMovingObjects}. * */ public class MovingObjectsGame extends GameLoop { + + private static Logger logger = LogManager.getLogger(MovingObjectsGame.class); /** * starts this game. @@ -17,6 +21,7 @@ public class MovingObjectsGame extends GameLoop { * thrown (and game ends). */ public static void main(String[] args) throws IOException { + logger.info("Starting Game Program...let's play and don't get hit!"); GameLoop myGame = new MovingObjectsGame(); myGame.runGame(args); } @@ -28,6 +33,7 @@ public class MovingObjectsGame extends GameLoop { public void defineLevels() { this.resetLevels(); this.addLevel(new LevelMovingObjects()); + this.addLevel(new LevelMovingHitObjects()); } } diff --git a/spaceinvaders/GameProject/src/controller/ReboundController2.java b/spaceinvaders/GameProject/src/controller/ReboundController2.java new file mode 100644 index 0000000..1bee573 --- /dev/null +++ b/spaceinvaders/GameProject/src/controller/ReboundController2.java @@ -0,0 +1,35 @@ +package controller; + +/** Controller to let Objects bounce from the outer level limits back and forth. + * + */ +public class ReboundController2 extends ObjectController { + + /** inverts the x y direction speeds if the outer limits are reached. + * + */ + @Override + public void updateObject() { + double sizeX = this.getPlayground().preferredSizeX(); + double sizeY = this.getPlayground().preferredSizeY(); + double objSizeX = 30; + double objSizeY = 30; + + if (this.getX() < objSizeX) { + this.setVX(this.getVX() * -1); + this.setX(objSizeX); + } else if (this.getX() > sizeX - objSizeX) { + this.setVX(this.getVX() * -1); + this.setX(sizeX - objSizeX); + } + if (this.getY() < objSizeY) { + this.setVY(this.getVY() * -1); + this.setY(objSizeY); + } else if (this.getY() > sizeY - objSizeY) { + this.setVY(this.getVY() * -1); + this.setY(sizeY - objSizeY); + } + this.applySpeedVector(); + } + +} diff --git a/spaceinvaders/GameProject/src/log4j2.xml b/spaceinvaders/GameProject/src/log4j2.xml index 21a7607..f21f70a 100644 --- a/spaceinvaders/GameProject/src/log4j2.xml +++ b/spaceinvaders/GameProject/src/log4j2.xml @@ -25,6 +25,6 @@ - + \ No newline at end of file diff --git a/spaceinvaders/GameProject/src/playground/BreakoutLevelBase.java b/spaceinvaders/GameProject/src/playground/BreakoutLevelBase.java new file mode 100644 index 0000000..0a67890 --- /dev/null +++ b/spaceinvaders/GameProject/src/playground/BreakoutLevelBase.java @@ -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 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"); + 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.
+ * 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); + +} diff --git a/spaceinvaders/GameProject/src/playground/LevelMovingHitObjects.java b/spaceinvaders/GameProject/src/playground/LevelMovingHitObjects.java new file mode 100644 index 0000000..533b41a --- /dev/null +++ b/spaceinvaders/GameProject/src/playground/LevelMovingHitObjects.java @@ -0,0 +1,28 @@ +package playground; + + +/** + * Level that creates two RectObjects moving around and if ego is hit by them game is directly lost + * (lives = 0). + * + */ +public class LevelMovingHitObjects extends SpaceInvadersLevel { + + // FIXME add logger here + + + //FIXME add your method overrides here + + + + /** + * "Moving Hitting Objects Level!" is the message. + * + * @return String "Moving & Hitting Objects Level!" + */ + @Override + protected String getStartupMessage() { + return "Moving & Hitting Objects Level!"; + } + +} diff --git a/spaceinvaders/GameProject/src/playground/package-info.java b/spaceinvaders/GameProject/src/playground/package-info.java index bbbf357..972351d 100644 --- a/spaceinvaders/GameProject/src/playground/package-info.java +++ b/spaceinvaders/GameProject/src/playground/package-info.java @@ -2,7 +2,7 @@ * The package playground contains all level specific logic and control of level logic. * The structure and general logic (with global and local flags to be stored/used) * is provided in abstract base class {@link Playground}.
- * Child-classes implement specific logic for one level and game type (e.g. {@link SpaceInvadersLevel}). + * Child-classes implement specific logic for one level and game type (e.g. {@link SpaceInvadersLevel}).
* * Generally, the base class {@link Playground} supports totally different game types to be implemented. */