/** * File: Spaceship.java * Author: Brian Borowski * Date created: August 1, 2011 * Date last modified: January 21, 2013 */ import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.util.Timer; import java.util.TimerTask; import javax.swing.JPanel; public class Spaceship { public static final int LEFT = 0, RIGHT = 1; private int xDropBomb; private float x; private boolean isHit, isDisposable, bombGenerated; private AlienMissile alienBomb; private final int y, direction, speed, pointsEarned, pointsLost; private final JPanel parent; private final Timer timer; private final AudioPlayer audioPlayer; private static final Image spaceshipImage, fireImage; private static final int spaceshipWidth, spaceshipHeight; static { spaceshipImage = Utility.getImage("images/spaceship.png"); fireImage = Utility.getImage("images/fire.png"); spaceshipWidth = spaceshipImage.getWidth(null); spaceshipHeight = spaceshipImage.getHeight(null); } class DisposeTask extends TimerTask { public void run() { isDisposable = true; } } public Spaceship(final JPanel parent, final int y, final int direction, final int speed, final int pointsEarned, final int pointsLost) { this.parent = parent; this.y = y; this.direction = direction; this.speed = speed; this.pointsEarned = pointsEarned; this.pointsLost = pointsLost; xDropBomb = GamePanel.RANDOM_GENERATOR.nextInt(parent.getWidth()); alienBomb = null; bombGenerated = false; audioPlayer = AudioPlayer.getInstance(); timer = new Timer(); if (direction == LEFT) { x = parent.getWidth() + spaceshipWidth; } else { x = -spaceshipWidth; } } public void setHit() { if (!isHit) { isHit = true; timer.schedule(new DisposeTask(), 1 * 1000); audioPlayer.play(AudioPlayer.SPACESHIP_IMPACT); } } public int getSpeed() { return speed; } public AlienMissile getAlienBomb() { if (alienBomb != null && !alienBomb.getHitGround()) { return alienBomb; } return null; } public void nullifyBomb() { alienBomb = null; } public int getPointsEarned() { return pointsEarned; } public int getPointsLost() { return pointsLost; } public boolean isHit() { return isHit; } public boolean isDisposable() { return isDisposable; } public boolean isVisible() { if (direction == LEFT && x > -spaceshipWidth || direction == RIGHT && x < parent.getWidth() + spaceshipWidth) { return true; } return false; } public Rectangle getBoundingRectangle() { return new Rectangle((int)x, y, spaceshipWidth, spaceshipHeight); } public void paintComponent(final Graphics g) { if (!isHit) { g.drawImage(spaceshipImage, (int)x, y, parent); } else { g.drawImage(fireImage, (int)x, y, parent); } } public void move(final float elapsedSeconds) { if (!isHit) { if (direction == LEFT) { x -= speed * elapsedSeconds; if (x <= xDropBomb && !bombGenerated) { alienBomb = new AlienMissile(300, (int)x, y + (spaceshipHeight >> 1)); bombGenerated = true; } } else { x += speed * elapsedSeconds; if (x >= xDropBomb && !bombGenerated) { alienBomb = new AlienMissile(300, (int)x, y + (spaceshipHeight >> 1)); bombGenerated = true; } } } } }