1 | package main;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import gamegui.Animation;
|
---|
7 |
|
---|
8 | public class Projectile extends Entity {
|
---|
9 | public static long explodedDuration = 300L;
|
---|
10 |
|
---|
11 | public boolean exploded;
|
---|
12 | public long timeExploded;
|
---|
13 |
|
---|
14 | private int damage;
|
---|
15 | private Location targetLoc;
|
---|
16 | private Unit target;
|
---|
17 | private int speed;
|
---|
18 |
|
---|
19 | public Projectile(final String name, final Animation img, final int speed, final Location targetLoc, final Unit target, final int damage) {
|
---|
20 | super(name, img);
|
---|
21 | this.speed = speed;
|
---|
22 | this.targetLoc = targetLoc;
|
---|
23 | this.target = target;
|
---|
24 | this.damage = damage;
|
---|
25 | this.exploded = false;
|
---|
26 | this.actions.add(new Action.Move(20L));
|
---|
27 | }
|
---|
28 |
|
---|
29 | public int getDamage() {
|
---|
30 | return this.damage;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public Unit getTarget() {
|
---|
34 | return this.target;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void perform(final Action action, final LinkedList<Entity> lstObjects, final LinkedList<Unit> lstTargets) {
|
---|
39 | if (action instanceof Action.Move) {
|
---|
40 | this.perform((Action.Move)action, lstObjects, lstTargets);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void perform(final Action.Move action, final LinkedList<Entity> lstObjects, final LinkedList<Unit> lstTargets) {
|
---|
45 | if (this.exploded) {
|
---|
46 | return;
|
---|
47 | }
|
---|
48 | if (this.loc.moveToTarget(this.targetLoc.getX(), this.targetLoc.getY(), (System.currentTimeMillis() - action.timeLastPerformed) * this.speed / 1000L)) {
|
---|
49 | this.remove = true;
|
---|
50 | this.target.takeDamage(this.damage);
|
---|
51 | if (this.target.getHitpoints() == 0) {
|
---|
52 | this.target.remove = true;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public void draw(final Graphics g) {
|
---|
59 | if (this.exploded) {
|
---|
60 | if (System.currentTimeMillis() - this.timeExploded >= Projectile.explodedDuration) {
|
---|
61 | this.remove = true;
|
---|
62 | }
|
---|
63 | else {
|
---|
64 | g.setColor(new Color(255, 0, 0, 100));
|
---|
65 | g.fillOval(this.loc.getX() - 15, this.loc.getY() - 15, 30, 30);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | else {
|
---|
69 | this.img.draw(g, this.loc.getX(), this.loc.getY() + this.img.getHeight() / 2);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|