-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
182 lines (121 loc) · 3.41 KB
/
Player.java
File metadata and controls
182 lines (121 loc) · 3.41 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
177
178
179
180
181
182
class Player {
private static int numPlayers = 0;
private String name;
private int hp;
private int direction;
private int x;
private int y;
private int z;
public Player() {
this("P" + (numPlayers + 1), 0, 0, 0, 20, 1); // default constructor
}
public Player(String name, int x, int y, int z) {
this(name, x, y, z, 20, 1);
}
public Player(String name, int x, int y, int z, int hp, int direction) {
this.name = name;
this.x = x;
this.y = y;
this.z = z;
setHp(hp);
if (direction >= 1 && direction <= 6){
this.direction = direction;
} else {
this.direction = 1;
}
//setDirection(direction);
numPlayers += 1;
}
// Gets the total number of players
public static int getNumPlayers() {
return numPlayers;
}
// Gets the x-coordinate of the current player
public int getX() {
return this.x;
}
// Gets the y-coordinate of the current player
public int getY() {
return this.y;
}
// Gets the z-coordinate of the current player
public int getZ() {
return this.z;
}
// Gets the current health of the player
public int getHp() {
return this.hp;
}
// Gets the direction of the player
public int getDirection() {
return this.direction;
}
// Sets the health of the player
public void setHp(int hp) {
if (hp <0) {
this.hp = 0;
}else {
this.hp = hp;
}
}
// Moves the player by a given number of units in the specified direction
public void move(int direction, int units) {
if (direction == 1) { this.x += units; }
else if (direction == 2) { this.x -= units; }
else if (direction == 3) { this.y += units; }
else if (direction == 4) { this.y -= units; }
else if (direction == 5) { this.z += units; }
else if (direction == 6) { this.z -= units; }
}
// Teleport the player to the given coordinates
public void teleport(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
//Teleport the player to the coordinates of the given player
public void teleport(Player player) {
this.x = player.x;
this.y = player.y;
this.z = player.z;
}
// Gets the distance between this player and another player
public double getDistance(Player player) {
return Math.sqrt(Math.pow(player.getX() - this.x, 2) +
Math.pow(player.getY() - this.y, 2) +
Math.pow(player.getZ() - this.z, 2));
}
// Gets the distance between this player and the given coordinates
public double getDistance(int x, int y, int z) {
return Math.sqrt(Math.pow(x - this.x, 2) +
Math.pow(y - this.y, 2) +
Math.pow(z - this.z, 2));
}
// Gets the player as a string in the specified format
public String toString() {
return "Name: " + this.name +
"\nHealth: " + this.hp +
"\nCoordinates: X " + this.x + " Y " + this.y + " Z " + this.z +
"\nDirection: " + this.direction;
}
// Subtracts the damage dealt from the given player's hp, and adds half the
// damage dealt to the attacking players hp
public void attack(Player player, int damage) {
player.hp -= damage;
if (player.hp < 0) {
player.hp = 0;
}
this.hp += damage / 2;
}
// Sets the direction that the player is facing
public void setDirection(int direction) {
if (direction >= 1 && direction <= 6){
this.direction = direction;
}
}
// Gets the player as a string in the specified format
// Gets the player's name
public String getName() {
return this.name;
}
}