source: last-defense/main/Ship.java

Last change on this file was fb4fc67, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit, code decompiled from a jar

  • Property mode set to 100644
File size: 2.7 KB
Line 
1package main;
2
3import java.util.LinkedList;
4import gamegui.Animation;
5
6public class Ship extends Unit {
7 int damage;
8 Unit target;
9 public Location targetLoc;
10 Animation projectileImg;
11 int speed;
12 boolean startFiring;
13
14 public Ship(final String name, final Animation img, final int damage, final int hitpoints, final int fireRate, final Animation projectileImg) {
15 super(name, img, hitpoints);
16 this.damage = damage;
17 this.target = null;
18 this.targetLoc = null;
19 this.projectileImg = projectileImg;
20 this.speed = 100;
21 this.targetLoc = new Location(400.0, 400.0);
22 this.startFiring = false;
23 this.actions.add(new Action.Fire(fireRate));
24 this.actions.add(new Action.Move(20L));
25 }
26
27 public Ship(final Ship copy) {
28 super(copy);
29 this.damage = copy.damage;
30 if (copy.target == null) {
31 this.target = null;
32 }
33 else {
34 this.target = new Unit(copy.target);
35 }
36 this.targetLoc = new Location(copy.targetLoc);
37 this.projectileImg = new Animation(copy.projectileImg);
38 this.speed = copy.speed;
39 this.startFiring = false;
40 }
41
42 public int getDamage() {
43 return this.damage;
44 }
45
46 @Override
47 public void perform(final Action action, final LinkedList<Entity> lstObjects, final LinkedList<Unit> lstTargets) {
48 if (action instanceof Action.Fire) {
49 this.perform((Action.Fire)action, lstObjects, lstTargets);
50 }
51 else if (action instanceof Action.Move) {
52 this.perform((Action.Move)action, lstObjects, lstTargets);
53 }
54 }
55
56 public void perform(final Action.Move action, final LinkedList<Entity> lstObjects, final LinkedList<Unit> lstTargets) {
57 if (this.loc.moveToTarget(this.targetLoc.getX(), this.targetLoc.getY(), (System.currentTimeMillis() - action.timeLastPerformed) * this.speed / 1000L)) {
58 this.startFiring = true;
59 }
60 }
61
62 public void perform(final Action.Fire action, final LinkedList<Entity> lstObjects, final LinkedList<Unit> lstTargets) {
63 if (lstTargets.size() == 0) {
64 this.target = null;
65 return;
66 }
67 if (this.target == null || this.target.remove) {
68 this.target = lstTargets.get((int)(Math.random() * lstTargets.size()));
69 }
70 if (this.startFiring) {
71 lstObjects.add(this.fire(this.target));
72 }
73 }
74
75 public Projectile fire(final Unit target) {
76 final Projectile proj = new Projectile("Projectile", this.projectileImg, 400, target.loc, target, this.damage);
77 proj.setLocation(this.loc);
78 return proj;
79 }
80}
Note: See TracBrowser for help on using the repository browser.