import net.sf.jirr.*;
/**
*
* @author Matthew Casperson
*
* A class used to display static or animated 2D images. This class can be extended
* to provide more functionality, or used as is.
*
*/
public class TwoDGameObject extends BaseObject
{
static protected ResourcePool pool = new ResourcePool(
new IBaseObjectCreator() {public BaseObject createNewBaseObject() { return new TwoDGameObject(); }});
/** a reference to the source image */
protected TwoDGraphicsResource graphics = null;
/** the current 2D position of this object */
public position2df position = null;
/** this defines the type of object in the collision detection system */
protected String collisionName;
/** the current animation frame (will always be 1 for static images) */
protected int currentFrame = 0;
/** a running count of the time, used to modify the currentFrame */
protected double frameTime = 0;
/** if this is true the object will shutdown once it has played the animation once */
protected boolean playOnce = false;
/** the width in pixels of one frame of animation */
protected int frameWidth = 0;
static public TwoDGameObject getTwoDGameObject()
{
return (TwoDGameObject)pool.getItemFromPool();
}
public String getCollisionName()
{
return collisionName;
}
public TwoDGraphicsResource getGraphics()
{
return graphics;
}
public recti getCollisionArea()
{
// recalculate the collision area with respect to the current position
int x = (int)position.getX();
int y = (int)position.getY();
return new recti(x, y, x + graphics.area.getWidth(), y + graphics.area.getHeight());
}
protected TwoDGameObject()
{
}
public void startupTwoDGameObject(TwoDGraphicsResource graphics, position2df position)
{
initialize(graphics, position, CollisionIdentifiers.NONE, false);
}
public void startupTwoDGameObject(TwoDGraphicsResource graphics, position2df position, String collisionName)
{
initialize(graphics, position, collisionName, false);
}
public void startupTwoDGameObject(TwoDGraphicsResource graphics, position2df position, String collisionName, boolean playOnce)
{
initialize(graphics, position, collisionName, playOnce);
}
protected void initialize(TwoDGraphicsResource graphics, position2df position, String collisionName, boolean playOnce)
{
this.position = position;
this.graphics = graphics;
this.collisionName = collisionName;
this.frameWidth = graphics.area.getWidth() / graphics.frames;
this.playOnce = playOnce;
EngineManager.getInstance().addTwoDGameObject(this);
super.startupBaseObject();
}
public void enterFrame(double dt)
{
// get the next frame
frameTime += dt;
if (graphics.fps > 0)
{
while (frameTime > 1/graphics.fps)
{
frameTime -= 1/graphics.fps;
currentFrame = (currentFrame + 1) % graphics.frames;
if (currentFrame == 0 && playOnce)
{
shutdown();
break;
}
}
}
}
public void shutdown()
{
EngineManager.getInstance().removeTwoDGameObject(this);
super.shutdown();
}
public void draw2D()
{
// calculate the area of the animation to draw this frame, or draw the whole image
// for static images
int x = graphics.area.getUpperLeftCorner().getX() + currentFrame * frameWidth;
int y = graphics.area.getUpperLeftCorner().getY();
int height = graphics.area.getHeight();
int width = frameWidth;
// actually draw the 2D image to the screen
EngineManager.getInstance().getVideoDriver().draw2DImage(
graphics.texture,
new position2di((int)position.getX(), (int)position.getY()),
new recti(x, y, x + width, y + height),
null,
new SColor(255,255,255,255),
true);
}
protected void keepOnScreen()
{
if (position.getX() < 0)
position.setX(0);
else if (position.getX() > EngineManager.WINDOW_WIDTH - graphics.area.getWidth())
position.setX(EngineManager.WINDOW_WIDTH - graphics.area.getWidth());
if (position.getY() < 0)
position.setY(0);
else if (position.getY() > EngineManager.WINDOW_HEIGHT - graphics.area.getHeight())
position.setY(EngineManager.WINDOW_HEIGHT - graphics.area.getHeight());
}
protected void shutdownIfOffScreen()
{
if (position.getX() < -graphics.area.getWidth() ||
position.getX() > EngineManager.WINDOW_WIDTH ||
position.getY() < -graphics.area.getHeight() ||
position.getY() > EngineManager.WINDOW_HEIGHT)
{
shutdown();
}
}
public void collision(TwoDGameObject other)
{
}
}
|