@rpgjs/action-battle 5.0.0-beta.1 → 5.0.0-beta.3

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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2026 by Samuel Ronce
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/README.md CHANGED
@@ -496,6 +496,100 @@ The module handles player attacks via the `action` input:
496
496
  // Knockback force is based on equipped weapon's knockbackForce property
497
497
  ```
498
498
 
499
+ ## Configurable Combat Animations
500
+
501
+ By default, player and AI attacks keep using the existing `attack` animation:
502
+
503
+ ```ts
504
+ player.setGraphicAnimation("attack", 1);
505
+ ```
506
+
507
+ Use `animations` when your combat sprites are stored in separate graphics such
508
+ as `hero_attack`, `hero_hurt`, or `hero_die`.
509
+
510
+ ```ts
511
+ import { provideActionBattle } from "@rpgjs/action-battle/server";
512
+
513
+ export default provideActionBattle({
514
+ animations: {
515
+ attack: "attack",
516
+ hurt: "hurt",
517
+ die: {
518
+ animationName: "die",
519
+ repeat: 1,
520
+ delayMs: 500
521
+ },
522
+ castSkill: "skill"
523
+ }
524
+ });
525
+ ```
526
+
527
+ For data-driven spritesheets, use resolver functions:
528
+
529
+ ```ts
530
+ provideActionBattle({
531
+ animations: {
532
+ attack: (entity) => ({
533
+ animationName: "walk",
534
+ graphic: entity.combatAnimations?.attack,
535
+ repeat: 1
536
+ }),
537
+ hurt: (entity) => ({
538
+ animationName: "walk",
539
+ graphic: entity.combatAnimations?.hurt,
540
+ repeat: 1
541
+ }),
542
+ die: (entity) => ({
543
+ animationName: "walk",
544
+ graphic: entity.combatAnimations?.die,
545
+ repeat: 1,
546
+ waitEnd: true
547
+ }),
548
+ castSkill: (entity, context) => ({
549
+ animationName: "walk",
550
+ graphic: entity.combatAnimations?.castSkill,
551
+ repeat: 1
552
+ })
553
+ }
554
+ });
555
+ ```
556
+
557
+ When `graphic` is provided, action-battle calls:
558
+
559
+ ```ts
560
+ entity.setGraphicAnimation(animationName, graphic, repeat);
561
+ ```
562
+
563
+ Otherwise it calls:
564
+
565
+ ```ts
566
+ entity.setGraphicAnimation(animationName, repeat);
567
+ ```
568
+
569
+ Return `null` or `undefined` from a resolver to skip the animation. `BattleAi`
570
+ can also override the global configuration per enemy:
571
+
572
+ ```ts
573
+ new BattleAi(this, {
574
+ animations: {
575
+ attack: {
576
+ animationName: "walk",
577
+ graphic: "slime_attack",
578
+ repeat: 1
579
+ },
580
+ die: {
581
+ animationName: "walk",
582
+ graphic: "slime_die",
583
+ repeat: 1,
584
+ delayMs: 700
585
+ }
586
+ }
587
+ });
588
+ ```
589
+
590
+ `waitEnd: true` delays event removal for defeated AI with the default delay used
591
+ by action-battle. Use `delayMs` when you need an exact duration.
592
+
499
593
  ## Knockback System
500
594
 
501
595
  Knockback force is determined by the equipped weapon's `knockbackForce` property.
@@ -1,7 +1,36 @@
1
1
  import { RpgEvent, RpgPlayer } from '@rpgjs/server';
2
+ import { ActionBattleAnimationOptions } from './types';
2
3
  type RpgEventWithBattleAi = RpgEvent & {
3
4
  battleAi: BattleAi;
4
5
  };
6
+ export interface BattleAiOptions {
7
+ enemyType?: EnemyType;
8
+ attackCooldown?: number;
9
+ visionRange?: number;
10
+ attackRange?: number;
11
+ dodgeChance?: number;
12
+ dodgeCooldown?: number;
13
+ fleeThreshold?: number;
14
+ attackSkill?: any;
15
+ attackPatterns?: AttackPattern[];
16
+ patrolWaypoints?: Array<{
17
+ x: number;
18
+ y: number;
19
+ }>;
20
+ groupBehavior?: boolean;
21
+ moveToCooldown?: number;
22
+ retreatCooldown?: number;
23
+ behavior?: {
24
+ baseScore?: number;
25
+ updateInterval?: number;
26
+ minStateDuration?: number;
27
+ assaultThreshold?: number;
28
+ retreatThreshold?: number;
29
+ };
30
+ animations?: ActionBattleAnimationOptions;
31
+ /** Callback called when the AI is defeated */
32
+ onDefeated?: (event: RpgEvent, attacker?: RpgPlayer) => void;
33
+ }
5
34
  /**
6
35
  * Hit result data returned after applying damage
7
36
  *
@@ -221,6 +250,7 @@ export declare class BattleAi {
221
250
  private fleeThreshold;
222
251
  private attackSkill;
223
252
  private attackPatterns;
253
+ private animations?;
224
254
  private comboCount;
225
255
  private comboMax;
226
256
  private chargingAttack;
@@ -271,33 +301,7 @@ export declare class BattleAi {
271
301
  * });
272
302
  * ```
