import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ship here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ship extends Actor
{
// True or false statement for touching other actors
boolean touchingAlien = false;
boolean touchingUFO = false;
boolean touchingUFOLaser = false;
private ScoreBoard score;
public Ship(ScoreBoard score)
{
this.score = score;
}
/**
* Act - do whatever the Ship wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
movement();
Boundry();
hitAlien();
hitUFO();
}
public void movement() {
// User input for navigating ship
if (Greenfoot.isKeyDown("up")) {
setLocation(getX(), getY() - 5);
}
if (Greenfoot.isKeyDown("down")) {
setLocation(getX(), getY() + 5);
}
if (Greenfoot.isKeyDown("right")) {
setLocation(getX() + 5, getY());
}
if (Greenfoot.isKeyDown("left")) {
setLocation(getX() - 5, getY());
}
// User input for shooting lasers (works when key is lifted)
if ("space".equals(Greenfoot.getKey())) {
shoot();
}
}
public void shoot() {
// Add laser object to world
getWorld().addObject(new Laser(score), getX(), getY());
}
public void hitAlien() {
// Define the actor as the ship touching an alien
Actor alien = getOneIntersectingObject(Alien.class);
if (alien != null ) {
// Call MyWorld and HealthBar into ship class
World myWorld = getWorld();
MyWorld myworld = (MyWorld)myWorld;
HealthBar healthbar = myworld.getHealthBar();
if (touchingAlien == false) {
healthbar.loseHealth();
touchingAlien = true;
if (healthbar.health <= 0) {
// Game over since health has hit 0
myWorld.removeObject(this);
Greenfoot.setWorld(new GameOver());
}
}
else {
// Continue game
touchingAlien = false;
}
}
}
public void hitUFO() {
// Define the actor as the ship touching an alien
Actor UFO = getOneIntersectingObject(UFO.class);
Actor UFOLaser = getOneIntersectingObject(UFOLaser.class);
if (UFO !=null || UFOLaser !=null ) {
// Call MyWorld and HealthBar into ship class
World finalWorld = getWorld();
FinalWorld finalworld = (FinalWorld)finalWorld;
HealthBar healthbar = finalworld.getHealthBar();
if (touchingUFO == false || touchingUFOLaser == false) {
healthbar.loseHealth();
touchingUFO = true;
touchingUFOLaser = true;
if (healthbar.health <= 0) {
// Game over since health has hit 0
finalWorld.removeObject(this);
Greenfoot.setWorld(new GameOver());
}
}
else {
// Continue game
touchingUFO = false;
touchingUFOLaser = false;
}
}
}
public void Boundry() {
if(getX() > 980) {
setLocation(980, getY());
}
if(getX() < 0) {
setLocation(0, getY());
}
if(getY() > 575) {
setLocation(getX(), 575);
}
if(getY() < 0) {
setLocation(getX(), 0);
}
}
}
