Class Game

java.lang.Object
  extended byGame
All Implemented Interfaces:
java.lang.Runnable, java.io.Serializable

public class Game
extends java.lang.Object
implements java.lang.Runnable, java.io.Serializable

Class Game runs one race.

The race can have two states, racing or not racing (attribute is_racing. If the race isn't in a racing state, all player boats wait at the starting line for the master player to begin the race. When this happens, the race switches to the racing state. The race runs in it's own thread where several things, like checking any changes in positions and wind velocity, are done in a race loop.

Version:
1.0
Author:
Stefan Brockmann, Tero Kuusela
See Also:
GameInfo, Track, TrackFileParser, Serialized Form

Field Summary
private  int change_wind_time
          time when the direction and strength of the wind will next be changed
private  int end_time
          time when the current race state (racing/not racing) ends
private  java.lang.String game_status
          A text that tells the race state.
private  int id
          race id
private  boolean is_racing
          is this race in a racing state or not
private  int master_id
          id of the master player
static int MaxPlayers
          maximum number of players in one race
private  java.lang.String name
          race name
private  int num_finished
          number of players who have reached the goal
private  java.util.ArrayList players
          A list of all players in this race.
private  java.lang.String[] race_result
          list of results is a String array where the name of each player reaching the goal is stored
private  java.util.Random rand
          Random object to generate random numbers
private  boolean running
          Is the race being run? The race loop is executed as long as running == true.
private  Server server
          Server object this race belongs to
private  int start_time
          starting time of the race
private  Track track
          track used in this race
private  int wind_direction
          direction of the wind in this race, given in degrees
private  int wind_velocity
          velocity of the wind in this race, given in m/s
 
Constructor Summary
Game(int id, java.lang.String name, int master_id, Server s)
          Constructor.
 
Method Summary
 boolean addPlayer(Player p)
          Adds a player to the race.
 void clearRaceResult()
          Clears the result array.
 void crashWithOtherPlayers(Player plr)
          Makes the collision detections between the given player and other players.
 java.lang.String getGameStatus()
          Returns the text that gives the race status.
 int getId()
          Returns the id of this race.
 GameInfo getInfo()
          Returns the information related to this race.
 int getMasterId()
          Returns the id of the master player of this race.
 java.lang.String getName()
          Returns the name of this race.
 int getNumPlayers()
          Returns the number of players in this race.
 java.util.Iterator getPlayersIterator()
          Returns an iterator for the players.
 java.lang.String[] getRaceResult()
          Returns the result array.
 int getRandomIntRange(int min, int max)
          Returns the next random number from the given range.
 int getTimeS()
          Returns the time of the server clock in seconds.
 Track getTrack()
          Returns a reference to the currect track object.
 java.lang.String getTrackName()
          Returns the name of the current track.
 int getWindDirection()
          Returns the direction of the wind in this race.
 int getWindVelocity()
          Returns the wind velocity in this race.
 boolean isRacing()
          Returns the race state.
 void loadTrack(java.lang.String track_name)
          Loads a new track to the race.
 void movePlayers(float seconds)
          Moves all the players and makes the collision detections.
 void preparePlayers()
          Prepares players for the race.
 void quit()
          Exits the race.
private  void readObject(java.io.ObjectInputStream in)
          Reads an object from a stream.
 void removeAllPlayers()
          Removes all players from the race.
 void removePlayer(Player p)
          Removes a player from the race.
 void run()
          The race is ran in this run method of the thread.
 void say(java.lang.String str)
          Sends a message to all players in this race.
 void setStatusText(java.lang.String str)
          Sets the text that gives the race status.
 void startGame()
          Begins a race.
 void stopGame()
          Ends a race.
private  void writeObject(java.io.ObjectOutputStream out)
          Writes the race data to an Object stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MaxPlayers

public static final int MaxPlayers
maximum number of players in one race

See Also:
Constant Field Values

id

private int id
race id


name

private java.lang.String name
race name


master_id

private int master_id
id of the master player


server

private Server server
Server object this race belongs to


race_result

private java.lang.String[] race_result
list of results is a String array where the name of each player reaching the goal is stored


num_finished

private int num_finished
number of players who have reached the goal


track

private Track track
track used in this race


is_racing

private boolean is_racing
is this race in a racing state or not


running

private boolean running
Is the race being run? The race loop is executed as long as running == true.


start_time

private int start_time
starting time of the race


end_time

private int end_time
time when the current race state (racing/not racing) ends


game_status

private java.lang.String game_status
A text that tells the race state. This is sent to the client programs.


players

private java.util.ArrayList players
A list of all players in this race.


wind_direction

private int wind_direction
direction of the wind in this race, given in degrees


wind_velocity

private int wind_velocity
velocity of the wind in this race, given in m/s


change_wind_time

private int change_wind_time
time when the direction and strength of the wind will next be changed


rand

private java.util.Random rand
Random object to generate random numbers

Constructor Detail

Game

public Game(int id,
            java.lang.String name,
            int master_id,
            Server s)
Constructor.

Parameters:
id - Race id
name - Race name
master_id - Id of the master player
s - Server object
Method Detail

getInfo

public GameInfo getInfo()
Returns the information related to this race.

Returns:
Race information.

getId

public int getId()
Returns the id of this race.

Returns:
Race id.

getName

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

Returns:
Race name.

getMasterId

public int getMasterId()
Returns the id of the master player of this race.

Returns:
Id of the master player.

clearRaceResult

public void clearRaceResult()
Clears the result array.


getRaceResult

public java.lang.String[] getRaceResult()
Returns the result array.

Returns:
Race results.

isRacing

public boolean isRacing()
Returns the race state.

Returns:
Race state.

setStatusText

public void setStatusText(java.lang.String str)
Sets the text that gives the race status.

Parameters:
str - Race status text

getGameStatus

public java.lang.String getGameStatus()
Returns the text that gives the race status.

Returns:
Race status text.

loadTrack

public void loadTrack(java.lang.String track_name)
Loads a new track to the race.

Parameters:
track_name - name of the track to load

getTrack

public Track getTrack()
Returns a reference to the currect track object.

Returns:
Reference to the current track object.

getTrackName

public java.lang.String getTrackName()
Returns the name of the current track.

Returns:
Name of the current track.

addPlayer

public boolean addPlayer(Player p)
Adds a player to the race.

Parameters:
p - Player object to add
Returns:
Boolean that tells was the player succesfully added.
true == added, false == not added.

getPlayersIterator

public java.util.Iterator getPlayersIterator()
Returns an iterator for the players.

Returns:
Player iterator.

removePlayer

public void removePlayer(Player p)
Removes a player from the race.

Parameters:
p - player's object

removeAllPlayers

public void removeAllPlayers()
Removes all players from the race.


getNumPlayers

public int getNumPlayers()
Returns the number of players in this race.

Returns:
Number of players in this race.

preparePlayers

public void preparePlayers()
Prepares players for the race.


getWindDirection

public int getWindDirection()
Returns the direction of the wind in this race.

Returns:
Direction of the wind in this race.

getWindVelocity

public int getWindVelocity()
Returns the wind velocity in this race.

Returns:
Wind velocity in this race as m/s.

getRandomIntRange

public int getRandomIntRange(int min,
                             int max)
Returns the next random number from the given range.

Parameters:
min - lower limit of the integer to return
max - the upper limit of the integer to return
Returns:
A random integer in the given range.

getTimeS

public int getTimeS()
Returns the time of the server clock in seconds.

NOTE: This means all timing is done at a precision of one second.

Returns:
Server clock time in seconds.

quit

public void quit()
Exits the race.


startGame

public void startGame()
Begins a race.


stopGame

public void stopGame()
Ends a race.


run

public void run()
The race is ran in this run method of the thread.

Specified by:
run in interface java.lang.Runnable

movePlayers

public void movePlayers(float seconds)
Moves all the players and makes the collision detections.


crashWithOtherPlayers

public void crashWithOtherPlayers(Player plr)
Makes the collision detections between the given player and other players.

Parameters:
plr - The player whose collisions to check.

say

public void say(java.lang.String str)
Sends a message to all players in this race.

Parameters:
str - The message to send.

writeObject

private void writeObject(java.io.ObjectOutputStream out)
                  throws java.io.IOException
Writes the race data to an Object stream.

NOTE: We only write the necessary attributes and objects to keep the race and network load as load as possible.

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

readObject

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

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