4 Commits

  1. 11
      GameProject/src/base/MovingObjectsGame.java
  2. 35
      GameProject/src/controller/ReboundController2.java
  3. 7
      GameProject/src/log4j2.xml
  4. 77
      GameProject/src/playground/LevelMovingHitObjects.java
  5. 2
      GameProject/src/playground/package-info.java

11
GameProject/src/base/MovingObjectsGame.java

@ -1,13 +1,17 @@
package base;
package base;
import java.io.IOException; 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}. * main class to start a game with only one level {@link playground.LevelMovingObjects}.
* *
*/ */
public class MovingObjectsGame extends GameLoop { public class MovingObjectsGame extends GameLoop {
private static Logger logger = LogManager.getLogger(MovingObjectsGame.class);
/** /**
* starts this game. * starts this game.
@ -17,6 +21,7 @@ public class MovingObjectsGame extends GameLoop {
* thrown (and game ends). * thrown (and game ends).
*/ */
public static void main(String[] args) throws IOException { 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(); GameLoop myGame = new MovingObjectsGame();
myGame.runGame(args); myGame.runGame(args);
} }
@ -27,7 +32,7 @@ public class MovingObjectsGame extends GameLoop {
@Override @Override
public void defineLevels() { public void defineLevels() {
this.resetLevels(); this.resetLevels();
this.addLevel(new LevelMovingObjects());
this.addLevel(new LevelMovingHitObjects());
} }
} }

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

7
GameProject/src/log4j2.xml

@ -19,12 +19,9 @@
<appender-ref ref="Console" /> <appender-ref ref="Console" />
<appender-ref ref="File" /> <appender-ref ref="File" />
</root> </root>
<Logger name="base.GameLoop" level="info">
</Logger>
<Logger name="playground" level="info">
<Logger name="playground.LevelMovingHitObjects" level="info">
</Logger> </Logger>
</loggers> </loggers>
</configuration> </configuration>

77
GameProject/src/playground/LevelMovingHitObjects.java

@ -0,0 +1,77 @@
package playground;
import java.awt.Color;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import collider.RectCollider;
import controller.ReboundController;
import controller.ReboundController2;
import gameobjects.GameObject;
import gameobjects.RectObject;
/**
* 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 {
private static Logger logger1 = LogManager.getLogger(LevelMovingHitObjects.class);
@Override
public void prepareLevel(String id) {
super.prepareLevel(id);
RectObject fly_enemy1 = new RectObject("fly_enemy1", this, 300, 300, 75, 40, 40, 40, Color.BLUE);
RectObject fly_enemy2 = new RectObject("fly_enemy2", this, 200, 200, 20, 90, 40, 40, Color.GREEN);
logger1.info("Created two rectobjects");
ReboundController2 fly1_controller = new ReboundController2();
ReboundController2 fly2_controller = new ReboundController2();
logger1.info("Created two controllers");
fly_enemy1.addController(fly1_controller);
fly_enemy2.addController(fly2_controller);
logger1.info("Added controller to Rectbjects");
this.addObject(fly_enemy1);
this.addObject(fly_enemy2);
logger1.info("Added objects to LevelMoving<HItObejcts");
RectCollider collider1 = new RectCollider("collider1", fly_enemy1, 40d, 40d);
RectCollider collider2 = new RectCollider("collider2", fly_enemy2, 40d, 40d);
logger1.info("Created two colliders");
fly_enemy1.addCollider(collider1);
fly_enemy2.addCollider(collider2);
logger1.info("Added collider to rectobjects");
}
@Override
void actionIfEgoCollidesWithEnemy(GameObject enemy, GameObject ego) {
if (enemy.id.equals("fly_enemy1") || enemy.id.equals("fly_enemy2")) {
setGlobalFlag("egoLive", 0);
this.lost = true;
} else {
super.actionIfEgoCollidesWithEnemy(enemy, ego);
}
}
//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!";
}
}

2
GameProject/src/playground/package-info.java

@ -2,7 +2,7 @@
* The package playground contains all level specific logic and control of level logic. * 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) * The structure and general logic (with global and local flags to be stored/used)
* is provided in abstract base class {@link Playground}.<br> * is provided in abstract base class {@link Playground}.<br>
* Child-classes implement specific logic for one level and game type (e.g. {@link SpaceInvadersLevel}).<b>
* Child-classes implement specific logic for one level and game type (e.g. {@link SpaceInvadersLevel}).<br>
* *
* Generally, the base class {@link Playground} supports totally different game types to be implemented. * Generally, the base class {@link Playground} supports totally different game types to be implemented.
*/ */

Loading…
Cancel
Save