-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actor.pde
311 lines (276 loc) · 7.65 KB
/
Actor.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright 2017 Billy Brown
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import java.util.Date;
import java.util.Queue;
/*
* The base character class for the game,
* for monsters, allies and players alike.
*/
class Actor {
public static final float ROT_SPEED = PI / 16;
public final String name;
public final Race race;
public final ActorAI ai = new ActorAI();
public final Inventory inventory;
Queue<PVector> path;
public boolean flee = false;
public long invuln = 0;
final Stats stats;
int score = 0;
int xp = 0;
int level = 1;
int health;
int maxHealth;
PVector position;
float rotation = 0;
Actor(final String name, final Race race, final int health) {
this.name = name;
this.race = race;
stats = new Stats(race);
inventory = new Inventory(50 + stats.STR() * 10);
maxHealth = health;
this.health = health;
}
public void update() {
// The AI might have a new path
if (ai.path != null) {
path = ai.path;
ai.path = null;
}
// If there is a path to follow
if (path != null) {
// Get the next point
final PVector next = path.peek();
// If the actor is in range of the point, pop it
if (inRange(next)) {
path.poll();
// If the path is finished, remove it and do not move
if (path.isEmpty()) {
path = null;
return;
}
}
// Move towards the next point
final PVector distance = next.get().sub(position);
// Rotate towards the target orientation
rotate(atan2(distance.y, distance.x));
// Move in the correct direction (faster if fleeing)
distance.normalize();
final float moveSpeed = speed() + (flee ? 1 : 0);
position.x += moveSpeed * distance.x;
position.y += moveSpeed * distance.y;
}
}
private void rotate(final float orientation) {
// Pointing in a good enough direction
if (abs(orientation - rotation) <= ROT_SPEED) {
rotation = orientation;
return;
}
// Turn
if (orientation < rotation) {
rotation += (rotation - orientation < PI) ? -ROT_SPEED : ROT_SPEED;
} else {
rotation += (orientation - rotation < PI) ? ROT_SPEED : -ROT_SPEED;
}
// Remain in bounds
if (rotation > PI) {
rotation -= 2 * PI;
} else if (rotation < -PI) {
rotation += 2 * PI;
}
}
public boolean inRange(final PVector pos) {
// Always in range of no target
return pos == null ||
// a^2 + b^2 = c^2
pow(position.x - pos.x, 2) +
pow(position.y - pos.y, 2) <=
// Max allowed distance from target
pow(race.size.radius, 2);
}
public void draw(final PVector offset) {
drawSelf(offset);
drawName(offset);
drawHealth(offset);
}
private void drawSelf(final PVector offset) {
// Draw the actor as a circle
final float diameter = race.size.radius * 2;
ellipse(position.x + offset.x, position.y + offset.y, diameter, diameter);
// Draw an eye to show where it is pointing
fill(0);
ellipse(
position.x + offset.x + race.size.radius / 2 * cos(rotation),
position.y + offset.y + race.size.radius / 2 * sin(rotation),
race.size.radius, race.size.radius);
fill(255);
}
private void drawName(final PVector offset) {
// Display the actor's name
textAlign(CENTER, BOTTOM);
textSize(FONT_SMALL);
text(name, position.x + offset.x, position.y - race.size.radius + offset.y);
}
private void drawHealth(final PVector offset) {
textAlign(CENTER, TOP);
textSize(FONT_SMALL);
// Health should always be <= maxHealth
health = min(health, getMaxHealth());
text(health + "/" + getMaxHealth(),
position.x + offset.x, position.y + race.size.radius + offset.y);
}
public void testLevelUp() {
if (xp >= getNextXP()) {
levelUp();
}
}
public void levelUp() {
// Decrease xp
xp -= getNextXP();
// Increase the level
++level;
// Gain additional health
final int addedHealth = Dice.D12() + getStats().CON();
maxHealth += addedHealth;
health += addedHealth;
// Gain to stats and attack calculated when fetched
}
// Get the character's movement speed
public float speed() {
float s = race.size.speed;
// Apply any equipped item effects
for (final Item item : inventory.getEquipped()) {
if (item.effect == Item.Modifier.SPEED) {
s += item.modifier;
}
}
return max(1, s);
}
public int getNextXP() {
// Increase quadratically
// 1 -> 2: 110xp
// 2 -> 3: 240xp
// 3 -> 4: 390xp
// 4 -> 5: 560xp
// ...
return level * 100 + (int) pow(level, 2) * 10;
}
public Stats getStats() {
// Stats increase every 4 levels
int STR = stats.STR() + level / 4;
int DEX = stats.DEX() + level / 4;
int CON = stats.CON() + level / 4;
// Apply any equipped item effects
for (final Item item : inventory.getEquipped()) {
if (item.effect == Item.Modifier.STATS) {
STR += item.modifiers.STR();
DEX += item.modifiers.DEX();
CON += item.modifiers.CON();
}
}
return new Stats(STR, DEX, CON);
}
public int getMaxHealth() {
int h = maxHealth;
// Apply any equipped item effects
for (final Item item : inventory.getEquipped()) {
if (item.effect == Item.Modifier.HEALTH) {
h += item.modifier;
}
}
return max(0, h);
}
public int getHealth() {
return min(health, getMaxHealth());
}
public int getAttack() {
int attack = level / 2;
// Apply any equipped item effects
for (final Item item : inventory.getEquipped()) {
if (item.effect == Item.Modifier.ATTACK) {
attack += item.modifier;
}
}
// STR is applied to attack
attack += getStats().STR();
// Cannot have negative attack
return max(0, attack);
}
public int getDefence() {
int defence = 10;
// Apply any equipped item effects
for (final Item item : inventory.getEquipped()) {
if (item.effect == Item.Modifier.DEFENCE) {
defence += item.modifier;
}
}
// DEX is applied to attack (dodging)
defence += getStats().DEX();
// Cannot have negative defence
return max(0, defence);
}
// Damage the character
public void damage(final int damage) {
health -= damage;
}
// Is the actor alive?
public boolean alive() {
return health > 0;
}
public void gainXP(final int amount) {
xp += amount;
// The total xp achieved is the score
score += amount;
// The actor may level up
testLevelUp();
}
public void stop() {
path = null;
}
public boolean hasTarget() {
return path != null && !path.isEmpty();
}
public void takePotion(final Item item) {
if (Inventory.isPotion(item)) {
// Apply the potion
health = min(health + item.modifier, getMaxHealth());
// Consume the potion
inventory.dropItem(item);
}
}
public List<Item> getPotions() {
final List<Item> potions = new LinkedList<Item>();
for (final Item item : inventory.getItems()) {
if (Inventory.isPotion(item)) {
potions.add(item);
}
}
return potions;
}
public void setPosition(final PVector pos) {
position = pos.get();
}
public PVector getPosition() {
return position.get();
}
public int getXP() {
return xp;
}
public int getLevel() {
return level;
}
public int getScore() {
return score;
}
public float getRotation() {
return rotation;
}
public boolean isInvuln() {
return millis() <= invuln;
}
}