1 | package main;
|
---|
2 |
|
---|
3 | import java.awt.Graphics;
|
---|
4 | import java.awt.image.BufferedImage;
|
---|
5 | import java.util.*;
|
---|
6 |
|
---|
7 | public class Projectile {
|
---|
8 |
|
---|
9 | private LinkedList<Effect> spawnEffects;
|
---|
10 | private LinkedList<Effect> contactEffects;
|
---|
11 | private BufferedImage img;
|
---|
12 | private int speed;
|
---|
13 | private Point loc;
|
---|
14 | private Point target;
|
---|
15 | private double lastMoved;
|
---|
16 | private boolean done;
|
---|
17 | private boolean penetrate;
|
---|
18 | private Creature creator;
|
---|
19 | private LinkedList<Creature> effectedCreatures;
|
---|
20 |
|
---|
21 | public Projectile(BufferedImage img, Point loc) {
|
---|
22 | this.spawnEffects = new LinkedList<Effect>();
|
---|
23 | this.contactEffects = new LinkedList<Effect>();
|
---|
24 | this.speed = 300;
|
---|
25 | this.img = img;
|
---|
26 | this.loc = loc;
|
---|
27 | this.target = loc;
|
---|
28 | this.lastMoved = 0.0D;
|
---|
29 | this.done = false;
|
---|
30 | this.penetrate = false;
|
---|
31 | this.creator = null;
|
---|
32 | this.effectedCreatures = new LinkedList<Creature>();
|
---|
33 | }
|
---|
34 |
|
---|
35 | public Projectile(BufferedImage img, Point loc, Creature creator) {
|
---|
36 | this.spawnEffects = new LinkedList<Effect>();
|
---|
37 | this.contactEffects = new LinkedList<Effect>();
|
---|
38 | this.speed = 100;
|
---|
39 | this.img = img;
|
---|
40 | this.loc = loc;
|
---|
41 | this.target = loc;
|
---|
42 | this.lastMoved = 0.0D;
|
---|
43 | this.done = false;
|
---|
44 | this.penetrate = false;
|
---|
45 | this.creator = creator;
|
---|
46 | this.effectedCreatures = new LinkedList<Creature>();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void effectCreature(Creature c) {
|
---|
50 | this.effectedCreatures.add(c);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public boolean creatureIsEffected(Creature c) {
|
---|
54 | return this.effectedCreatures.contains(c);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void draw(Graphics g, int playerX, int playerY) {
|
---|
58 | g.drawImage(this.img, 375 + this.loc.getX() - playerX, 275 + this.loc.getY() - playerY, null);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public Point move() {
|
---|
62 | Point newLoc;
|
---|
63 | double dist = this.speed * (System.currentTimeMillis() - this.lastMoved) / 1000.0D;
|
---|
64 | if (this.lastMoved == 0.0D) {
|
---|
65 | dist = 0.0D;
|
---|
66 | }
|
---|
67 | this.lastMoved = System.currentTimeMillis();
|
---|
68 | if (Point.dist(this.loc, this.target) <= dist) {
|
---|
69 | newLoc = this.target;
|
---|
70 | } else {
|
---|
71 | int xDif = (int)(Point.xDif(this.loc, this.target) * dist / Point.dist(this.loc, this.target));
|
---|
72 | int yDif = (int)(Point.yDif(this.loc, this.target) * dist / Point.dist(this.loc, this.target));
|
---|
73 | newLoc = new Point(this.loc.getX(), this.loc.getXMin() + xDif, this.loc.getY(), this.loc.getYMin() + yDif);
|
---|
74 | newLoc.setX(newLoc.getX() + newLoc.getXMin() / 100);
|
---|
75 | newLoc.setXMin(newLoc.getXMin() % 100);
|
---|
76 | newLoc.setY(newLoc.getY() + newLoc.getYMin() / 100);
|
---|
77 | newLoc.setYMin(newLoc.getYMin() % 100);
|
---|
78 | if (newLoc.getXMin() < 0) {
|
---|
79 | newLoc.setX(newLoc.getX() - 1);
|
---|
80 | newLoc.setXMin(newLoc.getXMin() + 100);
|
---|
81 | } else if (newLoc.getYMin() < 0) {
|
---|
82 | newLoc.setY(newLoc.getY() - 1);
|
---|
83 | newLoc.setYMin(newLoc.getYMin() + 100);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return newLoc;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void addSpawnEffect(Effect effect) {
|
---|
90 | this.spawnEffects.add(effect);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public void addSpawnEffects(LinkedList<Effect> effects) {
|
---|
94 | this.spawnEffects.addAll(effects);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void applySpawnEffects() {
|
---|
98 | Iterator<Effect> iter = this.spawnEffects.iterator();
|
---|
99 | while (iter.hasNext()) {
|
---|
100 | Effect cur = iter.next();
|
---|
101 | switch (cur.getTarget()) {
|
---|
102 | case Projectile:
|
---|
103 | cur.applyEffect(this);
|
---|
104 | case Creator:
|
---|
105 | cur.applyEffect(this.creator);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public void addContactEffect(Effect effect) {
|
---|
111 | this.contactEffects.add(effect);
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void addContactEffects(LinkedList<Effect> effects) {
|
---|
115 | this.contactEffects.addAll(effects);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void applyContactEffects(Creature cr) {
|
---|
119 | Iterator<Effect> iter = this.contactEffects.iterator();
|
---|
120 | while (iter.hasNext()) {
|
---|
121 | Effect cur = iter.next();
|
---|
122 | switch (cur.getTarget()) {
|
---|
123 | case Projectile:
|
---|
124 | cur.applyEffect(this);
|
---|
125 | case Creator:
|
---|
126 | cur.applyEffect(this.creator);
|
---|
127 | case Enemy:
|
---|
128 | cur.applyEffect(cr);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | public int getSpeed() {
|
---|
134 | return this.speed;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public Point getLoc() {
|
---|
138 | return this.loc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public Point getTarget() {
|
---|
142 | return this.target;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public boolean isDone() {
|
---|
146 | return this.done;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public boolean isPlayerOwned() {
|
---|
150 | return (this.creator.getType() == CreatureType.Player);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public boolean penetrates() {
|
---|
154 | return this.penetrate;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public Point setTarget() {
|
---|
158 | return this.target;
|
---|
159 | }
|
---|
160 |
|
---|
161 | public void setSpeed(int speed) {
|
---|
162 | this.speed = speed;
|
---|
163 | }
|
---|
164 |
|
---|
165 | public void setLoc(Point loc) {
|
---|
166 | this.loc = loc;
|
---|
167 | }
|
---|
168 |
|
---|
169 | public void setTarget(Point target) {
|
---|
170 | this.target = target;
|
---|
171 | }
|
---|
172 |
|
---|
173 | public void setDone(boolean done) {
|
---|
174 | this.done = done;
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void setPenetrate(boolean penetrate) {
|
---|
178 | this.penetrate = penetrate;
|
---|
179 | }
|
---|
180 |
|
---|
181 | public void setCreator(Creature creator) {
|
---|
182 | this.creator = creator;
|
---|
183 | }
|
---|
184 | }
|
---|