9 Commits

  1. 1
      .gitignore
  2. 7
      GameProject/src/base/GameLoop.java
  3. 33
      GameProject/src/base/MovingObjectsGame.java
  4. 3
      GameProject/src/base/MultiLevelGame.java
  5. 14
      GameProject/src/controller/CollisionAwareEgoController.java
  6. 46
      GameProject/src/controller/EgoController.java
  7. 2
      GameProject/src/playground/Level2.java
  8. 4
      GameProject/src/playground/Level4.java
  9. 28
      GameProject/src/playground/LevelMovingObjects.java
  10. 3
      GameProject/src/playground/LevelWithBox.java
  11. BIN
      GameProject/video/alexG.jpg

1
.gitignore

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

7
GameProject/src/base/GameLoop.java

@ -8,6 +8,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import gameobjects.GameObject;
import playground.Level1;
import playground.LevelMovingObjects;
import playground.LevelWithBox;
import playground.Playground;
import ui.GameUI;
@ -56,7 +57,7 @@ public class GameLoop {
*/
void defineLevels() {
this.resetLevels();
this.addLevel(new LevelWithBox());
this.addLevel(new LevelMovingObjects());
}
/**
@ -218,8 +219,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());
}
}

3
GameProject/src/base/MultiLevelGame.java

@ -3,13 +3,14 @@ package base;
import java.io.IOException;
import playground.Level5;
import playground.LevelMovingObjects;
import playground.LevelWithBox;
public class MultiLevelGame extends GameLoop {
void defineLevels() {
this.addLevel(new LevelWithBox());
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;

28
GameProject/src/playground/LevelMovingObjects.java

@ -0,0 +1,28 @@
package playground;
import java.awt.Color;
import gameobjects.RectObject;
/** This level adds two distracting objects to the canvas that cannot collide but bounce around all the time.
*/
public class LevelMovingObjects extends SpaceInvadersLevel {
public void prepareLevel(String id) {
super.prepareLevel(id);
LevelMovingObjects ml = this;
RectObject object2 = new RectObject(id +"Bluebox",ml,300,300,170,70,30,30,Color.BLUE);
this.addObject(object2);
}
/** "Moving Objects Level!" is the message.
*
* @return String "Moving Objects Level!"
*/
@Override
protected String getStartupMessage() {
return "Moving Objects Level!";
}
}

3
GameProject/src/playground/LevelWithBox.java

@ -25,7 +25,8 @@ public class LevelWithBox extends SpaceInvadersLevel {
public void prepareLevel(String id) {
super.prepareLevel(id);
RectObject Object = new RectObject(id,null,350,100,0,0,700,250,Color.RED);
LevelWithBox myLevel = this;
RectObject Object = new RectObject(id,myLevel,350,100,0,0,700,250,Color.RED);
this.addObject(Object);
}

BIN
GameProject/video/alexG.jpg

Before

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

Loading…
Cancel
Save