Browse Source

Merge branch 'main' of gogs.informatik.hs-fulda.de:jkonert/prog2-codebase into main

Manu 2 years ago
parent
commit
1c30583e1f
  1. 1
      .gitignore
  2. 4
      GameProject/src/base/GameLoop.java
  3. 33
      GameProject/src/base/MovingObjectsGame.java
  4. 14
      GameProject/src/controller/CollisionAwareEgoController.java
  5. 46
      GameProject/src/controller/EgoController.java
  6. 2
      GameProject/src/playground/Level2.java
  7. 4
      GameProject/src/playground/Level4.java
  8. 21
      GameProject/src/playground/LevelMovingObjects.java
  9. BIN
      GameProject/video/alexG.jpg

1
.gitignore

@ -1,6 +1,7 @@
*.DS_Store
*.metadata/
*.class
*.prefs
/GameProject/doc/
/GameProject/log/
/GameProject/highscore.txt

4
GameProject/src/base/GameLoop.java

@ -218,8 +218,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.

33
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());
}
}

14
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) {

46
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);
}
}

2
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 {

4
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.
* <ul>
* <li>Hit aliens twice to kill them
* <li>they say AUA when not destroyed
* </ul>
*/
public class Level4 extends SpaceInvadersLevel {
/** constant defining the number of shots needed to destroy an enemy */
public static final int MAX_HITS = 2;

21
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!";
}
}

BIN
GameProject/video/alexG.jpg

Before

Width: 1664  |  Height: 2496  |  Size: 181 KiB

Loading…
Cancel
Save