package org.newdawn.spaceinvaders;
/**
* An entity representing a shot fired by the player's ship
*
* @author Kevin Glass
*/
public class ShotEntity extends Entity {
/** The vertical speed at which the players shot moves */
private double moveSpeed = -300;
/** The game in which this entity exists */
private Game game;
/** True if this shot has been "used", i.e. its hit something */
private boolean used = false;
/**
* Create a new shot from the player
*
* @param game The game in which the shot has been created
* @param sprite The sprite representing this shot
* @param x The initial x location of the shot
* @param y The initial y location of the shot
*/
public ShotEntity(Game game,String sprite,int x,int y) {
super(sprite,x,y);
this.game = game;
dy = moveSpeed;
}
/**
* Request that this shot moved based on time elapsed
*
* @param delta The time that has elapsed since last move
*/
public void move(long delta) {
super.move(delta);
if (y < -100) {
game.removeEntity(this);
}
}
/**
* Notification that this shot has collided with another
* entity
*
* @parma other The other entity with which we've collided
*/
public void collidedWith(Entity other) {
if (used) {
return;
}
if (other instanceof AlienEntity) {
game.removeEntity(this);
game.removeEntity(other);
game.notifyAlienKilled();
used = true;
}
}
}
Total 72 Lines of Code.
|
Source code formatted using showsrc by William Denniss
|