dualroids
Class Asteroid

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

public class Asteroid
extends SpatialObject

The representation of an asteroid in the model.

The Layout of the Asteroid Table

The length of the asteroid table is required to be a multiple of 7. The first large asteroid is at index 0. The second is at 7, etc.

When a large asteroid is at index i, its residual medium-sized asteroids are at indeces i + 1 and i + 4.

When a medium-sized asteroid is at index i, its residual small asteroids are at indeces i + 1 and i + 2.

A Note About Thread Safety

The setters of an Asteroid may be used from multiple threads. Also, the getters may be used from a single thread safely even when there are other thread using the setters. However, there may not be multiple threads calling getTransformedShape. It is designed to be used from a single thread only.

Also, that thread should not retain references to objects returned by getTransformedShape across the method calls. When the method is called the object it returned previously will no longer be valid.

See Also:
Serialized Form

Field Summary
private  double angle
          The rotation angle (in radians) of the spatial object at the moment indicated by timestamp.
private  double angularVelocity
          The angular velocity (radians per millisecond) of the asteroid.
private  float[] centers
          An array for storing the coordinates of the centers of the edges of this asteroid.
static float COLOR_STEP
          The amount of time used for one outline color in an explosion.
static float DEBRIS_LIFETIME
          The lifetime of asteroid debris.
static float DEBRIS_VELOCITY_FACTOR
          Constant used when calculating the movement of asteroid debris.
private  boolean exploding
          A boolean indicating whether the asteroid is currently exploding (that is, whether debris is visible).
private  int hitsToExplosion
          The number of hits it takes to make this asteroid explode.
static int LARGE
          Constant for indicating that the asteroid is large
static double LARGE_EXPLOSION_EXTRA
          Constant used when modeling the extra energy released when a large asteroid explodes.
static int MEDIUM
          Constant for indicating that the asteroid is medium-sized
static double MEDIUM_EXPLOSION_EXTRA
          Constant used when modeling the extra energy released when a medium-sized asteroid explodes.
private  int outlineColor
          the outline color as an index to SpatialObject.COLOR_TABLE
private  int points
           
private  int size
          The size of the asteroid.
static int SMALL
          Constant for indicating that the asteroid is small
private  float[] vertices
          An array for storing the coordinates of the vertices of this asteroid.
private  boolean visible
          A boolean indicating whether the asteroid is visible.
 
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
Asteroid(double locationX, double locationY, double velocityX, double velocityY, long timestamp, int size)
          Contructs a new asteroid.
 
Method Summary
static void blowUpAsteroid(Asteroid[] asteroids, int index, double locationX, double locationY, long timestamp, boolean onServer)
          Makes an asteroid explode and initalizes the residual asteroids accordingly.
 void explode(long time)
          Tells the asteroid to explode.
 void explodeOnServer()
          Tells the asteroid to explode withou debris effects.
static void generateResidualAsteroids(Asteroid[] asteroids)
          Generates residual asteroids in the array of asteroids passed as the argument.
 double getLocationX(long time)
          Returns the x coordinate of the location of the asteroid at time.
 double getLocationY(long time)
          Returns the y coordinate of the location of the asteroid at time.
 Color getOutlineColor()
          Returns the outline color of the asteroid.
 int getPoints()
          Returns the points awarded for blowing up this asteroid.
 int getSize()
          Indicates the size of the asteroid.
 Shape getTransformedShape(long time)
          Returns a properly transformed Shape representing the spatial object at time
 boolean isVisible()
          Indicates whether the asteroid is visible.
 void makeVisible()
          Makes the asteroid visible.
private  void readObject(ObjectInputStream in)
          Deserializes the asteroid on the client side.
 boolean registerHit()
          Registers a bullet hit to the asteroid and checks whether it explodes.
 void setLocationAndAngle(double locationX, double locationY, double angle, long timestamp)
          Sets the location and angle of this asteroid.
 void updateLocationAndAngle(long time)
          Updates the location and angle of this asteroid to the values corresponding to a moment in time given as the argument.
private  void writeObject(ObjectOutputStream out)
          Serializes the asteroid for sending to the clients.
 void writeToConnector(int thisIndex, ServerModelConnector connector)
          Writes the state of this asteroid to a ServerModelConnector.
 
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

SMALL

public static final int SMALL
Constant for indicating that the asteroid is small

MEDIUM

public static final int MEDIUM
Constant for indicating that the asteroid is medium-sized

LARGE

public static final int LARGE
Constant for indicating that the asteroid is large

LARGE_EXPLOSION_EXTRA

public static final double LARGE_EXPLOSION_EXTRA
Constant used when modeling the extra energy released when a large asteroid explodes.

MEDIUM_EXPLOSION_EXTRA

public static final double MEDIUM_EXPLOSION_EXTRA
Constant used when modeling the extra energy released when a medium-sized asteroid explodes.

DEBRIS_VELOCITY_FACTOR

