| Person.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 * Class to define a NPC, carries Monsters, and Items.
10 *
11 * @see People
12 * @author Matt
13 */
14 public class Person extends People
15 {
16 /**
17 * The four Images to paint corresponding to the four directions
18 */
19 protected static Image img, bimg, limg, rimg; // The Four directions
20 /**
21 * The AI to control movement around the Grid
22 *
23 * @see PersonAI
24 * @see Grid
25 */
26 protected PersonAI ai;
27 /**
28 * Construct a default Player,
29 * Name of NPC
30 * No Money
31 * No Monsters
32 * No Items
33 * No Badges
34 * No Dialogue
35 * Standing on Grass
36 * Facing up
37 *
38 * @see Item
39 * @see Monster
40 */
41 public Person()
42 {
43 enterable = false;
44 type = MapObject.PERSON;
45 activeMon = 0;
46 money = 0;
47 behind = new Grass();
48 name = "NPC";
49 ai = new PersonAI(this);
50 mon = new Vector();
51 hmon = new Vector();
52 items = new Vector();
53 activeMon = 0;
54 dlg = "";
55 }
56 /**
57 * Construct a default Player,
58 * Name of NPC
59 * No Money
60 * No Monsters
61 * No Items
62 * No Badges
63 * No Dialogue
64 * Standing on Grass
65 * Facing as specified
66 *
67 * @param dir the direction facing as specified in Grid
68 * @see Grid
69 * @see Item
70 * @see Monster
71 */
72 public Person(int dir)
73 {
74 if(!setDirection(dir))
75 this.dir = Grid.DOWN;
76 enterable = false;
77 type = MapObject.PERSON;
78 activeMon = 0;
79 money = 0;
80 behind = new Grass();
81 name = "NPC";
82 ai = new PersonAI(this);
83 mon = new Vector();
84 hmon = new Vector();
85 items = new Vector();
86 activeMon = 0;
87 dlg = "";
88 }
89 /**
90 * Construct a default Player,
91 * Name of NPC
92 * No Money
93 * No Monsters
94 * No Items
95 * No Badges
96 * No Dialogue
97 * Standing on MapObject specified
98 * Facing up
99 *
100 * @param behind the MapObject to stand on
101 * @see Item
102 * @see Monster
103 */
104 public Person(MapObject behind)
105 {
106 if(behind.canBeEntered())
107 this.behind = behind;
108 else
109 this.behind = new Grass();
110 enterable = false;
111 type = MapObject.PERSON;
112 activeMon = 0;
113 money = 0;
114 name = "NPC";
115 ai = new PersonAI(this);
116 mon = new Vector();
117 hmon = new Vector();
118 items = new Vector();
119 }
120 /**
121 * Construct a default Player,
122 * Name of NPC
123 * No Money
124 * No Monsters
125 * No Items
126 * No Badges
127 * No Dialogue
128 * Standing on MapObject specified
129 * Facing as specified
130 *
131 * @param behind the MapObject to stand on
132 * @param dir the direction facing as specified in Grid
133 * @see Item
134 * @see Monster
135 */
136 public Person(MapObject behind, int dir)
137 {
138 if(!setDirection(dir))
139 this.dir = Grid.DOWN;
140 if(behind.canBeEntered())
141 this.behind = behind;
142 else
143 this.behind = new Grass();
144 enterable = false;
145 type = MapObject.PERSON;
146 activeMon = 0;
147 money = 0;
148 name = "NPC";
149 ai = new PersonAI(this);
150 mon = new Vector();
151 hmon = new Vector();
152 items = new Vector();
153 }
154 /**
155 * Construct a default Player,
156 * Name as specified
157 * No Money
158 * No Monsters
159 * No Items
160 * No Badges
161 * No Dialogue
162 * Standing on Grass
163 * Facing up
164 *
165 * @param name the Name for this, needs to be unique for a Grid
166 * @see Grid
167 * @see Item
168 * @see Monster
169 */
170 public Person(String name)
171 {
172 this.dir = Grid.DOWN;
173 this.behind = new Grass();
174 enterable = false;
175 type = MapObject.PERSON;
176 activeMon = 0;
177 money = 0;
178 this.name = name;
179 ai = new PersonAI(this);
180 mon = new Vector();
181 hmon = new Vector();
182 items = new Vector();
183 }
184 /**
185 * Construct a default Player,
186 * Name as specified
187 * No Money
188 * No Monsters
189 * No Items
190 * No Badges
191 * No Dialogue
192 * Standing on MapObject specified
193 * Facing as specified
194 *
195 * @param name the Name for this, needs to be unique for a Grid
196 * @param behind the MapObject to stand on
197 * @param dir the direction facing as specified in Grid
198 * @see Grid
199 * @see Item
200 * @see Monster
201 */
202 public Person(String name, MapObject behind, int dir)
203 {
204 if(!setDirection(dir))
205 this.dir = Grid.DOWN;
206 if(behind.canBeEntered())
207 this.behind = behind;
208 else
209 this.behind = new Grass();
210 enterable = false;
211 type = MapObject.PERSON;
212 activeMon = 0;
213 money = 0;
214 this.name = "name";
215 ai = new PersonAI(this);
216 mon = new Vector();
217 hmon = new Vector();
218 items = new Vector();
219 }
220
221 /**
222 * Sets the Image for this.
223 * Needs to be called before calls to paint
224 *
225 * @param img the array of Image to use, needs be length 4.
226 */
227 public void setImage(Image[] img)
228 {
229 if(img.length == 4)
230 {
231 this.img = img[0];
232 this.bimg = img[1];
233 this.limg = img[2];
234 this.rimg = img[3];
235 }
236 }
237 /**
238 * Sets th Grid for the PersonAI to use, and initiates the PersonAI.
239 *
240 * @param g the Grid to pass to PersonAI
241 * @see Grid
242 * @see PersonAI
243 */
244 public void setGrid(Grid g)
245 {
246 ai.setGrid(g);
247 }
248 /**
249 * Returns a clone of this having the same name, behind and direction facing.
250 *
251 * @return a clone of this having the same name, behind and direction facing.
252 */
253 public MapObject getClone() { return new Person(name, behind, dir); }
254 /**
255 * Draws the image for this based upon the direction.
256 *
257 * @param g the Graphics
258 */
259 public void paint(Graphics g)
260 {
261 behind.paint(g);
262
263 int wi, he;
264 switch(dir)
265 {
266 case Grid.LEFT:
267 {
268 g.drawImage(limg, 0, 0, this);
269 break;
270 }
271 case Grid.RIGHT:
272 {
273 g.drawImage(rimg, 0, 0, this);
274 break;
275 }
276 case Grid.UP:
277 {
278 g.drawImage(bimg, 0, 0, this);
279 break;
280 }
281 default:
282 {
283 g.drawImage(img, 0, 0, this);
284 break;
285 }
286 }
287 }
288 }