1   import java.awt.*;
2   import java.applet.*;
3   import javax.swing.*;
4   import java.awt.image.*;
5   import java.awt.geom.*;
6   import java.net.*;
7   import java.util.*;
8   /**
9    * Class to define People, the superclass of Person, Player and Trainer.  Carry Items
10   * Monsters and Monsters at Home
11   * 
12   * @see Monster
13   * @see Item
14   * @see Person
15   * @see Player
16   * @see Trainer
17   */
18  public abstract class People extends MapObject
19  {
20      /**
21       * The Maximum number of Monsters to cary
22       */
23      protected final int MAXMON = 4;
24  
25      /**
26       * The Monsters caught, and able to be used, ie in the Pepole's hand
27       */
28      protected Vector mon; // For the Monsters
29      /**
30       * The Monsters caught, and not able to be used, ie in the Pepole's home collection
31       */
32      protected Vector hmon; // Monsters at home
33      /**
34       * The Items bought.
35       */
36      protected Vector items; // For the items
37  
38      /**
39       * The index of the active Monster
40       */
41      protected int activeMon; // The index of the active Monster
42      /**
43       * True if has beed beaten by the Player
44       */
45      protected boolean beaten; // Have I been Beaten by player?
46      /**
47       * True if in a battle, with Player or Person
48       */
49      protected boolean battling; // Am I in a battle?
50      /**
51       * The amount of money carrying
52       */
53      protected int money; // Amount of cash
54      /**
55       * The MapObject to paint this on top of
56       */
57      protected static MapObject behind; // Object to Draw Behind
58      /**
59       * The direction facing as specified in Grid
60       * 
61       * @see Grid
62       */
63      protected int dir; // What way facing
64      /**
65       * The name of this
66       */
67      protected String name; // The name of the People
68      /**
69       * The dialogue to present when in Battle
70       * 
71       * @see Battle
72       */
73      protected String dlg;
74  
75      /**
76       * Gets the dialogue associated with this
77       * 
78       * @return the dialogue associated with this
79       */
80      public String getDialogue() { return dlg; }
81      /**
82       * Sets the dialogue associated with this
83       * 
84       * @param slf the dialogue associated with this
85       */
86      public void setDialogue(String slf)
87      {
88          if(slf != null)
89              this.dlg = slf;
90      }
91      /**
92       * Returns false, as this cannot be entered
93       */
94      public boolean isEnterable() { return false; }
95      /**
96       * Sets the directio of this as specified in Grid
97       * 
98       * @param direct the direction to face
99       * @return true iff direct is a valid direction
100      * @see Grid
101      */
102     public boolean setDirection(int direct)
103     {
104         if(direct == Grid.LEFT || direct == Grid.RIGHT || direct == Grid.UP || direct == Grid.DOWN)
105         {
106             dir = direct;
107             return true;
108         }
109         else
110             return false;
111     }
112     /**
113      * Gets the direction facing
114      * 
115      * @return the direction facing, as specified in Grid
116      * @see Grid
117      */
118     public int getDirection() { return dir; }
119     /**
120      * Sets the MapObject to paint behind
121      * 
122      * @param beh the MapObject to paint this over
123      */
124     public void setBehind(MapObject beh)
125     {
126         if(beh.canBeEntered())
127         {
128             behind = beh;
129         }
130     }
131     /**
132      * Gets the MapObject standing on
133      * 
134      * @return the MapObject standing on
135      */
136     public MapObject getBehind() { return behind; }
137     /**
138      * Defeat this, ie sets beaten to true.
139      */
140     public void beat() { beaten = true; }
141     /**
142      * Gets if this is in a Battle
143      * 
144      * @return true if is in a Battle
145      * @see Battle
146      * @see PersonAI
147      */
148     public boolean isBattling() { return battling; }
149     /**
150      * Begins a battle with this, sets battle to true.
151      */
152     public void battle() { battling = true; }
153     /**
154      * Ends a battle with this, sets battle to false.
155      */
156     public void stopBattle() { battling = false; }
157     /**
158      * Sets the amount of Money carried.
159      * 
160      * @param mon the amount of money carried
161      */
162     public void setMoney(int mon)
163     {
164         if(mon > 0) money = mon; 
165         else money = 0;
166     }
167     /**
168      * Gets the amount of Money carried.
169      * 
170      * @return mon the amount of money carried
171      */
172     public int getMoney() { return money; }
173     /**
174      * Gets the name of this.
175      * 
176      * @return the name of this
177      */
178     public String getName() { return name; }
179     /**
180      * Sets the name of this.
181      * 
182      * @param name the name of this
183      */
184     public void setName(String name)
185     {
186         if(name != null)
187             this.name = name;
188     }
189     /**
190      * Determine if this has been beaten.
191      * 
192      * @return true iff this has been beaten.
193      */
194     public boolean isBeaten() { return beaten; }
195     /**
196      * Adds a Monster to this, either to the hand or to 
197      * home if the hand has more than this.MAXMON
198      * 
199      * @param p the Monster to add
200      * @return true if added to hand, false otherwise
201      * @see Monster
202      */
203     public boolean addMonster(Monster p)
204     {
205         if(p == null)
206             return false;
207         if(mon.size() < MAXMON)
208         {
209             mon.add(p);
210             return true;
211         }
212         else
213         {
214             hmon.add(p);
215             return false;
216         }
217     }
218     /**
219      * Adds a Monster to the Home collection
220      * 
221      * @param the Monster to add
222      * @see Monster
223      */
224     public void addMonsterHome(Monster p)
225     {
226         if(p != null)
227         {
228             hmon.add(p);
229         }
230     }
231     /**
232      * Switches a Monster from Home collection to the 
233      * Hand collection.
234      * 
235      * @param hand the Monster in the hand to send home
236      * @param home the Monster at home to send to the hand
237      * @see Monster
238      */
239     public boolean switchMonster(Monster hand, Monster home)
240     {
241         int i, j;
242         i = mon.indexOf(hand);
243         j = hmon.indexOf(home);
244         if(i >= 0 && j >= 0)
245         {
246             hmon.remove(home);
247             mon.remove(hand);
248             hmon.add(hand);
249             mon.add(home);
250             return true;
251         }
252         return false;
253     }
254     /**
255      * Sets the ActiveMonster for this.  Does not count Monsters that
256      * have no HP.
257      * 
258      * @param i the new index of the ActiveMonster for this.
259      */
260     public void setActiveMonster(int i)
261     {
262         if(i >= 0 && i < mon.size())
263         {
264             activeMon = i;
265         }
266     }
267     /**
268      * Gets the index of the Active Monster.  Does not count Monsters that
269      * have no HP.
270      * 
271      * @return the index of the Active Monster
272      */
273     public int getActiveMonster() { return activeMon; }
274     /**
275      * Gets the Monster from the hand at the specified index, ignoring Monsters that have no HP.
276      * 
277      * @param i the index to retrieve the Monster from
278      * @return the Monster at the specified index, not counting Monsters that have no HP
279      */
280     public Monster getMonster(int i)
281     {
282         int j = 0;
283         if(i >= 0 && i < mon.size())
284         {
285             Monster m;
286             for(int q = 0; q < mon.size(); q++)
287             {
288                 m = (Monster)mon.get(q);
289                 if(m.getHP() > 0)
290                 {
291                     if(j == i)
292                         return m;
293                     j++;
294                 }
295             }
296         }
297         return null;
298     }
299     /**
300      * Gets the Monster from home at the specified index, ignoring Monsters that have no HP.
301      * 
302      * @param i the index to retrieve the Monster from
303      * @return the Monster at the specified index, not counting Monsters that have no HP
304      */
305     public Monster getMonsterAtHome(int i)
306     {
307         int j = 0;
308         if(i >= 0 && i < hmon.size())
309         {
310             Monster m;
311             for(int q = 0; q < hmon.size(); q++)
312             {
313                 m = (Monster)hmon.get(q);
314                 if(m.getHP() > 0)
315                 {
316                     if(j == i)
317                         return m;
318                     j++;
319                 }
320             }
321         }
322         return null;
323     }
324     /**
325      * Gets the number of Monsters in the hand that do not 
326      * have 0 hp.
327      * 
328      * @return the number of Monsters in the hand that do not have 0 hp.
329      */
330     public int getNumberOfMonsters()
331     {
332         int num = 0;
333         for(int i = 0; i < mon.size(); i++)
334         {
335             if(((Monster)mon.get(i)).getHP() > 0)
336                 num++;
337         }
338         return num;
339     }
340     /**
341      * Gets the number of Monsters at home that do not 
342      * have 0 hp.
343      * 
344      * @return the number of Monsters at home that do not have 0 hp.
345      */
346     public int getNumberOfMonstersAtHome()
347     {
348         int num = 0;
349         for(int i = 0; i < hmon.size(); i++)
350         {
351             if(((Monster)hmon.get(i)).getHP() > 0)
352                 num++;
353         }
354         return num; }
355     /**
356      * Adds the Item specified to this.
357      * 
358      * @param i the Item to add
359      * @see Item
360      */
361     public void addItem(Item i)
362     {
363         if(i == null)
364             return;
365         Item it;
366         for(int j = 0; j < items.size(); j++)
367         {
368             it = (Item)items.get(j);
369             if(i.equals(it))
370             {
371                 it.addItem();
372                 return;
373             }
374         }
375         items.add(i);
376     }
377 
378     /**
379      * Gets the Item corresponding to the name specified.
380      * 
381      * @param the name to search Item Collection for
382      * @return the Item corresponding to the name specified.
383      * @see Item
384      */
385     public Item getItem(String name)
386     {
387         Item it;
388         for(int i = 0; i < items.size(); i++)
389         {
390             it = (Item)items.get(i);
391             if(it.getName().equalsIgnoreCase(name))
392             {
393                 return it;
394             }
395         }
396         return null;
397     }
398     /**
399      * Uses the Item specified on the Monster specified
400      * 
401      * @param it the Item to use on the Monster
402      * @param m the Monster to use the Item on
403      * @see Monster
404      * @see Item
405      */
406     public void useItem(Item it, Monster m)
407     {
408         it.useItem(m);
409         if(it.getItemCount() == 0)
410             items.remove(it);
411     }
412     /**
413      * Gets the number of Item this has.
414      * 
415      * @param name the name of the Item to get the count of.
416      * @see Item
417      */
418     public int getItemCount(String name)
419     {
420         Item it;
421         for(int i = 0; i < items.size(); i++)
422         {
423             it = (Item)items.get(i);
424             if(it.getName().equalsIgnoreCase(name))
425             {
426                 return it.getItemCount();
427             }
428         }
429         return 0;
430     }
431     /**
432      * Heals all Monster to full HP, Attack Points, and Defense Points
433      * 
434      * @see Monster
435      * @see Shop
436      */
437     public void healAllMonsters()
438     {
439         Monster m;
440         for(int i = 0; i < mon.size(); i++)
441         {
442             m = (Monster)mon.get(i);
443             m.setHP(m.getMaxHP());
444             m.setAttack(m.getMaxAttack());
445             m.setDefense(m.getMaxDefense());
446         }
447     }
448     /**
449      * Gets an array of String for the Attacks available for the
450      * Monster at index p, ignoring the Monsters with no HP.
451      * 
452      * @param the index of the Monster to get the Attacks for, ignoring those with no HP
453      * @return an array of String for the Attacks available for the Monster at index p, ignoring the Monsters with no HP.
454      * @see Monster
455      * @see Battle
456      */
457     public String[] getMonsterAttacks(int p)
458     {
459         if(p < mon.size() && p >= 0)
460         {
461             int j = 0;
462             Monster m;
463             for(int q = 0; q < mon.size(); q++)
464             {
465                 m = (Monster)mon.get(q);
466                 if(m.getHP() > 0)
467                 {
468                     if(j == p)
469                         return m.getAttacks();
470                     j++;
471                 }
472             }
473         }
474         return new String[Monster.MAXATTACKS];
475     }
476     /**
477      * Gets an array of Strings of the Monsters that the Player is currently carrying, ignoring
478      * those with no HP.
479      * 
480      * @return an array of Strings of the Monsters that the Player is currently carrying, ignoring those with no HP.
481      * @see Monster
482      * @see Battle
483      */
484     public String[] getMonsters()
485     {
486         String[] s = new String[MAXMON];
487         Monster m;
488         for(int i = 0, j = 0; i < mon.size(); i++)
489         {
490             m = (Monster)mon.get(i);
491             if(m.getHP() > 0)
492             {
493                 s[j++] = m.getName();
494             }
495         }
496         return s;
497     }
498     /**
499      * Gets an array of Strings of the Item that the Player is currently carrying
500      * 
501      * @return an array of Strings of the Item that the Player is currently carrying.
502      * @see Item
503      * @see Battle
504      * @see Shop
505      */
506     public String[] getItems()
507     {
508         if(items.size() == 0)
509             return new String[4];
510         String[] s;
511         if(items.size() >= 4)
512             s = new String[items.size()];
513         else
514             s = new String[4];
515         for(int i = 0; i < items.size(); i++)
516         {
517             s[i] = ((Item)items.get(i)).getName();
518         }
519         return s;
520     }
521 }