-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractCombatEntity.java
70 lines (59 loc) · 1.7 KB
/
AbstractCombatEntity.java
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
package com.valkarinc.combat;
public abstract class AbstractCombatEntity implements Combat {
protected int health;
protected int maxHealth;
protected int attackSpeed;
protected int attackDamage;
protected int lastAttackTick;
protected CombatStats stats;
public AbstractCombatEntity(int maxHealth, int attackSpeed, int attackDamage) {
this.maxHealth = maxHealth;
this.health = maxHealth;
this.attackSpeed = attackSpeed;
this.attackDamage = attackDamage;
this.lastAttackTick = 0;
this.stats = new CombatStats();
}
@Override
public void attack(Combat target) {
if (target == null || !target.isAlive() || !isAlive()) {
return;
}
target.defend(calculateDamage());
setLastAttackTick(CombatSystem.getCurrentTick());
stats.incrementAttacks();
}
@Override
public void defend(int damage) {
if (!isAlive()) return;
int actualDamage = Math.max(0, damage - calculateDefense());
health = Math.max(0, health - actualDamage);
stats.addDamageTaken(actualDamage);
}
protected abstract int calculateDamage();
protected abstract int calculateDefense();
@Override
public boolean isAlive() {
return health > 0;
}
@Override
public int getHealth() {
return health;
}
@Override
public int getMaxHealth() {
return maxHealth;
}
@Override
public int getAttackSpeed() {
return attackSpeed;
}
@Override
public int getLastAttackTick() {
return lastAttackTick;
}
@Override
public void setLastAttackTick(int tick) {
this.lastAttackTick = tick;
}
}