client.framework.models
Class AbstractPlayer

java.lang.Object
  extended by client.framework.models.AbstractPlayer
All Implemented Interfaces:
Comparable
Direct Known Subclasses:
BBPlayer

public abstract class AbstractPlayer
extends java.lang.Object
implements Comparable

An AbstractPlayer object is a representation of a participant in a framework game. Each player has a Sprite object, which is his visual repesentation on the game board. In addition, the player has different attributes such as a name, id, color, score, and size. This class also contain support for simple movement prediction, where future positions are calculated based on the two last received position updates.

Author:
Eivind Sorteberg, Martin Jarrett

Constructor Summary
  AbstractPlayer(java.lang.String id)
          Constructor using an empty name and default values for size (10) and speed (1).
  AbstractPlayer(java.lang.String id, java.lang.String name)
          Constructor using default values for size (10) and speed (1).
protected AbstractPlayer(java.lang.String id, java.lang.String name, int size, int speed)
          Constructor for AbstractPlayer.
 
Method Summary
 int compareTo(Comparable object)
          Method inherited from the Comparable interface.
 boolean equals(java.lang.Object object)
          Checks whether the input object is the same player object as this.
 java.lang.String[] getAttributeValues()
          Returns a String containing the most important player attributes.
 int[] getColor()
          Returns the player's color in an array containing values between [0 and 255] for each of the colors red, green and blue.
 java.lang.String getId()
          Returns the player's id.
 java.lang.String getName()
          Returns the player's name.
 int[] getPosition()
          Returns an array containing the player's x- and y-coordinate in that order.
 java.lang.Integer getScore()
          Returns the player's score.
 int[] getScreenResolution()
          Returns an array containing the player's screen resolution.
 int getSize()
          Returns the size of the player's sprite.
 int getSpeed()
          Returns the speed in which the player moves on the game board.
 Sprite getSprite()
          Returns the player's sprite representation.
 int getTeam()
          Returns the team that the player is member of, if any.
 boolean isReady()
          Returns the player's ready status.
 void move()
          Called to move the player a distance detemined by the player's movement vector.
 void move(double dx, double dy)
          Moves the player a distance determined by the input parameters.
protected  void resetMovement()
          Resets the player's movement.
 void setAttributeValues(java.lang.String[] values)
          Called when a player's attributes has been received.
 void setColor(int red, int green, int blue)
          Sets the player's color.
 void setImage()
          Creates an image for the player's sprite.
 void setName(java.lang.String name)
          Sets the player's name.
 void setPosition(int x, int y)
          Sets the player's position on the game board to the specified position.
 void setReady(boolean ready)
          Sets the player's ready status to the specified value.
 void setScore(int score)
          Sets the player's score to the specified value.
 void setScreenResolution(int[] screenResolution)
          Sets the screen resolution variable to the screen resolution on the player's phone.
 void setSize(int size)
          Sets the size of the player's sprite to the specified size, and creates the sprite's image.
 void setSpeed(int speed)
          Set's the speed in which the player moves in the game board to the specified value.
 void setTeam(int team)
          Sets the player as member of the specified team.
 void setTeamColor(int[] color)
          Sets the player's team color.
 void setX(int x)
          Sets the x-position of the player.
 void setY(int y)
          Sets the y-position of the player.
 java.lang.String toString()
          Method for displaying the player's name and score.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractPlayer

protected AbstractPlayer(java.lang.String id,
                         java.lang.String name,
                         int size,
                         int speed)
Constructor for AbstractPlayer. All implementing subclasses should call this contructor through a super()-call. The constructor creates an AbstractPlayer-instance, and gives default values to all attributes not specified as input parameters.

Parameters:
id - The player's network id.
name - The player's name.
size - The size of the player's Sprite object.
speed - The speed in which the player is able to move.
See Also:
AbstractGame.newPlayer(String, String)

AbstractPlayer

public AbstractPlayer(java.lang.String id,
                      java.lang.String name)
Constructor using default values for size (10) and speed (1).

Parameters:
id - The player's id.
name - The player's name.

AbstractPlayer

public AbstractPlayer(java.lang.String id)
Constructor using an empty name and default values for size (10) and speed (1).

Parameters:
id - The id of the player.
Method Detail

getId

public java.lang.String getId()
Returns the player's id. The id is assigned by the server.

Returns:
The player's id.

getName

public java.lang.String getName()
Returns the player's name.

Returns:
The player's name.

setName

public void setName(java.lang.String name)
Sets the player's name.

Parameters:
name - The player's name.

getColor

public int[] getColor()
Returns the player's color in an array containing values between [0 and 255] for each of the colors red, green and blue.

Returns:
The player's color.

setColor

public void setColor(int red,
                     int green,
                     int blue)
Sets the player's color. Values above 255 are set to 255, and values below 0 are set to 0. Each of the colors red, green and blue needs to be defined.

