| Monster.java |
1 import java.awt.*;
2 import javax.swing.*;
3 import java.applet.*;
4 import java.awt.image.*;
5 import java.awt.geom.*;
6 import java.net.*;
7 import java.util.*;
8 /**
9 * Class to define a Monster and its attributes.
10 *
11 * @see FireMon
12 * @see EarthMon
13 * @see WaterMon
14 * @see GasMon
15 * @see ElectricMon
16 */
17 public abstract class Monster extends MapObject
18 {
19 // The Kinds of Monsters
20 /**
21 * Electric kind
22 */
23 public static final int ELECTRIC = 0;
24 /**
25 * Earth kind
26 */
27 public static final int EARTH = 1;
28 /**
29 * Fire kind
30 */
31 public static final int FIRE = 2;
32 /**
33 * Water kind
34 */
35 public static final int WATER = 3;
36 /**
37 * Gas kind
38 */
39 public static final int GAS = 4;
40
41 /**
42 * The Maximum level allowed for Monsters to have
43 */
44 public static final int MAXLEVEL = 100;
45 /**
46 * The Maximum number of attack to have
47 */
48 public static final int MAXATTACKS = 4;
49
50 /**
51 * The HP Of this
52 */
53 protected int hp; // Hit Points (life left)
54 /**
55 * The Defense Of this
56 */
57 protected int defp; //Defense Points
58 /**
59 * The Attack Of this
60 */
61 protected int attp; // Attack Points
62 /**
63 * The Experience Of this
64 */
65 protected int exp; // Experience Points
66 /**
67 * The Maximum HP Of this, ie full health
68 */
69 protected int maxHP; // Max HP for level at
70 /**
71 * The Maximum Defense Of this at this level
72 */
73 protected int maxDef; // Max def for level at
74 /**
75 * The Maximum Attack Of this at this level
76 */
77 protected int maxAtt; // Max att for level at
78 /**
79 * The Maximum Experience before a level up
80 */
81 protected int maxExp; // Max exp before Level up
82 /**
83 * The level of this
84 */
85 protected int level; // Level
86 /**
87 * The Maximum level before learning a new attack
88 */
89 protected int maxLevel; // Max level before learn new Attack
90 /**
91 * The kind of Monster this is
92 */
93 protected int kind; // Kind of Monster
94 /**
95 * The attacks this knows
96 */
97 protected Vector attacks; // Attacks I know
98 /**
99 * The name of this
100 */
101 protected String name;
102 /**
103 * tru iff battling
104 */
105 protected boolean battle;
106 /**
107 * The MapObjct to display Over
108 */
109 static MapObject mp;
110
111 /**
112 * Learn the attack specifid by attack
113 *
114 * @param attack The Attack to learn
115 */
116 public abstract void addAttack(String attack);
117 /**
118 * Gets the name of the first Attack
119 *
120 * @return the name of the first Attack
121 */
122 public abstract String getA1();
123 /**
124 * Gets the name of the second Attack
125 *
126 * @return the name of the second Attack
127 */
128 public abstract String getA2();
129 /**
130 * Gets the name of the third Attack
131 *
132 * @return the name of the third Attack
133 */
134 public abstract String getA3();
135 /**
136 * Gets the name of the fourth Attack
137 *
138 * @return the name of the fourth Attack
139 */
140 public abstract String getA4();
141 /**
142 * Gets the name of the fifth Attack
143 *
144 * @return the name of the fifth Attack
145 */
146 public abstract String getA5();
147 /**
148 * Gets the name of the sixth Attack
149 *
150 * @return the name of the sixth Attack
151 */
152 public abstract String getA6();
153 /**
154 * Substitute one Attack for another.
155 *
156 * @param removed the Attack to remove
157 * @param added the Attack to add
158 */
159 public abstract boolean substituteAttack(Attack removed, Attack added);
160 /**
161 * Sets the hp
162 *
163 * @param the new HP of this
164 */
165 public void setHP(int nhp)
166 {
167 if(nhp < 0)
168 hp = 0;
169 else if(nhp <= maxHP)
170 hp = nhp;
171 else
172 hp = maxHP;
173 }
174 /**
175 * Gets the HP of this
176 *
177 * @return the HP of this
178 */
179 public int getHP() { return hp; }
180 /**
181 * Sets the maximum hp
182 *
183 * @param the new maximum HP of this
184 */
185 protected void setMaxHP(int nmhp)
186 {
187 if(nmhp > maxHP && level <= MAXLEVEL)
188 maxHP = nmhp;
189 }
190 /**
191 * Gets the maximum hp
192 *
193 * @return the new maximum HP of this
194 */
195 public int getMaxHP() { return maxHP; }
196 /**
197 * Sets the defense of this
198 *
199 * @param ndp the new defense of this
200 */
201 public void setDefense(int ndp)
202 {
203 if(ndp <= 0)
204 return;
205 if(ndp <= maxDef)
206 defp = ndp;
207 else
208 defp = maxDef;
209 }
210 /**
211 * Gers the defense of this
212 *
213 * @return the defense of this
214 */
215 public int getDefense() { return defp; }
216 /**
217 * Sets the Maximum defense of this
218 *
219 * @param nmdp the new Maximum Defense of this
220 */
221 protected void setMaxDefense(int nmdp)
222 {
223 if(nmdp > maxDef && level <= MAXLEVEL)
224 maxDef = nmdp;
225 }
226 /**
227 * Gets the Maximum Defense
228 *
229 * @return the Maximum defense of this
230 */
231 public int getMaxDefense() { return maxDef; }
232 /**
233 * Sets the Attack points of this
234 *
235 * @param nap the new Attack Points
236 */
237 public void setAttack(int nap)
238 {
239 if(nap <= 0)
240 return;
241 if(nap <= maxAtt)
242 attp = nap;
243 else
244 attp = maxAtt;
245 }
246 /**
247 * Gets the Attack Points of this.
248 *
249 * @return the Attack Points of this
250 */
251 public int getAttack() { return attp; }
252 /**
253 * Sets the Maximum Attack Points of this
254 *
255 * @param nmap the new Maximum Attack Points of this
256 */
257 protected void setMaxAttack(int nmap)
258 {
259 if(nmap > maxAtt && level <= MAXLEVEL)
260 maxAtt = nmap;
261 }
262 /**
263 * Gets the Maximum Attack Points of this
264 *
265 * @return the Maximum Attack Points of this
266 */
267 public int getMaxAttack() { return maxAtt; }
268 /**
269 * Sets the Experience Pints and levels up if needed.
270 *
271 * @param nep the new Experience Points of this
272 * @return true iff leveled up
273 */
274 protected boolean setExperience(int nep) throws AttacksFullException
275 {
276 if(nep > exp && nep <= maxExp)
277 {
278 exp = nep;
279 return false;
280 }
281 else if(nep > maxExp && level <= MAXLEVEL)
282 {
283 levelUp();
284 return true;
285 }
286 return false;
287 }
288 /**
289 * Gets the Experience of this
290 *
291 * @return the Experience of this
292 */
293 public int getExperience() { return exp; }
294
295 /**
296 * Sets the maximum Experience of this, the amount required before a level up
297 *
298 * @param nmep the New Maximum Experience Ponts
299 */
300 protected void setMaxExperience(int nmep)
301 {
302 if(nmep > maxExp && level <= MAXLEVEL)
303 maxExp = nmep;
304 }
305 /**
306 * Gets the Maximum Experience Pointsup
307 *
308 * @return the Maximum Experience Points, the amount required before a level up
309 */
310 public int getMaxExperience() { return maxExp; }
311 /**
312 * Gets the level of this
313 *
314 * @return the level of this
315 */
316 public int getLevel() { return level; }
317 /**
318 * Gets the Kind of this
319 *
320 * @return the kind of this
321 */
322 public int getKind() { return kind; }
323 /**
324 * Sets the level of this.
325 *
326 * @param level the level to mak this
327 */
328 public void setLevel(int level) // Only used by MapLoader
329 {
330 if(level <= MAXLEVEL)
331 this.level = level;
332 }
333 /**
334 * Gets the Max level
335 *
336 * @return the max level before a new attack is learned
337 */
338 public int getMaxLevel() { return maxLevel; }
339 /**
340 * Gets the number of attacks this knows.
341 */
342 public int getNumberOfAttacks() { return attacks.size(); }
343 /**
344 * Preforms a level up increasing attack, defense, and hp.
345 *
346 * @exception AttacksFullException iff this has more than Monster.MAXATTACKS
347 */
348 protected void levelUp() throws AttacksFullException
349 {
350 if((level == MAXLEVEL))
351 return;
352 level++;
353
354 int maxh = 0, maxa = 0, maxd = 0;
355
356 setMaxExperience(10*level);
357
358 switch(getKind())
359 {
360 case ELECTRIC:
361 {
362 maxa = maxAtt + 10;
363 maxd = maxDef + 7;
364 maxh = maxHP + 6;
365 break;
366 }
367 case EARTH:
368 {
369 maxa = maxAtt + 7;
370 maxd = maxDef + 6;
371 maxh = maxHP + 10;
372 break;
373 }
374 case FIRE:
375 {
376 maxa = maxAtt + 6;
377 maxd = maxDef + 7;
378 maxh = maxHP + 10;
379 break;
380 }
381 case WATER:
382 {
383 maxa = maxAtt + 7;
384 maxd = maxDef + 10;
385 maxh = maxHP + 6;
386 break;
387 }
388 case GAS:
389 {
390 maxa = maxAtt + 6;
391 maxd = maxDef + 10;
392 maxh = maxHP + 7;
393 break;
394 }
395 }
396 setMaxAttack(maxa);
397 setMaxDefense(maxd);
398 setMaxHP(maxh);
399 setMaxExperience(maxExp+10);
400
401 // Make sure any amount missing is missing after level up
402 setAttack((attp - maxAtt) + maxa);
403 setDefense((defp - maxDef) + maxd);
404 setHP((hp - maxHP) + maxh);
405 }
406
407 /**
408 * Finish the fight with the opposing Monster, ie give this the requisite experience
409 *
410 * @param m the Opposing Monster
411 * @exception AttacksFullException iff this has more than Monster.MAXATTACKS, and is leveling up
412 */
413 public boolean finishFight(Monster m) throws AttacksFullException
414 {
415 int nmep = (2 * m.getLevel()) - this.level;
416 boolean b = false;
417 if(level < 10)
418 {
419 if(nmep > 0)
420 {
421 if(setExperience(exp+nmep))
422 b = true;
423 }
424 else
425 {
426 if(setExperience(exp + 2))
427 b = true;
428 }
429 }
430 else if(level < 50)
431 {
432 if(nmep > 15)
433 {
434 if(setExperience(exp+nmep))
435 b = true;
436 }
437 else
438 {
439 if(setExperience(exp + 15))
440 b = true;
441 }
442 }
443 else
444 {
445 if(nmep > 40)
446 {
447 if(setExperience(exp+nmep))
448 b = true;
449 }
450 else
451 {
452 if(setExperience(exp + 40))
453 b = true;
454 }
455 }
456 return b;
457 }
458 /**
459 * Sets the name of this Monster
460 *
461 * @param name The Name of this
462 */
463 public void setName(String name)
464 {
465 if(name != null)
466 this.name = name;
467 }
468 /**
469 * Gets the name of this.
470 *
471 * @return the name of this
472 */
473 public String getName() { return name; }
474 /**
475 * Gets the array of attacks available to this, adds null if less then Monster.MAXATTACKS
476 *
477 * @return the array of attacks available to this, adds null if less then Monster.MAXATTACKS
478 */
479 public String[] getAttacks()
480 {
481 String[] s = new String[MAXATTACKS];
482 for(int i = 0; i < attacks.size(); i++)
483 {
484 s[i] = ((Attack)attacks.get(i)).getName();
485 }
486 return s;
487 }
488 /**
489 * Gets the Attack known at the specified position
490 *
491 * @param pos the index of the Attack to return
492 * @return the Attack at the specified position
493 */
494 public Attack getAttack(int pos)
495 {
496 if(pos >= 0 && pos < attacks.size())
497 {
498 return (Attack)attacks.get(pos);
499 }
500 return null;
501 }
502 /**
503 * Tests for equality
504 *
505 * @param o the Object to compare to
506 * @return true iff equivalant
507 */
508 public boolean equals(Object o)
509 {
510 if(o instanceof Monster)
511 return equals((Monster)o);
512 return false;
513 }
514 /**
515 * Tests for equality, ie if all the parameters are the same
516 *
517 * @param m the Monster to compare to
518 * @return true iff equivalant
519 */
520 public boolean equals(Monster m)
521 {
522 if(m.getName().equals(name) && m.getHP() == hp && m.getDefense() == defp && m.getAttack() == attp && m.getExperience() == exp && m.getLevel() == level
523 && m.getKind() == kind && m.getMaxHP() == maxHP && m.getMaxDefense() == maxDef && m.getMaxAttack() == maxAtt && m.getMaxExperience() == maxExp
524 && m.getMaxLevel() == maxLevel)
525 return true;
526 return false;
527 }
528 /**
529 * Returns true iff battling
530 *
531 * @return true iff battling
532 */
533 public boolean isBattling() { return battle; }
534 /**
535 * Starts a fight
536 */
537 public void startBattle() { battle = true; }
538 /**
539 * Stop a fight
540 */
541 public void stopBattle() { battle = false; }
542 /**
543 * Sets the MapObject to paint where this is.
544 *
545 * @param m the MapObject to paint where this is
546 */
547 public void setBehind(MapObject m)
548 {
549 if(m == null)
550 return;
551 else if(m.canBeEntered())
552 this.mp = m;
553 }
554 /**
555 * Draws this for the Grid.
556 *
557 * @param g the Graphics.
558 */
559 public void paint(Graphics g)
560 {
561 if(mp != null)
562 mp.paint(g);
563 }
564 }