source: lost-haven/main/Creature.java

Last change on this file was 3d64884, checked in by Dmitry Portnoy <dportnoy@…>, 4 years ago

Fix a bug involving player movement and another involving inventory rendering

  • Property mode set to 100644
File size: 27.8 KB
Line 
1package main;
2
3import java.awt.*;
4import java.awt.image.BufferedImage;
5import java.io.*;
6import java.util.*;
7import javax.imageio.ImageIO;
8
9import gamegui.Animation;
10
11public class Creature implements Comparable<Creature> {
12
13 public int passive;
14 public int thorns;
15 public int confused;
16 public int sand;
17
18 protected String name;
19 protected CreatureType type;
20 protected Model model;
21 protected int level;
22 protected Point loc;
23 protected int[] attributes;
24 protected int hitpoints;
25 protected int manapoints;
26 protected int maxHitpoints;
27 protected int maxManapoints;
28 protected int experience;
29 protected EquippedWeapon weapon;
30 protected EquippedWeapon spell;
31
32 private Point target;
33 private Creature enemyTarget;
34 private int speed;
35 private long lastMoved;
36 private long lastAttacked;
37 private LinkedList<ItemDrop> drops;
38 private LinkedList<EffectTimer> effectTimers;
39 private boolean disabled;
40 private SpawnPoint spawnPoint;
41 private boolean dying;
42
43 public Creature() {
44 this.name = "";
45 this.loc = new Point(0, 0);
46 this.target = this.loc;
47 this.attributes = new int[4];
48 this.experience = 0;
49 this.weapon = null;
50 this.spell = null;
51 this.drops = new LinkedList<ItemDrop>();
52 this.effectTimers = new LinkedList<EffectTimer>();
53 this.disabled = false;
54 this.spawnPoint = null;
55 this.passive = 0;
56 this.thorns = 0;
57 this.confused = 0;
58 this.sand = 0;
59 this.dying = false;
60 }
61
62 public Creature(String name, CreatureType type) {
63 this.name = name;
64 this.type = type;
65 this.loc = new Point(0, 0);
66 this.target = this.loc;
67 this.attributes = new int[4];
68 this.experience = 0;
69 this.weapon = null;
70 this.spell = null;
71 this.drops = new LinkedList<ItemDrop>();
72 this.effectTimers = new LinkedList<EffectTimer>();
73 this.disabled = false;
74 this.spawnPoint = null;
75 this.passive = 0;
76 this.thorns = 0;
77 this.confused = 0;
78 this.sand = 0;
79 this.dying = false;
80 }
81
82 public Creature(Creature cr) {
83 this.name = cr.name;
84 this.type = cr.type;
85 this.model = new Model(cr.model);
86 this.level = cr.level;
87 this.loc = new Point(cr.loc);
88 this.target = cr.target;
89 this.speed = cr.speed;
90 this.lastMoved = cr.lastMoved;
91 this.lastAttacked = cr.lastAttacked;
92 this.attributes = (int[])cr.attributes.clone();
93 this.hitpoints = cr.hitpoints;
94 this.manapoints = cr.manapoints;
95 this.maxHitpoints = cr.maxHitpoints;
96 this.maxManapoints = cr.maxManapoints;
97 this.experience = cr.experience;
98 this.weapon = cr.weapon.copy((Point)null);
99 if (cr.spell != null)
100 this.spell = cr.spell.copy((Point)null);
101 this.drops = cr.drops;
102 this.effectTimers = new LinkedList<EffectTimer>(cr.effectTimers);
103 this.disabled = cr.disabled;
104 this.spawnPoint = null;
105 this.passive = cr.passive;
106 this.thorns = cr.thorns;
107 this.confused = cr.confused;
108 this.sand = cr.sand;
109 this.dying = cr.dying;
110 }
111
112 public Creature copy() {
113 return new Creature(this);
114 }
115
116 public void setDying(boolean dying) {
117 if (!this.model.hasDeath())
118 return;
119 if (dying) {
120 this.model.setAction(Action.Dying);
121 } else {
122 this.model.getAnimation(Direction.West, Action.Dying).reset();
123 }
124 this.dying = dying;
125 }
126
127 public boolean isDying() {
128 return this.dying;
129 }
130
131 public void checkTimedEffects() {
132 Iterator<EffectTimer> iter = this.effectTimers.iterator();
133 while (iter.hasNext()) {
134 EffectTimer cur = iter.next();
135 if (cur.expired()) {
136 cur.getEffect().cancelEffect(this);
137 iter.remove();
138 }
139 }
140 }
141
142 public void addEffect(TimedEffect e) {
143 this.effectTimers.add(new EffectTimer(e, e.getDuration()));
144 }
145
146 public Point move() {
147 Point newLoc;
148 double dist = (this.speed * (System.currentTimeMillis() - this.lastMoved) / 1000L);
149 if (this.lastMoved == 0L) {
150 dist = 0.0D;
151 this.lastMoved = System.currentTimeMillis();
152 } else if (dist != 0.0D) {
153 this.lastMoved = System.currentTimeMillis();
154 }
155 if (this.enemyTarget != null) {
156 this.target = this.enemyTarget.loc;
157 }
158 if (Point.dist(this.loc, this.target) <= dist) {
159 newLoc = this.target;
160 if (this.model.getAction() != Action.Attacking) {
161 this.model.setAction(Action.Standing);
162 }
163 } else {
164 if (this.model.getAction() != Action.Attacking) {
165 this.model.setAction(Action.Walking);
166 }
167 int xDif = (int)(Point.xDif(this.loc, this.target) * dist / Point.dist(this.loc, this.target));
168 int yDif = (int)(Point.yDif(this.loc, this.target) * dist / Point.dist(this.loc, this.target));
169 newLoc = new Point(this.loc.getX(), this.loc.getXMin() + xDif, this.loc.getY(), this.loc.getYMin() + yDif);
170 newLoc.setX(newLoc.getX() + newLoc.getXMin() / 100);
171 newLoc.setXMin(newLoc.getXMin() % 100);
172 newLoc.setY(newLoc.getY() + newLoc.getYMin() / 100);
173 newLoc.setYMin(newLoc.getYMin() % 100);
174 if (newLoc.getXMin() < 0) {
175 newLoc.setX(newLoc.getX() - 1);
176 newLoc.setXMin(newLoc.getXMin() + 100);
177 } else if (newLoc.getYMin() < 0) {
178 newLoc.setY(newLoc.getY() - 1);
179 newLoc.setYMin(newLoc.getYMin() + 100);
180 }
181 this.model.setDirection(calcDirection());
182 }
183 return newLoc;
184 }
185
186 private Direction calcDirection() {
187 Direction dir;
188 int xDif2 = Point.xDif(this.loc, this.target);
189 int yDif2 = Point.yDif(this.target, this.loc);
190 double angle = 1.5707963267948966D;
191 if (xDif2 == 0) {
192 if (yDif2 != 0) {
193 angle *= Math.abs(yDif2 / yDif2);
194 }
195 } else {
196 angle = Math.atan(yDif2 / xDif2);
197 }
198 if (angle >= 0.7853981633974483D) {
199 dir = Direction.North;
200 } else if (-0.7853981633974483D <= angle && angle <= 0.7853981633974483D) {
201 dir = Direction.East;
202 } else {
203 dir = Direction.South;
204 }
205 if (xDif2 < 0)
206 switch (dir) {
207 case North:
208 dir = Direction.South;
209 break;
210 case South:
211 dir = Direction.North;
212 break;
213 case East:
214 dir = Direction.West;
215 break;
216 }
217 return dir;
218 }
219
220 public Projectile attack(Creature enemy, AttackType attType) {
221 Weapon weap = selectWeapon(attType);
222 if (System.currentTimeMillis() - this.lastAttacked >= (weap.getAttackSpeed() * 100) && !this.disabled) {
223 this.lastAttacked = System.currentTimeMillis();
224 Point oldTarget = this.target;
225 this.target = enemy.getLoc();
226 this.model.setDirection(calcDirection());
227 this.target = oldTarget;
228 if (attType != AttackType.Spell) {
229 this.model.setAction(Action.Attacking);
230 this.model.getAnimation(this.model.getDirection(), this.model.getAction()).reset();
231 }
232 if (weap.getType() == ItemType.RangedWeapon || weap.getType() == ItemType.Spell) {
233 Point loc = new Point(this.loc);
234 loc.setY(loc.getY() - 55);
235 return weap.spawnProjectile(loc, enemy.getLoc());
236 }
237 weap.applyContactEffects(enemy);
238 }
239 return null;
240 }
241
242 public Projectile attack(Point targ, AttackType attType) {
243 Weapon weap = selectWeapon(attType);
244 if ((System.currentTimeMillis() - this.lastAttacked) / 100L >= weap.getAttackSpeed() && !this.disabled) {
245 this.lastAttacked = System.currentTimeMillis();
246 Point oldTarget = this.target;
247 this.target = targ;
248 this.model.setDirection(calcDirection());
249 this.target = oldTarget;
250 if (attType != AttackType.Spell) {
251 this.model.setAction(Action.Attacking);
252 this.model.getAnimation(this.model.getDirection(), this.model.getAction()).reset();
253 }
254 if (weap.getType() == ItemType.RangedWeapon || weap.getType() == ItemType.Spell) {
255 Point loc = new Point(this.loc);
256 loc.setY(loc.getY() - 30);
257 return weap.spawnProjectile(loc, targ);
258 }
259 }
260 return null;
261 }
262
263 private Weapon selectWeapon(AttackType attType) {
264 switch (attType) {
265 case Weapon:
266 return this.weapon;
267 case Spell:
268 return this.spell;
269 }
270 return this.weapon;
271 }
272
273 public void addDrop(ItemDrop item) {
274 this.drops.add(item);
275 }
276
277 public LinkedList<ItemDrop> getDrops() {
278 return this.drops;
279 }
280
281 public LinkedList<Item> spawnDrops() {
282 LinkedList<Item> newDrops = new LinkedList<Item>();
283 Iterator<ItemDrop> iter = this.drops.iterator();
284 Random gen = new Random();
285 while (iter.hasNext()) {
286 ItemDrop cur = iter.next();
287 if (gen.nextDouble() <= cur.getDropChance()) {
288 newDrops.add(cur.getItem());
289 }
290 }
291 return newDrops;
292 }
293
294 public void draw(Graphics g, int playerX, int playerY) {
295 if (this.passive > 0) {
296 g.drawImage(LostHavenRPG.passiveAura, 375 + this.loc.getX() - playerX, 300 - this.model.getHeight() + this.loc.getY() - playerY, null);
297 }
298 if (this.thorns > 0) {
299 g.drawImage(LostHavenRPG.thornsAura, 375 + this.loc.getX() - playerX, 300 - this.model.getHeight() + this.loc.getY() - playerY, null);
300 }
301 this.model.draw(g, 400 + this.loc.getX() - playerX, 300 + this.loc.getY() - playerY);
302 if (this.confused > 0) {
303 g.drawImage(LostHavenRPG.confuseAura, 375 + this.loc.getX() - playerX, 300 - this.model.getHeight() + this.loc.getY() - playerY, null);
304 }
305 if (this.sand > 0) {
306 g.drawImage(LostHavenRPG.sandAura, 375 + this.loc.getX() - playerX, 300 - this.model.getHeight() + this.loc.getY() - playerY, null);
307 }
308 g.setColor(Color.red);
309 g.fillRect(360 + this.loc.getX() - playerX, 290 - this.model.getHeight() + this.loc.getY() - playerY, 80 * this.hitpoints / this.maxHitpoints, 10);
310 g.setColor(Color.black);
311 Font font12 = new Font("Arial", 0, 12);
312 FontMetrics metrics = g.getFontMetrics(font12);
313 g.setFont(font12);
314 g.drawString(this.name, 400 + this.loc.getX() - playerX - metrics.stringWidth(this.name) / 2, 300 - this.model.getHeight() + this.loc.getY() - playerY);
315 }
316
317 public void save(PrintWriter out) {
318 out.println(this.type);
319 if (this.spawnPoint == null) {
320 out.println(-1);
321 } else {
322 out.println(this.spawnPoint.idNum);
323 }
324 out.println(String.valueOf(this.loc.getX()) + "," + this.loc.getY());
325 out.println(this.model.getDirection());
326 out.println(this.model.getAction());
327 out.println(this.hitpoints);
328 out.println(this.manapoints);
329 out.println(this.passive);
330 out.println(this.thorns);
331 out.println(this.confused);
332 out.println(this.sand);
333 if (this.weapon == null) {
334 out.println("None");
335 } else {
336 out.println(this.weapon.getName());
337 }
338 }
339
340 public static Creature loadTemplate(BufferedReader in) {
341 Creature cr = new Creature();
342 BufferedImage[] imgProj = new BufferedImage[8];
343 try {
344 cr.name = in.readLine();
345 cr.type = CreatureType.valueOf(cr.name);
346 if (cr.type == CreatureType.Player) {
347 cr = Player.loadTemplate(in);
348 }
349 cr.setSpeed(Integer.parseInt(in.readLine()));
350 cr.setAttribute(Attribute.Strength, Integer.parseInt(in.readLine()));
351 cr.setAttribute(Attribute.Dexterity, Integer.parseInt(in.readLine()));
352 cr.setAttribute(Attribute.Constitution, Integer.parseInt(in.readLine()));
353 cr.setAttribute(Attribute.Wisdom, Integer.parseInt(in.readLine()));
354 ItemType wType = ItemType.valueOf(in.readLine());
355 if (wType == ItemType.RangedWeapon) {
356 imgProj = LostHavenRPG.imgBow;
357 } else if (wType == ItemType.Spell) {
358 imgProj[0] = ImageIO.read(cr.getClass().getResource("/images/" + cr.type.toString() + "/proj.png"));
359 imgProj[1] = ImageIO.read(cr.getClass().getResource("/images/" + cr.type.toString() + "/proj.png"));
360 imgProj[2] = ImageIO.read(cr.getClass().getResource("/images/" + cr.type.toString() + "/proj.png"));
361 imgProj[3] = ImageIO.read(cr.getClass().getResource("/images/" + cr.type.toString() + "/proj.png"));
362 imgProj[4] = ImageIO.read(cr.getClass().getResource("/images/" + cr.type.toString() + "/proj.png"));
363 imgProj[5] = ImageIO.read(cr.getClass().getResource("/images/" + cr.type.toString() + "/proj.png"));
364 imgProj[6] = ImageIO.read(cr.getClass().getResource("/images/" + cr.type.toString() + "/proj.png"));
365 imgProj[7] = ImageIO.read(cr.getClass().getResource("/images/" + cr.type.toString() + "/proj.png"));
366 }
367 cr.model = cr.loadModel(cr.type.toString());
368 cr.setWeapon(new Weapon("None", wType, "Dagger.png", imgProj, 10, Integer.parseInt(in.readLine()), Integer.parseInt(in.readLine())));
369 cr.setExperience(Integer.parseInt(in.readLine()));
370 } catch (IOException ioe) {
371 ioe.printStackTrace();
372 }
373 return cr;
374 }
375
376 public Model loadModel(String folder) {
377 boolean noAnims = false;
378 Animation anmStandN = new Animation("north", 0, 0, 40, 60, 300, true);
379 Animation anmStandS = new Animation("south", 0, 0, 40, 60, 300, true);
380 Animation anmStandE = new Animation("east", 0, 0, 40, 60, 300, true);
381 Animation anmStandW = new Animation("west", 0, 0, 40, 60, 300, true);
382 Animation anmWalkN = new Animation("north", 0, 0, 40, 60, 300, true);
383 Animation anmWalkS = new Animation("south", 0, 0, 40, 60, 300, true);
384 Animation anmWalkE = new Animation("east", 0, 0, 40, 60, 300, true);
385 Animation anmWalkW = new Animation("west", 0, 0, 40, 60, 300, true);
386 Animation anmAttackN = new Animation("north", 0, 0, 40, 60, 500, false);
387 Animation anmAttackS = new Animation("south", 0, 0, 40, 60, 500, false);
388 Animation anmAttackE = new Animation("east", 0, 0, 40, 60, 500, false);
389 Animation anmAttackW = new Animation("west", 0, 0, 40, 60, 500, false);
390 Animation anmDeath = new Animation("west", 0, 0, 40, 60, 1000, false);
391 BufferedImage walkN = null;
392 BufferedImage walkS = null;
393 BufferedImage walkE = null;
394 BufferedImage walkW = null;
395 boolean hasDeathAnim = (getClass().getResource("/images/" + folder + "/Death1.png") != null);
396 try {
397 if (getClass().getResource("/images/" + folder + "/WalkN.png") != null) {
398 walkN = ImageIO.read(getClass().getResource("/images/" + folder + "/WalkN.png"));
399 anmWalkN.addFrame(walkN);
400 walkS = ImageIO.read(getClass().getResource("/images/" + folder + "/WalkS.png"));
401 anmWalkS.addFrame(walkS);
402 walkE = ImageIO.read(getClass().getResource("/images/" + folder + "/WalkE.png"));
403 anmWalkE.addFrame(walkE);
404 walkW = ImageIO.read(getClass().getResource("/images/" + folder + "/WalkW.png"));
405 anmWalkW.addFrame(walkW);
406 } else if (getClass().getResource("/images/" + folder + "/WalkN1.png") != null) {
407 walkN = ImageIO.read(getClass().getResource("/images/" + folder + "/WalkN1.png"));
408 anmWalkN.addFrame(walkN);
409 anmWalkN.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/WalkN2.png")));
410 walkS = ImageIO.read(getClass().getResource("/images/" + folder + "/WalkS1.png"));
411 anmWalkS.addFrame(walkS);
412 anmWalkS.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/WalkS2.png")));
413 walkE = ImageIO.read(getClass().getResource("/images/" + folder + "/WalkE1.png"));
414 anmWalkE.addFrame(walkE);
415 anmWalkE.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/WalkE2.png")));
416 walkW = ImageIO.read(getClass().getResource("/images/" + folder + "/WalkW1.png"));
417 anmWalkW.addFrame(walkW);
418 anmWalkW.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/WalkW2.png")));
419 } else {
420 noAnims = true;
421 anmStandN.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
422 anmStandS.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
423 anmStandE.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
424 anmStandW.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
425 anmWalkN.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
426 anmWalkN.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
427 anmWalkS.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
428 anmWalkS.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
429 anmWalkE.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
430 anmWalkE.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
431 anmWalkW.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
432 anmWalkW.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
433 anmAttackN.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
434 anmAttackS.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
435 anmAttackE.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
436 anmAttackW.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/" + folder + ".png")));
437 }
438 if (!noAnims) {
439 BufferedImage standN, standS, standE, standW;
440 if (getClass().getResource("/images/" + folder + "/StandN.png") == null) {
441 standN = walkN;
442 standS = walkS;
443 standE = walkE;
444 standW = walkW;
445 } else {
446 standN = ImageIO.read(getClass().getResource("/images/" + folder + "/StandN.png"));
447 standS = ImageIO.read(getClass().getResource("/images/" + folder + "/StandS.png"));
448 standE = ImageIO.read(getClass().getResource("/images/" + folder + "/StandE.png"));
449 standW = ImageIO.read(getClass().getResource("/images/" + folder + "/StandW.png"));
450 anmWalkN = new Animation("north", 0, 0, 40, 60, 150, true);
451 anmWalkS = new Animation("south", 0, 0, 40, 60, 150, true);
452 anmWalkE = new Animation("east", 0, 0, 40, 60, 150, true);
453 anmWalkW = new Animation("west", 0, 0, 40, 60, 150, true);
454 anmWalkN.addFrame(walkN);
455 anmWalkN.addFrame(standN);
456 anmWalkN.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/WalkN2.png")));
457 anmWalkN.addFrame(standN);
458 anmWalkS.addFrame(walkS);
459 anmWalkS.addFrame(standS);
460 anmWalkS.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/WalkS2.png")));
461 anmWalkS.addFrame(standS);
462 anmWalkE.addFrame(walkE);
463 anmWalkE.addFrame(standE);
464 anmWalkE.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/WalkE2.png")));
465 anmWalkE.addFrame(standE);
466 anmWalkW.addFrame(walkW);
467 anmWalkW.addFrame(standW);
468 anmWalkW.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/WalkW2.png")));
469 anmWalkW.addFrame(standW);
470 }
471 anmStandN.addFrame(standN);
472 anmStandS.addFrame(standS);
473 anmStandE.addFrame(standE);
474 anmStandW.addFrame(standW);
475 if (getClass().getResource("/images/" + folder + "/AttackN.png") != null) {
476 anmAttackN.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackN.png")));
477 anmAttackN.addFrame(standN);
478 anmAttackS.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackS.png")));
479 anmAttackS.addFrame(standS);
480 anmAttackE.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackE.png")));
481 anmAttackE.addFrame(standE);
482 anmAttackW.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackW.png")));
483 anmAttackW.addFrame(standW);
484 } else if (getClass().getResource("/images/" + folder + "/AttackN1.png") != null) {
485 anmAttackN.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackN1.png")));
486 anmAttackN.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackN2.png")));
487 anmAttackS.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackS1.png")));
488 anmAttackS.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackS2.png")));
489 anmAttackE.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackE1.png")));
490 anmAttackE.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackE2.png")));
491 anmAttackW.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackW1.png")));
492 anmAttackW.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/AttackW2.png")));
493 } else {
494 anmAttackN.addFrame(standN);
495 anmAttackS.addFrame(standS);
496 anmAttackE.addFrame(standE);
497 anmAttackW.addFrame(standW);
498 }
499 if (hasDeathAnim) {
500 anmDeath.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/Death1.png")));
501 anmDeath.addFrame(ImageIO.read(getClass().getResource("/images/" + folder + "/Death2.png")));
502 }
503 }
504 } catch (IOException ioe) {
505 ioe.printStackTrace();
506 }
507 Model model = new Model(hasDeathAnim);
508 model.addAnimation(Direction.North, Action.Standing, anmStandN);
509 model.addAnimation(Direction.South, Action.Standing, anmStandS);
510 model.addAnimation(Direction.East, Action.Standing, anmStandE);
511 model.addAnimation(Direction.West, Action.Standing, anmStandW);
512 model.addAnimation(Direction.North, Action.Walking, anmWalkN);
513 model.addAnimation(Direction.South, Action.Walking, anmWalkS);
514 model.addAnimation(Direction.East, Action.Walking, anmWalkE);
515 model.addAnimation(Direction.West, Action.Walking, anmWalkW);
516 model.addAnimation(Direction.North, Action.Attacking, anmAttackN);
517 model.addAnimation(Direction.South, Action.Attacking, anmAttackS);
518 model.addAnimation(Direction.East, Action.Attacking, anmAttackE);
519 model.addAnimation(Direction.West, Action.Attacking, anmAttackW);
520 if (hasDeathAnim) {
521 model.addAnimation(Direction.West, Action.Dying, anmDeath);
522 }
523 return model;
524 }
525
526 public void load(BufferedReader in) {
527 try {
528 this.spawnPoint = LostHavenRPG.getSpawnPoint(Integer.parseInt(in.readLine()));
529 if (this.spawnPoint != null) {
530 this.spawnPoint.numSpawns++;
531 }
532 String strLoc = in.readLine();
533 getLoc().setX(Integer.valueOf(strLoc.substring(0, strLoc.indexOf(","))).intValue());
534 getLoc().setY(Integer.valueOf(strLoc.substring(strLoc.indexOf(",") + 1)).intValue());
535 getModel().setDirection(Direction.valueOf(in.readLine()));
536 getModel().setAction(Action.valueOf(in.readLine()));
537 setHitpoints(Integer.valueOf(in.readLine()).intValue());
538 setManapoints(Integer.valueOf(in.readLine()).intValue());
539 this.passive = Integer.valueOf(in.readLine()).intValue();
540 this.thorns = Integer.valueOf(in.readLine()).intValue();
541 this.confused = Integer.valueOf(in.readLine()).intValue();
542 this.sand = Integer.valueOf(in.readLine()).intValue();
543 String strWeapon = in.readLine();
544 if (!strWeapon.equals("None")) {
545 setWeapon((Weapon)LostHavenRPG.items.get(strWeapon));
546 }
547 } catch (IOException ioe) {
548 ioe.printStackTrace();
549 }
550 }
551
552 protected int attributeIndex(Attribute attribute) {
553 switch (attribute) {
554 case Strength:
555 return 0;
556 case Dexterity:
557 return 1;
558 case Constitution:
559 return 2;
560 case Wisdom:
561 return 3;
562 }
563 return -1;
564 }
565
566 public int getAttribute(Attribute attribute) {
567 return this.attributes[attributeIndex(attribute)];
568 }
569
570 public void setAttribute(Attribute attribute, int num) {
571 this.attributes[attributeIndex(attribute)] = num;
572 updateStats();
573 }
574
575 private void updateStats() {
576 this.hitpoints = 2 * this.attributes[2];
577 this.maxHitpoints = 2 * this.attributes[2];
578 this.manapoints = 2 * this.attributes[3];
579 this.maxManapoints = 2 * this.attributes[3];
580 }
581
582 public Creature getEnemyTarget() {
583 return this.enemyTarget;
584 }
585
586 public int getExperience() {
587 return this.experience;
588 }
589
590 public int getHitpoints() {
591 return this.hitpoints;
592 }
593
594 public Model getModel() {
595 return this.model;
596 }
597
598 public long getLastAttacked() {
599 return this.lastAttacked;
600 }
601
602 public long getLastMoved() {
603 return this.lastMoved;
604 }
605
606 public int getLevel() {
607 return this.level;
608 }
609
610 public Point getLoc() {
611 return this.loc;
612 }
613
614 public int getManapoints() {
615 return this.manapoints;
616 }
617
618 public int getMaxHitpoints() {
619 return this.maxHitpoints;
620 }
621
622 public int getMaxManapoints() {
623 return this.maxManapoints;
624 }
625
626 public String getName() {
627 return this.name;
628 }
629
630 public int getSpeed() {
631 return this.speed;
632 }
633
634 public Point getTarget() {
635 return this.target;
636 }
637
638 public CreatureType getType() {
639 return this.type;
640 }
641
642 public EquippedWeapon getWeapon() {
643 return this.weapon;
644 }
645
646 public EquippedWeapon getSpell() {
647 return this.spell;
648 }
649
650 public void setEnemyTarget(Creature enemyTarget) {
651 this.enemyTarget = enemyTarget;
652 }
653
654 public void setExperience(int experience) {
655 this.experience = experience;
656 }
657
658 public void setHitpoints(int hitpoints) {
659 if (hitpoints > 0) {
660 this.hitpoints = hitpoints;
661 } else {
662 this.hitpoints = 0;
663 }
664 }
665
666 public void setModel(Model model) {
667 this.model = model;
668 }
669
670 public void setLastAttacked(long lastAttacked) {
671 this.lastAttacked = lastAttacked;
672 }
673
674 public void setLastMoved(long lastMoved) {
675 this.lastMoved = lastMoved;
676 }
677
678 public void setLevel(int level) {
679 this.level = level;
680 }
681
682 public void setLoc(Point loc) {
683 this.loc = loc;
684 }
685
686 public void setManapoints(int manapoints) {
687 if (manapoints > 0) {
688 this.manapoints = manapoints;
689 } else {
690 this.manapoints = 0;
691 }
692 }
693
694 public void setMaxHitpoints(int maxHitpoints) {
695 this.maxHitpoints = maxHitpoints;
696 }
697
698 public void setMaxManapoints(int maxManapoints) {
699 this.maxManapoints = maxManapoints;
700 }
701
702 public void setName(String name) {
703 this.name = name;
704 }
705
706 public void setSpeed(int speed) {
707 this.speed = speed;
708 }
709
710 public void setTarget(Point target) {
711 this.target = target;
712 }
713
714 public void setType(CreatureType type) {
715 this.type = type;
716 }
717
718 public void setWeapon(Weapon weapon) {
719 this.weapon = new EquippedWeapon(this, weapon);
720 this.weapon.update();
721 (this.model.getAnimation(Direction.North, Action.Attacking)).drawInterval = (this.weapon.getAttackSpeed() * 100 / (this.model.getAnimation(Direction.North, Action.Attacking)).frames.size());
722 (this.model.getAnimation(Direction.South, Action.Attacking)).drawInterval = (this.weapon.getAttackSpeed() * 100 / (this.model.getAnimation(Direction.South, Action.Attacking)).frames.size());
723 (this.model.getAnimation(Direction.East, Action.Attacking)).drawInterval = (this.weapon.getAttackSpeed() * 100 / (this.model.getAnimation(Direction.East, Action.Attacking)).frames.size());
724 (this.model.getAnimation(Direction.West, Action.Attacking)).drawInterval = (this.weapon.getAttackSpeed() * 100 / (this.model.getAnimation(Direction.West, Action.Attacking)).frames.size());
725 }
726
727 public void setSpell(Weapon spell) {
728 this.spell = new EquippedWeapon(this, spell);
729 this.spell.update();
730 }
731
732 public SpawnPoint getSpawnPoint() {
733 return this.spawnPoint;
734 }
735
736 public void setSpawnPoint(SpawnPoint sp) {
737 this.spawnPoint = sp;
738 }
739
740 public boolean farFromSpawnPoint() {
741 if (this.spawnPoint == null)
742 return false;
743 return (Point.dist(this.spawnPoint.getLoc(), this.loc) > 800.0D);
744 }
745
746 public boolean closerToSpawnPoint(Point newLoc) {
747 if (this.spawnPoint == null)
748 return false;
749 return (Point.dist(this.spawnPoint.getLoc(), this.loc) >= Point.dist(this.spawnPoint.getLoc(), newLoc));
750 }
751
752 public void disable() {
753 this.disabled = true;
754 }
755
756 public void enable() {
757 this.disabled = false;
758 }
759
760 public String toString() {
761 return this.name;
762 }
763
764 public int compareTo(Creature c) {
765 return 0;
766 }
767}
Note: See TracBrowser for help on using the repository browser.