1 | package jpacman.model; |
2 | |
3 | /** |
4 | * The player which can walk around on the board, eat food, and die when meeting |
5 | * a monster. |
6 | * |
7 | * @author Arie van Deursen; Jul 28, 2003 |
8 | * @version $Id: Player.java,v 1.8 2008/02/07 17:59:20 arie Exp $ |
9 | */ |
10 | public class Player extends MovingGuest { |
11 | |
12 | /** |
13 | * Amount of food eaten so far. |
14 | */ |
15 | private int pointsEaten; |
16 | |
17 | /** |
18 | * Are we dead or alive? |
19 | */ |
20 | private boolean alive; |
21 | |
22 | /** |
23 | * Most recent movements in x and y direction. |
24 | */ |
25 | private int lastDx, lastDy; |
26 | |
27 | |
28 | /** |
29 | * Create a new player. |
30 | */ |
31 | public Player() { |
32 | pointsEaten = 0; |
33 | alive = true; |
34 | } |
35 | |
36 | /** |
37 | * A player cannot eat negative food. |
38 | * |
39 | * @return true iff the guest invariant holds and the food is non-negative. |
40 | */ |
41 | public boolean playerInvariant() { |
42 | return guestInvariant() && pointsEaten >= 0; |
43 | } |
44 | |
45 | /** |
46 | * Is the player still alive? |
47 | * |
48 | * @return true iff the player is still alive. |
49 | */ |
50 | public boolean living() { |
51 | return alive; |
52 | } |
53 | |
54 | /** |
55 | * Return the amount of food eaten. |
56 | * |
57 | * @return Points collected so far. |
58 | */ |
59 | public int getPointsEaten() { |
60 | return pointsEaten; |
61 | } |
62 | |
63 | /** |
64 | * Increase the amount of food eaten by an extra meal worth a certain amount |
65 | * of points. |
66 | * |
67 | * @param foodPoints |
68 | * calories that should be added. |
69 | */ |
70 | protected void eat(int foodPoints) { |
71 | assert playerInvariant(); |
72 | assert living(); |
73 | pointsEaten += foodPoints; |
74 | assert living(); |
75 | assert playerInvariant(); |
76 | } |
77 | |
78 | /** |
79 | * The player has been killed by a monster -- set the state accordingly. |
80 | * Precondition: not killed before. |
81 | */ |
82 | protected void die() { |
83 | assert playerInvariant(); |
84 | assert living(); |
85 | alive = false; |
86 | assert playerInvariant(); |
87 | } |
88 | |
89 | |
90 | /** |
91 | * Another player wants to occupy this player's state. With one player |
92 | * active this will be impossible, but with multiple players this may |
93 | * happen. In any case, the move itself will not be possible. |
94 | * |
95 | * @param theMove |
96 | * move object representing intended move and its effects. |
97 | * @return false, the player cannot occupy another player's cell. |
98 | * |
99 | * @see jpacman.model.Guest#meetPlayer(jpacman.model.PlayerMove) |
100 | */ |
101 | @Override |
102 | protected boolean meetPlayer(PlayerMove theMove) { |
103 | assert playerInvariant(); |
104 | assert theMove != null; |
105 | assert !theMove.initialized(); |
106 | assert this.equals(theMove.getPlayer()) |
107 | : "Move: only one player supported"; |
108 | return false; |
109 | } |
110 | |
111 | |
112 | /** |
113 | * @see jpacman.model.Guest#guestType() |
114 | * @return character encoding for a player. |
115 | */ |
116 | @Override |
117 | public char guestType() { |
118 | return Guest.PLAYER_TYPE; |
119 | } |
120 | |
121 | /** |
122 | * @return The player's most recent advancement in the x-direction. |
123 | */ |
124 | public int getLastDx() { |
125 | return lastDx; |
126 | } |
127 | |
128 | /** |
129 | * @param dx Most recent advancement in x-direction. |
130 | * @param dy Most recent advancement in y-direction. |
131 | */ |
132 | public void setLastDirection(int dx, int dy) { |
133 | lastDx = dx; |
134 | lastDy = dy; |
135 | } |
136 | |
137 | /** |
138 | * @return The player's most recent advancement in the y-direction. |
139 | */ |
140 | public int getLastDy() { |
141 | return lastDy; |
142 | } |
143 | } |