JTP
Class LevelPlayer

java.lang.Object
  extended by JTP.LevelPlayer

public class LevelPlayer
extends Object

Handles gameplay mechanics of individual levels. Note: This class only understands two modes: single and two-player. (The GamePlayer class deals with game mode specifics.)

Author:
V.Termanis

Field Summary
static int FINISHED
          moveOne return code indicating the level has been completed
static int INVALID_MOVE
          moveOne return code indicating the move was invalid
static int LOST
          moveOne return code indicating no more moves are available
static int MOVED
          moveOne return code indicating succesful move
static int STUCK
          moveOne return code indicating the just moved player is stuck
protected  boolean teleportDeath
           
 
Constructor Summary
LevelPlayer(Level level)
          Creates a new LevelPlayer instance with the given level.
LevelPlayer(Level level, boolean twoPlayer)
          Creates a new LevelPlayer instance with the given level and in either single or two player mode.
 
Method Summary
 boolean activePlayerDead()
          Indicates whether the currently active player is dead or not
 boolean automaticMovesLeft(int direction)
          Returns whether the active player has automatic moves left given the last direction they moved in
protected  boolean canStillMove(boolean playerTwo)
          Calculates whether the player can still move or not
 Point getActivePosition()
          Returns position of currently active player
protected  GameTile[][] getGameTiles()
          Used by GamePlayer to directly access the game board (for rendering)
 Point getP1Position()
          Returns the grid position of player 1
 Point getP2Position()
          Returns the grid position of player 2
 boolean hasNotStarted()
          Returns whether a level has been started.
 int moveOne(int direction)
          Moves active player by one in the given direction.
 void reset()
          Resets currently loaded level back to starting position.
 boolean secondPlayerActive()
          Indicates whether the second or first player is currently active.
 int setActivePlayerDead()
          Flags currently active player as dead
 void setActivePlayerTo(boolean secondPlayer)
          Set the active player.
 void setLevel(Level level)
          Loads the provided level.
 void setLevel(Level level, boolean twoPlayer)
          Loads the provided level in either single or two-player mode.
 void setLevel(Level level, boolean twoPlayer, boolean swappedStarts)
          Loads the provided level in either single or two-player mode.
 int tilesClearedByP1()
          Returns number of tiles cleared by player 1
 int tilesClearedByP2()
          Returns number of tiles cleared by player 2
 int tilesLeft()
          Returns how many tiles are left in the level
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MOVED

public static final int MOVED
moveOne return code indicating succesful move

See Also:
Constant Field Values

FINISHED

public static final int FINISHED
moveOne return code indicating the level has been completed

See Also:
Constant Field Values

INVALID_MOVE

public static final int INVALID_MOVE
moveOne return code indicating the move was invalid

See Also:
Constant Field Values

LOST

public static final int LOST
moveOne return code indicating no more moves are available

See Also:
Constant Field Values

STUCK

public static final int STUCK
moveOne return code indicating the just moved player is stuck

See Also:
Constant Field Values

teleportDeath

protected boolean teleportDeath
Constructor Detail

LevelPlayer

public LevelPlayer(Level level)
            throws NullPointerException
Creates a new LevelPlayer instance with the given level.

Parameters:
level - the level to load
Throws:
NullPointerException - if the parameter is null

LevelPlayer

public LevelPlayer(Level level,
                   boolean twoPlayer)
            throws NullPointerException,
                   IllegalArgumentException
Creates a new LevelPlayer instance with the given level and in either single or two player mode.

Parameters:
level - the level to load
twoPlayer - indicate whether this should be a two player level or not
Throws:
NullPointerException - if the parameter is null
IllegalArgumentException - if two player mode has been requested yet the supplied level is a single player level
Method Detail

setLevel

public void setLevel(Level level)
              throws NullPointerException
Loads the provided level.

Parameters:
level - the level to load
Throws:
NullPointerException - if the paramter is null

setLevel

public void setLevel(Level level,
                     boolean twoPlayer)
              throws NullPointerException,
                     IllegalArgumentException
Loads the provided level in either single or two-player mode.

Parameters:
level - the level to load
twoPlayer - indicate whether this should be a two player level or not
Throws:
NullPointerException - if the paramter is null
IllegalArgumentException - if two player mode has been requested yet the supplied level is a single player level

setLevel

public void setLevel(Level level,
                     boolean twoPlayer,
                     boolean swappedStarts)
              throws NullPointerException,
                     IllegalArgumentException
Loads the provided level in either single or two-player mode.

Parameters:
level - the level to load
twoPlayer - indicate whether this should be a two player level or not
swappedStarts - exchanges first and second player starting positions. Note: This has no effect if the twoPlayer parameter is not set to true
Throws:
NullPointerException - if the paramter is null
IllegalArgumentException - if two player mode has been requested yet the supplied level is a single player level

moveOne

public int moveOne(int direction)
Moves active player by one in the given direction.

Parameters:
direction - e.g UP or GamePlayer.AUTOSTART which is used to start a level (and perform initial automatic moves if there are any, one by one)
Returns:
see field summary

automaticMovesLeft

public boolean automaticMovesLeft(int direction)
Returns whether the active player has automatic moves left given the last direction they moved in

Parameters:
direction - e.g. UP
Returns:
true if there are more automatic moves left

reset

public void reset()
Resets currently loaded level back to starting position.


canStillMove

protected boolean canStillMove(boolean playerTwo)
Calculates whether the player can still move or not

Parameters:
playerTwo - indicate whether to calculate for first or second player
Returns:
true if there are still moves available

setActivePlayerTo

public void setActivePlayerTo(boolean secondPlayer)
Set the active player.

Parameters:
secondPlayer - indicate whether Player 2 should be active or not

secondPlayerActive

public boolean secondPlayerActive()
Indicates whether the second or first player is currently active.

Returns:
true if Player 2 is active

getGameTiles

protected GameTile[][] getGameTiles()
Used by GamePlayer to directly access the game board (for rendering)

Returns:
the game board

setActivePlayerDead

public int setActivePlayerDead()
Flags currently active player as dead

Returns:
LOST if both players are dead or STUCK the other player can still move

activePlayerDead

public boolean activePlayerDead()
Indicates whether the currently active player is dead or not

Returns:
true if the active player is dead

getActivePosition

public Point getActivePosition()
Returns position of currently active player

Returns:
x and y coordinates of player's position

getP1Position

public Point getP1Position()
Returns the grid position of player 1

Returns:
x and y coordinates of player's position

getP2Position

public Point getP2Position()
Returns the grid position of player 2

Returns:
x and y coordinates of player's position

tilesLeft

public int tilesLeft()
Returns how many tiles are left in the level

Returns:
the number of tiles left

tilesClearedByP1

public int tilesClearedByP1()
Returns number of tiles cleared by player 1

Returns:
number of tiles cleared by player 1

tilesClearedByP2

public int tilesClearedByP2()
Returns number of tiles cleared by player 2

Returns:
number of tiles cleared by player 2

hasNotStarted

public boolean hasNotStarted()
Returns whether a level has been started. Or in other words whether at least one move, valid or invalid, has been made.

Returns:
true if the level has been started