package main; import java.util.LinkedList; import gamegui.Animation; public class Ship extends Unit { int damage; Unit target; public Location targetLoc; Animation projectileImg; int speed; boolean startFiring; public Ship(final String name, final Animation img, final int damage, final int hitpoints, final int fireRate, final Animation projectileImg) { super(name, img, hitpoints); this.damage = damage; this.target = null; this.targetLoc = null; this.projectileImg = projectileImg; this.speed = 100; this.targetLoc = new Location(400.0, 400.0); this.startFiring = false; this.actions.add(new Action.Fire(fireRate)); this.actions.add(new Action.Move(20L)); } public Ship(final Ship copy) { super(copy); this.damage = copy.damage; if (copy.target == null) { this.target = null; } else { this.target = new Unit(copy.target); } this.targetLoc = new Location(copy.targetLoc); this.projectileImg = new Animation(copy.projectileImg); this.speed = copy.speed; this.startFiring = false; } public int getDamage() { return this.damage; } @Override public void perform(final Action action, final LinkedList lstObjects, final LinkedList lstTargets) { if (action instanceof Action.Fire) { this.perform((Action.Fire)action, lstObjects, lstTargets); } else if (action instanceof Action.Move) { this.perform((Action.Move)action, lstObjects, lstTargets); } } public void perform(final Action.Move action, final LinkedList lstObjects, final LinkedList lstTargets) { if (this.loc.moveToTarget(this.targetLoc.getX(), this.targetLoc.getY(), (System.currentTimeMillis() - action.timeLastPerformed) * this.speed / 1000L)) { this.startFiring = true; } } public void perform(final Action.Fire action, final LinkedList lstObjects, final LinkedList lstTargets) { if (lstTargets.size() == 0) { this.target = null; return; } if (this.target == null || this.target.remove) { this.target = lstTargets.get((int)(Math.random() * lstTargets.size())); } if (this.startFiring) { lstObjects.add(this.fire(this.target)); } } public Projectile fire(final Unit target) { final Projectile proj = new Projectile("Projectile", this.projectileImg, 400, target.loc, target, this.damage); proj.setLocation(this.loc); return proj; } }