-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameCharacter.java
More file actions
64 lines (56 loc) · 1.84 KB
/
Copy pathGameCharacter.java
File metadata and controls
64 lines (56 loc) · 1.84 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
import java.util.*;
public abstract class GameCharacter {
protected final int playerId;
protected String username;
protected long experience;
protected int damagePoints;
protected int healthPoints;
public GameCharacter(int id, String name, long xp, int dmg, int hp){
if(xp < 0L){
throw new IllegalArgumentException();
}
this.playerId = id;
this.username = name;
this.experience = xp;
this.damagePoints = dmg;
this.healthPoints = hp;
}
public int getPlayerId(){
return this.playerId;
}
public String toString(){
return String.format("%s %d xp",this.username, this.experience);
}
//Method to decrease the player's HP if they suffered an attack
public void takeDamage(int damage){
this.healthPoints -= damage;
if(this.healthPoints < 0){
this.healthPoints = 0;
}
}
//Method to check if the player is dead
public Boolean isDead(){
return (this.healthPoints == 0);
}
//Method to check if the current player's ID is equal to some other ID
public Boolean checkPlayer(int otherPlayerId){
return (this.playerId == otherPlayerId);
}
//Method to calculate and return the level of a player, based on their XP
/**A player's level is calculated like this:
* Take the square root of the player's XP
* divide the result by 10,
* Add 1
* Take the integer part of the result (Math.floor)
*/
public int getPlayerLevel(){
return ((int)(Math.floor((Math.sqrt(this.experience) / 10) + 1)));
}
public String getUsername(){
return this.username;
}
public long getExperience(){
return this.experience;
}
abstract Boolean attack(GameCharacter otherCharacter) throws AttackCanNotBePerformedException;
}