Class Player

java.lang.Object
  extended byPlayer
All Implemented Interfaces:
java.io.Serializable

public class Player
extends java.lang.Object
implements java.io.Serializable

Class Player represents one player.

Contains the data regarding a player, his/her boat, player's status in the race and the functionality of the player's boat.

Version:
1.0
Author:
Stefan Brockmann, Tero Kuusela
See Also:
Vector2, Serialized Form

Field Summary
private  ClientInterface client
          interface to the client object of this player
private  int cur_lap
          Stores the lap the player is in.
private  int direction
          direction of the player's boat in degrees
private  float dist_to_next_port
          distance to the next gate to pass
private  boolean finished
          has the player reached the goal
private  Game game
          player's current race
private  int id
          player's unique id
private  java.lang.String name
          player's name
private  int next_port
          the next gate to pass
(package private)  Vector2 pos
          position of the player's boat
private  int sail_value
          openness of the sail, an integer in range 0..100
 
Constructor Summary
Player(int id, java.lang.String name, ClientInterface cobj)
          Constructor.
 
Method Summary
 int calcSailDirection(int wind_direction)
          Calculates the direction of the sail in relation to the world.
 float calcWindPush(int wind_direction, int wind_velocity)
          Calculates the force pushing the boat forward.
 ClientInterface getClient()
          Returns the interface of the player's client object.
 int getCurLap()
          Returns the lap the player is in.
 int getDirection()
          Returns the movement angle of the player's boat.
 Vector2 getDirectionVector()
          Returns the movement direction vector of the player's boat.
 float getDistToNextPort()
          Returns the distance to the next gate.
 Game getGame()
          Returns the race the player is in.
 int getId()
          Returns the unique id of the player.
 java.lang.String getName()
          Returns the name of the player.
 int getNextPort()
          Returns the number of the gate the player must pass through next.
 Vector2 getPosition()
          Returns a copy of the position vector of the player's boat.
 boolean isFinished()
          Returns the value of finished.
 void move(float seconds, int wind_direction, int wind_velocity)
          Moves the boat for a certain timeslice.
 void passedPort(int passed_port, int ports_in_track)
          Tells the player (s)he has just passed a gate.
 void preparePlayerToRace(Game game, int grid_pos)
          Prepares the player for a race.
private  void readObject(java.io.ObjectInputStream in)
          Reads an object form a stream.
 void setCurLap(int l)
          Sets a new lap number to the player.
 void setDirection(int dir)
          Sets the movement direction of the player's boat.
 void setDistToNextPort(Vector2 v)
          Calculates the disctance between the player's boat and a given location (the next gate).
 void setFinished(boolean flag)
          Sets the value of finished.
 void setGame(Game g)
          Joins the player in a race.
 void setNextPort(int p)
          Sets the next gate the player must pass through.
 void setPosition(float x, float y)
          Sets the coordinates of the player's boat.
 void setPosition(Vector2 v)
          Sets the position vector of the player's boat.
 void setSailValue(int pa)
          Adjusts the position of the player's sail.
 void turnLeft()
          Rotates the player's boat to portboard.
 void turnRight()
          Rotates the player's boat to starboard.
private  void writeObject(java.io.ObjectOutputStream out)
          Writes an object to a stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pos

Vector2 pos
position of the player's boat


direction

private int direction
direction of the player's boat in degrees


sail_value

private int sail_value
openness of the sail, an integer in range 0..100


id

private int id
player's unique id


name

private java.lang.String name
player's name


game

private Game game
player's current race


client

private ClientInterface client
interface to the client object of this player


cur_lap

private int cur_lap
Stores the lap the player is in.

Lap 1 is the first lap.
At start, before the player has crossed the starting line, lap == 0.


next_port

private int next_port
the next gate to pass


dist_to_next_port

private float dist_to_next_port
distance to the next gate to pass


finished

private boolean finished
has the player reached the goal

Constructor Detail

Player

public Player(int id,
              java.lang.String name,
              ClientInterface cobj)
Constructor.

Parameters:
id - the unique id of the player
name - the name of the player
cobj - the interface of the player's client object
Method Detail

getClient

public ClientInterface getClient()
Returns the interface of the player's client object.

Returns:
Interface of the player's client object.

setGame

public void setGame(Game g)
Joins the player in a race.

Parameters:
g - the race the player joined in.

getGame

public Game getGame()
Returns the race the player is in.

Returns:
Race the player is in or NULL if the player is not in a race.

setFinished

public void setFinished(boolean flag)
Sets the value of finished.

Parameters:
flag - the new value of finished

