Class Sprite

java.lang.Object
  |
  +--Sprite
Direct Known Subclasses:
SpriteAsteroid, SpriteBullet, SpriteExplosion, SpriteMissile, SpriteShip, SpriteUfo, SpriteWall

abstract class Sprite
extends java.lang.Object

The Sprite class defines a game object, including its shape, position, movement and rotation. It also can detemine if two objects collide. We have a number of derived classes for the special kinds of sprites.


Field Summary
(package private)  Vector2 acceleration
          The acceleration vector.
(package private)  boolean active
          A flag to indicate if the sprite is currently alive and visible.
(package private)  double age
          Measure the sprite's age in seconds to time things like explosions, hyperjumps and waits between bullet shots.
(package private)  double ageDone
          Sprites can use the updateCounter to time things like explosions or a fade to black.
(package private)  double angle
          The current rotation aspect.
(package private)  double angularAcceleration
          The "acceleration" or speed at which the sprite is rotating.
 boolean bounce
          What to do when the asteroids hit the edge of the screen - initially false
(package private)  java.awt.Color edgecolor
          The color to use for the sprite's outline.
(package private)  java.awt.Color fillcolor
          The color to fill in the sprite with.
(package private)  boolean fillflag
          Whether or not to fill the sprite in.
(package private)  AsteroidsGame game
          The "owner" game.
private  double mass
          We'll maintain the mass as the cube of the radius, setting it in the same fixRadius function that fixes the radius.
(package private)  double maxspeed
          The maximum speed.
(package private)  Asteroids ownerApp
          The "owner" applet or application.
(package private)  Vector2 position
          The screen pixel position.
(package private)  Vector2 positiontransform
          The pixel position.
private  double radius
          The size of the thing.
(package private)  java.awt.Polygon shape
          The basic shape which gets translated, rotated, and pixeld.
(package private)  java.awt.Polygon shapetransform
          The transformed shape polygon, after the rotation and the translation.
(package private)  boolean soundplaying
          Is the sound currently on?
(package private)  Vector2 velocity
          The velocity vector.
 
Constructor Summary
Sprite(RealPixelConverter rtopc)
          The Sprite constructor sets everything to 0, except for the fillcolor, which gets randomized.
 
Method Summary
 boolean advance(double dt)
          Update the rotation and position coordinates based on the delta values.
 void draw(java.awt.Graphics g, boolean detail)
          Use the fillPolygon and drawPolygon functions to draw.
 void explode(RealPixelConverter rtopc)
          Create sprites for explosion animation.
 void fixRadius()
          First sets the centroid to be the average of the shape vertices, then translatees the shape so that its centroid is at the origin, then sets the radius value as the maximum distance from the origin to one of the shape vertices.
 boolean isColliding(Sprite s)
          Just look at the distances between the positions of the two and compare to the sum of the radii.
 boolean isTouching(Sprite s)
          This is more accurate than isColliding, but a bit slower.
 void loopsound(boolean onoff)
          The base method bails out if the the Asteroids soundsloaded isn't true, and it won't turn on a sound if Asteroids soundflag is off.
 double mass()
          Accessor
 double radius()
          We need an accessor for radius.
 void randomizeFillcolor()
           
 void render(RealPixelConverter rtopc)
          Applies the rotation and the position translation to the shape to get the shapetransform polygon.
abstract  void reset(RealPixelConverter rtopc)
          The abstract reset method is called by the child sprite constructors, and may also be called when resetting a sprite's state.
 void setFakeRadius(double fakeradius)
          Use this to set a fake radius to make something hard to hit.
 void stop()
          The stop method sets active to false and turns off the sound.
abstract  void update(RealPixelConverter rtopc)
          The update method can be overloaded by the child sprite.
 boolean wrapandbounce()
          Handles the wrapping or bouncing.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

shape

java.awt.Polygon shape
The basic shape which gets translated, rotated, and pixeld.

active

boolean active
A flag to indicate if the sprite is currently alive and visible.

age

double age
Measure the sprite's age in seconds to time things like explosions, hyperjumps and waits between bullet shots.

bounce

public boolean bounce
What to do when the asteroids hit the edge of the screen - initially false

angle

double angle
The current rotation aspect.

radius

private double radius
The size of the thing. Make this private so nobody chagnes it directly, must be chagned using setRadius, so as to keep mass in synch.

