cicaprojekt/cicaprojekt/PlayerBase.java

169 lines
4.4 KiB
Java

package cicaprojekt;
/**
* A játékban megjelenő személyeket reprezentáló osztály.
*/
public class PlayerBase implements Destroyable {
/**
* A csempe, amelyen a személy áll.
*/
protected Tile currentTile;
/**
* Az irány, amerre a személy néz.
*/
protected Direction facingDirection;
/**
* A személy neve.
*/
protected String name;
/**
* A személy elpusztulását számontartó változó.
*/
protected boolean destroyed;
/**
* Az osztály konstruktora. Kezdetben a személy életben van és a kapott
* értékekre állítódnak be a tulajdonságai.
*
* @param name a személy neve
* @param startTile a csempe, ahol kezd
* @param startDirection az irány, ahova kezdetben néz
*/
public PlayerBase(String name, Tile startTile, Direction startDirection) {
this.name = name;
currentTile = startTile;
facingDirection = startDirection;
destroyed = false;
}
/**
* A személy nevével és aktuális tartózkodási helyével tér vissza.
*
* @return a személy neve és aktuális csempéje
*/
@Override
public String toString() {
return String.format("%s: %s", name, currentTile);
}
/**
* A személy elpusztulását megvalósító függvény.
*
* @see Game#playerBaseDestroyed(PlayerBase)
*/
@Override
public void destroy() {
destroyed = true;
Game.instance.playerBaseDestroyed(this);
}
/**
* A személy aktuális tartózkodási helyével tér vissza.
*
* @return aktuális csempe
*/
public Tile getCurrentTile() {
return currentTile;
}
/**
* A személy aktuális tartózkodási helyét állítja be a kapott csempére.
*
* @param newCurrentTile a kapott csempe
*/
public void setCurrentTile(Tile newCurrentTile) {
currentTile = newCurrentTile;
}
/**
* A személy mozgását megvalósító függvény, amely keretében a személy
* egy csempényit próbál lépni a kapott irányba.
*
* @param direction a kapott irány
*/
public void move(Direction direction) {
if(!isDestroyed()) {
this.setFacingDirection(direction);
Tile adjtile = this.getCurrentTile().getAdjacentTile(direction);
if (adjtile != null) {
if (adjtile.isSteppable()) {
currentTile.onExit(this);
setCurrentTile(adjtile);
adjtile.onEntry(this);
}
}
}
}
public void pickZPM(Tile tile) { /* PlayerBase does not collect ZPM modules */ }
/**
* A személy balra forgatását megvalósító függvény.
*/
public void rotateLeft() {
switch (facingDirection) {
case NORTH:
facingDirection = Direction.WEST;
break;
case WEST:
facingDirection = Direction.SOUTH;
break;
case SOUTH:
facingDirection = Direction.EAST;
break;
case EAST:
facingDirection = Direction.NORTH;
break;
}
}
/**
* A személy jobbra forgatását megvalósító függvény.
*/
public void rotateRight() {
switch (facingDirection) {
case NORTH:
facingDirection = Direction.EAST;
break;
case EAST:
facingDirection = Direction.SOUTH;
break;
case SOUTH:
facingDirection = Direction.WEST;
break;
case WEST:
facingDirection = Direction.NORTH;
break;
}
}
/**
* A függvény a személy aktuális nézési irányával tér vissza.
*
* @return irány, amerre a személy néz
*/
public Direction getFacingDirection() {
return facingDirection;
}
/**
* Beállítja a kapott értékre, hogy arra nézzen a személy.
*
* @param direction kapott irány
*/
public void setFacingDirection(Direction direction) {
facingDirection = direction;
}
/**
* Igazzal tér vissza, ha a személy elpusztult, hamissal, ha nem.
*
* @return true, ha elpusztult
* false, egyébként
*/
public boolean isDestroyed(){
return destroyed;
}
}