@rpgjs/action-battle 5.0.0-beta.2 → 5.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -0
- package/dist/ai.server.d.ts +8 -1
- package/dist/client/index.js +19 -31
- package/dist/client/index10.js +142 -29
- package/dist/client/index11.js +25 -0
- package/dist/client/index12.js +1222 -0
- package/dist/client/index13.js +46 -0
- package/dist/client/index14.js +10 -0
- package/dist/client/index15.js +448 -0
- package/dist/client/index2.js +93 -46
- package/dist/client/index3.js +82 -1329
- package/dist/client/index4.js +305 -344
- package/dist/client/index5.js +36 -291
- package/dist/client/index6.js +99 -95
- package/dist/client/index7.js +78 -61
- package/dist/client/index8.js +57 -65
- package/dist/client/index9.js +97 -62
- package/dist/client.d.ts +3 -2
- package/dist/core/context.d.ts +5 -0
- package/dist/core/defaults.d.ts +81 -0
- package/dist/core/hit.d.ts +2 -0
- package/dist/enemies/factory.d.ts +7 -0
- package/dist/index.d.ts +9 -4
- package/dist/server/index.js +18 -31
- package/dist/server/index10.js +10 -0
- package/dist/server/index2.js +59 -345
- package/dist/server/index3.js +92 -1329
- package/dist/server/index4.js +141 -67
- package/dist/server/index5.js +24 -29
- package/dist/server/index6.js +1219 -62
- package/dist/server/index7.js +37 -0
- package/dist/server/index8.js +46 -0
- package/dist/server/index9.js +447 -0
- package/dist/server.d.ts +5 -3
- package/dist/ui/state.d.ts +20 -3
- package/package.json +5 -5
- package/src/ai.server.ts +91 -24
- package/src/animations.ts +43 -4
- package/src/canvas-engine-shim.ts +4 -0
- package/src/client.ts +122 -2
- package/src/components/action-bar.ce +5 -3
- package/src/components/attack-preview.ce +90 -0
- package/src/config.ts +30 -0
- package/src/core/context.ts +35 -0
- package/src/core/contracts.ts +123 -0
- package/src/core/defaults.ts +162 -0
- package/src/core/hit.spec.ts +58 -0
- package/src/core/hit.ts +66 -0
- package/src/enemies/factory.ts +25 -0
- package/src/index.ts +40 -0
- package/src/server.ts +235 -71
- package/src/targeting.spec.ts +24 -0
- package/src/types/canvas-engine.d.ts +4 -0
- package/src/types.ts +46 -1
- package/src/ui/state.ts +57 -0
package/dist/server/index3.js
CHANGED
|
@@ -1,1331 +1,94 @@
|
|
|
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
|
-
|
|
1
|
+
//#region src/config.ts
|
|
2
|
+
var DEFAULT_ACTION_BATTLE_OPTIONS = {
|
|
3
|
+
ui: {
|
|
4
|
+
actionBar: {
|
|
5
|
+
enabled: false,
|
|
6
|
+
autoOpen: false,
|
|
7
|
+
mode: "both"
|
|
8
|
+
},
|
|
9
|
+
targeting: {
|
|
10
|
+
enabled: true,
|
|
11
|
+
showGrid: true,
|
|
12
|
+
colors: {
|
|
13
|
+
area: 3120887,
|
|
14
|
+
edge: 1796760,
|
|
15
|
+
cursor: 16765286
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
skills: { defaultAoeMask: ["#"] },
|
|
20
|
+
targeting: {
|
|
21
|
+
affects: "events",
|
|
22
|
+
allowEmptyTarget: true
|
|
23
|
+
},
|
|
24
|
+
attack: {
|
|
25
|
+
lockMovement: true,
|
|
26
|
+
lockDurationMs: 350,
|
|
27
|
+
showPreview: true,
|
|
28
|
+
previewDurationMs: 180,
|
|
29
|
+
previewColor: 16774064,
|
|
30
|
+
previewAccentColor: 16777215
|
|
31
|
+
},
|
|
32
|
+
animations: {}
|
|
30
33
|
};
|
|
31
|
-
var
|
|
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
|
-
constructor(event, options = {}) {
|
|
87
|
-
this.target = null;
|
|
88
|
-
this.lastAttackTime = 0;
|
|
89
|
-
this.state = "idle";
|
|
90
|
-
this.stateStartTime = 0;
|
|
91
|
-
this.stunnedUntil = 0;
|
|
92
|
-
this.lastDodgeTime = 0;
|
|
93
|
-
this.comboCount = 0;
|
|
94
|
-
this.comboMax = 3;
|
|
95
|
-
this.chargingAttack = false;
|
|
96
|
-
this.nearbyEnemies = [];
|
|
97
|
-
this.groupUpdateInterval = 0;
|
|
98
|
-
this.patrolWaypoints = [];
|
|
99
|
-
this.currentPatrolIndex = 0;
|
|
100
|
-
this.lastHpCheck = 0;
|
|
101
|
-
this.recentDamageTaken = 0;
|
|
102
|
-
this.damageCheckInterval = 2e3;
|
|
103
|
-
this.isMovingToTarget = false;
|
|
104
|
-
this.lastFacingDirection = null;
|
|
105
|
-
this.behaviorScore = 50;
|
|
106
|
-
this.behaviorMode = "tactical";
|
|
107
|
-
this.behaviorLastUpdate = 0;
|
|
108
|
-
this.behaviorUpdateInterval = 400;
|
|
109
|
-
this.behaviorAssaultThreshold = 65;
|
|
110
|
-
this.behaviorRetreatThreshold = 35;
|
|
111
|
-
this.behaviorMinStateDuration = 600;
|
|
112
|
-
this.behaviorEnabled = false;
|
|
113
|
-
this.moveToCooldown = 400;
|
|
114
|
-
this.lastMoveToTime = 0;
|
|
115
|
-
this.retreatCooldown = 600;
|
|
116
|
-
this.lastRetreatTime = 0;
|
|
117
|
-
event.battleAi = this;
|
|
118
|
-
this.event = event;
|
|
119
|
-
this.enemyType = options.enemyType || "aggressive";
|
|
120
|
-
this.applyEnemyTypeBehavior(options);
|
|
121
|
-
this.attackSkill = options.attackSkill || null;
|
|
122
|
-
this.animations = {
|
|
123
|
-
...getActionBattleOptions().animations,
|
|
124
|
-
...options.animations
|
|
125
|
-
};
|
|
126
|
-
this.attackPatterns = options.attackPatterns || [
|
|
127
|
-
"melee",
|
|
128
|
-
"combo",
|
|
129
|
-
"dashAttack"
|
|
130
|
-
/* DashAttack */
|
|
131
|
-
];
|
|
132
|
-
this.groupBehavior = options.groupBehavior || false;
|
|
133
|
-
this.patrolWaypoints = options.patrolWaypoints || [];
|
|
134
|
-
this.currentPatrolIndex = 0;
|
|
135
|
-
this.onDefeatedCallback = options.onDefeated;
|
|
136
|
-
if (options.behavior) {
|
|
137
|
-
this.behaviorEnabled = true;
|
|
138
|
-
if (options.behavior.baseScore !== void 0) {
|
|
139
|
-
this.behaviorScore = options.behavior.baseScore;
|
|
140
|
-
}
|
|
141
|
-
if (options.behavior.updateInterval !== void 0) {
|
|
142
|
-
this.behaviorUpdateInterval = options.behavior.updateInterval;
|
|
143
|
-
}
|
|
144
|
-
if (options.behavior.minStateDuration !== void 0) {
|
|
145
|
-
this.behaviorMinStateDuration = options.behavior.minStateDuration;
|
|
146
|
-
}
|
|
147
|
-
if (options.behavior.assaultThreshold !== void 0) {
|
|
148
|
-
this.behaviorAssaultThreshold = options.behavior.assaultThreshold;
|
|
149
|
-
}
|
|
150
|
-
if (options.behavior.retreatThreshold !== void 0) {
|
|
151
|
-
this.behaviorRetreatThreshold = options.behavior.retreatThreshold;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
if (options.moveToCooldown !== void 0) {
|
|
155
|
-
this.moveToCooldown = options.moveToCooldown;
|
|
156
|
-
}
|
|
157
|
-
if (options.retreatCooldown !== void 0) {
|
|
158
|
-
this.retreatCooldown = options.retreatCooldown;
|
|
159
|
-
}
|
|
160
|
-
this.setupVision();
|
|
161
|
-
this.startAiBehaviorLoop();
|
|
162
|
-
this.changeState(
|
|
163
|
-
"idle"
|
|
164
|
-
/* Idle */
|
|
165
|
-
);
|
|
166
|
-
this.debugLog("init", `AI created (type=${this.enemyType}, visionRange=${this.visionRange}, attackRange=${this.attackRange})`);
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Log AI debug message for this event
|
|
170
|
-
*/
|
|
171
|
-
debugLog(category, message, data) {
|
|
172
|
-
AiDebug.log(category, this.event.id, message, data);
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Apply enemy type-specific behavior modifiers
|
|
176
|
-
*
|
|
177
|
-
* This only affects AI behavior (cooldowns, ranges, dodge).
|
|
178
|
-
* Stats should be set on the event itself.
|
|
179
|
-
*/
|
|
180
|
-
applyEnemyTypeBehavior(options) {
|
|
181
|
-
switch (this.enemyType) {
|
|
182
|
-
case "aggressive":
|
|
183
|
-
this.attackCooldown = options.attackCooldown ?? 600;
|
|
184
|
-
this.visionRange = options.visionRange ?? 150;
|
|
185
|
-
this.attackRange = options.attackRange ?? 50;
|
|
186
|
-
this.dodgeChance = options.dodgeChance ?? 0.1;
|
|
187
|
-
this.dodgeCooldown = options.dodgeCooldown ?? 3e3;
|
|
188
|
-
this.fleeThreshold = options.fleeThreshold ?? 0.15;
|
|
189
|
-
break;
|
|
190
|
-
case "defensive":
|
|
191
|
-
this.attackCooldown = options.attackCooldown ?? 1500;
|
|
192
|
-
this.visionRange = options.visionRange ?? 120;
|
|
193
|
-
this.attackRange = options.attackRange ?? 60;
|
|
194
|
-
this.dodgeChance = options.dodgeChance ?? 0.5;
|
|
195
|
-
this.dodgeCooldown = options.dodgeCooldown ?? 1500;
|
|
196
|
-
this.fleeThreshold = options.fleeThreshold ?? 0.3;
|
|
197
|
-
break;
|
|
198
|
-
case "ranged":
|
|
199
|
-
this.attackCooldown = options.attackCooldown ?? 1200;
|
|
200
|
-
this.visionRange = options.visionRange ?? 200;
|
|
201
|
-
this.attackRange = options.attackRange ?? 120;
|
|
202
|
-
this.dodgeChance = options.dodgeChance ?? 0.4;
|
|
203
|
-
this.dodgeCooldown = options.dodgeCooldown ?? 2e3;
|
|
204
|
-
this.fleeThreshold = options.fleeThreshold ?? 0.25;
|
|
205
|
-
break;
|
|
206
|
-
case "tank":
|
|
207
|
-
this.attackCooldown = options.attackCooldown ?? 2e3;
|
|
208
|
-
this.visionRange = options.visionRange ?? 100;
|
|
209
|
-
this.attackRange = options.attackRange ?? 50;
|
|
210
|
-
this.dodgeChance = 0;
|
|
211
|
-
this.dodgeCooldown = options.dodgeCooldown ?? 5e3;
|
|
212
|
-
this.fleeThreshold = options.fleeThreshold ?? 0.1;
|
|
213
|
-
break;
|
|
214
|
-
case "berserker":
|
|
215
|
-
this.attackCooldown = options.attackCooldown ?? 800;
|
|
216
|
-
this.visionRange = options.visionRange ?? 180;
|
|
217
|
-
this.attackRange = options.attackRange ?? 55;
|
|
218
|
-
this.dodgeChance = options.dodgeChance ?? 0.15;
|
|
219
|
-
this.dodgeCooldown = options.dodgeCooldown ?? 2500;
|
|
220
|
-
this.fleeThreshold = options.fleeThreshold ?? 0.05;
|
|
221
|
-
break;
|
|
222
|
-
default:
|
|
223
|
-
this.attackCooldown = options.attackCooldown ?? 1e3;
|
|
224
|
-
this.visionRange = options.visionRange ?? 150;
|
|
225
|
-
this.attackRange = options.attackRange ?? 60;
|
|
226
|
-
this.dodgeChance = options.dodgeChance ?? 0.2;
|
|
227
|
-
this.dodgeCooldown = options.dodgeCooldown ?? 2e3;
|
|
228
|
-
this.fleeThreshold = options.fleeThreshold ?? 0.2;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Setup vision detection
|
|
233
|
-
*/
|
|
234
|
-
setupVision() {
|
|
235
|
-
const diameter = this.visionRange * 2;
|
|
236
|
-
this.event.attachShape(`vision_${this.event.id}`, {
|
|
237
|
-
radius: this.visionRange,
|
|
238
|
-
width: diameter,
|
|
239
|
-
height: diameter,
|
|
240
|
-
angle: 360
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Start the AI behavior loop
|
|
245
|
-
*/
|
|
246
|
-
startAiBehaviorLoop() {
|
|
247
|
-
const updateInterval = setInterval(() => {
|
|
248
|
-
if (!this.event.getCurrentMap()) {
|
|
249
|
-
this.destroy();
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
this.updateAiBehavior();
|
|
253
|
-
}, 100);
|
|
254
|
-
this.updateInterval = updateInterval;
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Change AI state with validated transitions
|
|
258
|
-
*/
|
|
259
|
-
changeState(newState) {
|
|
260
|
-
const validTransitions = {
|
|
261
|
-
[
|
|
262
|
-
"idle"
|
|
263
|
-
/* Idle */
|
|
264
|
-
]: [
|
|
265
|
-
"alert",
|
|
266
|
-
"combat"
|
|
267
|
-
/* Combat */
|
|
268
|
-
],
|
|
269
|
-
[
|
|
270
|
-
"alert"
|
|
271
|
-
/* Alert */
|
|
272
|
-
]: [
|
|
273
|
-
"idle",
|
|
274
|
-
"combat"
|
|
275
|
-
/* Combat */
|
|
276
|
-
],
|
|
277
|
-
[
|
|
278
|
-
"combat"
|
|
279
|
-
/* Combat */
|
|
280
|
-
]: [
|
|
281
|
-
"idle",
|
|
282
|
-
"flee",
|
|
283
|
-
"stunned"
|
|
284
|
-
/* Stunned */
|
|
285
|
-
],
|
|
286
|
-
[
|
|
287
|
-
"flee"
|
|
288
|
-
/* Flee */
|
|
289
|
-
]: [
|
|
290
|
-
"idle",
|
|
291
|
-
"combat"
|
|
292
|
-
/* Combat */
|
|
293
|
-
],
|
|
294
|
-
[
|
|
295
|
-
"stunned"
|
|
296
|
-
/* Stunned */
|
|
297
|
-
]: [
|
|
298
|
-
"combat",
|
|
299
|
-
"idle"
|
|
300
|
-
/* Idle */
|
|
301
|
-
]
|
|
302
|
-
};
|
|
303
|
-
if (!validTransitions[this.state].includes(newState)) {
|
|
304
|
-
this.debugLog("state", `INVALID transition ${this.state} -> ${newState}`);
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
this.debugLog("state", `STATE change: ${this.state} -> ${newState}`);
|
|
308
|
-
this.state = newState;
|
|
309
|
-
this.stateStartTime = Date.now();
|
|
310
|
-
switch (newState) {
|
|
311
|
-
case "idle":
|
|
312
|
-
if (this.patrolWaypoints.length > 0) {
|
|
313
|
-
this.startPatrol();
|
|
314
|
-
}
|
|
315
|
-
break;
|
|
316
|
-
case "alert":
|
|
317
|
-
this.event.stopMoveTo();
|
|
318
|
-
break;
|
|
319
|
-
case "combat":
|
|
320
|
-
this.comboCount = 0;
|
|
321
|
-
break;
|
|
322
|
-
case "flee":
|
|
323
|
-
if (this.target) {
|
|
324
|
-
this.fleeFromTarget();
|
|
325
|
-
}
|
|
326
|
-
break;
|
|
327
|
-
case "stunned":
|
|
328
|
-
this.event.stopMoveTo();
|
|
329
|
-
break;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* Main AI behavior update loop
|
|
334
|
-
*/
|
|
335
|
-
updateAiBehavior() {
|
|
336
|
-
const currentTime = Date.now();
|
|
337
|
-
if (this.groupBehavior) {
|
|
338
|
-
this.updateGroupBehavior();
|
|
339
|
-
}
|
|
340
|
-
if (this.state === "stunned") {
|
|
341
|
-
if (currentTime >= this.stunnedUntil) {
|
|
342
|
-
this.changeState(
|
|
343
|
-
"combat"
|
|
344
|
-
/* Combat */
|
|
345
|
-
);
|
|
346
|
-
}
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
if (this.enemyType === "berserker" && this.event.param[MAXHP]) {
|
|
350
|
-
const hpPercent = this.event.hp / this.event.param[MAXHP];
|
|
351
|
-
const berserkerModifier = Math.max(0.3, hpPercent);
|
|
352
|
-
this.attackCooldown = 800 * berserkerModifier;
|
|
353
|
-
}
|
|
354
|
-
if (this.behaviorEnabled) {
|
|
355
|
-
this.updateBehavior(currentTime);
|
|
356
|
-
}
|
|
357
|
-
switch (this.state) {
|
|
358
|
-
case "idle":
|
|
359
|
-
this.updateIdleBehavior();
|
|
360
|
-
break;
|
|
361
|
-
case "alert":
|
|
362
|
-
this.updateAlertBehavior();
|
|
363
|
-
break;
|
|
364
|
-
case "combat":
|
|
365
|
-
this.updateCombatBehavior(currentTime);
|
|
366
|
-
break;
|
|
367
|
-
case "flee":
|
|
368
|
-
this.updateFleeBehavior();
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
this.checkDamageTaken();
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Update idle behavior (patrolling)
|
|
375
|
-
*/
|
|
376
|
-
updateIdleBehavior() {
|
|
377
|
-
if (this.patrolWaypoints.length > 0) {
|
|
378
|
-
const waypoint = this.patrolWaypoints[this.currentPatrolIndex];
|
|
379
|
-
const distance = this.getDistance(this.event, {
|
|
380
|
-
x: () => waypoint.x,
|
|
381
|
-
y: () => waypoint.y
|
|
382
|
-
});
|
|
383
|
-
if (distance < 10) {
|
|
384
|
-
this.currentPatrolIndex = (this.currentPatrolIndex + 1) % this.patrolWaypoints.length;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Update alert behavior
|
|
390
|
-
*/
|
|
391
|
-
updateAlertBehavior() {
|
|
392
|
-
if (this.target) {
|
|
393
|
-
this.faceTarget();
|
|
394
|
-
const distance = this.getDistance(this.event, this.target);
|
|
395
|
-
if (distance <= this.attackRange * 1.5) {
|
|
396
|
-
this.changeState(
|
|
397
|
-
"combat"
|
|
398
|
-
/* Combat */
|
|
399
|
-
);
|
|
400
|
-
}
|
|
401
|
-
} else {
|
|
402
|
-
this.changeState(
|
|
403
|
-
"idle"
|
|
404
|
-
/* Idle */
|
|
405
|
-
);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Update combat behavior
|
|
410
|
-
*/
|
|
411
|
-
updateCombatBehavior(currentTime) {
|
|
412
|
-
if (!this.target) {
|
|
413
|
-
this.debugLog("combat", "No target, returning to idle");
|
|
414
|
-
this.changeState(
|
|
415
|
-
"idle"
|
|
416
|
-
/* Idle */
|
|
417
|
-
);
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
const distance = this.getDistance(this.event, this.target);
|
|
421
|
-
if (distance > this.visionRange * 1.5) {
|
|
422
|
-
this.debugLog("combat", `Target out of range (dist=${distance.toFixed(1)}, maxRange=${(this.visionRange * 1.5).toFixed(1)})`);
|
|
423
|
-
this.target = null;
|
|
424
|
-
this.isMovingToTarget = false;
|
|
425
|
-
this.event.stopMoveTo();
|
|
426
|
-
this.changeState(
|
|
427
|
-
"idle"
|
|
428
|
-
/* Idle */
|
|
429
|
-
);
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
if (this.event.param[MAXHP]) {
|
|
433
|
-
const hpPercent = this.event.hp / this.event.param[MAXHP];
|
|
434
|
-
if (hpPercent <= this.fleeThreshold) {
|
|
435
|
-
this.debugLog("combat", `HP low (${(hpPercent * 100).toFixed(0)}%), fleeing`);
|
|
436
|
-
this.isMovingToTarget = false;
|
|
437
|
-
this.changeState(
|
|
438
|
-
"flee"
|
|
439
|
-
/* Flee */
|
|
440
|
-
);
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
if (this.canDodge() && this.shouldDodge()) {
|
|
445
|
-
this.debugLog("combat", "Attempting dodge");
|
|
446
|
-
this.isMovingToTarget = false;
|
|
447
|
-
this.tryDodge();
|
|
448
|
-
return;
|
|
449
|
-
}
|
|
450
|
-
if (this.behaviorEnabled) {
|
|
451
|
-
if (this.behaviorMode === "tactical") {
|
|
452
|
-
this.handleTacticalMovement(distance);
|
|
453
|
-
} else if (this.behaviorMode === "assault") {
|
|
454
|
-
this.handleAssaultMovement(distance);
|
|
455
|
-
} else if (this.behaviorMode === "retreat") {
|
|
456
|
-
this.isMovingToTarget = false;
|
|
457
|
-
this.fleeFromTarget();
|
|
458
|
-
return;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
if (this.behaviorEnabled && this.behaviorMode === "assault") ;
|
|
462
|
-
else if (this.behaviorEnabled && this.behaviorMode === "tactical") ;
|
|
463
|
-
else if (this.enemyType === "ranged") {
|
|
464
|
-
if (distance < this.attackRange * 0.6) {
|
|
465
|
-
this.debugLog("movement", `Retreating (dist=${distance.toFixed(1)}, minRange=${(this.attackRange * 0.6).toFixed(1)})`);
|
|
466
|
-
this.isMovingToTarget = false;
|
|
467
|
-
this.retreatFromTarget();
|
|
468
|
-
} else if (distance > this.attackRange) {
|
|
469
|
-
if (!this.isMovingToTarget) {
|
|
470
|
-
this.debugLog("movement", `Moving to target (dist=${distance.toFixed(1)}, attackRange=${this.attackRange})`);
|
|
471
|
-
this.isMovingToTarget = true;
|
|
472
|
-
this.requestMoveTo(this.target);
|
|
473
|
-
}
|
|
474
|
-
} else {
|
|
475
|
-
if (this.isMovingToTarget) {
|
|
476
|
-
this.debugLog("movement", `In range, stopping (dist=${distance.toFixed(1)})`);
|
|
477
|
-
this.isMovingToTarget = false;
|
|
478
|
-
this.event.stopMoveTo();
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
} else {
|
|
482
|
-
if (distance > this.attackRange) {
|
|
483
|
-
if (!this.isMovingToTarget) {
|
|
484
|
-
this.debugLog("movement", `Moving to target (dist=${distance.toFixed(1)}, attackRange=${this.attackRange})`);
|
|
485
|
-
this.isMovingToTarget = true;
|
|
486
|
-
this.requestMoveTo(this.target);
|
|
487
|
-
}
|
|
488
|
-
} else {
|
|
489
|
-
if (this.isMovingToTarget) {
|
|
490
|
-
this.debugLog("movement", `In range, stopping (dist=${distance.toFixed(1)})`);
|
|
491
|
-
this.isMovingToTarget = false;
|
|
492
|
-
this.event.stopMoveTo();
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
if (distance <= this.attackRange && currentTime - this.lastAttackTime >= this.attackCooldown) {
|
|
497
|
-
if (!this.chargingAttack) {
|
|
498
|
-
this.debugLog("attack", `Attacking (dist=${distance.toFixed(1)}, cooldown=${this.attackCooldown}ms)`);
|
|
499
|
-
this.selectAndPerformAttack();
|
|
500
|
-
this.lastAttackTime = currentTime;
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
/**
|
|
505
|
-
* Update flee behavior
|
|
506
|
-
*/
|
|
507
|
-
updateFleeBehavior() {
|
|
508
|
-
if (!this.target) {
|
|
509
|
-
this.changeState(
|
|
510
|
-
"idle"
|
|
511
|
-
/* Idle */
|
|
512
|
-
);
|
|
513
|
-
return;
|
|
514
|
-
}
|
|
515
|
-
const distance = this.getDistance(this.event, this.target);
|
|
516
|
-
if (this.event.param[MAXHP]) {
|
|
517
|
-
const hpPercent = this.event.hp / this.event.param[MAXHP];
|
|
518
|
-
if (hpPercent > this.fleeThreshold * 1.5 || distance > this.visionRange * 2) {
|
|
519
|
-
this.changeState(
|
|
520
|
-
"combat"
|
|
521
|
-
/* Combat */
|
|
522
|
-
);
|
|
523
|
-
return;
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
this.fleeFromTarget();
|
|
527
|
-
}
|
|
528
|
-
/**
|
|
529
|
-
* Select and perform an attack pattern
|
|
530
|
-
*/
|
|
531
|
-
selectAndPerformAttack() {
|
|
532
|
-
if (!this.target) return;
|
|
533
|
-
if (this.comboCount > 0 && this.comboCount < this.comboMax) {
|
|
534
|
-
this.debugLog("attack", `Continuing combo (${this.comboCount}/${this.comboMax})`);
|
|
535
|
-
this.performComboAttack();
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
const pattern = this.selectAttackPattern();
|
|
539
|
-
this.debugLog("attack", `Selected pattern: ${pattern}`);
|
|
540
|
-
this.performAttackPattern(pattern);
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* Select attack pattern with weighted probability
|
|
544
|
-
*/
|
|
545
|
-
selectAttackPattern() {
|
|
546
|
-
const weights = {
|
|
547
|
-
[
|
|
548
|
-
"melee"
|
|
549
|
-
/* Melee */
|
|
550
|
-
]: 40,
|
|
551
|
-
[
|
|
552
|
-
"combo"
|
|
553
|
-
/* Combo */
|
|
554
|
-
]: 25,
|
|
555
|
-
[
|
|
556
|
-
"charged"
|
|
557
|
-
/* Charged */
|
|
558
|
-
]: 15,
|
|
559
|
-
[
|
|
560
|
-
"zone"
|
|
561
|
-
/* Zone */
|
|
562
|
-
]: 10,
|
|
563
|
-
[
|
|
564
|
-
"dashAttack"
|
|
565
|
-
/* DashAttack */
|
|
566
|
-
]: 10
|
|
567
|
-
};
|
|
568
|
-
switch (this.enemyType) {
|
|
569
|
-
case "aggressive":
|
|
570
|
-
weights[
|
|
571
|
-
"combo"
|
|
572
|
-
/* Combo */
|
|
573
|
-
] += 20;
|
|
574
|
-
weights[
|
|
575
|
-
"dashAttack"
|
|
576
|
-
/* DashAttack */
|
|
577
|
-
] += 15;
|
|
578
|
-
break;
|
|
579
|
-
case "defensive":
|
|
580
|
-
weights[
|
|
581
|
-
"charged"
|
|
582
|
-
/* Charged */
|
|
583
|
-
] += 25;
|
|
584
|
-
break;
|
|
585
|
-
case "ranged":
|
|
586
|
-
weights[
|
|
587
|
-
"zone"
|
|
588
|
-
/* Zone */
|
|
589
|
-
] += 20;
|
|
590
|
-
break;
|
|
591
|
-
case "tank":
|
|
592
|
-
weights[
|
|
593
|
-
"charged"
|
|
594
|
-
/* Charged */
|
|
595
|
-
] += 30;
|
|
596
|
-
weights[
|
|
597
|
-
"zone"
|
|
598
|
-
/* Zone */
|
|
599
|
-
] += 15;
|
|
600
|
-
break;
|
|
601
|
-
case "berserker":
|
|
602
|
-
weights[
|
|
603
|
-
"combo"
|
|
604
|
-
/* Combo */
|
|
605
|
-
] += 35;
|
|
606
|
-
break;
|
|
607
|
-
}
|
|
608
|
-
let total = 0;
|
|
609
|
-
const available = [];
|
|
610
|
-
this.attackPatterns.forEach((p) => {
|
|
611
|
-
const weight = weights[p] || 10;
|
|
612
|
-
total += weight;
|
|
613
|
-
available.push({ pattern: p, weight });
|
|
614
|
-
});
|
|
615
|
-
let random = Math.random() * total;
|
|
616
|
-
for (const item of available) {
|
|
617
|
-
random -= item.weight;
|
|
618
|
-
if (random <= 0) return item.pattern;
|
|
619
|
-
}
|
|
620
|
-
return this.attackPatterns[0] || "melee";
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* Perform attack pattern
|
|
624
|
-
*/
|
|
625
|
-
performAttackPattern(pattern) {
|
|
626
|
-
switch (pattern) {
|
|
627
|
-
case "melee":
|
|
628
|
-
this.performMeleeAttack();
|
|
629
|
-
break;
|
|
630
|
-
case "combo":
|
|
631
|
-
this.performComboAttack();
|
|
632
|
-
break;
|
|
633
|
-
case "charged":
|
|
634
|
-
this.performChargedAttack();
|
|
635
|
-
break;
|
|
636
|
-
case "zone":
|
|
637
|
-
this.performZoneAttack();
|
|
638
|
-
break;
|
|
639
|
-
case "dashAttack":
|
|
640
|
-
this.performDashAttack();
|
|
641
|
-
break;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
/**
|
|
645
|
-
* Perform melee attack
|
|
646
|
-
* Uses skill if configured, otherwise creates hitbox
|
|
647
|
-
*/
|
|
648
|
-
performMeleeAttack() {
|
|
649
|
-
if (!this.target) return;
|
|
650
|
-
this.faceTarget();
|
|
651
|
-
playActionBattleAnimation("attack", this.event, this.animations, {
|
|
652
|
-
target: this.target
|
|
653
|
-
});
|
|
654
|
-
if (this.attackSkill) {
|
|
655
|
-
try {
|
|
656
|
-
playActionBattleAnimation("castSkill", this.event, this.animations, {
|
|
657
|
-
skill: this.attackSkill,
|
|
658
|
-
target: this.target
|
|
659
|
-
});
|
|
660
|
-
this.event.useSkill(this.attackSkill, this.target);
|
|
661
|
-
} catch (e) {
|
|
662
|
-
this.performBasicHitbox();
|
|
663
|
-
}
|
|
664
|
-
} else {
|
|
665
|
-
this.performBasicHitbox();
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
/**
|
|
669
|
-
* Perform basic hitbox attack when no skill is set
|
|
670
|
-
*/
|
|
671
|
-
performBasicHitbox() {
|
|
672
|
-
if (!this.target) return;
|
|
673
|
-
const eventX = this.event.x();
|
|
674
|
-
const eventY = this.event.y();
|
|
675
|
-
const dx = this.target.x() - eventX;
|
|
676
|
-
const dy = this.target.y() - eventY;
|
|
677
|
-
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
678
|
-
if (dist === 0) return;
|
|
679
|
-
const dirX = dx / dist;
|
|
680
|
-
const dirY = dy / dist;
|
|
681
|
-
const hitboxes = [{
|
|
682
|
-
x: eventX + dirX * 30,
|
|
683
|
-
y: eventY + dirY * 30,
|
|
684
|
-
width: 40,
|
|
685
|
-
height: 40
|
|
686
|
-
}];
|
|
687
|
-
const map = this.event.getCurrentMap();
|
|
688
|
-
map?.createMovingHitbox(hitboxes, { speed: 5 }).subscribe({
|
|
689
|
-
next: (hits) => {
|
|
690
|
-
hits.forEach((hit) => {
|
|
691
|
-
if (hit instanceof RpgPlayer && hit !== this.event) {
|
|
692
|
-
this.applyHit(hit);
|
|
693
|
-
}
|
|
694
|
-
});
|
|
695
|
-
}
|
|
696
|
-
});
|
|
697
|
-
}
|
|
698
|
-
/**
|
|
699
|
-
* Apply hit to target using RPGJS damage system with knockback
|
|
700
|
-
*
|
|
701
|
-
* Calculates damage using RPGJS formula, applies knockback based on
|
|
702
|
-
* equipped weapon's knockbackForce property, and triggers visual effects.
|
|
703
|
-
* Supports hooks for customizing behavior.
|
|
704
|
-
*
|
|
705
|
-
* @param target - The player or entity being hit
|
|
706
|
-
* @param hooks - Optional hooks for customizing hit behavior
|
|
707
|
-
* @returns The hit result containing damage and knockback info
|
|
708
|
-
*
|
|
709
|
-
* @example
|
|
710
|
-
* ```ts
|
|
711
|
-
* // Basic hit
|
|
712
|
-
* this.applyHit(player);
|
|
713
|
-
*
|
|
714
|
-
* // With custom hooks
|
|
715
|
-
* this.applyHit(player, {
|
|
716
|
-
* onBeforeHit(result) {
|
|
717
|
-
* result.knockbackForce *= 1.5; // Increase knockback
|
|
718
|
-
* return result;
|
|
719
|
-
* },
|
|
720
|
-
* onAfterHit(result) {
|
|
721
|
-
* console.log(`Dealt ${result.damage} damage!`);
|
|
722
|
-
* }
|
|
723
|
-
* });
|
|
724
|
-
* ```
|
|
725
|
-
*/
|
|
726
|
-
applyHit(target, hooks) {
|
|
727
|
-
const { damage } = target.applyDamage(this.event);
|
|
728
|
-
const knockbackForce = this.getWeaponKnockbackForce();
|
|
729
|
-
const knockbackDuration = DEFAULT_KNOCKBACK.duration;
|
|
730
|
-
let hitResult = {
|
|
731
|
-
damage,
|
|
732
|
-
knockbackForce,
|
|
733
|
-
knockbackDuration,
|
|
734
|
-
defeated: target.hp <= 0,
|
|
735
|
-
attacker: this.event,
|
|
736
|
-
target
|
|
737
|
-
};
|
|
738
|
-
if (hooks?.onBeforeHit) {
|
|
739
|
-
const modified = hooks.onBeforeHit(hitResult);
|
|
740
|
-
if (modified) {
|
|
741
|
-
hitResult = modified;
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
target.flash({
|
|
745
|
-
type: "tint",
|
|
746
|
-
tint: "red",
|
|
747
|
-
duration: 200,
|
|
748
|
-
cycles: 1
|
|
749
|
-
});
|
|
750
|
-
target.showHit(`-${hitResult.damage}`);
|
|
751
|
-
if (hitResult.knockbackForce > 0) {
|
|
752
|
-
const dx = target.x() - this.event.x();
|
|
753
|
-
const dy = target.y() - this.event.y();
|
|
754
|
-
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
755
|
-
if (distance > 0) {
|
|
756
|
-
const knockbackDirection = {
|
|
757
|
-
x: dx / distance,
|
|
758
|
-
y: dy / distance
|
|
759
|
-
};
|
|
760
|
-
target.knockback(knockbackDirection, hitResult.knockbackForce, hitResult.knockbackDuration);
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
if (hooks?.onAfterHit) {
|
|
764
|
-
hooks.onAfterHit(hitResult);
|
|
765
|
-
}
|
|
766
|
-
return hitResult;
|
|
767
|
-
}
|
|
768
|
-
/**
|
|
769
|
-
* Get knockback force from equipped weapon
|
|
770
|
-
*
|
|
771
|
-
* Retrieves the knockbackForce property from the event's equipped weapon.
|
|
772
|
-
* Falls back to DEFAULT_KNOCKBACK.force if no weapon or property is set.
|
|
773
|
-
*
|
|
774
|
-
* @returns Knockback force value
|
|
775
|
-
*
|
|
776
|
-
* @example
|
|
777
|
-
* ```ts
|
|
778
|
-
* // Weapon with knockbackForce: 80
|
|
779
|
-
* const force = this.getWeaponKnockbackForce(); // 80
|
|
780
|
-
*
|
|
781
|
-
* // No weapon equipped
|
|
782
|
-
* const force = this.getWeaponKnockbackForce(); // 50 (default)
|
|
783
|
-
* ```
|
|
784
|
-
*/
|
|
785
|
-
getWeaponKnockbackForce() {
|
|
786
|
-
try {
|
|
787
|
-
const equipments = this.event.equipments?.() || [];
|
|
788
|
-
for (const item of equipments) {
|
|
789
|
-
const itemData = this.event.databaseById?.(item.id());
|
|
790
|
-
if (itemData?._type === "weapon" && itemData.knockbackForce !== void 0) {
|
|
791
|
-
return itemData.knockbackForce;
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
} catch {
|
|
795
|
-
}
|
|
796
|
-
return DEFAULT_KNOCKBACK.force;
|
|
797
|
-
}
|
|
798
|
-
/**
|
|
799
|
-
* Perform combo attack
|
|
800
|
-
*/
|
|
801
|
-
performComboAttack() {
|
|
802
|
-
if (!this.target) return;
|
|
803
|
-
this.comboCount++;
|
|
804
|
-
this.performMeleeAttack();
|
|
805
|
-
if (this.comboCount < this.comboMax) {
|
|
806
|
-
setTimeout(() => {
|
|
807
|
-
if (this.target && this.state === "combat") {
|
|
808
|
-
this.performComboAttack();
|
|
809
|
-
} else {
|
|
810
|
-
this.comboCount = 0;
|
|
811
|
-
}
|
|
812
|
-
}, 300);
|
|
813
|
-
} else {
|
|
814
|
-
this.comboCount = 0;
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
/**
|
|
818
|
-
* Perform charged attack
|
|
819
|
-
*/
|
|
820
|
-
performChargedAttack() {
|
|
821
|
-
if (!this.target) return;
|
|
822
|
-
this.chargingAttack = true;
|
|
823
|
-
this.faceTarget();
|
|
824
|
-
playActionBattleAnimation(
|
|
825
|
-
"attack",
|
|
826
|
-
this.event,
|
|
827
|
-
this.animations,
|
|
828
|
-
{
|
|
829
|
-
target: this.target
|
|
830
|
-
},
|
|
831
|
-
{ repeat: 2 }
|
|
832
|
-
);
|
|
833
|
-
setTimeout(() => {
|
|
834
|
-
if (!this.target || this.state !== "combat") {
|
|
835
|
-
this.chargingAttack = false;
|
|
836
|
-
return;
|
|
837
|
-
}
|
|
838
|
-
if (this.attackSkill) {
|
|
839
|
-
try {
|
|
840
|
-
playActionBattleAnimation("castSkill", this.event, this.animations, {
|
|
841
|
-
skill: this.attackSkill,
|
|
842
|
-
target: this.target
|
|
843
|
-
});
|
|
844
|
-
this.event.useSkill(this.attackSkill, this.target);
|
|
845
|
-
} catch (e) {
|
|
846
|
-
this.performBasicHitbox();
|
|
847
|
-
}
|
|
848
|
-
} else {
|
|
849
|
-
this.performBasicHitbox();
|
|
850
|
-
}
|
|
851
|
-
this.chargingAttack = false;
|
|
852
|
-
}, 800);
|
|
853
|
-
}
|
|
854
|
-
/**
|
|
855
|
-
* Perform zone attack (360 degrees)
|
|
856
|
-
*/
|
|
857
|
-
performZoneAttack() {
|
|
858
|
-
playActionBattleAnimation("attack", this.event, this.animations, {
|
|
859
|
-
target: this.target ?? void 0
|
|
860
|
-
});
|
|
861
|
-
const eventX = this.event.x();
|
|
862
|
-
const eventY = this.event.y();
|
|
863
|
-
const radius = 50;
|
|
864
|
-
const hitboxes = [];
|
|
865
|
-
const angles = [0, 90, 180, 270];
|
|
866
|
-
angles.forEach((angle) => {
|
|
867
|
-
const rad = angle * Math.PI / 180;
|
|
868
|
-
hitboxes.push({
|
|
869
|
-
x: eventX + Math.cos(rad) * radius,
|
|
870
|
-
y: eventY + Math.sin(rad) * radius,
|
|
871
|
-
width: 40,
|
|
872
|
-
height: 40
|
|
873
|
-
});
|
|
874
|
-
});
|
|
875
|
-
const map = this.event.getCurrentMap();
|
|
876
|
-
map?.createMovingHitbox(hitboxes, { speed: 5 }).subscribe({
|
|
877
|
-
next: (hits) => {
|
|
878
|
-
hits.forEach((hit) => {
|
|
879
|
-
if (hit instanceof RpgPlayer && hit !== this.event) {
|
|
880
|
-
this.applyHit(hit);
|
|
881
|
-
}
|
|
882
|
-
});
|
|
883
|
-
}
|
|
884
|
-
});
|
|
885
|
-
}
|
|
886
|
-
/**
|
|
887
|
-
* Perform dash attack
|
|
888
|
-
*/
|
|
889
|
-
performDashAttack() {
|
|
890
|
-
if (!this.target) return;
|
|
891
|
-
const dx = this.target.x() - this.event.x();
|
|
892
|
-
const dy = this.target.y() - this.event.y();
|
|
893
|
-
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
894
|
-
if (dist === 0) return;
|
|
895
|
-
const dirX = dx / dist;
|
|
896
|
-
const dirY = dy / dist;
|
|
897
|
-
this.faceTarget();
|
|
898
|
-
this.event.dash({ x: dirX, y: dirY }, 10, 200);
|
|
899
|
-
setTimeout(() => {
|
|
900
|
-
if (!this.target || this.state !== "combat") return;
|
|
901
|
-
this.performMeleeAttack();
|
|
902
|
-
}, 200);
|
|
903
|
-
}
|
|
904
|
-
/**
|
|
905
|
-
* Face the current target with hysteresis to prevent animation flickering
|
|
906
|
-
*
|
|
907
|
-
* Uses multiple strategies to prevent flickering:
|
|
908
|
-
* 1. When very close to target (collision), keep current direction
|
|
909
|
-
* 2. When near diagonal, require significant difference to change
|
|
910
|
-
* 3. Only change if direction is clearly wrong (opposite)
|
|
911
|
-
*/
|
|
912
|
-
faceTarget() {
|
|
913
|
-
if (!this.target) return;
|
|
914
|
-
const dx = this.target.x() - this.event.x();
|
|
915
|
-
const dy = this.target.y() - this.event.y();
|
|
916
|
-
const absX = Math.abs(dx);
|
|
917
|
-
const absY = Math.abs(dy);
|
|
918
|
-
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
919
|
-
const minDistanceForDirectionChange = 40;
|
|
920
|
-
if (this.lastFacingDirection && distance < minDistanceForDirectionChange) {
|
|
921
|
-
return;
|
|
922
|
-
}
|
|
923
|
-
let newDirection;
|
|
924
|
-
if (absX >= absY) {
|
|
925
|
-
newDirection = dx >= 0 ? "right" : "left";
|
|
926
|
-
} else {
|
|
927
|
-
newDirection = dy >= 0 ? "down" : "up";
|
|
928
|
-
}
|
|
929
|
-
const hysteresisThreshold = 0.2;
|
|
930
|
-
const ratio = absX > 0 || absY > 0 ? Math.min(absX, absY) / Math.max(absX, absY) : 0;
|
|
931
|
-
if (this.lastFacingDirection && ratio > 1 - hysteresisThreshold) {
|
|
932
|
-
const isOpposite = this.lastFacingDirection === "left" && dx > 20 || this.lastFacingDirection === "right" && dx < -20 || this.lastFacingDirection === "up" && dy > 20 || this.lastFacingDirection === "down" && dy < -20;
|
|
933
|
-
if (!isOpposite) {
|
|
934
|
-
return;
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
this.lastFacingDirection = newDirection;
|
|
938
|
-
this.event.changeDirection(newDirection);
|
|
939
|
-
}
|
|
940
|
-
/**
|
|
941
|
-
* Try to dodge
|
|
942
|
-
*/
|
|
943
|
-
tryDodge() {
|
|
944
|
-
const currentTime = Date.now();
|
|
945
|
-
if (currentTime - this.lastDodgeTime < this.dodgeCooldown) {
|
|
946
|
-
this.debugLog("dodge", `Dodge on cooldown (${this.dodgeCooldown - (currentTime - this.lastDodgeTime)}ms remaining)`);
|
|
947
|
-
return;
|
|
948
|
-
}
|
|
949
|
-
if (Math.random() > this.dodgeChance) {
|
|
950
|
-
this.debugLog("dodge", `Dodge roll failed (chance=${(this.dodgeChance * 100).toFixed(0)}%)`);
|
|
951
|
-
return;
|
|
952
|
-
}
|
|
953
|
-
if (!this.target) return;
|
|
954
|
-
const dx = this.target.x() - this.event.x();
|
|
955
|
-
const dy = this.target.y() - this.event.y();
|
|
956
|
-
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
957
|
-
if (dist === 0) return;
|
|
958
|
-
const dodgeDirX = -dy / dist;
|
|
959
|
-
const dodgeDirY = dx / dist;
|
|
960
|
-
const side = Math.random() > 0.5 ? 1 : -1;
|
|
961
|
-
this.debugLog("dodge", `Dodging (dir=${side > 0 ? "right" : "left"})`);
|
|
962
|
-
this.event.dash({ x: dodgeDirX * side, y: dodgeDirY * side }, 12, 300);
|
|
963
|
-
this.lastDodgeTime = currentTime;
|
|
964
|
-
if (this.enemyType === "defensive" && Math.random() < 0.5) {
|
|
965
|
-
this.debugLog("dodge", "Counter-attack after dodge");
|
|
966
|
-
setTimeout(() => {
|
|
967
|
-
if (this.target && this.state === "combat") {
|
|
968
|
-
this.selectAndPerformAttack();
|
|
969
|
-
}
|
|
970
|
-
}, 400);
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
canDodge() {
|
|
974
|
-
if (this.dodgeChance === 0) return false;
|
|
975
|
-
return Date.now() - this.lastDodgeTime >= this.dodgeCooldown;
|
|
976
|
-
}
|
|
977
|
-
shouldDodge() {
|
|
978
|
-
if (!this.target) return false;
|
|
979
|
-
const distance = this.getDistance(this.event, this.target);
|
|
980
|
-
return distance < this.attackRange * 0.8;
|
|
981
|
-
}
|
|
982
|
-
/**
|
|
983
|
-
* Flee from target
|
|
984
|
-
*/
|
|
985
|
-
fleeFromTarget() {
|
|
986
|
-
if (!this.target) return;
|
|
987
|
-
const dx = this.event.x() - this.target.x();
|
|
988
|
-
const dy = this.event.y() - this.target.y();
|
|
989
|
-
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
990
|
-
if (dist === 0) return;
|
|
991
|
-
const fleeTarget = {
|
|
992
|
-
x: () => this.event.x() + dx / dist * 200,
|
|
993
|
-
y: () => this.event.y() + dy / dist * 200
|
|
994
|
-
};
|
|
995
|
-
this.requestMoveTo(fleeTarget);
|
|
996
|
-
}
|
|
997
|
-
/**
|
|
998
|
-
* Retreat from target (temporary)
|
|
999
|
-
*/
|
|
1000
|
-
retreatFromTarget() {
|
|
1001
|
-
if (!this.target) return;
|
|
1002
|
-
const currentTime = Date.now();
|
|
1003
|
-
if (currentTime - this.lastRetreatTime < this.retreatCooldown) {
|
|
1004
|
-
return;
|
|
1005
|
-
}
|
|
1006
|
-
const dx = this.event.x() - this.target.x();
|
|
1007
|
-
const dy = this.event.y() - this.target.y();
|
|
1008
|
-
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
1009
|
-
if (dist === 0) return;
|
|
1010
|
-
this.event.dash({ x: dx / dist, y: dy / dist }, 8, 200);
|
|
1011
|
-
this.lastRetreatTime = currentTime;
|
|
1012
|
-
}
|
|
1013
|
-
/**
|
|
1014
|
-
* Check damage taken for retreat decision
|
|
1015
|
-
*/
|
|
1016
|
-
checkDamageTaken() {
|
|
1017
|
-
const currentTime = Date.now();
|
|
1018
|
-
if (currentTime - this.lastHpCheck >= this.damageCheckInterval) {
|
|
1019
|
-
this.recentDamageTaken = 0;
|
|
1020
|
-
this.lastHpCheck = currentTime;
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
/**
|
|
1024
|
-
* Start patrol
|
|
1025
|
-
*/
|
|
1026
|
-
startPatrol() {
|
|
1027
|
-
if (this.patrolWaypoints.length === 0) return;
|
|
1028
|
-
const waypoint = this.patrolWaypoints[this.currentPatrolIndex];
|
|
1029
|
-
this.requestMoveTo({ x: () => waypoint.x, y: () => waypoint.y });
|
|
1030
|
-
}
|
|
1031
|
-
/**
|
|
1032
|
-
* Update group behavior
|
|
1033
|
-
*/
|
|
1034
|
-
updateGroupBehavior() {
|
|
1035
|
-
if (!this.groupBehavior) return;
|
|
1036
|
-
this.groupUpdateInterval++;
|
|
1037
|
-
if (this.groupUpdateInterval >= 20) {
|
|
1038
|
-
this.groupUpdateInterval = 0;
|
|
1039
|
-
this.findNearbyEnemies();
|
|
1040
|
-
}
|
|
1041
|
-
if (this.nearbyEnemies.length > 0 && this.target && this.state === "combat") {
|
|
1042
|
-
this.applyFormation();
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
/**
|
|
1046
|
-
* Find nearby enemies
|
|
1047
|
-
*/
|
|
1048
|
-
findNearbyEnemies() {
|
|
1049
|
-
this.nearbyEnemies = [];
|
|
1050
|
-
const map = this.event.getCurrentMap();
|
|
1051
|
-
if (!map) return;
|
|
1052
|
-
const allEvents = Object.values(map.events());
|
|
1053
|
-
const groupRadius = 150;
|
|
1054
|
-
allEvents.forEach((event) => {
|
|
1055
|
-
if (event === this.event) return;
|
|
1056
|
-
const ai = event.battleAi;
|
|
1057
|
-
if (ai && ai.groupBehavior) {
|
|
1058
|
-
const distance = this.getDistance(this.event, event);
|
|
1059
|
-
if (distance <= groupRadius) {
|
|
1060
|
-
this.nearbyEnemies.push(ai);
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
});
|
|
1064
|
-
}
|
|
1065
|
-
/**
|
|
1066
|
-
* Apply formation around target
|
|
1067
|
-
*/
|
|
1068
|
-
applyFormation() {
|
|
1069
|
-
if (!this.target || this.nearbyEnemies.length === 0) return;
|
|
1070
|
-
const totalEnemies = this.nearbyEnemies.length + 1;
|
|
1071
|
-
const angleStep = 2 * Math.PI / totalEnemies;
|
|
1072
|
-
let ourIndex = 0;
|
|
1073
|
-
for (let i = 0; i < this.nearbyEnemies.length; i++) {
|
|
1074
|
-
if (this.nearbyEnemies[i].event.id < this.event.id) {
|
|
1075
|
-
ourIndex++;
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
const angle = angleStep * ourIndex;
|
|
1079
|
-
const formationRadius = this.attackRange * 1.2;
|
|
1080
|
-
const formationX = this.target.x() + Math.cos(angle) * formationRadius;
|
|
1081
|
-
const formationY = this.target.y() + Math.sin(angle) * formationRadius;
|
|
1082
|
-
const distanceToFormation = Math.sqrt(
|
|
1083
|
-
Math.pow(this.event.x() - formationX, 2) + Math.pow(this.event.y() - formationY, 2)
|
|
1084
|
-
);
|
|
1085
|
-
if (distanceToFormation > 20) {
|
|
1086
|
-
this.requestMoveTo({ x: () => formationX, y: () => formationY });
|
|
1087
|
-
}
|
|
1088
|
-
}
|
|
1089
|
-
/**
|
|
1090
|
-
* Handle player entering vision
|
|
1091
|
-
*/
|
|
1092
|
-
onDetectInShape(player, shape) {
|
|
1093
|
-
this.debugLog("vision", `Player ${player.id} entered vision (state=${this.state})`);
|
|
1094
|
-
this.target = player;
|
|
1095
|
-
if (this.state === "idle") {
|
|
1096
|
-
this.changeState(
|
|
1097
|
-
"alert"
|
|
1098
|
-
/* Alert */
|
|
1099
|
-
);
|
|
1100
|
-
} else if (this.state === "alert") {
|
|
1101
|
-
this.changeState(
|
|
1102
|
-
"combat"
|
|
1103
|
-
/* Combat */
|
|
1104
|
-
);
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
/**
|
|
1108
|
-
* Handle player leaving vision
|
|
1109
|
-
*/
|
|
1110
|
-
onDetectOutShape(player, shape) {
|
|
1111
|
-
this.debugLog("vision", `Player ${player.id} left vision (wasTarget=${this.target === player})`);
|
|
1112
|
-
if (this.target === player) {
|
|
1113
|
-
this.target = null;
|
|
1114
|
-
this.isMovingToTarget = false;
|
|
1115
|
-
this.event.stopMoveTo();
|
|
1116
|
-
this.changeState(
|
|
1117
|
-
"idle"
|
|
1118
|
-
/* Idle */
|
|
1119
|
-
);
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
/**
|
|
1123
|
-
* Handle taking damage (called from server.ts)
|
|
1124
|
-
*
|
|
1125
|
-
* This triggers state changes like stun and flee check.
|
|
1126
|
-
* The actual damage is applied externally via RPGJS API.
|
|
1127
|
-
*/
|
|
1128
|
-
takeDamage(attacker) {
|
|
1129
|
-
const { damage } = this.event.applyDamage(attacker);
|
|
1130
|
-
this.debugLog("damage", `Took ${damage} damage from ${attacker.id} (HP: ${this.event.hp}/${this.event.param[MAXHP] || "?"})`);
|
|
1131
|
-
this.event.flash({
|
|
1132
|
-
type: "tint",
|
|
1133
|
-
tint: "red",
|
|
1134
|
-
duration: 200,
|
|
1135
|
-
cycles: 1
|
|
1136
|
-
});
|
|
1137
|
-
this.event.showHit(`-${damage}`);
|
|
1138
|
-
playActionBattleAnimation("hurt", this.event, this.animations, {
|
|
1139
|
-
attacker
|
|
1140
|
-
});
|
|
1141
|
-
this.recentDamageTaken += damage;
|
|
1142
|
-
if (this.state !== "stunned" && this.state !== "flee") {
|
|
1143
|
-
this.debugLog("damage", "Stunned from damage");
|
|
1144
|
-
this.isMovingToTarget = false;
|
|
1145
|
-
this.stunnedUntil = Date.now() + 150;
|
|
1146
|
-
this.changeState(
|
|
1147
|
-
"stunned"
|
|
1148
|
-
/* Stunned */
|
|
1149
|
-
);
|
|
1150
|
-
}
|
|
1151
|
-
if (this.event.hp <= 0) {
|
|
1152
|
-
this.debugLog("damage", "Defeated!");
|
|
1153
|
-
this.kill(attacker);
|
|
1154
|
-
return true;
|
|
1155
|
-
}
|
|
1156
|
-
return false;
|
|
1157
|
-
}
|
|
1158
|
-
/**
|
|
1159
|
-
* Kill this AI
|
|
1160
|
-
*
|
|
1161
|
-
* Stops all movements, cleans up resources, calls the onDefeated hook,
|
|
1162
|
-
* and removes the event from the map.
|
|
1163
|
-
*/
|
|
1164
|
-
kill(attacker) {
|
|
1165
|
-
const dieAnimation = playActionBattleAnimation(
|
|
1166
|
-
"die",
|
|
1167
|
-
this.event,
|
|
1168
|
-
this.animations,
|
|
1169
|
-
{
|
|
1170
|
-
attacker
|
|
1171
|
-
}
|
|
1172
|
-
);
|
|
1173
|
-
const removeDelay = getActionBattleAnimationRemovalDelay(dieAnimation);
|
|
1174
|
-
if (this.onDefeatedCallback) {
|
|
1175
|
-
this.onDefeatedCallback(this.event, attacker);
|
|
1176
|
-
}
|
|
1177
|
-
this.destroy();
|
|
1178
|
-
if (removeDelay > 0) {
|
|
1179
|
-
setTimeout(() => this.event.remove(), removeDelay);
|
|
1180
|
-
} else {
|
|
1181
|
-
this.event.remove();
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1184
|
-
/**
|
|
1185
|
-
* Get distance between entities
|
|
1186
|
-
*/
|
|
1187
|
-
getDistance(entity1, entity2) {
|
|
1188
|
-
const dx = entity1.x() - entity2.x();
|
|
1189
|
-
const dy = entity1.y() - entity2.y();
|
|
1190
|
-
return Math.sqrt(dx * dx + dy * dy);
|
|
1191
|
-
}
|
|
1192
|
-
updateBehavior(currentTime) {
|
|
1193
|
-
if (currentTime - this.behaviorLastUpdate < this.behaviorUpdateInterval) {
|
|
1194
|
-
return;
|
|
1195
|
-
}
|
|
1196
|
-
this.behaviorLastUpdate = currentTime;
|
|
1197
|
-
let score = this.behaviorScore;
|
|
1198
|
-
const maxHp = this.event.param[MAXHP];
|
|
1199
|
-
if (maxHp) {
|
|
1200
|
-
const hpPercent = this.event.hp / maxHp;
|
|
1201
|
-
score += (hpPercent - 0.5) * 40;
|
|
1202
|
-
}
|
|
1203
|
-
if (this.recentDamageTaken > 0) {
|
|
1204
|
-
score -= Math.min(30, this.recentDamageTaken * 0.5);
|
|
1205
|
-
}
|
|
1206
|
-
if (this.target) {
|
|
1207
|
-
const distance = this.getDistance(this.event, this.target);
|
|
1208
|
-
if (distance <= this.attackRange) {
|
|
1209
|
-
score += 10;
|
|
1210
|
-
} else if (distance > this.visionRange) {
|
|
1211
|
-
score -= 10;
|
|
1212
|
-
}
|
|
1213
|
-
}
|
|
1214
|
-
if (this.groupBehavior && this.nearbyEnemies.length > 0) {
|
|
1215
|
-
score += Math.min(15, this.nearbyEnemies.length * 5);
|
|
1216
|
-
}
|
|
1217
|
-
score = Math.max(0, Math.min(100, score));
|
|
1218
|
-
this.behaviorScore = score;
|
|
1219
|
-
const previousMode = this.behaviorMode;
|
|
1220
|
-
if (score >= this.behaviorAssaultThreshold) {
|
|
1221
|
-
this.behaviorMode = "assault";
|
|
1222
|
-
} else if (score <= this.behaviorRetreatThreshold) {
|
|
1223
|
-
this.behaviorMode = "retreat";
|
|
1224
|
-
} else {
|
|
1225
|
-
this.behaviorMode = "tactical";
|
|
1226
|
-
}
|
|
1227
|
-
if (previousMode !== this.behaviorMode) {
|
|
1228
|
-
this.debugLog("state", `Behavior mode: ${previousMode} -> ${this.behaviorMode} (score=${score.toFixed(0)})`);
|
|
1229
|
-
}
|
|
1230
|
-
if (this.behaviorMode === "retreat" && this.state === "combat") {
|
|
1231
|
-
if (currentTime - this.stateStartTime >= this.behaviorMinStateDuration) {
|
|
1232
|
-
this.isMovingToTarget = false;
|
|
1233
|
-
this.changeState(
|
|
1234
|
-
"flee"
|
|
1235
|
-
/* Flee */
|
|
1236
|
-
);
|
|
1237
|
-
}
|
|
1238
|
-
} else if (this.behaviorMode === "assault" && this.state === "flee") {
|
|
1239
|
-
if (currentTime - this.stateStartTime >= this.behaviorMinStateDuration) {
|
|
1240
|
-
this.changeState(
|
|
1241
|
-
"combat"
|
|
1242
|
-
/* Combat */
|
|
1243
|
-
);
|
|
1244
|
-
}
|
|
1245
|
-
}
|
|
1246
|
-
}
|
|
1247
|
-
handleTacticalMovement(distance) {
|
|
1248
|
-
if (!this.target) return;
|
|
1249
|
-
const minRange = this.attackRange * 0.7;
|
|
1250
|
-
const maxRange = this.attackRange * 1.2;
|
|
1251
|
-
if (distance < minRange) {
|
|
1252
|
-
this.debugLog("movement", `Tactical retreat (dist=${distance.toFixed(1)}, minRange=${minRange.toFixed(1)})`);
|
|
1253
|
-
this.isMovingToTarget = false;
|
|
1254
|
-
this.retreatFromTarget();
|
|
1255
|
-
return;
|
|
1256
|
-
}
|
|
1257
|
-
if (distance > maxRange) {
|
|
1258
|
-
if (!this.isMovingToTarget) {
|
|
1259
|
-
this.debugLog("movement", `Tactical approach (dist=${distance.toFixed(1)}, maxRange=${maxRange.toFixed(1)})`);
|
|
1260
|
-
this.isMovingToTarget = true;
|
|
1261
|
-
this.requestMoveTo(this.target);
|
|
1262
|
-
}
|
|
1263
|
-
return;
|
|
1264
|
-
}
|
|
1265
|
-
if (this.isMovingToTarget) {
|
|
1266
|
-
this.debugLog("movement", `Tactical hold (dist=${distance.toFixed(1)})`);
|
|
1267
|
-
this.isMovingToTarget = false;
|
|
1268
|
-
this.event.stopMoveTo();
|
|
1269
|
-
}
|
|
1270
|
-
}
|
|
1271
|
-
handleAssaultMovement(distance) {
|
|
1272
|
-
if (!this.target) return;
|
|
1273
|
-
if (distance > this.attackRange) {
|
|
1274
|
-
if (!this.isMovingToTarget) {
|
|
1275
|
-
this.debugLog("movement", `Assault approach (dist=${distance.toFixed(1)}, attackRange=${this.attackRange})`);
|
|
1276
|
-
this.isMovingToTarget = true;
|
|
1277
|
-
this.requestMoveTo(this.target);
|
|
1278
|
-
}
|
|
1279
|
-
return;
|
|
1280
|
-
}
|
|
1281
|
-
if (this.isMovingToTarget) {
|
|
1282
|
-
this.debugLog("movement", `Assault hold (dist=${distance.toFixed(1)})`);
|
|
1283
|
-
this.isMovingToTarget = false;
|
|
1284
|
-
this.event.stopMoveTo();
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
requestMoveTo(target) {
|
|
1288
|
-
const currentTime = Date.now();
|
|
1289
|
-
if (currentTime - this.lastMoveToTime < this.moveToCooldown) {
|
|
1290
|
-
return false;
|
|
1291
|
-
}
|
|
1292
|
-
this.event.moveTo(target);
|
|
1293
|
-
this.lastMoveToTime = currentTime;
|
|
1294
|
-
return true;
|
|
1295
|
-
}
|
|
1296
|
-
// Public getters
|
|
1297
|
-
getHealth() {
|
|
1298
|
-
return this.event.hp;
|
|
1299
|
-
}
|
|
1300
|
-
getMaxHealth() {
|
|
1301
|
-
return this.event.param[MAXHP];
|
|
1302
|
-
}
|
|
1303
|
-
getTarget() {
|
|
1304
|
-
return this.target;
|
|
1305
|
-
}
|
|
1306
|
-
getState() {
|
|
1307
|
-
return this.state;
|
|
1308
|
-
}
|
|
1309
|
-
getEnemyType() {
|
|
1310
|
-
return this.enemyType;
|
|
1311
|
-
}
|
|
1312
|
-
/**
|
|
1313
|
-
* Clean up
|
|
1314
|
-
*/
|
|
1315
|
-
destroy() {
|
|
1316
|
-
if (this.updateInterval) {
|
|
1317
|
-
clearInterval(this.updateInterval);
|
|
1318
|
-
this.updateInterval = void 0;
|
|
1319
|
-
}
|
|
1320
|
-
this.target = null;
|
|
1321
|
-
this.nearbyEnemies = [];
|
|
1322
|
-
}
|
|
34
|
+
var currentActionBattleOptions = DEFAULT_ACTION_BATTLE_OPTIONS;
|
|
35
|
+
function normalizeActionBattleOptions(options = {}) {
|
|
36
|
+
return {
|
|
37
|
+
ui: {
|
|
38
|
+
actionBar: {
|
|
39
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.ui?.actionBar,
|
|
40
|
+
...options.ui?.actionBar
|
|
41
|
+
},
|
|
42
|
+
targeting: {
|
|
43
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.ui?.targeting,
|
|
44
|
+
...options.ui?.targeting,
|
|
45
|
+
colors: {
|
|
46
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.ui?.targeting?.colors,
|
|
47
|
+
...options.ui?.targeting?.colors
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
skills: {
|
|
52
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.skills,
|
|
53
|
+
...options.skills
|
|
54
|
+
},
|
|
55
|
+
targeting: {
|
|
56
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.targeting,
|
|
57
|
+
...options.targeting
|
|
58
|
+
},
|
|
59
|
+
attack: {
|
|
60
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.attack,
|
|
61
|
+
...options.attack
|
|
62
|
+
},
|
|
63
|
+
animations: {
|
|
64
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.animations,
|
|
65
|
+
...options.animations
|
|
66
|
+
},
|
|
67
|
+
systems: {
|
|
68
|
+
combat: {
|
|
69
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.systems?.combat,
|
|
70
|
+
...options.systems?.combat,
|
|
71
|
+
hooks: {
|
|
72
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.systems?.combat?.hooks,
|
|
73
|
+
...options.systems?.combat?.hooks
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
ai: {
|
|
77
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.systems?.ai,
|
|
78
|
+
...options.systems?.ai,
|
|
79
|
+
behaviors: {
|
|
80
|
+
...DEFAULT_ACTION_BATTLE_OPTIONS.systems?.ai?.behaviors,
|
|
81
|
+
...options.systems?.ai?.behaviors
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
1323
86
|
}
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
};
|
|
87
|
+
function setActionBattleOptions(options) {
|
|
88
|
+
currentActionBattleOptions = options;
|
|
89
|
+
}
|
|
90
|
+
function getActionBattleOptions() {
|
|
91
|
+
return currentActionBattleOptions;
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
export { getActionBattleOptions, normalizeActionBattleOptions, setActionBattleOptions };
|