dualroids
Class SpaceShip

java.lang.Object
  |
  +--dualroids.SpatialObject
        |
        +--dualroids.SpaceShip
All Implemented Interfaces:
Serializable

public final class SpaceShip
extends SpatialObject

The representation of a space ship.

A Note About Thread Safety

The implementation of SpaceShip is thread-safe in the sense that the one thread may call the setters while another calls the getters. However, there may not be multiple threads calling getTransformedShape and getVertices. They are designed to be used from a single thread only.

Also, that thread should not retain references to objects returned by getTransformedShape and getVertices across the method calls. When the methods are called the objects they returned previously will no longer be valid.

See Also:
Serialized Form

Field Summary
private static double ACCELERATION
          The acceleration of the ship when the ship is thrusting.
private  double angle
          The rotation angle (in radians) of the spatial object at the moment indicated by timestamp.
private static double ANGULAR_VELOCITY
          The angular velocity of the ship when the ship is turning.
private static long BULLET_INTERVAL
          The minimum delay between firing bullets.
private  int bulletFireIndex
          The index of the previously fired bullet.
private  Bullet[] bullets
          An array holding all the (recyclable) bullets owned by this ship.
private static float[] CENTER_POINTS
          The coordinated of the center poins of the edges of the ship.
private static float COLOR_STEP
          The amount of time used for one outline color in explosion
private  ClientModelConnector connector
          The ClientModelConnector through which this ship can send its state to the server.
private static double DEBRIS_ANGULAR_VELOCITY
          The angular velocity of ship debris.
private static float[] DEBRIS_POINTS
          An array holding coordinate constants used when drawing debris.
private static float DEBRIS_VELOCITY_FACTOR
          A factor used when calculating the velocity of debris.
private  long earliestNextBullet
          Indicates when the next bullet can fired at the earliest.
private static double MAX_VELOCITY
          The maximum velocity of the ship.
private  long newAppears
          Indicates when a 'new' ship appears after the ship has exploded.
private static int NUM_OF_BULLETS
          The number of bullets this ship can have flying at a time.
private static int NUM_OF_VERTICES
          A constant indicating the number of vertices in the ship polygon.
private  int outlineColor
          the outline color as an index to SpatialObject.COLOR_TABLE
private  boolean thrustOn
          Indicates whether the ship is thrusting.
private  Point2D[] translatableVertices
          An array holding the translated copies of the vertices ship polygon.
private static int TURN_CCW
          A constant for indicating that the ship is turning counter-clockwise.
private static int TURN_CW
          A constant for indicating that the ship is turning clockwise.
private static int TURN_NO
          A constant for indicating that the ship is not turning.
private  int turning
          Indicates turning state of the ship.
private  Point2D[] vertices
          An array holding the untranslated vertices of the ship polygon.
 
Fields inherited from class dualroids.SpatialObject
COLOR_TABLE, COLOR_TABLE_BLACK, COLOR_TABLE_GRAY_50, COLOR_TABLE_WHITE, geometry, locationX, locationY, timestamp, transform, velocityX, velocityY
 
Constructor Summary
SpaceShip(double locationX, double locationY, long timestamp)
          Contructs a new SpaceShip
 
Method Summary
 void explode(long newAppears)
          Makes the ship explode.
 void fireBullet(long time)
          Fires a bullet and marks it fired at time.
 Bullet[] getBullets()
          Returns the Bullet array of this ship.
 Color getOutlineColor()
          Returns the outline color of the ship.
 Shape getTransformedShape(long time)
          Returns a properly transformed Shape representing the spatial object at time
 Point2D[] getVertices(long time)
          Returns the vertices of the ship polygon at time.
private  void init()
          Does real ship initialization.
private  void readObject(ObjectInputStream in)
          Deserializes the ship on the client side.
 void registerConnector(ClientModelConnector connector)
          Registers a ClientModelConnector with the ship for sending ship state to the server.
 void setStateFromRemote(double locationX, double locationY, double velocityX, double velocityY, double angle, long timestamp)
          Sets the state of the ship to values received from the network.
 void startThrust(long time)
          Starts thrusting.
 void startTurnCCW(long time)
          Starts turning counter-clockwise.
 void startTurnCW(long time)
          Starts turning clockwise.
 void stopThrust(long time)
          Stops thrusting.
 void stopTurnCCW(long time)
          Stops turning counter-clockwise.
 void stopTurnCW(long time)
          Stops turning clockwise.
 void updateState(long time)
          Updates the state of this ship to the values corresponding to a moment in time given as the argument.
private  void updateTransformation(long time)
          A helper method for updating the transormation.
private  void writeObject(ObjectOutputStream out)
          Serializes the ship for sending to the clients.
 void writeToConnector()
          Writes the state of ship to the registered ClientModelConnector
 
Methods inherited from class dualroids.SpatialObject
moveToRangeX, moveToRangeY
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

NUM_OF_VERTICES

private static final int NUM_OF_VERTICES
A constant indicating the number of vertices in the ship polygon.

NUM_OF_BULLETS

private static final int NUM_OF_BULLETS
The number of bullets this ship can have flying at a time.

TURN_NO

private static final int TURN_NO
A constant for indicating that the ship is not turning.

TURN_CCW

private static final int TURN_CCW
A constant for indicating that the ship is turning counter-clockwise.

TURN_CW

private static final int TURN_CW
A constant for indicating that the ship is turning clockwise.

ANGULAR_VELOCITY

private static final double ANGULAR_VELOCITY
The angular velocity of the ship when the ship is turning.

DEBRIS_ANGULAR_VELOCITY

