| Trainer.java |
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 /**
10 * Implementation of a Trainer, a Person with a Badge,
11 * and not able to move around the Grid. Name still needs to be
12 * unique for a Grid as this is used for equals.
13 *
14 * @see Person
15 * @see People
16 * @see Grid
17 * @author Matt, Sam
18 * @version 1.2.5.8
19 */
20 public class Trainer extends People implements Runnable
21 {
22 /**
23 * The Images associated with the Four directions,
24 * Up, Left, Down, Right/
25 */
26 protected static Image img, bimg, limg, rimg; // The Four directions
27 /**
28 * The name of the badge
29 */
30 protected String badge;
31 /**
32 * Where I am on the Grid
33 */
34 int pi, pj;
35 /**
36 * The Grid I am on
37 */
38 protected Grid g;
39 /**
40 * My Thread Object
41 */
42 Thread t;
43
44 /**
45 * Constructs a default Trainer
46 * No money,
47 * Grass as behind MapObject
48 * Name of NPC
49 * Badge of Name
50 * No Monsters
51 * No Dialogue
52 */
53 public Trainer()
54 {
55 enterable = false;
56 type = MapObject.TRAINER;
57 activeMon = 0;
58 money = 0;
59 behind = new Grass();
60 name = "NPC";
61 badge = name;
62 mon = new Vector();
63 hmon = new Vector();
64 items = new Vector();
65 dlg = "";
66 }
67 /**
68 * Constructs a Trainer facing in direction dir
69 * No money,
70 * Grass as behind MapObject
71 * Name of NPC
72 * Badge of Name
73 * No Monsters
74 * No Dialogue
75 *
76 * @param dir the direction to face, specified in Grid
77 * @see Grid
78 */
79 public Trainer(int dir)
80 {
81 if(!setDirection(dir))
82 this.dir = Grid.DOWN;
83 enterable = false;
84 type = MapObject.TRAINER;
85 activeMon = 0;
86 money = 0;
87 behind = new Grass();
88 name = "NPC";
89 badge = name;
90 mon = new Vector();
91 hmon = new Vector();
92 items = new Vector();
93 dlg = "";
94 }
95 /**
96 * Constructs a Trainer with MapObject underneath
97 * No money,
98 * Name of NPC
99 * Badge of Name
100 * No Monsters
101 * No Dialogue
102 *
103 * @param behind The MapObject standing on
104 */
105 public Trainer(MapObject behind)
106 {
107 if(behind.canBeEntered())
108 this.behind = behind;
109 else
110 this.behind = new Grass();
111 enterable = false;
112 type = MapObject.TRAINER;
113 activeMon = 0;
114 money = 0;
115 name = "NPC";
116 badge = name;
117 mon = new Vector();
118 hmon = new Vector();
119 items = new Vector();
120 }
121 /**
122 * Constructs a Trainer with MapObject underneath
123 * and facing dir.
124 * No money,
125 * Name of NPC
126 * Badge of Name
127 * No Monsters
128 * No Dialogue
129 *
130 * @param behind The MapObject standing on
131 * @param dir the Direction facing, specified in Grid
132 * @see Grid
133 */
134 public Trainer(MapObject behind, int dir)
135 {
136 if(!setDirection(dir))
137 this.dir = Grid.DOWN;
138 if(behind.canBeEntered())
139 this.behind = behind;
140 else
141 this.behind = new Grass();
142 enterable = false;
143 type = MapObject.TRAINER;
144 activeMon = 0;
145 money = 0;
146 name = "NPC";
147 badge = name;
148 mon = new Vector();
149 hmon = new Vector();
150 items = new Vector();
151 }
152 /**
153 * Constructs a Trainer with Name name.
154 * No money,
155 * Grass as behind MapObject
156 * Badge of Name
157 * No Monsters
158 * No Dialogue
159 *
160 * @param name The name of the Trainer
161 */
162 public Trainer(String name)
163 {
164 this.dir = Grid.DOWN;
165 this.behind = new Grass();
166 enterable = false;
167 type = MapObject.TRAINER;
168 activeMon = 0;
169 money = 0;
170 this.name = name;
171 badge = name;
172 mon = new Vector();
173 hmon = new Vector();
174 items = new Vector();
175 }
176 /**
177 * Constructs a Trainer with Name name,
178 * Standing on behind, and facing dir.
179 * No money,
180 * Badge of Name
181 * No Monsters
182 * No Dialogue
183 *
184 * @param name The Name of the Trainer
185 * @param behind The MapObject standing on
186 * @param dir The direction facing, specified in Grid
187 * @see Grid
188 */
189 public Trainer(String name, MapObject behind, int dir)
190 {
191 if(!setDirection(dir))
192 this.dir = Grid.DOWN;
193 if(behind.canBeEntered())
194 this.behind = behind;
195 else
196 this.behind = new Grass();
197 enterable = false;
198 type = MapObject.TRAINER;
199 activeMon = 0;
200 money = 0;
201 this.name = "name";
202 badge = name;
203 mon = new Vector();
204 hmon = new Vector();
205 items = new Vector();
206 }
207
208 /**
209 * Sets the static Image for this class.
210 * Requires a call to this before a call to paint.
211 *
212 * @param img the Image of the Trainer
213 */
214 public void setImage(Image[] img)
215 {
216 if(img.length == 4)
217 {
218 this.img = img[0];
219 this.bimg = img[1];
220 this.limg = img[2];
221 this.rimg = img[3];
222 }
223 }
224 /**
225 * Sets the name of the Trainer.
226 *
227 * @param name The Name of the Trainer, must be unique on a Grid.
228 */
229 public void setName(String name)
230 {
231 this.name = name;
232 }
233 /**
234 * Sets the name of the badge carried by the Trainer.
235 *
236 * @param name The Name of the Badge carried, set to "" to stop Thread
237 */
238 public void setBadge(String bad) { if(bad != null) badge = bad; }
239 /**
240 * Gets the name of the Badge carried.
241 *
242 * @return The name of the badge carried.
243 */
244 public String getBadge() { return badge; }
245 /**
246 * Returns a clone of this, a Trainer with the same
247 * name and direction facing.
248 *
249 * @return A new MapObject of this.
250 */
251 public synchronized MapObject getClone() { return new Trainer(name, behind, dir); }
252 /**
253 * Determines the Player that is next to this, that
254 * are within one square any direction including diagonally.
255 *
256 * @return The MapObject of the Player near this.
257 * @param i the i'th location in Grid.
258 * @param j the j'th location in Grid.
259 * @see Grid
260 * @see Player
261 * @see People
262 */
263 public synchronized MapObject peopleNextTo(int i, int j)
264 {
265 MapObject mptemp;
266 int k = 0;
267 for (int x = pi - 1; x <= pi + 1; x++)
268 {
269 for (int y = pj - 1; y <= pj + 1; y++)
270 {
271 if (x != pi || y != pj)
272 {
273 if ( g.isValidLocation(x, y) )
274 {
275 try
276 {
277 mptemp = g.mapObjectAt(x, y);
278 if(mptemp.getType() == MapObject.PERSON || mptemp.getType() == MapObject.PLAYER)
279 {
280 if( !( ((People)mptemp).getName().equals(t.getName()) ) && !( ((People)mptemp).isBattling() ) )
281 return mptemp;
282 }
283 }
284 catch(Exception e) { } // Normal to have happen
285 }
286 }
287 }
288 }
289 return null;
290 }
291 /**
292 * Sets the Grid that this is on, and starts the Thread
293 * for checking if a Player enters the sight of this.
294 *
295 * @param g the Grid this is on, set to null to stop this Thread
296 */
297 public synchronized void setGrid(Grid g)
298 {
299 this.g = g;
300 try
301 {
302 Location l = g.locationOf(this);
303 pi = l.getI();
304 pj = l.getJ();
305 t = new Thread(this);
306 t.start();
307 }
308 catch(Exception e) { }
309 }
310 /**
311 * Check if there is a Player to battle, and sleep
312 * for one second between checks. Ends when this.grid is null or
313 * the name of this.badge is not "".
314 */
315 public void run()
316 {
317 while(g != null && !badge.equals(""))
318 {
319 if(!battling)
320 {
321 synchronized(g)
322 {
323 MapObject mp = peopleNextTo(pi, pj);
324 if(mp != null)
325 {
326 if(mp.getType() == MapObject.PLAYER)
327 {
328 if(((Player)mp).hasBadge(badge))
329 {
330 return;
331 }
332 battling = true;
333 g.beginBattleWithPlayer(this);
334 }
335 }
336 }
337 }
338 try
339 {
340 Thread.sleep(1000);
341 }
342 catch(Exception e) { }
343 }
344 }
345 /**
346 * Draws the image associated with Trainer, and the MapObject this is on.
347 *
348 * @see MapObject
349 */
350 public void paint(Graphics g)
351 {
352 behind.paint(g);
353
354 int wi, he;
355 switch(dir)
356 {
357 case Grid.LEFT:
358 {
359 g.drawImage(limg, 0, 0, this);
360 break;
361 }
362 case Grid.RIGHT:
363 {
364 g.drawImage(rimg, 0, 0, this);
365 break;
366 }
367 case Grid.UP:
368 {
369 g.drawImage(bimg, 0, 0, this);
370 break;
371 }
372 default:
373 {
374 g.drawImage(img, 0, 0, this);
375 break;
376 }
377 }
378 }
379 }