import java.awt.Point;
import java.awt.Rectangle;
import java.util.HashMap;
/**
*
* @author Justin Mobijohn and Zander
*/
public class Missile extends GameObject {
public boolean m_IsFromMonster;
/**
* When a missile is created, we have to tell it what time of object it
* came from, which direction to travel in and it's start position.
*
* @param startPosition The missile's start position, usually the
* current location of the object who is firing it.
* @param yVal The direction which the missile should travel to.
* @param isFromMonster Whether or not this missile came from a monster.
*/
public Missile(Point startPosition, int yVal, boolean isFromMonster) {
this.m_Image = GameObject.m_ImageManager.LoadImageFromDisk("GameImages/Missile.jpg");
this.m_Currentposition = new Rectangle(startPosition.x, startPosition.y, 8, 2);
this.m_MovementVector.x = 0;
this.m_MovementVector.y = yVal;
this.m_IsFromMonster = isFromMonster;
this.SetGameObjectType(GameObject.GameObjectType.MISSLE);
}
/**
* This function figures out if a missile has collided with another GameObject.
* If the missile originated form a monster, the missile only cares about if
* it collided with the spaceship. Likewise for the spaceship's missile.
*
* @param allGameObjects A reference to all the game objectso on the screen.
* The missile needs to know this information because it has to detect if it
* collided with something.
*
* @return true If it has collided with something. Note that if the missile
* goes off the screen, it has "collided" with the boundary of the window.
* @return false It has not collided with anything.
*/
public boolean DetectCollision(HashMap<String, GameObject> allGameObjects) {
// First make sure the missile didn't leave the area:
if (this.m_Currentposition.y > SpaceInvaders.HEIGHT || this.m_Currentposition.y < 0) {
return true;
}
for (String gameObject : allGameObjects.keySet()) {
if (allGameObjects.get(gameObject).IsAlive()) {
if (this.m_Currentposition.intersects(allGameObjects.get(gameObject).m_Currentposition)) {
if (this.m_IsFromMonster && allGameObjects.get(gameObject).GetGameObjectType() == GameObject.GameObjectType.SPACESHIP) {
// It's a missile from a monster, and you've been hit!
allGameObjects.get(gameObject).ModifyLife(-1);
return true;
}
if (!this.m_IsFromMonster && allGameObjects.get(gameObject).GetGameObjectType() == GameObject.GameObjectType.ENEMY) {
// It's a missile from a you, and you've hit a monster!
allGameObjects.get(gameObject).ModifyLife(-1);
return true;
}
}
}
}
// Missile didn't collide with anything:
return false;
}
}
|