import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
/**
*
* @author Justin Mobijohn and Zander
*/
public class SpaceShip extends GameObject {
public SpaceShip() {
this.m_Image = GameObject.m_ImageManager.LoadImageFromDisk("GameImages/SpaceShip.jpg");
this.m_Currentposition = new Rectangle(400 - 16, 600 - 64, 32, 32);
this.m_MovementVector = new Point(0, 0);
this.SetGameObjectType(GameObject.GameObjectType.SPACESHIP);
this.m_Life = 3;
}
/**
* If a key is released, stop the movement of the spaceship, and if the
* spacebar is released, fire a missile.
*
*/
public void HandleKeyRelease(KeyEvent e) {
// On a key release we stop the spaceship
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
this.m_MovementVector.x = 0;
break;
case KeyEvent.VK_SPACE:
Missile m = new Missile(new Point(this.m_Currentposition.x + this.m_Currentposition.width / 2, this.m_Currentposition.y), -3, false);
GameObject.m_AllMissiles.add(m);
break;
}
}
/**
* If the left or right arrow keys have been pressed, move the spaceship in
* the correct direction.
*
*/
public void HandleKeyPress(KeyEvent e) {
// On a key down we move the spaceship
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
this.m_MovementVector.x = -2;
break;
case KeyEvent.VK_RIGHT:
this.m_MovementVector.x = 2;
break;
}
}
/**
* The update function makes sure the spaceship does not leave the screen
* when the arrow keys are pressed down.
*
*/
@Override
public void Update(float elapsedTime) {
// Do nothing. Don't call the super class's update, because if
// we do, then when we hit the far left or right of the screen,
// our y position will decrease!
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;
}
if (this.m_Currentposition.x < 0) {
this.m_Currentposition.x = 0;
}
}
}
|