package main; import java.awt.Color; import java.awt.Graphics; import java.util.LinkedList; import gamegui.Animation; public class Projectile extends Entity { public static long explodedDuration = 300L; public boolean exploded; public long timeExploded; private int damage; private Location targetLoc; private Unit target; private int speed; public Projectile(final String name, final Animation img, final int speed, final Location targetLoc, final Unit target, final int damage) { super(name, img); this.speed = speed; this.targetLoc = targetLoc; this.target = target; this.damage = damage; this.exploded = false; this.actions.add(new Action.Move(20L)); } public int getDamage() { return this.damage; } public Unit getTarget() { return this.target; } @Override public void perform(final Action action, final LinkedList lstObjects, final LinkedList lstTargets) { 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.exploded) { return; } if (this.loc.moveToTarget(this.targetLoc.getX(), this.targetLoc.getY(), (System.currentTimeMillis() - action.timeLastPerformed) * this.speed / 1000L)) { this.remove = true; this.target.takeDamage(this.damage); if (this.target.getHitpoints() == 0) { this.target.remove = true; } } } @Override public void draw(final Graphics g) { if (this.exploded) { if (System.currentTimeMillis() - this.timeExploded >= Projectile.explodedDuration) { this.remove = true; } else { g.setColor(new Color(255, 0, 0, 100)); g.fillOval(this.loc.getX() - 15, this.loc.getY() - 15, 30, 30); } } else { this.img.draw(g, this.loc.getX(), this.loc.getY() + this.img.getHeight() / 2); } } }