isFinished

public boolean isFinished()
Returns the value of finished.

Returns:
Value of finished.

turnLeft

public void turnLeft()
Rotates the player's boat to portboard.


turnRight

public void turnRight()
Rotates the player's boat to starboard.


setSailValue

public void setSailValue(int pa)
Adjusts the position of the player's sail.

Parameters:
pa - An integer that defines the position of the sail.
Value in range [0, 100].
pa==0 means that the sail if fully locked (pointing towards the stern).
pa==100 means that the sail is completely loose (may point directly to the sides).

setPosition

public void setPosition(float x,
                        float y)
Sets the coordinates of the player's boat.

Parameters:
x - the new x coordinate of the boat
y - the new y coordinate of the boat

setPosition

public void setPosition(Vector2 v)
Sets the position vector of the player's boat.

Parameters:
v - the new position vector of the player's boat

setDirection

public void setDirection(int dir)
Sets the movement direction of the player's boat.

Parameters:
dir - The new movement direction of the boat, an angle in two-dimensional plane in degrees.
The angle grows when rotating counterclockwise.

getPosition

public Vector2 getPosition()
Returns a copy of the position vector of the player's boat.

Returns:
Copy of the position vector of the boat.

getDirection

public int getDirection()
Returns the movement angle of the player's boat.

Returns:
Movement angle of the player's boat in degrees.

getId

public int getId()
Returns the unique id of the player.

Returns:
Unique id of the player.

getName

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

Returns:
Name of the player.

setNextPort

public void setNextPort(int p)
Sets the next gate the player must pass through.

Parameters:
p - the number of the next gate to pass, in range [0...num_ports-1]

getNextPort

public int getNextPort()
Returns the number of the gate the player must pass through next.

Returns:
Number of the gate to pass through next
in range [0...num_ports-1]

setCurLap

public void setCurLap(int l)
Sets a new lap number to the player.

Parameters:
l - the number of the lap the player is moving into

getCurLap

public int getCurLap()
Returns the lap the player is in.

Returns:
Number of the lap the player is currently in.

setDistToNextPort

public void setDistToNextPort(Vector2 v)
Calculates the disctance between the player's boat and a given location (the next gate).

The result is saved in an attribute and can be requested through the method getDistToNextPort().

Parameters:
v - the location of the next gate

getDistToNextPort

public float getDistToNextPort()
Returns the distance to the next gate.

Returns:
The distance to the next gate to pass.

calcWindPush

public float calcWindPush(int wind_direction,
                          int wind_velocity)
Calculates the force pushing the boat forward.

The force is affected by the direction and velocity of the wind together with the direction of the boat and the position of the sail.

Parameters:
wind_direction - the direction of the wind in degrees
wind_velocity - the velocity of the wind in m/s
Returns:
Thrust force that the wind gives to the sail (with respect to the direction of the boat).

calcSailDirection

public int calcSailDirection(int wind_direction)
Calculates the direction of the sail in relation to the world.

Needed e.g. to draw the sail correctly.
The direction of the sail is measured outwards from the mast.

Parameters:
wind_direction - The direction of the wind in degrees. This affects to the direction of the sail, since the sail is below the wind when it's loose.
Returns:
New direction of the sail.

move

public void move(float seconds,
                 int wind_direction,
                 int wind_velocity)
Moves the boat for a certain timeslice.

Parameters:
seconds - the timeslice to move the boat for (in seconds)
wind_direction - the direction of the wind in degrees
wind_velocity - the velocity of the wind in m/s

passedPort

public void passedPort(int passed_port,
                       int ports_in_track)
Tells the player (s)he has just passed a gate.

Parameters:
passed_port - the number of the passed gate, in range [0...num_ports-1]
ports_in_track - the number of gates to pass in one lap

preparePlayerToRace

public void preparePlayerToRace(Game game,
                                int grid_pos)
Prepares the player for a race.

Parameters:
game - the object of the race the player is racing in
grid_pos - the number of the player's starting grid, according to which the player is placed in the correct position before the gate.

getDirectionVector

public Vector2 getDirectionVector()
Returns the movement direction vector of the player's boat.

Returns:
Movement direction vector.

writeObject

private void writeObject(java.io.ObjectOutputStream out)
                  throws java.io.IOException
Writes an object to a stream.

Parameters:
out - ObjectOutputStream to write to
Throws:
java.io.IOException

readObject

private void readObject(java.io.ObjectInputStream in)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Reads an object form a stream.

Parameters:
in - ObjectInputStream to read from
Throws:
java.io.IOException
java.lang.ClassNotFoundException