mass

private double mass
We'll maintain the mass as the cube of the radius, setting it in the same fixRadius function that fixes the radius.

angularAcceleration

double angularAcceleration
The "acceleration" or speed at which the sprite is rotating. That is, this is d angle/ dt.

position

Vector2 position
The screen pixel position.

positiontransform

Vector2 positiontransform
The pixel position.

velocity

Vector2 velocity
The velocity vector.

maxspeed

double maxspeed
The maximum speed.

acceleration

Vector2 acceleration
The acceleration vector.

shapetransform

java.awt.Polygon shapetransform
The transformed shape polygon, after the rotation and the translation.

fillflag

boolean fillflag
Whether or not to fill the sprite in. Default true. Set false for the Spritebullet only.

fillcolor

java.awt.Color fillcolor
The color to fill in the sprite with.

edgecolor

java.awt.Color edgecolor
The color to use for the sprite's outline. Normally white.

ageDone

double ageDone
Sprites can use the updateCounter to time things like explosions or a fade to black. UFO and missiles use this to time how long they live.

soundplaying

boolean soundplaying
Is the sound currently on?

game

AsteroidsGame game
The "owner" game. We need this as a way of referring to the other sprites that are around.

ownerApp

Asteroids ownerApp
The "owner" applet or application. We need this as a way of referring to the AudioClip sounds and the sound flags.
Constructor Detail

Sprite

public Sprite(RealPixelConverter rtopc)
The Sprite constructor sets everything to 0, except for the fillcolor, which gets randomized.
Method Detail

radius

public double radius()
We need an accessor for radius.

setFakeRadius

public void setFakeRadius(double fakeradius)
Use this to set a fake radius to make something hard to hit.

mass

public double mass()
Accessor

randomizeFillcolor

public void randomizeFillcolor()

fixRadius

public void fixRadius()
First sets the centroid to be the average of the shape vertices, then translatees the shape so that its centroid is at the origin, then sets the radius value as the maximum distance from the origin to one of the shape vertices.

advance

public boolean advance(double dt)
Update the rotation and position coordinates based on the delta values. If the coordinates move off the edge of the screen, they are wrapped around to the other side.

wrapandbounce

public boolean wrapandbounce()
Handles the wrapping or bouncing. We have four possibilites: wrap or bounce in either a disk world or a rectangular world.

render

public void render(RealPixelConverter rtopc)
Applies the rotation and the position translation to the shape to get the shapetransform polygon. The shape is rotated angle degrees around the origin and then translated by the amount pixel*position + (width/2, height/2).

isColliding

public boolean isColliding(Sprite s)
Just look at the distances between the positions of the two and compare to the sum of the radii. This is a faster method than isTouching, though less accurate. We use this for asteroid-asteroid collisions only. It is good for this purpose becuase it makes the collision back-off correction look smooth.

isTouching

public boolean isTouching(Sprite s)
This is more accurate than isColliding, but a bit slower. We use it for the ship-asteroid collisions, for ship-bullet, and for bullet-missile collisions. You have to use inside for JDK 1.0, but don't use it for JDK 1.1 as it doesn't work there.

reset

public abstract void reset(RealPixelConverter rtopc)
The abstract reset method is called by the child sprite constructors, and may also be called when resetting a sprite's state. It needs the RealPixelConverter because if we change a sprite's postion we like to call render right away so as to have correct information in shapetransform for use by the isTouching mehtod.

update

public abstract void update(RealPixelConverter rtopc)
The update method can be overloaded by the child sprite. It needs the RealPixelCoverter argument because it will sometimes reset anothe sprite (like a bullet or an explosion).

loopsound

public void loopsound(boolean onoff)
The base method bails out if the the Asteroids soundsloaded isn't true, and it won't turn on a sound if Asteroids soundflag is off.

stop

public void stop()
The stop method sets active to false and turns off the sound.

explode

public void explode(RealPixelConverter rtopc)
Create sprites for explosion animation. Each individual line segment of the given shapetransform is used to create a new shapetransform that will move outward from the shapetransform's original position with a random rotation. We deliberately don't call setRadius for the explosions because we want their centers not to be in the middle of their segments. Instead we set their radii by hand.

draw

public void draw(java.awt.Graphics g,
                 boolean detail)
Use the fillPolygon and drawPolygon functions to draw.