You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.6 KiB

package controller;
import playground.*;
import gameobjects.*;
import java.util.*;
import java.awt.event.*;
import java.io.File;
/**
* An EgoController which cannot move through obstacle objects (is collission aware). Only respects
* GameObjects that have the String 'obstacle' in their name.
*
*/
public class CollisionAwareEgoController extends EgoController {
double savex, savey, savevx, savevy;
double lastSpaceAt = -1;
private File shot = null;
/**
*
* @param egoRad radius of ego object to be used.
*/
public CollisionAwareEgoController(double egoRad) {
super(egoRad);
}
/**
*
* @param egoRad radius of ego object to be used.
* @param soundOnShot WAV file to be played on shot
*/
public CollisionAwareEgoController(double egoRad, File soundOnShot) {
super(egoRad);
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();
this.savevx = this.getVX();
this.savevy = this.getVY();
}
/**
* 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);
this.setVX(savevx);
this.setVY(savevy);
}
/**
* 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();
Playground pg = this.getPlayground();
LinkedList<GameObject> obstacles = pg.collectObjects("obstacle", false);
this.saveDynamicState();
this.applySpeedVector();
for (GameObject ob : obstacles) {
if (ob.collisionDetection(this.gameObject)) {
this.restoreDynamicState();
return true;
}
}
this.restoreDynamicState();
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) {
super.onSpace(e, ego);
Music.music(this.shot);
}
}
}