source: lost-perception/main/Creature.java

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

Create a working makefile target for MapEditor

  • Property mode set to 100644
File size: 11.9 KB
Line 
1package main;
2
3import java.awt.FontMetrics;
4import java.awt.Font;
5import java.awt.Graphics;
6import java.util.Iterator;
7import astar.AStarSearch;
8import collision.Bound;
9import java.awt.Color;
10import utils.DynamicImage;
11import java.awt.Point;
12import java.util.LinkedList;
13import java.awt.geom.Point2D;
14
15public class Creature extends MapObject {
16 String name;
17 private Model model;
18 private Point2D.Double exactLoc;
19 LinkedList<Point> path;
20 Creature enemyTarget;
21 int speed;
22 long lastMoved;
23 long lastAttacked;
24 boolean attacked;
25 int xOffset;
26 int yOffset;
27 public int strikeFrame;
28 boolean dead;
29 private int damage;
30 int minRange;
31 int startRange;
32 int maxRange;
33 int hitpoints;
34 int manapoints;
35 int maxHitpoints;
36 int maxManapoints;
37 int[] stats;
38 public int id;
39 private static int lastId;
40 public static DynamicImage hpBar;
41
42 static {
43 Creature.lastId = 0;
44 }
45
46 public Creature(final String name, final int xOffset, final int yOffset) {
47 super(0, 0, 0);
48 this.name = name;
49 this.model = null;
50 this.loc = new Point(0, 0);
51 (this.exactLoc = new Point2D.Double()).setLocation(this.loc);
52 this.path = null;
53 this.speed = 50;
54 this.attacked = false;
55 this.xOffset = xOffset;
56 this.yOffset = yOffset;
57 this.strikeFrame = -1;
58 this.damage = 0;
59 this.minRange = 0;
60 this.startRange = 0;
61 this.maxRange = 0;
62 final boolean b = false;
63 this.hitpoints = (b ? 1 : 0);
64 this.maxHitpoints = (b ? 1 : 0);
65 final boolean b2 = false;
66 this.manapoints = (b2 ? 1 : 0);
67 this.maxManapoints = (b2 ? 1 : 0);
68 (this.stats = new int[8])[StatType.Strength.ordinal()] = 0;
69 this.stats[StatType.Dexterity.ordinal()] = 0;
70 this.stats[StatType.Constitution.ordinal()] = 0;
71 this.stats[StatType.Intelligence.ordinal()] = 0;
72 this.stats[StatType.Fire.ordinal()] = 0;
73 this.stats[StatType.Ice.ordinal()] = 0;
74 this.stats[StatType.Wind.ordinal()] = 0;
75 this.stats[StatType.Fire.ordinal()] = 0;
76 this.dead = false;
77 this.id = Creature.lastId;
78 Creature.lastId++;
79 }
80
81 protected Creature(final Creature cr) {
82 super(cr, 0, 0, 0);
83 this.name = cr.name;
84 this.model = new Model(cr.model);
85 this.path = null;
86 this.speed = cr.speed;
87 this.attacked = false;
88 this.xOffset = cr.xOffset;
89 this.yOffset = cr.yOffset;
90 this.strikeFrame = cr.strikeFrame;
91 this.damage = cr.damage;
92 this.minRange = cr.minRange;
93 this.startRange = cr.startRange;
94 this.maxRange = cr.maxRange;
95 final int maxHitpoints = cr.maxHitpoints;
96 this.hitpoints = maxHitpoints;
97 this.maxHitpoints = maxHitpoints;
98 final int maxHitpoints2 = cr.maxHitpoints;
99 this.manapoints = maxHitpoints2;
100 this.maxManapoints = maxHitpoints2;
101 this.stats = new int[cr.stats.length];
102 for (int x = 0; x < cr.stats.length; ++x) {
103 this.stats[x] = cr.stats[x];
104 }
105 this.dead = false;
106 this.id = cr.id;
107 }
108
109 public Creature copy(final Point newLoc) {
110 final Creature newCr = new Creature(this);
111 newCr.initLoc(newLoc);
112 return newCr;
113 }
114
115 protected void initLoc(final Point newLoc) {
116 this.loc = newLoc;
117 (this.exactLoc = new Point2D.Double()).setLocation(this.loc);
118 }
119
120 public Model getModel() {
121 return this.model;
122 }
123
124 public void loadModel(final String folder, final Color bgColor, final Color shadowColor, final int[] numFramesArr) {
125 this.model = new Model(folder, bgColor, shadowColor, numFramesArr);
126 }
127
128 public void setModel(final Model model) {
129 this.model = model;
130 }
131
132 public void setStats(final int strength, final int dexterity, final int constitution, final int intelligence) {
133 this.stats[StatType.Strength.ordinal()] = strength;
134 this.stats[StatType.Dexterity.ordinal()] = dexterity;
135 this.stats[StatType.Constitution.ordinal()] = constitution;
136 this.stats[StatType.Intelligence.ordinal()] = intelligence;
137 this.propagateStatChanges();
138 this.hitpoints = this.maxHitpoints;
139 this.manapoints = this.maxManapoints;
140 }
141
142 public void setAttack(final int damage, final int minRange, final int startRange, final int maxRange) {
143 this.damage = damage;
144 this.minRange = minRange;
145 this.startRange = startRange;
146 this.maxRange = maxRange;
147 }
148
149 public void propagateStatChanges() {
150 this.maxHitpoints = this.stats[StatType.Constitution.ordinal()] * 5;
151 this.maxManapoints = this.stats[StatType.Intelligence.ordinal()] * 5;
152 }
153
154 public void setWalkSpeed(final int speed) {
155 this.speed = speed;
156 }
157
158 @Override
159 public double getSortZ() {
160 if (this.dead) {
161 return super.getSortZ() + 0.5;
162 }
163 return super.getSortZ() + 1.0;
164 }
165
166 public int getDamage() {
167 return this.damage;
168 }
169
170 public void takeDamage(final int damage) {
171 this.hitpoints -= damage;
172 }
173
174 public boolean readyToAttack() {
175 final boolean ans = this.model.action == Action.Attacking && this.model.getCurrentAnimation().getCurrentFrame() == this.strikeFrame;
176 if (this.attacked) {
177 this.attacked = ans;
178 return false;
179 }
180 return this.attacked = ans;
181 }
182
183 public void AI_move(final Player player, final Map map) {
184 }
185
186 public void AI_react(final Map map) {
187 }
188
189 public void startAttack(final Creature enemy) {
190 final Direction dir = calcDirection(this.exactLoc, enemy.exactLoc);
191 if (dir != null) {
192 this.model.direction = dir;
193 }
194 this.model.action = Action.Attacking;
195 }
196
197 public void attack(final Creature enemy) {
198 this.enemyTarget.takeDamage(this.getDamage());
199 if (this.enemyTarget.hitpoints <= 0) {
200 this.enemyTarget.die();
201 this.enemyTarget = null;
202 this.path = null;
203 }
204 }
205
206 public void setLoc(final Point loc) {
207 this.loc.setLocation(loc);
208 this.exactLoc.setLocation(loc);
209 }
210
211 public void die() {
212 this.hitpoints = 0;
213 this.path = null;
214 this.model.action = Action.Dying;
215 this.setBound(null);
216 this.dead = true;
217 }
218
219 public void setEnemyTarget(final Creature cr, final Map map) {
220 this.enemyTarget = cr;
221 if (this.enemyTarget != null) {
222 this.setTarget(new Point(cr.loc), map);
223 }
224 }
225
226 public void setTarget(final Point p, final Map map) {
227 this.path = AStarSearch.findPath(this.loc, p, map.asMap);
228 if (this.path != null) {
229 this.path.removeFirst();
230 }
231 }
232
233 public void move(final LinkedList<MapObject> nearbyObjects, final Map map) {
234 if (this.lastMoved == 0L) {
235 this.lastMoved = System.nanoTime();
236 return;
237 }
238 if (this.dead) {
239 return;
240 }
241 final double dist = this.speed * (System.nanoTime() - this.lastMoved) / 1.0E9;
242 final Point2D.Double newLoc = new Point2D.Double();
243 final Point oldLoc = this.loc;
244 double xDif = 0.0;
245 double yDif = 0.0;
246 this.lastMoved = System.nanoTime();
247 this.AI_react(map);
248 if ((this.model.action == Action.Attacking || this.model.action == Action.BeenHit) && this.model.getCurrentAnimation().reachedEnd()) {
249 this.model.getCurrentAnimation().reset();
250 this.model.action = Action.Standing;
251 this.enemyTarget = null;
252 this.path = null;
253 }
254 if (this.model.action == Action.Attacking || this.model.action == Action.BeenHit) {
255 return;
256 }
257 Point target = null;
258 if (this.path == null) {
259 if (this.model.action == Action.Walking || this.model.action == Action.Running) {
260 this.model.getCurrentAnimation().reset();
261 this.model.action = Action.Standing;
262 }
263 return;
264 }
265 target = this.path.getFirst();
266 if (this.exactLoc.distance(target) <= dist) {
267 newLoc.setLocation(target);
268 this.path.removeFirst();
269 if (this.path.size() == 0) {
270 this.path = null;
271 }
272 }
273 else {
274 xDif = (target.x - this.exactLoc.x) * dist / this.exactLoc.distance(target);
275 yDif = (target.y - this.exactLoc.y) * dist / this.exactLoc.distance(target);
276 newLoc.setLocation(this.exactLoc.x + xDif, this.exactLoc.y + yDif);
277 if ((xDif != 0.0 || yDif != 0.0) && this.model.action != Action.Walking) {
278 this.model.getCurrentAnimation().reset();
279 this.model.action = Action.Walking;
280 }
281 }
282 final Direction dir = calcDirection(this.exactLoc, newLoc);
283 if (dir != null && target != null && this.model.direction != dir) {
284 this.model.getCurrentAnimation().reset();
285 this.model.direction = dir;
286 }
287 (this.loc = new Point(0, 0)).setLocation(newLoc);
288 for (final MapObject obj : nearbyObjects) {
289 if (this != obj && obj.intersects(this) && Creature.class.isAssignableFrom(obj.getClass())) {
290 if (!Creature.class.isAssignableFrom(obj.getClass()) || ((Creature)obj).path == null) {
291 this.model.getCurrentAnimation().reset();
292 this.model.action = Action.Standing;
293 }
294 this.AI_react(map);
295 this.loc = oldLoc;
296 this.path = null;
297 return;
298 }
299 }
300 (this.loc = oldLoc).setLocation(newLoc);
301 this.exactLoc.setLocation(newLoc);
302 }
303
304 private static Direction calcDirection(final Point2D.Double p1, final Point2D.Double p2) {
305 final double xDif = p2.x - p1.x;
306 final double yDif = p1.y - p2.y;
307 double angle = Math.atan(yDif / xDif) * 180.0 / 3.141592653589793;
308 if (Double.isNaN(angle)) {
309 return null;
310 }
311 if (xDif < 0.0) {
312 angle += 180.0;
313 }
314 else if (yDif < 0.0) {
315 angle += 360.0;
316 }
317 if (337.5 < angle || angle <= 22.5) {
318 return Direction.East;
319 }
320 if (22.5 < angle && angle <= 67.5) {
321 return Direction.NorthEast;
322 }
323 if (67.5 < angle && angle <= 112.5) {
324 return Direction.North;
325 }
326 if (112.5 < angle && angle <= 157.5) {
327 return Direction.NorthWest;
328 }
329 if (157.5 < angle && angle <= 202.25) {
330 return Direction.West;
331 }
332 if (202.5 < angle && angle <= 247.5) {
333 return Direction.SouthWest;
334 }
335 if (247.5 < angle && angle <= 292.5) {
336 return Direction.South;
337 }
338 if (292.5 < angle && angle <= 337.5) {
339 return Direction.SouthEast;
340 }
341 return null;
342 }
343
344 @Override
345 public void draw(final Graphics g, final int playerX, final int playerY) {
346 this.model.draw(g, this.loc.x + playerX + this.xOffset, this.loc.y + playerY + this.yOffset);
347 final int x = -40 + this.loc.x + playerX + this.xOffset;
348 final int y = -10 - this.model.getHeight() + this.loc.y + playerY + this.yOffset;
349 Creature.hpBar.draw(g, x, y, x + 80 * this.hitpoints / this.maxHitpoints, y + 10, 0, 0, 80 * this.hitpoints / this.maxHitpoints, 10);
350 if (!this.dead) {
351 g.setColor(Color.black);
352 final Font font12 = new Font("Arial", 0, 12);
353 final FontMetrics metrics = g.getFontMetrics(font12);
354 g.setFont(font12);
355 g.drawString(this.name, this.loc.x + playerX + this.xOffset - metrics.stringWidth(this.name) / 2, -this.model.getHeight() + this.loc.y + playerY + this.yOffset);
356 }
357 }
358}
Note: See TracBrowser for help on using the repository browser.