package org.newdawn.spaceinvaders;
/**
* An entity which represents one of our space invader aliens.
*
* @author Kevin Glass
*/
public class AlienEntity extends Entity {
/** The speed at which the alient moves horizontally */
private double moveSpeed = 75;
/** The game in which the entity exists */
private Game game;
/** The animation frames */
private Sprite[] frames = new Sprite[4];
/** The time since the last frame change took place */
private long lastFrameChange;
/** The frame duration in milliseconds, i.e. how long any given frame of animation lasts */
private long frameDuration = 250;
/** The current frame of animation being displayed */
private int frameNumber;
/**
* Create a new alien entity
*
* @param game The game in which this entity is being created
* @param x The intial x location of this alien
* @param y The intial y location of this alient
*/
public AlienEntity(Game game,int x,int y) {
super("sprites/alien.gif",x,y);
frames[0] = sprite;
frames[1] = SpriteStore.get().getSprite("sprites/alien2.gif");
frames[2] = sprite;
frames[3] = SpriteStore.get().getSprite("sprites/alien3.gif");
this.game = game;
dx = -moveSpeed;
}
/**
* Request that this alien moved based on time elapsed
*
* @param delta The time that has elapsed since last move
*/
public void move(long delta) {
lastFrameChange += delta;
if (lastFrameChange > frameDuration) {
lastFrameChange = 0;
frameNumber++;
if (frameNumber >= frames.length) {
frameNumber = 0;
}
sprite = frames[frameNumber];
}
if ((dx < 0) && (x < 10)) {
game.updateLogic();
}
if ((dx > 0) && (x > 750)) {
game.updateLogic();
}
super.move(delta);
}
/**
* Update the game logic related to aliens
*/
public void doLogic() {
dx = -dx;
y += 10;
if (y > 570) {
game.notifyDeath();
}
}
/**
* Notification that this alien has collided with another entity
*
* @param other The other entity
*/
public void collidedWith(Entity other) {
}
}
Total 108 Lines of Code.
|
Source code formatted using showsrc by William Denniss
|