Parameters:
red - The amount of red.
green - The amound of green.
blue - The amount of blue.
See Also:
setImage()

setTeamColor

public void setTeamColor(int[] color)
Sets the player's team color. If the player does not have a team color, the team color is set to player's color.

Parameters:
color - The player's team's color
See Also:
setImage()

setImage

public void setImage()
Creates an image for the player's sprite. If the player has no team color, the image is a simple square with just the player's color. If the player does have a team color, the sprite's top right corner is filled with this color.


toString

public java.lang.String toString()
Method for displaying the player's name and score. This method is used to create the player lists in the lobby and in-game.

Overrides:
toString in class java.lang.Object
Returns:
The player's name and score.
See Also:
Utils#drawPlayerList(javax.microedition.lcdui.Graphics, int, int, AbstractGame, boolean)

getScore

public java.lang.Integer getScore()
Returns the player's score.

Returns:
The player's score.

setScore

public void setScore(int score)
Sets the player's score to the specified value.

Parameters:
score - The player's score.

isReady

public boolean isReady()
Returns the player's ready status.

Returns:
True if the player is ready, false if not.

setReady

public void setReady(boolean ready)
Sets the player's ready status to the specified value.

Parameters:
ready - Whether the player is ready or not.

getTeam

public int getTeam()
Returns the team that the player is member of, if any. If the player is not member of any teams, the method will return -1 (the default team value).

Returns:
The player's team number, or -1 if none.

setTeam

public void setTeam(int team)
Sets the player as member of the specified team. If this method is never called, the player's team number will be -1.

Parameters:
team - The player's team number.

equals

public boolean equals(java.lang.Object object)
Checks whether the input object is the same player object as this. If the object is not an AbstractPlayer object, or if the player names and/or addresses differ, the method returns false.

Overrides:
equals in class java.lang.Object
Parameters:
object - The object to compare this to.
Returns:
True if the players are the same, false if not.

getSprite

public Sprite getSprite()
Returns the player's sprite representation.

Returns:
The player's sprite.

setScreenResolution

public void setScreenResolution(int[] screenResolution)
Sets the screen resolution variable to the screen resolution on the player's phone.

Parameters:
screenResolution - The screen resolution retrieved from the phone.

getScreenResolution

public int[] getScreenResolution()
Returns an array containing the player's screen resolution.

Returns:
The player's screen resolution.

setX

public void setX(int x)
Sets the x-position of the player.

Parameters:
x - The player's x-position.

setY

public void setY(int y)
Sets the y-position of the player.

Parameters:
y - The player's y-position.

setPosition

public void setPosition(int x,
                        int y)
Sets the player's position on the game board to the specified position. When this is done, the player's movement direction is calculated based on the previously received position update.

Parameters:
x - The player's x-position.
y - The player's y-position.

move

public void move(double dx,
                 double dy)
Moves the player a distance determined by the input parameters.

Parameters:
dx - The x-movement.
dy - The y-movement.

move

public void move()
Called to move the player a distance detemined by the player's movement vector. Is called whenever the game thread loops, and the player needs to be moved by the movement prediction algorithm.


getPosition

public int[] getPosition()
Returns an array containing the player's x- and y-coordinate in that order.

Returns:
The player's position.

setSize

public void setSize(int size)
Sets the size of the player's sprite to the specified size, and creates the sprite's image.

Parameters:
size - The player's size.

getSize

public int getSize()
Returns the size of the player's sprite.

Returns:
The player's size.

getSpeed

public int getSpeed()
Returns the speed in which the player moves on the game board.

Returns:
The player's speed.

setSpeed

public void setSpeed(int speed)
Set's the speed in which the player moves in the game board to the specified value.

Parameters:
speed - The player's speed.

getAttributeValues

public java.lang.String[] getAttributeValues()
Returns a String containing the most important player attributes. From this array, a String is created and transmitted to the server when joining connecting to a server, and the player's representation on the other mobile phones is created from this attribute String.

Returns:
A String array containing the player's attributes.
See Also:
AbstractGame.sendPlayerInfo(), setAttributeValues(String[])

setAttributeValues

public void setAttributeValues(java.lang.String[] values)
Called when a player's attributes has been received. Parses a String containing attribute values and applies the correct values to the different settings. The attribute String is always created by the getAttributeValues() method, and so the rules from that method are used for parsing.

Parameters:
values - The player's attributes
See Also:
getAttributeValues()

compareTo

public int compareTo(Comparable object)
Method inherited from the Comparable interface. Is used to sort player objects according to their score. If the object passed as input parameter is not a AbstractPlayer object, 1 is returned. If the input parameter is an AbstractPlayer, the method returns the difference between this player's score and the input parameter's score.

Specified by:
compareTo in interface Comparable
Parameters:
object - The object to compare to.
Returns:
Positive value if this player has higher score than the input player, negative value if the input player has the higher score and 0 if they have equal score.

resetMovement

protected void resetMovement()
Resets the player's movement.