import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.util.ArrayList;
/**
*
* @author Justin Mobijohn and Zander
*/
abstract public class GameObject {
/**
*
* A variable of type ImageManager
*/
protected static ImageManager m_ImageManager = new ImageManager();
protected BufferedImage m_Image;
protected Rectangle m_Currentposition;
protected Point m_MovementVector;
protected GameObjectType m_GameObjectType;
// The GameObject knows about all missiles:
public static ArrayList<Missile> m_AllMissiles = new ArrayList<Missile>();
protected int m_Life;
protected boolean m_IsAlive;
public enum GameObjectType {
MISSLE, SPACESHIP, ENEMY
}
public GameObject() {
this.m_Life = 1;
this.m_IsAlive = true;
this.m_Currentposition = new Rectangle();
this.m_Image = null;
this.m_MovementVector = new Point();
}
/**
*
* Returns an enum describing what type of object you are.
*/
public GameObjectType GetGameObjectType() {
return this.m_GameObjectType;
}
/**
*
* SetGameObject sets the type of GameObject. It's called from the
* constructor. The SpaceInvaders class calls this function to determine
* what time of object is in its HashMap. This is probably better than
* calling getClass followed by getName, and then doing a string comparison.
*
* @param gameObjectType Enum of what type of object you are.
*/
public void SetGameObjectType(GameObjectType gameObjectType) {
this.m_GameObjectType = gameObjectType;
}
/**
* Draw this game object on the screen.
*
* @param g Graphics object to draw.
* @param whereToDraw We are drying to the main window.
*/
public void Draw(Graphics g, SpaceInvaders whereToDraw) {
g.drawImage(this.m_Image, this.m_Currentposition.x, this.m_Currentposition.y, whereToDraw);
}
/**
* Draw this game object on the screen.
*
* @param elapsedTime This is the elapsed time since the last refresh.
*/
public void Update(float elapsedTime) {
// Update this Game Object (whether it's an Enemy, SpaceShip or Missile):
this.m_Currentposition.x += this.m_MovementVector.x;
this.m_Currentposition.y += this.m_MovementVector.y;
if (this.m_Currentposition.x > 800 - this.m_Currentposition.width) {
this.m_Currentposition.x = 800 - this.m_Currentposition.width;
this.m_MovementVector.x = this.m_MovementVector.x * -1;
this.m_Currentposition.y += 10;
}
if (this.m_Currentposition.x < 0) {
this.m_Currentposition.x = 0;
this.m_MovementVector.x = this.m_MovementVector.x * -1;
this.m_Currentposition.y += 10;
}
}
/**
* ModifyLife can increase or decrease (most likely) the life variable
* of an Gameobject. If the life is less than or equal to zero, this
* object has been killed by a missile.
*
* @param amount Positive or Negative amount to modify life.
*/
public void ModifyLife(int amount) {
this.m_Life += amount;
if (this.m_Life <= 0) {
this.m_IsAlive = false;
}
}
/**
* Returns a flag if the object is alive.
*/
public boolean IsAlive() {
return this.m_IsAlive;
}
/**
* Returns a the amount of life in object.
*/
public int getLife() {
return this.m_Life;
}
}
|