| Player.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 Player, carries Monsters, Items and Badges.
10 *
11 * @see People
12 * @author Matt
13 */
14 public class Player 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 badges acquired
22 */
23 Vector badges;
24
25 /**
26 * Construct a default Player,
27 * Name of Player1
28 * No Money
29 * No Monsters
30 * No Items
31 * No Badges
32 * Standing on Grass
33 * Facing up
34 *
35 * @see Item
36 * @see Monster
37 */
38 public Player()
39 {
40 enterable = false;
41 type = MapObject.PLAYER;
42 activeMon = 0;
43 money = 0;
44 behind = new Grass();
45 name = "Player1";
46 mon = new Vector();
47 hmon = new Vector();
48 items = new Vector();
49 badges = new Vector();
50 }
51 /**
52 * Construct a default Player,
53 * Name of Player1
54 * No Money
55 * No Monsters
56 * No Items
57 * No Badges
58 * Standing on Grass
59 * Facing dir
60 *
61 * @param dir the direction to face, specified in Grid
62 * @see Item
63 * @see Monster
64 * @see Grid
65 */
66 public Player(int dir)
67 {
68 if(!setDirection(dir))
69 this.dir = Grid.DOWN;
70 enterable = false;
71 type = MapObject.PLAYER;
72 activeMon = 0;
73 money = 0;
74 behind = new Grass();
75 name = "Player1";
76 mon = new Vector();
77 hmon = new Vector();
78 items = new Vector();
79 badges = new Vector();
80 }
81 /**
82 * Construct a default Player,
83 * Name of Player1
84 * No Money
85 * No Monsters
86 * No Items
87 * No Badges
88 * Standing on MapObject specified
89 * Facing up
90 *
91 * @param behind the MapObjct to be standing on
92 * @see Item
93 * @see Monster
94 */
95 public Player(MapObject behind)
96 {
97 if(behind.canBeEntered())
98 this.behind = behind;
99 else
100 this.behind = new Grass();
101 enterable = false;
102 type = MapObject.PLAYER;
103 activeMon = 0;
104 money = 0;
105 name = "Player1";
106 mon = new Vector();
107 hmon = new Vector();
108 items = new Vector();
109 badges = new Vector();
110 }
111 /**
112 * Construct a default Player,
113 * Name of Player1
114 * No Money
115 * No Monsters
116 * No Items
117 * No Badges
118 * Standing on the MapObject specifid
119 * Facing the direction specified
120 *
121 * @param behind the MapObject to stand on
122 * @param dir the direction to face as specified in Grid
123 * @see Item
124 * @see Monster
125 * @see Grid
126 */
127 public Player(MapObject behind, int dir)
128 {
129 if(!setDirection(dir))
130 this.dir = Grid.DOWN;
131 if(behind.canBeEntered())
132 this.behind = behind;
133 else
134 this.behind = new Grass();
135 enterable = false;
136 type = MapObject.PLAYER;
137 activeMon = 0;
138 money = 0;
139 name = "Player1";
140 mon = new Vector();
141 hmon = new Vector();
142 items = new Vector();
143 badges = new Vector();
144 }
145 /**
146 * Construct a Player,
147 * Name as specified
148 * No Money
149 * No Monsters
150 * No Items
151 * No Badges
152 * Standing on Grass
153 * Facing up
154 *
155 * @param name the name of this Player, non null
156 * @see Item
157 * @see Monster
158 */
159 public Player(String name)
160 {
161 this.dir = Grid.DOWN;
162 this.behind = new Grass();
163 enterable = false;
164 type = MapObject.PLAYER;
165 activeMon = 0;
166 money = 0;
167 this.name = name;
168 mon = new Vector();
169 hmon = new Vector();
170 items = new Vector();
171 badges = new Vector();
172 }
173 /**
174 * Construct a default Player,
175 * Name as specified
176 * No Money
177 * No Monsters
178 * No Items
179 * No Badges
180 * Standing on as specified
181 * Facing as specified
182 *
183 * @param name the Name of this
184 * @param behind the MapObject standing on
185 * @param dir the Direction facing as specified in Grid
186 * @see Item
187 * @see Monster
188 * @see Grid
189 */
190 public Player(String name, MapObject behind, int dir)
191 {
192 if(!setDirection(dir))
193 this.dir = Grid.DOWN;
194 if(behind.canBeEntered())
195 this.behind = behind;
196 else
197 this.behind = new Grass();
198 enterable = false;
199 type = MapObject.PLAYER;
200 activeMon = 0;
201 money = 0;
202 this.name = name;
203 mon = new Vector();
204 hmon = new Vector();
205 items = new Vector();
206 badges = new Vector();
207 }
208 /**
209 * Sets the Image for this.
210 * Needs to be called before calls to paint
211 *
212 * @param img the array of Image to use, needs be length 4.
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 * Determines if the Player has the badge already.
226 *
227 * @return true if the Player has the badge already, else false
228 * @see Trainer
229 */
230 public boolean hasBadge(String badge)
231 {
232 if(badge == null)
233 return false;
234 for(int i = 0; i < badges.size(); i++)
235 {
236 if(((String)badges.get(i)).equalsIgnoreCase(badge))
237 {
238 System.out.println((String)badges.get(i) + " || " + badge);
239 return true;
240 }
241 }
242 return false;
243 }
244 /**
245 * Adds the badge to the Player.
246 *
247 * @param badge the name of the Badge to add
248 * @see Trainer
249 */
250 public void addBadge(String badge)
251 {
252 if(badge == null)
253 return;
254 if(!hasBadge(badge))
255 badges.add(badge);
256 }
257 /**
258 * Gets the number of Badges acquired.
259 *
260 * @return the number of Badges acquired.
261 */
262 public int getBadgeCount() { return badges.size(); }
263 /**
264 * Returns a default Player as a MapObject
265 *
266 * @see MapObject
267 */
268 public MapObject getClone() { return new Player(); }
269 /**
270 * Draws the Player using the Images passed into setImage.
271 *
272 * @param g the Graphics
273 */
274 public void paint(Graphics g)
275 {
276 behind.paint(g);
277
278 int wi, he;
279 switch(dir)
280 {
281 case Grid.LEFT:
282 {
283 g.drawImage(limg, 0, 0, this);
284 break;
285 }
286 case Grid.RIGHT:
287 {
288 g.drawImage(rimg, 0, 0, this);
289 break;
290 }
291 case Grid.UP:
292 {
293 g.drawImage(bimg, 0, 0, this);
294 break;
295 }
296 default:
297 {
298 g.drawImage(img, 0, 0, this);
299 break;
300 }
301 }
302 }
303 }