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
20 public class Trainer extends People implements Runnable
21 {
22
26 protected static Image img, bimg, limg, rimg;
30 protected String badge;
31
34 int pi, pj;
35
38 protected Grid g;
39
42 Thread t;
43
44
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
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
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
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
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
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
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
229 public void setName(String name)
230 {
231 this.name = name;
232 }
233
238 public void setBadge(String bad) { if(bad != null) badge = bad; }
239
244 public String getBadge() { return badge; }
245
251 public synchronized MapObject getClone() { return new Trainer(name, behind, dir); }
252
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) { } }
286 }
287 }
288 }
289 return null;
290 }
291
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
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
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 }