private static final double DEBRIS_ANGULAR_VELOCITY
The angular velocity of ship debris.

ACCELERATION

private static final double ACCELERATION
The acceleration of the ship when the ship is thrusting.

MAX_VELOCITY

private static final double MAX_VELOCITY
The maximum velocity of the ship.

BULLET_INTERVAL

private static final long BULLET_INTERVAL
The minimum delay between firing bullets.

DEBRIS_VELOCITY_FACTOR

private static final float DEBRIS_VELOCITY_FACTOR
A factor used when calculating the velocity of debris.

COLOR_STEP

private static final float COLOR_STEP
The amount of time used for one outline color in explosion

DEBRIS_POINTS

private static final float[] DEBRIS_POINTS
An array holding coordinate constants used when drawing debris.

CENTER_POINTS

private static final float[] CENTER_POINTS
The coordinated of the center poins of the edges of the ship.

angle

private double angle
The rotation angle (in radians) of the spatial object at the moment indicated by timestamp.

vertices

private transient Point2D[] vertices
An array holding the untranslated vertices of the ship polygon.

translatableVertices

private transient Point2D[] translatableVertices
An array holding the translated copies of the vertices ship polygon.

bullets

private transient Bullet[] bullets
An array holding all the (recyclable) bullets owned by this ship.

bulletFireIndex

private transient int bulletFireIndex
The index of the previously fired bullet. Used in bullet recycling rotation.

earliestNextBullet

private transient long earliestNextBullet
Indicates when the next bullet can fired at the earliest.

connector

private transient ClientModelConnector connector
The ClientModelConnector through which this ship can send its state to the server.

newAppears

private transient long newAppears
Indicates when a 'new' ship appears after the ship has exploded.

thrustOn

private transient boolean thrustOn
Indicates whether the ship is thrusting.

turning

private transient int turning
Indicates turning state of the ship.

outlineColor

private transient int outlineColor
the outline color as an index to SpatialObject.COLOR_TABLE
Constructor Detail

SpaceShip

public SpaceShip(double locationX,
                 double locationY,
                 long timestamp)
Contructs a new SpaceShip
Method Detail

updateTransformation

private final void updateTransformation(long time)
A helper method for updating the transormation.
Parameters:
time - the point of time for which the transformation is needed.

getVertices

public final Point2D[] getVertices(long time)
Returns the vertices of the ship polygon at time. The caller should not write to the returned array.
Parameters:
time - the point of time for which the vertices are needed.
Returns:
an array of the vertices of the ship polygon

getTransformedShape

public final Shape getTransformedShape(long time)
Returns a properly transformed Shape representing the spatial object at time
Overrides:
getTransformedShape in class SpatialObject
Parameters:
time - the moment in game (server) time for which the representation is requested
Returns:
the Shape representation at time or null if the object isn't visible

init

private void init()
Does real ship initialization. This method is separate from the constuctur due to problems with object serialization.

fireBullet

public final void fireBullet(long time)
Fires a bullet and marks it fired at time.
Parameters:
time - when the bullet was fired

registerConnector

public final void registerConnector(ClientModelConnector connector)
Registers a ClientModelConnector with the ship for sending ship state to the server. Used only for the ship controlled by the client itself.
Parameters:
connector - the ClientModelConnector to be registered

getBullets

public final Bullet[] getBullets()
Returns the Bullet array of this ship. The caller should not write to the array.
Returns:
the Bullet array of this ship

getOutlineColor

public final Color getOutlineColor()
Returns the outline color of the ship.
Returns:
the outline color of the ship

explode

public final void explode(long newAppears)
Makes the ship explode.
Parameters:
newAppears - when the new ship appears

updateState

public final void updateState(long time)
Updates the state of this ship to the values corresponding to a moment in time given as the argument. The timestamp is updated accordingly.
Parameters:
time - the new timestamp

startThrust

public final void startThrust(long time)
Starts thrusting.
Parameters:
time - when the thrusting was started

stopThrust

public final void stopThrust(long time)
Stops thrusting.
Parameters:
time - when the thrusting was stopped

startTurnCCW

public final void startTurnCCW(long time)
Starts turning counter-clockwise.
Parameters:
time - when the turning was started

stopTurnCCW

public final void stopTurnCCW(long time)
Stops turning counter-clockwise.
Parameters:
time - when the turning was stopped

startTurnCW

public final void startTurnCW(long time)
Starts turning clockwise.
Parameters:
time - when the turning was started

stopTurnCW

public final void stopTurnCW(long time)
Stops turning clockwise.
Parameters:
time - when the turning was stopped

setStateFromRemote

public final void setStateFromRemote(double locationX,
                                     double locationY,
                                     double velocityX,
                                     double velocityY,
                                     double angle,
                                     long timestamp)
Sets the state of the ship to values received from the network.
Parameters:
locationX - the x coordinate of the space ship
locationY - the y coordinate of the space ship
velocityX - the x component of the velocity of the space ship
velocityY - the y component of the velocity of the space ship
angle - the rotational angle of the space ship
timestamp - the timestamp

writeToConnector

public final void writeToConnector()
Writes the state of ship to the registered ClientModelConnector

writeObject

private void writeObject(ObjectOutputStream out)
                  throws IOException
Serializes the ship for sending to the clients.
Parameters:
out - the target stream

readObject

private void readObject(ObjectInputStream in)
                 throws IOException,
                        ClassNotFoundException
Deserializes the ship on the client side.
Parameters:
in - the source stream


Dualroids was written by Oskar Ojala, Yrjö Kari-Koskinen and Henri Sivonen