-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameCharacter.java
More file actions
176 lines (139 loc) · 4.07 KB
/
GameCharacter.java
File metadata and controls
176 lines (139 loc) · 4.07 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
*
* @author Sanjeeb Sangraula
* This abstract class represents a GameCharacter
*
*/
public abstract class GameCharacter {
private String mName;
private int mHealthPoints;
private int mX;
private int mY;
private boolean mIsInactive = false;
private String mType;
/**
* Creates a new GameCharacter with the name { @code name }, { @code x } as the x-coordinate and the { @code y } as the y-coordinate
* @param name the name of the GameCharacter
* @param x the initial x-coordinate of the GameCharacter
* @param y the initial y-coordinate of the GameCharacter
* @param type the type of the GameCharacter
*/
public GameCharacter (String name, int x, int y, String type) {
mHealthPoints = 100;
mName = name;
mX = x;
mY = y;
mType = type;
}
/**
* Return the character's name
* @return the name of the GameCharacter
*/
public String getName() {
return mName;
}
/**
* Return the character's health points
* @return the health points of the GameCharacter
*/
public int getHealthPoints() {
return mHealthPoints;
}
/**
* Return the character's x-coordinate
* @return the current x-coordinate of the GameCharacter
*/
public int getX() {
return mX;
}
/**
* Return the character's y-coordinate
* @return the current y-coordinate of the GameCharacter
*/
public int getY() {
return mY;
}
/**
* Return the distance from this character to the { @code character }
* @param character the character whose distance from this GameCharacter is to be calculated
* @return the distance between this character and { @code character }
*/
public double getDistanceFrom(GameCharacter character) {
double xDistance = Math.pow((character.getX() - this.getX()), 2);
double yDistance = Math.pow((character.getY() - this.getY()), 2);
double distance = Math.sqrt(xDistance + yDistance);
return distance;
}
/**
* Move the GameCharacter in the { @code direction } for { @code distance }
* @param direction the direction where the GameCharacter is supposed to move
* @param distance the distance to move the GameCharacter
*/
public void move(char direction, int distance) {
if (this.isInActive()) {
return;
}
switch (direction) {
case 'N':
mY += distance;
break;
case 'S':
mY -= distance;
break;
case 'E':
mX += distance;
break;
case 'W':
mX -= distance;
break;
}
}
/**
* Abstract method to attack to be implemented in sub class
* @param target the GameCharacter which this GameCharacter is supposed to attack
* @return true if this GameCharacter attacked target, false otherwise
*/
public abstract boolean attack(GameCharacter target);
/**
*
* @param isInactive true if this GameCharacter is active, false otherwise
*/
public void setInactive(boolean isInactive){
mIsInactive = isInactive;
}
/**
* Decreases the health points by { @code decreaseBy }
* @param decreaseBy the number by which we are supposed to decrease the health points of this GameCharacter
*/
public void decreaseHealthPoints(int decreaseBy) {
mHealthPoints -= decreaseBy;
if (mHealthPoints < 10) {
this.setInactive(true);
}
if (mHealthPoints < 0) {
mHealthPoints = 0;
}
}
/**
* Return if the GameCharacter is active
* @return true if this GameCharacter is active, false if it is inactive
*/
public boolean isInActive() {
return mIsInactive;
}
/**
* Return the type of the GameCharacter
* @return the type of the GameCharacter
*/
public String getType(){
return mType;
}
/**
* Return a summary of the GameCharacter
* @return a summary of this GameCharacter in string representation
*/
public String toString() {
String string = "Name : " + this.getName() + " Type : " + this.getType() + " Health Points : " + this.getHealthPoints() + " X Coordinate : " + this.getX() + " Y Coordinate : " + this.getY();
return string;
}
}