273
303
  */
274
- constructor(event: RpgEventWithBattleAi, options?: {
275
- enemyType?: EnemyType;
276
- attackCooldown?: number;
277
- visionRange?: number;
278
- attackRange?: number;
279
- dodgeChance?: number;
280
- dodgeCooldown?: number;
281
- fleeThreshold?: number;
282
- attackSkill?: any;
283
- attackPatterns?: AttackPattern[];
284
- patrolWaypoints?: Array<{
285
- x: number;
286
- y: number;
287
- }>;
288
- groupBehavior?: boolean;
289
- moveToCooldown?: number;
290
- retreatCooldown?: number;
291
- behavior?: {
292
- baseScore?: number;
293
- updateInterval?: number;
294
- minStateDuration?: number;
295
- assaultThreshold?: number;
296
- retreatThreshold?: number;
297
- };
298
- /** Callback called when the AI is defeated */
299
- onDefeated?: (event: RpgEvent, attacker?: RpgPlayer) => void;
300
- });
304
+ constructor(event: RpgEventWithBattleAi, options?: BattleAiOptions);
301
305
  /**
302
306
  * Apply enemy type-specific behavior modifiers
303
307
  *
@@ -0,0 +1,16 @@
1
+ import { ActionBattleAnimationContext, ActionBattleAnimationEntity, ActionBattleAnimationKey, ActionBattleAnimationOptions } from './types';
2
+ export declare const DEFAULT_DIE_ANIMATION_DELAY_MS = 500;
3
+ export interface ResolvedActionBattleAnimation {
4
+ animationName: string;
5
+ graphic?: string | string[];
6
+ repeat: number;
7
+ waitEnd: boolean;
8
+ delayMs?: number;
9
+ }
10
+ export interface ActionBattleAnimationDefaults {
11
+ animationName?: string;
12
+ repeat?: number;
13
+ }
14
+ export declare function resolveActionBattleAnimation(key: ActionBattleAnimationKey, entity: ActionBattleAnimationEntity, animations?: ActionBattleAnimationOptions, context?: ActionBattleAnimationContext, defaults?: ActionBattleAnimationDefaults): ResolvedActionBattleAnimation | null;
15
+ export declare function playActionBattleAnimation(key: ActionBattleAnimationKey, entity: ActionBattleAnimationEntity, animations?: ActionBattleAnimationOptions, context?: ActionBattleAnimationContext, defaults?: ActionBattleAnimationDefaults): ResolvedActionBattleAnimation | null;
16
+ export declare function getActionBattleAnimationRemovalDelay(animation: ResolvedActionBattleAnimation | null): number;
@@ -1,35 +1,19 @@
1
- import client, { createActionBattleClient } from "./index2.js";
1
+ import client_default, { createActionBattleClient } from "./index7.js";
2
+ import { AiDebug, AiState, AttackPattern, BattleAi, DEFAULT_KNOCKBACK, EnemyType } from "./index9.js";
3
+ import { ACTION_BATTLE_ACTION_BAR_GUI_ID, DEFAULT_PLAYER_ATTACK_HITBOXES, applyPlayerHitToEvent, createActionBattleServer, getPlayerWeaponKnockbackForce, openActionBattleActionBar, updateActionBattleActionBar } from "./index10.js";
2
4
  import { createModule } from "@rpgjs/common";
3
- import { AiDebug, AiState, AttackPattern, BattleAi, DEFAULT_KNOCKBACK, EnemyType } from "./index3.js";
4
- import { ACTION_BATTLE_ACTION_BAR_GUI_ID, DEFAULT_PLAYER_ATTACK_HITBOXES, applyPlayerHitToEvent, createActionBattleServer, getPlayerWeaponKnockbackForce, openActionBattleActionBar, updateActionBattleActionBar } from "./index4.js";
5
- const server = null;
6
- const createActionBattleServer2 = null;
5
+ //#region src/index.ts
6
+ var server = null;
7
+ var createActionBattleServer$1 = null;
7
8
  function provideActionBattle(options = {}) {
8
- return createModule("ActionBattle", [
9
- {
10
- server: createActionBattleServer2?.(options),
11
- client: createActionBattleClient?.(options)
12
- }
13
- ]);
9
+ return createModule("ActionBattle", [{
10
+ server: createActionBattleServer$1?.(options),
11
+ client: createActionBattleClient?.(options)
12
+ }]);
14
13
  }
15
- const index = {
16
- server,
17
- client
18
- };
19
- export {
20
- ACTION_BATTLE_ACTION_BAR_GUI_ID,
21
- AiDebug,
22
- AiState,
23
- AttackPattern,
24
- BattleAi,
25
- DEFAULT_KNOCKBACK,
26
- DEFAULT_PLAYER_ATTACK_HITBOXES,
27
- EnemyType,
28
- applyPlayerHitToEvent,
29
- createActionBattleServer,
30
- index as default,
31
- getPlayerWeaponKnockbackForce,
32
- openActionBattleActionBar,
33
- provideActionBattle,
34
- updateActionBattleActionBar
14
+ var src_default = {
15
+ server,
16
+ client: client_default
35
17
  };
18
+ //#endregion
19
+ export { ACTION_BATTLE_ACTION_BAR_GUI_ID, AiDebug, AiState, AttackPattern, BattleAi, DEFAULT_KNOCKBACK, DEFAULT_PLAYER_ATTACK_HITBOXES, EnemyType, applyPlayerHitToEvent, createActionBattleServer, src_default as default, getPlayerWeaponKnockbackForce, openActionBattleActionBar, provideActionBattle, updateActionBattleActionBar };
@@ -0,0 +1,376 @@
1
+ import { normalizeActionBattleOptions, setActionBattleOptions } from "./index2.js";
2
+ import { manhattanDistance, parseAoeMask } from "./index5.js";
3
+ import { playActionBattleAnimation } from "./index8.js";
4
+ import { Control, defineModule } from "@rpgjs/common";
5
+ //#region src/server.ts
6
+ var RpgEvent = null;
7
+ var DEFAULT_KNOCKBACK = null;
8
+ var ACTION_BATTLE_ACTION_BAR_GUI_ID = "action-battle-action-bar";
9
+ /**
10
+ * Default player attack hitboxes offsets for each direction
11
+ *
12
+ * These hitboxes define the attack areas relative to the player's position
13
+ * for each cardinal direction. They are converted to absolute coordinates
14
+ * when creating the moving hitbox.
15
+ */
16
+ var DEFAULT_PLAYER_ATTACK_HITBOXES = {
17
+ up: {
18
+ offsetX: -16,
19
+ offsetY: -48,
20
+ width: 32,
21
+ height: 32
22
+ },
23
+ down: {
24
+ offsetX: -16,
25
+ offsetY: 16,
26
+ width: 32,
27
+ height: 32
28
+ },
29
+ left: {
30
+ offsetX: -48,
31
+ offsetY: -16,
32
+ width: 32,
33
+ height: 32
34
+ },
35
+ right: {
36
+ offsetX: 16,
37
+ offsetY: -16,
38
+ width: 32,
39
+ height: 32
40
+ },
41
+ default: {
42
+ offsetX: 0,
43
+ offsetY: -32,
44
+ width: 32,
45
+ height: 32
46
+ }
47
+ };
48
+ /**
49
+ * Get knockback force from player's equipped weapon
50
+ *
51
+ * Retrieves the knockbackForce property from the player's equipped weapon.
52
+ * Falls back to DEFAULT_KNOCKBACK.force if no weapon or property is set.
53
+ *
54
+ * @param player - The player to get weapon knockback from
55
+ * @returns Knockback force value
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * // Player with weapon having knockbackForce: 80
60
+ * const force = getPlayerWeaponKnockbackForce(player); // 80
61
+ *
62
+ * // No weapon equipped
63
+ * const force = getPlayerWeaponKnockbackForce(player); // 50 (default)
64
+ * ```
65
+ */
66
+ function getPlayerWeaponKnockbackForce(player) {
67
+ try {
68
+ const equipments = player.equipments?.() || [];
69
+ for (const item of equipments) {
70
+ const itemData = player.databaseById?.(item.id());
71
+ if (itemData?._type === "weapon" && itemData.knockbackForce !== void 0) return itemData.knockbackForce;
72
+ }
73
+ } catch {}
74
+ return DEFAULT_KNOCKBACK.force;
75
+ }
76
+ /**
77
+ * Apply hit from player to target (event with AI)
78
+ *
79
+ * Handles damage calculation, knockback based on weapon, and visual effects.
80
+ * Can be customized using hooks.
81
+ *
82
+ * @param player - The attacking player
83
+ * @param target - The event being hit
84
+ * @param hooks - Optional hooks for customizing hit behavior
85
+ * @returns Hit result if AI exists, undefined otherwise
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * // Basic hit
90
+ * const result = applyPlayerHitToEvent(player, event);
91
+ *
92
+ * // With custom hooks
93
+ * const result = applyPlayerHitToEvent(player, event, {
94
+ * onBeforeHit(result) {
95
+ * result.knockbackForce *= 2; // Double knockback
96
+ * return result;
97
+ * },
98
+ * onAfterHit(result) {
99
+ * if (result.defeated) {
100
+ * player.gold += 10;
101
+ * }
102
+ * }
103
+ * });
104
+ * ```
105
+ */
106
+ function applyPlayerHitToEvent(player, target, hooks) {
107
+ const ai = target.battleAi;
108
+ if (!ai) return void 0;
109
+ const knockbackForce = getPlayerWeaponKnockbackForce(player);
110
+ const defeated = ai.takeDamage(player);
111
+ const dx = target.x() - player.x();
112
+ const dy = target.y() - player.y();
113
+ const distance = Math.sqrt(dx * dx + dy * dy);
114
+ let hitResult = {
115
+ damage: 0,
116
+ knockbackForce,
117
+ knockbackDuration: DEFAULT_KNOCKBACK.duration,
118
+ defeated,
119
+ attacker: player,
120
+ target
121
+ };
122
+ if (hooks?.onBeforeHit) {
123
+ const modified = hooks.onBeforeHit(hitResult);
124
+ if (modified) hitResult = modified;
125
+ }
126
+ if (!hitResult.defeated && hitResult.knockbackForce > 0 && distance > 0) {
127
+ const knockbackDirection = {
128
+ x: dx / distance,
129
+ y: dy / distance
130
+ };
131
+ target.knockback(knockbackDirection, hitResult.knockbackForce, hitResult.knockbackDuration);
132
+ }
133
+ if (hooks?.onAfterHit) hooks.onAfterHit(hitResult);
134
+ return hitResult;
135
+ }
136
+ var resolveSignal = (value) => typeof value === "function" ? value() : value;
137
+ var resolveItemData = (player, itemId) => {
138
+ try {
139
+ return player.databaseById?.(itemId);
140
+ } catch {
141
+ return null;
142
+ }
143
+ };
144
+ var resolveSkillData = (player, skillId) => {
145
+ try {
146
+ return player.databaseById?.(skillId);
147
+ } catch {
148
+ return null;
149
+ }
150
+ };
151
+ var resolveSkillTargeting = (player, skillId, options) => {
152
+ const skillsOptions = options.skills;
153
+ const skillData = resolveSkillData(player, skillId);
154
+ if (skillsOptions?.getTargeting) return skillsOptions.getTargeting(skillData);
155
+ const range = skillData?.range ?? skillData?.targeting?.range ?? skillData?.targeting?.distance;
156
+ const aoeMask = skillData?.aoeMask ?? skillData?.targeting?.aoeMask ?? skillData?.targeting?.mask;
157
+ if (range === void 0 && aoeMask === void 0) return null;
158
+ return {
159
+ range: range ?? 0,
160
+ aoeMask
161
+ };
162
+ };
163
+ var normalizeMaskRows = (mask) => {
164
+ if (!mask) return [];
165
+ if (Array.isArray(mask)) return mask;
166
+ return mask.trim().split("\n").map((row) => row.replace(/\r/g, ""));
167
+ };
168
+ var buildActionBarData = (player, options) => {
169
+ return {
170
+ items: (player.items?.() || []).map((item) => {
171
+ const id = item.id?.() ?? item.id;
172
+ const data = resolveItemData(player, id);
173
+ const name = resolveSignal(data?.name) ?? resolveSignal(item.name) ?? id;
174
+ const description = resolveSignal(data?.description) ?? resolveSignal(item.description) ?? "";
175
+ const icon = resolveSignal(data?.icon) ?? resolveSignal(item.icon);
176
+ const quantity = resolveSignal(item.quantity) ?? 1;
177
+ const consumable = resolveSignal(data?.consumable);
178
+ const itemType = resolveSignal(data?._type);
179
+ return {
180
+ id,
181
+ name,
182
+ description,
183
+ icon,
184
+ quantity,
185
+ usable: quantity > 0 && consumable !== false && (itemType ? itemType === "item" : true)
186
+ };
187
+ }),
188
+ skills: (player.skills?.() || []).map((skill) => {
189
+ const id = skill.id?.() ?? skill.id;
190
+ const data = resolveSkillData(player, id) || skill;
191
+ const name = resolveSignal(data?.name) ?? resolveSignal(skill.name) ?? id;
192
+ const description = resolveSignal(data?.description) ?? resolveSignal(skill.description) ?? "";
193
+ const icon = resolveSignal(data?.icon) ?? resolveSignal(skill.icon);
194
+ const spCost = resolveSignal(data?.spCost) ?? resolveSignal(skill.spCost) ?? 0;
195
+ const usable = spCost <= player.sp;
196
+ const targeting = resolveSkillTargeting(player, id, options);
197
+ const skillEntry = {
198
+ id,
199
+ name,
200
+ description,
201
+ icon,
202
+ spCost,
203
+ usable,
204
+ range: targeting?.range ?? 0
205
+ };
206
+ if (targeting) {
207
+ const mask = targeting.aoeMask ?? options.skills?.defaultAoeMask;
208
+ if (mask) skillEntry.aoeMask = normalizeMaskRows(mask);
209
+ }
210
+ return skillEntry;
211
+ })
212
+ };
213
+ };
214
+ var ensureActionBarGui = (player, options) => {
215
+ const gui = player.getGui?.("action-battle-action-bar") || player.gui("action-battle-action-bar");
216
+ if (!gui.__actionBattleReady) {
217
+ gui.__actionBattleReady = true;
218
+ gui.on("useItem", ({ id }) => {
219
+ try {
220
+ player.useItem(id);
221
+ } catch {}
222
+ gui.update(buildActionBarData(player, options));
223
+ });
224
+ gui.on("useSkill", ({ id, target }) => {
225
+ handleActionBattleSkillUse(player, id, target, options);
226
+ gui.update(buildActionBarData(player, options));
227
+ });
228
+ gui.on("refresh", () => {
229
+ gui.update(buildActionBarData(player, options));
230
+ });
231
+ }
232
+ return gui;
233
+ };
234
+ var openActionBattleActionBar = (player, rawOptions = {}) => {
235
+ const options = normalizeActionBattleOptions(rawOptions);
236
+ ensureActionBarGui(player, options).open(buildActionBarData(player, options));
237
+ };
238
+ var updateActionBattleActionBar = (player, rawOptions = {}) => {
239
+ const options = normalizeActionBattleOptions(rawOptions);
240
+ const gui = player.getGui?.(ACTION_BATTLE_ACTION_BAR_GUI_ID);
241
+ if (gui) gui.update(buildActionBarData(player, options));
242
+ };
243
+ var getTileSize = (map) => ({
244
+ width: map?.tileWidth ?? 32,
245
+ height: map?.tileHeight ?? 32
246
+ });
247
+ var getEntityTile = (entity, tileSize) => {
248
+ const hitbox = entity.hitbox?.() || {
249
+ w: tileSize.width,
250
+ h: tileSize.height
251
+ };
252
+ return {
253
+ x: Math.floor((entity.x() + hitbox.w / 2) / tileSize.width),
254
+ y: Math.floor((entity.y() + hitbox.h / 2) / tileSize.height)
255
+ };
256
+ };
257
+ var handleActionBattleSkillUse = (player, skillId, target, options) => {
258
+ const skillData = resolveSkillData(player, skillId);
259
+ const map = player.getCurrentMap();
260
+ if (!map) {
261
+ playActionBattleAnimation("castSkill", player, options.animations, { skill: skillData });
262
+ player.useSkill(skillId);
263
+ return;
264
+ }
265
+ const targeting = resolveSkillTargeting(player, skillId, options);
266
+ if (!targeting || !target) {
267
+ playActionBattleAnimation("castSkill", player, options.animations, { skill: skillData });
268
+ player.useSkill(skillId);
269
+ return;
270
+ }
271
+ const tileSize = getTileSize(map);
272
+ const origin = getEntityTile(player, tileSize);
273
+ const targetTile = {
274
+ x: target.x,
275
+ y: target.y
276
+ };
277
+ if (manhattanDistance(origin, targetTile) > targeting.range) return;
278
+ const mask = parseAoeMask(targeting.aoeMask || options.skills?.defaultAoeMask);
279
+ const affected = /* @__PURE__ */ new Set();
280
+ mask.cells.forEach((cell) => {
281
+ const x = targetTile.x + cell.dx;
282
+ const y = targetTile.y + cell.dy;
283
+ affected.add(`${x},${y}`);
284
+ });
285
+ const targets = [];
286
+ const affects = options.targeting?.affects || "events";
287
+ if (affects === "events" || affects === "both") map.getEvents().forEach((event) => {
288
+ const tile = getEntityTile(event, tileSize);
289
+ if (affected.has(`${tile.x},${tile.y}`)) targets.push(event);
290
+ });
291
+ if (affects === "players" || affects === "both") map.getPlayers().forEach((other) => {
292
+ if (other.id === player.id) return;
293
+ const tile = getEntityTile(other, tileSize);
294
+ if (affected.has(`${tile.x},${tile.y}`)) targets.push(other);
295
+ });
296
+ if (!options.targeting?.allowEmptyTarget && targets.length === 0) return;
297
+ playActionBattleAnimation("castSkill", player, options.animations, {
298
+ skill: skillData,
299
+ target: targets[0]
300
+ });
301
+ player.useSkill(skillId, targets);
302
+ };
303
+ var createActionBattleServer = (rawOptions = {}) => {
304
+ const options = normalizeActionBattleOptions(rawOptions);
305
+ setActionBattleOptions(options);
306
+ return defineModule({
307
+ player: {
308
+ /**
309
+ * Handle player input for combat actions
310
+ *
311
+ * When a player presses the action key, create an attack hitbox
312
+ * that can damage AI enemies within range and knockback the event.
313
+ * Knockback force is based on the player's equipped weapon.
314
+ * Triggers attack animation and visual effects.
315
+ *
316
+ * @param player - The player performing the action
317
+ * @param input - Input data containing pressed keys
318
+ */
319
+ onInput(player, input) {
320
+ if (input.action == Control.Action) {
321
+ playActionBattleAnimation("attack", player, options.animations);
322
+ const playerX = player.x();
323
+ const playerY = player.y();
324
+ const hitboxConfig = DEFAULT_PLAYER_ATTACK_HITBOXES[player.getDirection()] || DEFAULT_PLAYER_ATTACK_HITBOXES.default;
325
+ const hitboxes = [{
326
+ x: playerX + hitboxConfig.offsetX,
327
+ y: playerY + hitboxConfig.offsetY,
328
+ width: hitboxConfig.width,
329
+ height: hitboxConfig.height
330
+ }];
331
+ player.getCurrentMap()?.createMovingHitbox(hitboxes, { speed: 3 }).subscribe({ next(hits) {
332
+ hits.forEach((hit) => {
333
+ if (hit instanceof RpgEvent) {
334
+ if (applyPlayerHitToEvent(player, hit)?.defeated) console.log(`Player ${player.id} defeated AI ${hit.id}`);
335
+ }
336
+ });
337
+ } });
338
+ }
339
+ },
340
+ onConnected(player) {
341
+ if (options.ui?.actionBar?.enabled && options.ui?.actionBar?.autoOpen) openActionBattleActionBar(player, options);
342
+ }
343
+ },
344
+ event: {
345
+ /**
346
+ * Handle player detection when entering AI vision
347
+ *
348
+ * Called when a player enters an AI event's vision range.
349
+ * The AI will start pursuing and attacking the player.
350
+ *
351
+ * @param event - The AI event
352
+ * @param player - The player entering vision
353
+ * @param shape - The vision shape
354
+ */
355
+ onDetectInShape(event, player, shape) {
356
+ event.battleAi?.onDetectInShape(player, shape);
357
+ },
358
+ /**
359
+ * Handle player leaving AI vision
360
+ *
361
+ * Called when a player leaves an AI event's vision range.
362
+ * The AI will stop pursuing the player.
363
+ *
364
+ * @param event - The AI event
365
+ * @param player - The player leaving vision
366
+ * @param shape - The vision shape
367
+ */
368
+ onDetectOutShape(event, player, shape) {
369
+ event.battleAi?.onDetectOutShape(player, shape);
370
+ }
371
+ }
372
+ });
373
+ };
374
+ createActionBattleServer();
375
+ //#endregion
376
+ export { ACTION_BATTLE_ACTION_BAR_GUI_ID, DEFAULT_PLAYER_ATTACK_HITBOXES, applyPlayerHitToEvent, createActionBattleServer, getPlayerWeaponKnockbackForce, openActionBattleActionBar, updateActionBattleActionBar };