From 059c032beed6cced89511c6e35b97e4cbf7d16a7 Mon Sep 17 00:00:00 2001 From: fdai7222 Date: Wed, 18 May 2022 15:36:01 +0200 Subject: [PATCH] aktualisierung konertversion --- spaceinvaders/GameProject/.classpath | 7 ++- .../GameProject/src/base/GameLoop.java | 4 +- .../src/base/MovingObjectsGame.java | 33 +++++++++++++ .../src/collider/package-info.java | 9 ++++ .../CollisionAwareEgoController.java | 14 ++++++ .../src/controller/EgoController.java | 46 +++++++++++++------ .../src/controller/ObjectController.java | 2 +- .../src/gameobjects/package-info.java | 7 +++ spaceinvaders/GameProject/src/log4j2.xml | 2 +- .../GameProject/src/playground/Level2.java | 2 +- .../GameProject/src/playground/Level4.java | 4 +- .../src/playground/LevelMovingObjects.java | 21 +++++++++ .../src/playground/SpaceInvadersLevel.java | 2 +- .../src/playground/package-info.java | 9 ++++ 14 files changed, 137 insertions(+), 25 deletions(-) create mode 100644 spaceinvaders/GameProject/src/base/MovingObjectsGame.java create mode 100644 spaceinvaders/GameProject/src/collider/package-info.java create mode 100644 spaceinvaders/GameProject/src/gameobjects/package-info.java create mode 100644 spaceinvaders/GameProject/src/playground/LevelMovingObjects.java create mode 100644 spaceinvaders/GameProject/src/playground/package-info.java diff --git a/spaceinvaders/GameProject/.classpath b/spaceinvaders/GameProject/.classpath index a32988c..9c1fe8d 100644 --- a/spaceinvaders/GameProject/.classpath +++ b/spaceinvaders/GameProject/.classpath @@ -1,8 +1,11 @@ - - + + + + + diff --git a/spaceinvaders/GameProject/src/base/GameLoop.java b/spaceinvaders/GameProject/src/base/GameLoop.java index eb72c12..f98bd42 100644 --- a/spaceinvaders/GameProject/src/base/GameLoop.java +++ b/spaceinvaders/GameProject/src/base/GameLoop.java @@ -217,8 +217,8 @@ public class GameLoop { /** - * main to start the whole application. - * initializes the {@link #levels} ArrayList of Playground instances (levels) to be played with one level {@link SpaceInvadersLevel} in constructor of {@link #GameLoop}. + * main to start the whole application. It calls. {@link #runGame(String[])}. + * (levels are automatically added/loaded by constructor of {@link #GameLoop}). * * @param args Java default command line args, forwarded to {@link #runGame(String[])} * @throws IOException in case highscore.txt cannot be written. diff --git a/spaceinvaders/GameProject/src/base/MovingObjectsGame.java b/spaceinvaders/GameProject/src/base/MovingObjectsGame.java new file mode 100644 index 0000000..ab52fec --- /dev/null +++ b/spaceinvaders/GameProject/src/base/MovingObjectsGame.java @@ -0,0 +1,33 @@ +package base; + +import java.io.IOException; +import playground.LevelMovingObjects; + +/** + * main class to start a game with only one level {@link playground.LevelMovingObjects}. + * + */ +public class MovingObjectsGame extends GameLoop { + + /** + * 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 MovingObjectsGame(); + myGame.runGame(args); + } + + /** + * adds only one level to play ({@link playground.LevelMovingObjects}). + */ + @Override + public void defineLevels() { + this.resetLevels(); + this.addLevel(new LevelMovingObjects()); + } + +} diff --git a/spaceinvaders/GameProject/src/collider/package-info.java b/spaceinvaders/GameProject/src/collider/package-info.java new file mode 100644 index 0000000..f101edb --- /dev/null +++ b/spaceinvaders/GameProject/src/collider/package-info.java @@ -0,0 +1,9 @@ +/** + * The package contains classes implementing a 'bounding box' area around game objects.
+ * The abstract base class {@link Collider} provides the abstract method {@link Collider#collidesWith(Collider)}, + * which needs to be implemented by child classes to detect and decide whether or not an object with such instance really collides with the other. + * {@link Collider} instances are to be used for game objects ({@link gameobjects}); see constructors.
+ * + * The benefit of seperating Colliders from visual representations is that the area for collisions can be smaller/bigger/other shape to improve game play experience. + */ +package collider; diff --git a/spaceinvaders/GameProject/src/controller/CollisionAwareEgoController.java b/spaceinvaders/GameProject/src/controller/CollisionAwareEgoController.java index 12ccf29..e223487 100644 --- a/spaceinvaders/GameProject/src/controller/CollisionAwareEgoController.java +++ b/spaceinvaders/GameProject/src/controller/CollisionAwareEgoController.java @@ -37,6 +37,9 @@ public class CollisionAwareEgoController extends EgoController { this.shot = soundOnShot; } + /** + * Copies current values of x,y position and speed vx,vy into attributes. These can be restored by call to {@link #restoreDynamicState()}. + */ public void saveDynamicState() { this.savex = this.getX(); this.savey = this.getY(); @@ -45,6 +48,10 @@ public class CollisionAwareEgoController extends EgoController { } + /** + * Restores formally saved values of x,y position and speed vx,vy from attributes back to the ego object. + * These values should have been stored before by a call to {@link #saveDynamicState()}, otherwise all values will be 0.00. + */ public void restoreDynamicState() { this.setX(savex); this.setY(savey); @@ -53,6 +60,10 @@ public class CollisionAwareEgoController extends EgoController { } + /** + * extends parent class implementation by a check whether or not the ego object collides with any other "obstacle" object. + * If yes, the position stays fixed (by using {@link #saveDynamicState()} and {@link #restoreDynamicState()}. + */ public boolean stopObject() { boolean s = super.stopObject(); @@ -73,6 +84,9 @@ public class CollisionAwareEgoController extends EgoController { return s; } + /** + * calls superclass {@link EgoController#onSpace(KeyEvent, GameObject)} only, if the time elapsed since last pressing of space is above 0.1 ms. + */ public void onSpace(KeyEvent e, GameObject ego) { double cgt = ego.getGameTime(); if ((cgt - this.lastSpaceAt) > 0.1) { diff --git a/spaceinvaders/GameProject/src/controller/EgoController.java b/spaceinvaders/GameProject/src/controller/EgoController.java index 85f7a16..e00877a 100644 --- a/spaceinvaders/GameProject/src/controller/EgoController.java +++ b/spaceinvaders/GameProject/src/controller/EgoController.java @@ -42,26 +42,51 @@ public class EgoController extends ObjectController { } + /** + * moves ego up by {@link SpaceInvadersLevel#EGOSPEED}. + * @param kc KeyEvent to process (ignored) + * @param ego the ego object + */ public void onUp(KeyEvent kc, GameObject ego) { ego.setVX(0.0); ego.setVY(-SpaceInvadersLevel.EGOSPEED); } + /** + * moves ego down by {@link SpaceInvadersLevel#EGOSPEED}. + * @param kc KeyEvent to process (ignored) + * @param ego the ego object + */ public void onDown(KeyEvent kc, GameObject ego) { ego.setVX(0.0); ego.setVY(SpaceInvadersLevel.EGOSPEED); } + /** + * moves ego left by {@link SpaceInvadersLevel#EGOSPEED}. + * @param kc KeyEvent to process (ignored) + * @param ego the ego object + */ public void onLeft(KeyEvent kc, GameObject ego) { ego.setVY(0.0); ego.setVX(-SpaceInvadersLevel.EGOSPEED); } + /** + * moves ego right by {@link SpaceInvadersLevel#EGOSPEED}. + * @param kc KeyEvent to process (ignored) + * @param ego the ego object + */ public void onRight(KeyEvent kc, GameObject ego) { ego.setVY(0.0); ego.setVX(SpaceInvadersLevel.EGOSPEED); } + /** + * sets speed to 0.0 + * @param kc KeyEvent to process (ignored) + * @param ego the ego object + */ public void onStop(KeyEvent kc, GameObject ego) { ego.setVY(0.0); ego.setVX(0.0); @@ -72,7 +97,7 @@ public class EgoController extends ObjectController { /** checks the position and respects level boundaries and own radius or width/height set on constructor. * - * @return true if the object reached the boundaries of the level, false otherwise + * @return true if the object reached the boundaries of the level, false otherwise. */ public boolean stopObject() { // check whether ego object is at level boundaries @@ -93,7 +118,7 @@ public class EgoController extends ObjectController { } - /** behavior for shooting on key space + /** behavior for shooting on key space. Creates a new shot using {#link SimpleShotController} with a {@link RectObject}. * * @param e KeyEvent of the space key * @param ego EgoObject instance (used to determine position of shot object's start) @@ -119,7 +144,7 @@ public class EgoController extends ObjectController { /** - * updates position based on key events (mouse currently ignored) + * updates position based on key events (mouse currently ignored). */ public void updateObject() { @@ -140,23 +165,14 @@ public class EgoController extends ObjectController { released = false; } - /** - * Generelle Idee: Wenn eine Taste gedrückt wird wird sie gespeichert. wenn die zuvor - * gespeicherte Taste wieder losgelassen wird stoppt das Ego-Objekt. Falls vor dem Loslassen - * eine andere Taste gedrückt wird, wird diese gespeichert und die alte vergessen. Dh das - * loslassen der alten Taste stoppt das Objekt nicht. Spezialfall: space, das loslassen von - * space stoppt das Objekt nicht! - */ if (pressed == true) { lastPressedKey = pressedKey; pressedKey = kc; } - /** - * Nur eine losgelassene Taste die auch vorher gedrückt wurde stoppt das Objekt. Eine - * losgelassene Taste die nicht vorher gedrückt wurde bzw vergessen wurde stoppt das Objekt - * nicht + /* + * Only if the released key is the same as the before pressed one, it stops the ego object movement. */ if (released == true) { if (pressedKey != null) { @@ -194,7 +210,7 @@ public class EgoController extends ObjectController { // shot if (kc == KeyEvent.VK_SPACE) { - // space is not registered! Releasing space does not stop the egoobject + // space is not registered! Releasing space does not stop the ego object this.onSpace(e, ego); } } diff --git a/spaceinvaders/GameProject/src/controller/ObjectController.java b/spaceinvaders/GameProject/src/controller/ObjectController.java index dd16f16..65478f2 100644 --- a/spaceinvaders/GameProject/src/controller/ObjectController.java +++ b/spaceinvaders/GameProject/src/controller/ObjectController.java @@ -56,7 +56,7 @@ public abstract class ObjectController { public void applySpeedVector() { double ts = this.getPlayground().getTimestep(); this.setX(this.getX() + this.getVX() * ts); - gameObject.setY(this.getY() + this.getVY() * ts); + this.setY(this.getY() + this.getVY() * ts); } diff --git a/spaceinvaders/GameProject/src/gameobjects/package-info.java b/spaceinvaders/GameProject/src/gameobjects/package-info.java new file mode 100644 index 0000000..7ec704e --- /dev/null +++ b/spaceinvaders/GameProject/src/gameobjects/package-info.java @@ -0,0 +1,7 @@ +/** + * The package gameobjects contains all objects with a visual representation on screen. + * They can be combined to use controller instances for their behavior (subclasses of {@link controller.ObjectController}). + * The abstract base class is {@link GameObject}, which forces child-classes to implement the method + * {@link GameObject#updateObject()}. + */ +package gameobjects; diff --git a/spaceinvaders/GameProject/src/log4j2.xml b/spaceinvaders/GameProject/src/log4j2.xml index 87ecf74..21a7607 100644 --- a/spaceinvaders/GameProject/src/log4j2.xml +++ b/spaceinvaders/GameProject/src/log4j2.xml @@ -7,7 +7,7 @@ - + diff --git a/spaceinvaders/GameProject/src/playground/Level2.java b/spaceinvaders/GameProject/src/playground/Level2.java index e553575..be0a998 100644 --- a/spaceinvaders/GameProject/src/playground/Level2.java +++ b/spaceinvaders/GameProject/src/playground/Level2.java @@ -3,7 +3,7 @@ package playground; /** - * extends extends {@link SpaceInvadersLevel} with a different startup message. + * extends {@link SpaceInvadersLevel} with a different startup message. */ public class Level2 extends SpaceInvadersLevel { diff --git a/spaceinvaders/GameProject/src/playground/Level4.java b/spaceinvaders/GameProject/src/playground/Level4.java index cfb39b3..dacfd4f 100644 --- a/spaceinvaders/GameProject/src/playground/Level4.java +++ b/spaceinvaders/GameProject/src/playground/Level4.java @@ -10,14 +10,14 @@ import org.apache.logging.log4j.Logger; /** - * extends extends {@link SpaceInvadersLevel} + * extends {@link SpaceInvadersLevel} with aliens that need two hits to be destroyed. *
    *
  • Hit aliens twice to kill them *
  • they say AUA when not destroyed *
*/ public class Level4 extends SpaceInvadersLevel { - + /** constant defining the number of shots needed to destroy an enemy */ public static final int MAX_HITS = 2; diff --git a/spaceinvaders/GameProject/src/playground/LevelMovingObjects.java b/spaceinvaders/GameProject/src/playground/LevelMovingObjects.java new file mode 100644 index 0000000..dd93acf --- /dev/null +++ b/spaceinvaders/GameProject/src/playground/LevelMovingObjects.java @@ -0,0 +1,21 @@ +package playground; + + + +/** This level adds two distracting objects to the canvas that cannot collide but bounce around all the time. + */ +public class LevelMovingObjects extends SpaceInvadersLevel { + + // TODO your code here + + + /** "Moving Objects Level!" is the message. + * + * @return String "Moving Objects Level!" + */ + @Override + protected String getStartupMessage() { + return "Moving Objects Level!"; + } +} + diff --git a/spaceinvaders/GameProject/src/playground/SpaceInvadersLevel.java b/spaceinvaders/GameProject/src/playground/SpaceInvadersLevel.java index 1231c06..4d16fb5 100644 --- a/spaceinvaders/GameProject/src/playground/SpaceInvadersLevel.java +++ b/spaceinvaders/GameProject/src/playground/SpaceInvadersLevel.java @@ -78,7 +78,7 @@ public class SpaceInvadersLevel extends Playground { protected Animation enemyAnim = null; protected Animation heartAnim = null; - protected static Logger logger = LogManager.getLogger(SpaceInvadersLevel.class); + private static Logger logger = LogManager.getLogger(SpaceInvadersLevel.class); public SpaceInvadersLevel() { super(); diff --git a/spaceinvaders/GameProject/src/playground/package-info.java b/spaceinvaders/GameProject/src/playground/package-info.java new file mode 100644 index 0000000..bbbf357 --- /dev/null +++ b/spaceinvaders/GameProject/src/playground/package-info.java @@ -0,0 +1,9 @@ +/** + * 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}). + * + * Generally, the base class {@link Playground} supports totally different game types to be implemented. + */ +package playground;