-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.java
More file actions
134 lines (113 loc) · 2.82 KB
/
Character.java
File metadata and controls
134 lines (113 loc) · 2.82 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* A class to represent character.
*
* Used to subclass Player and NPC.
*
* @author Thomas Vakili
* @version 2013.12.14
*/
public abstract class Character {
private String name;
private Room currentRoom;
private boolean alive;
// Fighting variables
private boolean blocking;
private int hp;
public Character(String name, Room room) {
this.name = name;
alive = true;
currentRoom = room;
hp = 100; // MAGICAL CONSTANT
}
/**
* Moves the character to a new room.
*
* @param direction what direction to move in
* @return whether or not the move succeeded
*/
public boolean move(Direction direction) {
Room newRoom = currentRoom.getExit(direction);
if(newRoom != null) {
setRoom(newRoom);
return true;
}
return false;
}
/**
* This method is used in fights to get an action.
*
* @param enemy the enemy the Character is fighting
* @return the action the Character chooses
*/
public abstract Action getAction(Character enemy);
/**
* This method is called when a character dies.
*/
public void die() {
alive = false;
}
/**
* Deal damage to a target.
*
* TODO: The amount of damage "sent" should be calculated
* from some form of "strength".
*
* @param target the target to attack
* @return the amount of damage dealt
*/
public int attack(Character target) {
blocking = false; // Is this necessary? Unsure.
int damageDealt = target.takeDamage(10);
return damageDealt;
}
/**
* Deal damage to a target.
*
* TODO: The amount of damage taken should be calculated
* from some form of "strength".
*
* @param amount the amount of damage "sent" by the dealer
* @return the amount of damage taken
*/
public int takeDamage(int amount) {
int oldHP = hp;
if (blocking) {
amount /= 2;
blocking = false;
}
hp -= amount;
if (hp < 0) {
hp = 0;
}
return oldHP - hp;
}
/**
* Sets the character's blocking field to true. This lowers the amount
* of damage taken when attacked.
*/
public void block() {
blocking = true;
}
/**
* Method to run after a battle is finished.
*/
public void battleOver() {
blocking = false;
hp = 100; // MAGICAL CONSTANT
}
public int getHP() {
return hp;
}
public String getName() {
return name;
}
public Room getRoom() {
return currentRoom;
}
protected void setRoom(Room room) {
currentRoom = room;
}
public boolean isAlive() {
return alive;
}
}