-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlayer.java
More file actions
29 lines (24 loc) · 832 Bytes
/
Player.java
File metadata and controls
29 lines (24 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/***
* A player in the game.
*
* @author David
*/
public class Player {
private Room currentRoom; // room player is in
private Location loc; // location of the player in the room
public Player(Room r) {
currentRoom = r;
loc = new Location(currentRoom.getHeight()/2, currentRoom.getWidth()/2);
currentRoom.put(loc.row, loc.col, Game.PLAYER);
}
// returns true if player was able to move in that direction.
public boolean move(int direction) {
Location moveTo = Location.locationInDirection(loc, direction);
if (currentRoom.isEmpty(moveTo.row, moveTo.col)) {
currentRoom.moveElementAt(loc, direction);
loc = moveTo; // update own location
return true;
}
return false;
}
}