This repository was archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit.pde
125 lines (98 loc) · 2.5 KB
/
Unit.pde
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
class Unit extends CBeing {
protected Game game;
protected int type;
protected int taxonomy;
protected UnitStats stats;
protected Movement movement;
protected UnitAnimator anim;
protected UnitHealth health;
protected UnitHpBar hpBar;
protected JSONObject data;
Unit(int type, int taxonomy, PVector spawnpoint, Game game) {
super(new HRectangle(
spawnpoint.x - UnitUtils.WIDTH * 0.5,
spawnpoint.y - UnitUtils.HEIGHT * 0.5,
UnitUtils.WIDTH,
UnitUtils.HEIGHT
));
this.game = game;
this.taxonomy = taxonomy;
this.type = type;
_extractUnitData();
this.stats = new UnitStats(data);
this.movement = new UnitMovement(this);
this.anim = new UnitAnimator(this);
this.health = new UnitHealth(this);
this.hpBar = new UnitHpBar(this, game);
this.game.register(this);
}
public int getType() {
return type;
}
public int getDirection() {
return movement.getDirection();
}
public UnitStats getStats() {
return stats;
}
public void receiveDmg(int dmg) {
health.receiveDmg(dmg);
}
public boolean isAlive() {
return health.isAlive();
}
public void induceFrostbite() {
health.induceFrostbite();
}
public boolean isFrostbitten() {
return health.isFrostbitten();
}
public int getRemainingHp() {
return health.getRemainingHp();
}
protected String getDataPath() {
return Utils.pluralize(UnitUtils.taxonomyToString(taxonomy)) + "/" + UnitUtils.typeToString(type);
}
public UnitAnimator getAnimator() {
return anim;
}
public Movement getMovement() {
return movement;
}
private void _extractUnitData() {
try {
data = parseJSONObject(
Utils.readFile(
dataPath(UnitUtils.DIR + "/" + getDataPath() + "/" + UnitUtils.DATAFILE)
)
);
} catch(IOException e) {
println(e);
}
}
public void moveTo(PVector dest) {
movement.moveTo(dest);
}
public boolean destReached() {
return movement.destReached();
}
public void update() {
if (isAlive()) {
movement.update();
} else {
_preDeath();
this.unregister();
}
}
public void draw() {
image(anim.animate(), 0, 0); // draw the current animation frame
}
public void unregister() {
game.delete(this);
game.delete(hpBar);
}
public String toString() {
return UnitUtils.typeToString(type) + "(hp=" + getRemainingHp() + "/" + stats.hp + ")";
}
protected void _preDeath() {}
}