import net.jscience.math.kvm.MathFP;
import javax.microedition.lcdui.*;
import java.io.IOException;
/**
* A Bullet Actor is a simple flying object fired from a Ship. It has a limited
* lifetime of 3 seconds using code in the cycle method after which the object
* is removed from the GameScreen.
* @author Martin J. Wells
*/
public class Bullet extends Actor
{
private static ImageSet bulletImageSet; // the one and only imageset
private Sprite bulletSprite;
/**
* Creates a new Bullet at the specified starting position and facing
* direction. This is called by the Ship class when the player hits the fire
* key.
* @param worldArg The world this actor is in.
* @param startX The starting x position.
* @param startY The starting y position.
* @param direction The starting direction.
*/
public Bullet(World worldArg, int startX, int startY, int direction)
{
super(worldArg, startX, startY, direction, 16, MathFP.toFP("6.0"),
MathFP.toFP("0.0"), MathFP.toFP("6.0"), MathFP.toFP("-1.0"), 0);
if (bulletImageSet == null)
{
try
{
Image[] frames = ImageSet.extractFrames(Image.createImage(
"/general.png"), 0, 0, 4, 1, 3, 3);
bulletImageSet = new ImageSet(1);
bulletImageSet.addState(frames, 0);
}
catch (IOException ioe)
{
System.out.println("unable to load image");
}
}
bulletSprite = new Sprite(bulletImageSet, 0, 0);
}
/**
* The render method is basically the same as the Ship except that the bullet
* graphic doesn抰 face a particular direction so you don抰 need to worry
* about setting that.
* @param graphics The graphics context upon which to draw the bullet.
* @param offsetX The amount to offset the x drawing position by.
* @param offsetY The amount to offset the y drawing position by.
*/
public void render(Graphics graphics, int offsetX, int offsetY)
{
bulletSprite.draw(graphics, getX()-offsetX, getY()-offsetY);
}
/**
* The cycle method calls the base class cycle (Actor) which handles the
* movement, then we cycle the Sprite object so it will animate (progress to
* the next frame) before checking the amount of time the object has been
* around and destroy it by calling the GameScreen.removeActor method.
* @param deltaMS The amount of time that has passed sine the last call to
* cycle (in milliseconds).
*/
public void cycle(long deltaMS)
{
super.cycle(deltaMS);
bulletSprite.cycle(deltaMS);
// terminate this bullet if it's older than 3 seconds
if (bulletSprite.getTimeInCurrentState() > 3000)
getWorld().removeActor(this);
}
/**
* @return The height of the Bullet object (always 1 for this case).
*/
public int getHeight() { return 1; }
/**
* @return The width of the Bullet object (always 1 for this case).
*/
public int getWidth() { return 1; }
}