public static final float DEBRIS_VELOCITY_FACTOR
Constant used when calculating the movement of asteroid debris.

DEBRIS_LIFETIME

public static final float DEBRIS_LIFETIME
The lifetime of asteroid debris.

COLOR_STEP

public static final float COLOR_STEP
The amount of time used for one outline color in an explosion.

outlineColor

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

angularVelocity

private double angularVelocity
The angular velocity (radians per millisecond) of the asteroid.

size

private int size
The size of the asteroid. SMALL, MEDIUM or LARGE

visible

private boolean visible
A boolean indicating whether the asteroid is visible.

exploding

private boolean exploding
A boolean indicating whether the asteroid is currently exploding (that is, whether debris is visible).

angle

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

hitsToExplosion

private transient int hitsToExplosion
The number of hits it takes to make this asteroid explode.

vertices

private float[] vertices
An array for storing the coordinates of the vertices of this asteroid. The array is in the format accepted by AffineTransform: x0, y0, ... xn, yn.

points

private transient int points

centers

private transient float[] centers
An array for storing the coordinates of the centers of the edges of this asteroid. The array is in the format accepted by AffineTransform: x0, y0, ... xn, yn.
Constructor Detail

Asteroid

public Asteroid(double locationX,
                double locationY,
                double velocityX,
                double velocityY,
                long timestamp,
                int size)
Contructs a new asteroid. The geometry is created automatically. The angular velocity is chosen automatically. Large asteroids are visible initially. Other asteroids are invisible by default.
Parameters:
locationX - the x coordinate of the initial location
locationY - the y coordinate of the initial location
velocityX - the x component of the velocity
velocityY - the y component of the velocity
timestamp - the moment in game (server) time for which the location is valid
size - the size of the asteroid: SMALL, MEDIUM or LARGE
Method Detail

generateResidualAsteroids

public static void generateResidualAsteroids(Asteroid[] asteroids)
Generates residual asteroids in the array of asteroids passed as the argument.
Parameters:
asteroids - the asteroid table

blowUpAsteroid

public static void blowUpAsteroid(Asteroid[] asteroids,
                                  int index,
                                  double locationX,
                                  double locationY,
                                  long timestamp,
                                  boolean onServer)
Makes an asteroid explode and initalizes the residual asteroids accordingly.
Parameters:
asteroids - the asteroid table
index - the index of the asteroid to be blown up
locationX - the x coordinate of the asteroid at the time of the explosion
locationY - the y coordinate of the asteroid at the time of the explosion
timestamp - the point in time when the explosion happens
onServer - a boolean indicating whether this method is called from the server thread

getTransformedShape

public 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

setLocationAndAngle

public void setLocationAndAngle(double locationX,
                                double locationY,
                                double angle,
                                long timestamp)
Sets the location and angle of this asteroid. This method is supposed to be called from ClientModelController.
Parameters:
locationX - the x coordinate of the new location
locationY - the y coordinate of the new location
angle - the new angle
timestamp - the moment in game (server) time for which at which the other values were valid.

updateLocationAndAngle

public void updateLocationAndAngle(long time)
Updates the location and angle of this asteroid to the values corresponding to a moment in time given as the argument. The timestamp is updated accordingly.
Parameters:
time - the new timestamp

makeVisible

public final void makeVisible()
Makes the asteroid visible. used when a larger asteroid explodes. Be sure to call setLocationAndAngle first.

explode

public final void explode(long time)
Tells the asteroid to explode. It is up to the caller to make residual asteroids visible appropriately.
Parameters:
time - the point in time when the explosion starts

explodeOnServer

public void explodeOnServer()
Tells the asteroid to explode withou debris effects. It is up to the caller to make residual asteroids visible appropriately.

isVisible

public final boolean isVisible()
Indicates whether the asteroid is visible.
Returns:
a boolean indicating visibility

registerHit

public boolean registerHit()
Registers a bullet hit to the asteroid and checks whether it explodes.
Returns:
true if the hit causes the asteroid to explode and false otherwise

getSize

public final int getSize()
Indicates the size of the asteroid.
Returns:
the size of the asteroid

getOutlineColor

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

getLocationX

public final double getLocationX(long time)
Returns the x coordinate of the location of the asteroid at time.
Parameters:
time - the point in time for which the x coordinate is requested
Returns:
the x coordinate of the location of the asteroid at time

getLocationY

public final double getLocationY(long time)
Returns the y coordinate of the location of the asteroid at time.
Parameters:
time - the point in time for which the y coordinate is requested
Returns:
the y coordinate of the location of the asteroid at time

getPoints

public final int getPoints()
Returns the points awarded for blowing up this asteroid.
Returns:
the points awarded for blowing up this asteroid

writeToConnector

public final void writeToConnector(int thisIndex,
                                   ServerModelConnector connector)
Writes the state of this asteroid to a ServerModelConnector.
Parameters:
thisIndex - the index of this asteroid
connector - the target ServerModelConnector

writeObject

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

readObject

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


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