source: lost-perception/main/Creature.java@ ebd3538

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

Initial commit. This codebase for the Lost Perception game was created by decompiling code from a jar file.

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