@rpgjs/action-battle 5.0.0-beta.4 → 5.0.0-beta.6

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.
Files changed (63) hide show
  1. package/README.md +115 -0
  2. package/dist/ai.server.d.ts +17 -2
  3. package/dist/client/index.js +13 -8
  4. package/dist/client/index10.js +54 -136
  5. package/dist/client/index11.js +52 -23
  6. package/dist/client/index12.js +101 -1217
  7. package/dist/client/index13.js +139 -42
  8. package/dist/client/index14.js +23 -8
  9. package/dist/client/index15.js +68 -444
  10. package/dist/client/index16.js +1281 -0
  11. package/dist/client/index17.js +13 -0
  12. package/dist/client/index18.js +60 -0
  13. package/dist/client/index19.js +10 -0
  14. package/dist/client/index2.js +25 -87
  15. package/dist/client/index20.js +504 -0
  16. package/dist/client/index3.js +45 -83
  17. package/dist/client/index4.js +98 -297
  18. package/dist/client/index5.js +81 -33
  19. package/dist/client/index6.js +284 -78
  20. package/dist/client/index7.js +33 -74
  21. package/dist/client/index8.js +95 -55
  22. package/dist/client/index9.js +75 -96
  23. package/dist/core/attack-profile.d.ts +9 -0
  24. package/dist/core/attack-runtime.d.ts +20 -0
  25. package/dist/core/enemy-attack-profiles.d.ts +6 -0
  26. package/dist/core/equipment.d.ts +2 -0
  27. package/dist/core/hit-reaction.d.ts +5 -0
  28. package/dist/index.d.ts +6 -1
  29. package/dist/server/index.js +12 -7
  30. package/dist/server/index10.js +1278 -8
  31. package/dist/server/index11.js +37 -0
  32. package/dist/server/index12.js +60 -0
  33. package/dist/server/index13.js +13 -0
  34. package/dist/server/index14.js +503 -0
  35. package/dist/server/index15.js +10 -0
  36. package/dist/server/index3.js +25 -87
  37. package/dist/server/index4.js +45 -141
  38. package/dist/server/index5.js +104 -21
  39. package/dist/server/index6.js +137 -1215
  40. package/dist/server/index7.js +22 -34
  41. package/dist/server/index8.js +70 -44
  42. package/dist/server/index9.js +44 -437
  43. package/dist/server.d.ts +7 -1
  44. package/package.json +5 -5
  45. package/src/ai.server.ts +172 -43
  46. package/src/client.ts +21 -12
  47. package/src/config.ts +17 -2
  48. package/src/core/attack-profile.spec.ts +118 -0
  49. package/src/core/attack-profile.ts +100 -0
  50. package/src/core/attack-runtime.spec.ts +103 -0
  51. package/src/core/attack-runtime.ts +83 -0
  52. package/src/core/contracts.ts +3 -0
  53. package/src/core/enemy-attack-profiles.spec.ts +35 -0
  54. package/src/core/enemy-attack-profiles.ts +103 -0
  55. package/src/core/equipment.spec.ts +37 -0
  56. package/src/core/equipment.ts +17 -0
  57. package/src/core/hit-reaction.spec.ts +43 -0
  58. package/src/core/hit-reaction.ts +70 -0
  59. package/src/core/hit.spec.ts +54 -1
  60. package/src/core/hit.ts +26 -0
  61. package/src/index.ts +36 -0
  62. package/src/server.ts +180 -33
  63. package/src/types.ts +62 -6
@@ -1,37 +1,25 @@
1
- //#region src/targeting.ts
2
- var normalizeMaskRows = (mask) => {
3
- if (!mask) return ["#"];
4
- if (Array.isArray(mask)) return mask;
5
- return mask.trim().split("\n").map((row) => row.replace(/\r/g, ""));
6
- };
7
- var parseAoeMask = (mask) => {
8
- const rows = normalizeMaskRows(mask);
9
- const height = rows.length;
10
- const width = rows.reduce((max, row) => Math.max(max, row.length), 0);
11
- const centerX = Math.floor(width / 2);
12
- const centerY = Math.floor(height / 2);
13
- const cells = [];
14
- rows.forEach((row, y) => {
15
- for (let x = 0; x < row.length; x++) {
16
- const char = row[x];
17
- if (char && char !== "." && char !== " ") cells.push({
18
- dx: x - centerX,
19
- dy: y - centerY
20
- });
1
+ import { defaultActionBattleSystems } from "./index6.js";
2
+ //#region src/core/context.ts
3
+ var mergeSystems = (options = {}) => ({
4
+ combat: {
5
+ ...defaultActionBattleSystems.combat,
6
+ resolveDamage: options.systems?.combat?.damage ?? defaultActionBattleSystems.combat.resolveDamage,
7
+ resolveKnockback: options.systems?.combat?.knockback ?? defaultActionBattleSystems.combat.resolveKnockback,
8
+ hooks: {
9
+ ...defaultActionBattleSystems.combat.hooks,
10
+ ...options.systems?.combat?.hooks
21
11
  }
22
- });
23
- if (cells.length === 0) cells.push({
24
- dx: 0,
25
- dy: 0
26
- });
27
- return {
28
- width,
29
- height,
30
- centerX,
31
- centerY,
32
- cells
33
- };
12
+ },
13
+ ai: { behaviors: {
14
+ ...defaultActionBattleSystems.ai.behaviors,
15
+ ...options.systems?.ai?.behaviors
16
+ } }
17
+ });
18
+ var currentActionBattleSystems = mergeSystems();
19
+ var setActionBattleSystems = (options = {}) => {
20
+ currentActionBattleSystems = mergeSystems(options);
34
21
  };
35
- var manhattanDistance = (a, b) => Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
22
+ var getActionBattleSystems = () => currentActionBattleSystems;
23
+ var createActionBattleSystems = mergeSystems;
36
24
  //#endregion
37
- export { manhattanDistance, parseAoeMask };
25
+ export { createActionBattleSystems, getActionBattleSystems, setActionBattleSystems };
@@ -1,46 +1,72 @@
1
- //#region src/core/hit.ts
2
- var applyActionBattleHit = (system, context) => {
3
- let hitContext = { ...context };
4
- const before = system.hooks?.beforeHit?.(hitContext);
5
- if (before === false) return {
6
- damage: 0,
7
- knockbackForce: 0,
8
- knockbackDuration: 0,
9
- defeated: false,
10
- attacker: hitContext.attacker,
11
- target: hitContext.target,
12
- cancelled: true,
13
- metadata: hitContext.metadata
14
- };
15
- if (before) hitContext = before;
16
- const damage = hitContext.damage ?? system.resolveDamage({
17
- attacker: hitContext.attacker,
18
- target: hitContext.target,
19
- skill: hitContext.skill,
20
- pattern: hitContext.pattern
21
- });
22
- hitContext.damage = damage;
23
- const afterDamage = system.hooks?.afterDamage?.(hitContext);
24
- if (afterDamage) hitContext = afterDamage;
25
- const knockback = hitContext.knockback ?? system.resolveKnockback({
26
- attacker: hitContext.attacker,
27
- target: hitContext.target,
28
- damage
29
- });
30
- hitContext.knockback = knockback;
31
- if (!damage.defeated && knockback.force > 0 && knockback.direction) hitContext.target.knockback?.(knockback.direction, knockback.force, knockback.duration);
32
- const result = {
33
- damage: damage.damage,
34
- knockbackForce: knockback.force,
35
- knockbackDuration: knockback.duration,
36
- defeated: damage.defeated,
37
- attacker: hitContext.attacker,
38
- target: hitContext.target,
39
- rawDamage: damage.raw,
40
- metadata: hitContext.metadata
41
- };
42
- system.hooks?.afterHit?.(result);
43
- return result;
1
+ import { normalizeActionBattleAttackProfile } from "./index4.js";
2
+ //#region src/core/enemy-attack-profiles.ts
3
+ var DEFAULT_ACTION_BATTLE_ENEMY_ATTACK_PROFILES = {
4
+ melee: {
5
+ id: "enemy-melee",
6
+ startupMs: 120,
7
+ activeMs: 100,
8
+ recoveryMs: 220,
9
+ cooldownMs: 440,
10
+ reaction: {
11
+ invincibilityMs: 250,
12
+ hitstunMs: 120,
13
+ staggerPower: 1
14
+ }
15
+ },
16
+ combo: {
17
+ id: "enemy-combo",
18
+ startupMs: 80,
19
+ activeMs: 80,
20
+ recoveryMs: 140,
21
+ cooldownMs: 300,
22
+ reaction: {
23
+ invincibilityMs: 180,
24
+ hitstunMs: 90,
25
+ staggerPower: .75
26
+ }
27
+ },
28
+ charged: {
29
+ id: "enemy-charged",
30
+ startupMs: 800,
31
+ activeMs: 140,
32
+ recoveryMs: 320,
33
+ cooldownMs: 1260,
34
+ reaction: {
35
+ invincibilityMs: 350,
36
+ hitstunMs: 220,
37
+ staggerPower: 2
38
+ }
39
+ },
40
+ zone: {
41
+ id: "enemy-zone",
42
+ startupMs: 450,
43
+ activeMs: 180,
44
+ recoveryMs: 320,
45
+ cooldownMs: 950,
46
+ reaction: {
47
+ invincibilityMs: 300,
48
+ hitstunMs: 160,
49
+ staggerPower: 1.25
50
+ }
51
+ },
52
+ dashAttack: {
53
+ id: "enemy-dash",
54
+ startupMs: 180,
55
+ activeMs: 120,
56
+ recoveryMs: 260,
57
+ cooldownMs: 560,
58
+ reaction: {
59
+ invincibilityMs: 280,
60
+ hitstunMs: 150,
61
+ staggerPower: 1.2
62
+ }
63
+ }
44
64
  };
65
+ function normalizeActionBattleEnemyAttackProfiles(overrides = {}) {
66
+ return Object.fromEntries(Object.entries(DEFAULT_ACTION_BATTLE_ENEMY_ATTACK_PROFILES).map(([key, defaultProfile]) => [key, normalizeActionBattleAttackProfile({
67
+ ...defaultProfile,
68
+ ...overrides[key]
69
+ })]));
70
+ }
45
71
  //#endregion
46
- export { applyActionBattleHit };
72
+ export { DEFAULT_ACTION_BATTLE_ENEMY_ATTACK_PROFILES, normalizeActionBattleEnemyAttackProfiles };
@@ -1,447 +1,54 @@
1
- import { playActionBattleAnimation } from "./index2.js";
2
- import { normalizeActionBattleOptions, setActionBattleOptions } from "./index3.js";
3
- import { DEFAULT_ZELDA_PLAYER_HITBOXES } from "./index4.js";
4
- import { getActionBattleSystems, setActionBattleSystems } from "./index5.js";
5
- import { DEFAULT_KNOCKBACK } from "./index6.js";
6
- import { manhattanDistance, parseAoeMask } from "./index7.js";
7
- import { applyActionBattleHit } from "./index8.js";
8
- import { RpgEvent } from "@rpgjs/server";
9
- import { Control, defineModule } from "@rpgjs/common";
10
- //#region src/server.ts
11
- var ACTION_BATTLE_ACTION_BAR_GUI_ID = "action-battle-action-bar";
12
- var DEFAULT_ATTACK_LOCK_DURATION_MS = 350;
13
- /**
14
- * Default player attack hitboxes offsets for each direction
15
- *
16
- * These hitboxes define the attack areas relative to the player's position
17
- * for each cardinal direction. They are converted to absolute coordinates
18
- * when creating the moving hitbox.
19
- */
20
- var DEFAULT_PLAYER_ATTACK_HITBOXES = { ...DEFAULT_ZELDA_PLAYER_HITBOXES };
21
- var beginPlayerAttackLock = (player, map, durationMs) => {
22
- if (durationMs <= 0) return true;
23
- const runtimePlayer = player;
24
- const now = Date.now();
25
- if (typeof runtimePlayer.__actionBattleAttackLockedUntil === "number" && runtimePlayer.__actionBattleAttackLockedUntil > now) return false;
26
- const lockId = (runtimePlayer.__actionBattleAttackLockId ?? 0) + 1;
27
- runtimePlayer.__actionBattleAttackLockId = lockId;
28
- runtimePlayer.__actionBattleAttackLockedUntil = now + durationMs;
29
- const previousCanMove = typeof player.canMove === "function" ? player.canMove() : true;
30
- const previousDirectionFixed = player.directionFixed;
31
- const previousAnimationFixed = player.animationFixed;
32
- player.pendingInputs = [];
33
- player.lastProcessedInputTs = 0;
34
- map?.stopMovement?.(player);
35
- player.canMove.set(false);
36
- player.directionFixed = true;
37
- setTimeout(() => {
38
- if (runtimePlayer.__actionBattleAttackLockId !== lockId) return;
39
- runtimePlayer.__actionBattleAttackLockedUntil = 0;
40
- player.canMove.set(previousCanMove);
41
- player.directionFixed = previousDirectionFixed;
42
- player.animationFixed = previousAnimationFixed;
43
- }, durationMs);
44
- return true;
45
- };
46
- var isBattleEvent = (event) => !!event.battleAi;
47
- var rectsOverlap = (a, b) => a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
48
- var eventRect = (event) => {
49
- const hitbox = typeof event.hitbox === "function" ? event.hitbox() : event.hitbox;
50
- return {
51
- x: event.x(),
52
- y: event.y(),
53
- width: hitbox?.w ?? 32,
54
- height: hitbox?.h ?? 32
55
- };
56
- };
57
- var getVisibleActionEvents = (player, map, hitboxes) => {
58
- if (!map) return [];
59
- const eventsById = /* @__PURE__ */ new Map();
60
- const addEvent = (event) => {
61
- if (!event) return;
62
- if (!(typeof map.isEventVisibleForPlayer === "function" ? map.isEventVisibleForPlayer(event, player) : true)) return;
63
- eventsById.set(event.id, event);
64
- };
65
- const collisions = map.getCollisions?.(player.id);
66
- if (Array.isArray(collisions)) collisions.forEach((id) => addEvent(map.getEvent(id)));
67
- for (const event of map.getEvents()) {
68
- const rect = eventRect(event);
69
- if (hitboxes.some((hitbox) => rectsOverlap(hitbox, rect))) addEvent(event);
70
- }
71
- return Array.from(eventsById.values());
72
- };
73
- var isActionReservedForNormalEvent = (player, map, hitboxes) => {
74
- const events = getVisibleActionEvents(player, map, hitboxes);
75
- return events.length > 0 && !events.some(isBattleEvent);
76
- };
77
- /**
78
- * Get knockback force from player's equipped weapon
79
- *
80
- * Retrieves the knockbackForce property from the player's equipped weapon.
81
- * Falls back to DEFAULT_KNOCKBACK.force if no weapon or property is set.
82
- *
83
- * @param player - The player to get weapon knockback from
84
- * @returns Knockback force value
85
- *
86
- * @example
87
- * ```ts
88
- * // Player with weapon having knockbackForce: 80
89
- * const force = getPlayerWeaponKnockbackForce(player); // 80
90
- *
91
- * // No weapon equipped
92
- * const force = getPlayerWeaponKnockbackForce(player); // 50 (default)
93
- * ```
94
- */
95
- function getPlayerWeaponKnockbackForce(player) {
96
- try {
97
- const equipments = player.equipments?.() || [];
98
- for (const item of equipments) {
99
- const itemData = player.databaseById?.(item.id());
100
- if (itemData?._type === "weapon" && itemData.knockbackForce !== void 0) return itemData.knockbackForce;
101
- }
102
- } catch {}
103
- return DEFAULT_KNOCKBACK.force;
104
- }
105
- /**
106
- * Apply hit from player to target (event with AI)
107
- *
108
- * Handles damage calculation, knockback based on weapon, and visual effects.
109
- * Can be customized using hooks.
110
- *
111
- * @param player - The attacking player
112
- * @param target - The event being hit
113
- * @param hooks - Optional hooks for customizing hit behavior
114
- * @returns Hit result if AI exists, undefined otherwise
115
- *
116
- * @example
117
- * ```ts
118
- * // Basic hit
119
- * const result = applyPlayerHitToEvent(player, event);
120
- *
121
- * // With custom hooks
122
- * const result = applyPlayerHitToEvent(player, event, {
123
- * onBeforeHit(result) {
124
- * result.knockbackForce *= 2; // Double knockback
125
- * return result;
126
- * },
127
- * onAfterHit(result) {
128
- * if (result.defeated) {
129
- * player.gold += 10;
130
- * }
131
- * }
132
- * });
133
- * ```
134
- */
135
- function applyPlayerHitToEvent(player, target, hooks) {
136
- const ai = target.battleAi;
137
- if (!ai) return void 0;
138
- const systems = getActionBattleSystems();
139
- const result = applyActionBattleHit({
140
- ...systems.combat,
141
- hooks: hooks ? {
142
- ...systems.combat.hooks,
143
- beforeHit(context) {
144
- const before = systems.combat.hooks?.beforeHit?.(context);
145
- if (before === false) return false;
146
- const nextContext = before || context;
147
- const legacyResult = toLegacyHitResult(nextContext);
148
- const modified = hooks.onBeforeHit?.(legacyResult);
149
- if (!modified) return nextContext;
150
- return {
151
- ...nextContext,
152
- damage: {
153
- damage: modified.damage,
154
- defeated: modified.defeated,
155
- raw: nextContext.damage?.raw
156
- },
157
- knockback: {
158
- force: modified.knockbackForce,
159
- duration: modified.knockbackDuration,
160
- direction: nextContext.knockback?.direction
161
- }
162
- };
163
- },
164
- afterHit(result) {
165
- systems.combat.hooks?.afterHit?.(result);
166
- hooks.onAfterHit?.(result);
167
- }
168
- } : systems.combat.hooks
169
- }, {
170
- attacker: player,
171
- target
172
- });
173
- if (!result.cancelled) ai.handleDamage(player, {
174
- damage: result.damage,
175
- defeated: result.defeated,
176
- raw: result.rawDamage
1
+ import { normalizeActionBattleAttackProfile } from "./index4.js";
2
+ //#region src/core/attack-runtime.ts
3
+ var ACTION_BATTLE_HITBOX_FRAME_MS = 16;
4
+ function getNormalizedActionBattleAttackProfile(options = {}) {
5
+ const attack = options.attack ?? {};
6
+ return normalizeActionBattleAttackProfile(attack.profile, {
7
+ lockMovement: attack.lockMovement,
8
+ lockDurationMs: attack.lockDurationMs,
9
+ hitboxes: attack.hitboxes
177
10
  });
178
- return result;
179
11
  }
180
- var toLegacyHitResult = (context) => ({
181
- damage: context.damage?.damage ?? 0,
182
- knockbackForce: context.knockback?.force ?? getPlayerWeaponKnockbackForce(context.attacker),
183
- knockbackDuration: context.knockback?.duration ?? DEFAULT_KNOCKBACK.duration,
184
- defeated: context.damage?.defeated ?? false,
185
- attacker: context.attacker,
186
- target: context.target
187
- });
188
- var resolvePlayerAttackHitboxes = (player, directionKey, options) => {
189
- const configuredHitboxes = {
190
- ...DEFAULT_PLAYER_ATTACK_HITBOXES,
191
- ...options.attack?.hitboxes
192
- };
193
- const hitboxConfig = configuredHitboxes[directionKey] || configuredHitboxes.default;
194
- const defaultHitboxes = [{
195
- x: player.x() + hitboxConfig.offsetX,
196
- y: player.y() + hitboxConfig.offsetY,
197
- width: hitboxConfig.width,
198
- height: hitboxConfig.height
199
- }];
200
- return options.attack?.resolveHitboxes?.({
201
- player,
202
- direction: directionKey,
203
- defaultHitboxes
204
- }) ?? defaultHitboxes;
205
- };
206
- var resolveSignal = (value) => typeof value === "function" ? value() : value;
207
- var resolveItemData = (player, itemId) => {
208
- try {
209
- return player.databaseById?.(itemId);
210
- } catch {
12
+ function resolveActionBattleHitboxSpeed(profile, hitboxCount) {
13
+ const positions = Math.max(1, Math.floor(hitboxCount));
14
+ const activeFrames = Math.max(1, Math.ceil(profile.activeMs / 16));
15
+ return Math.max(1, Math.ceil(activeFrames / positions));
16
+ }
17
+ function scheduleActionBattleStartup(profile, callback, scheduler = setTimeout) {
18
+ if (profile.startupMs <= 0) {
19
+ callback();
211
20
  return null;
212
21
  }
213
- };
214
- var resolveSkillData = (player, skillId) => {
215
- try {
216
- return player.databaseById?.(skillId);
217
- } catch {
218
- return null;
22
+ return scheduler(callback, profile.startupMs);
23
+ }
24
+ var attackIdCounter = 0;
25
+ function createActionBattleAttackId(attackerId, profileId) {
26
+ attackIdCounter++;
27
+ return `${attackerId ?? "unknown"}:${profileId}:${Date.now()}:${attackIdCounter}`;
28
+ }
29
+ var getTargetKey = (target) => {
30
+ if (!target || target.id === void 0 || target.id === null) return null;
31
+ return String(target.id);
32
+ };
33
+ var ActionBattleHitTracker = class {
34
+ hitTargets = /* @__PURE__ */ new Set();
35
+ constructor(hitPolicy) {
36
+ this.hitPolicy = hitPolicy;
219
37
  }
220
- };
221
- var resolveSkillTargeting = (player, skillId, options) => {
222
- const skillsOptions = options.skills;
223
- const skillData = resolveSkillData(player, skillId);
224
- if (skillsOptions?.getTargeting) return skillsOptions.getTargeting(skillData);
225
- const range = skillData?.range ?? skillData?.targeting?.range ?? skillData?.targeting?.distance;
226
- const aoeMask = skillData?.aoeMask ?? skillData?.targeting?.aoeMask ?? skillData?.targeting?.mask;
227
- if (range === void 0 && aoeMask === void 0) return null;
228
- return {
229
- range: range ?? 0,
230
- aoeMask
231
- };
232
- };
233
- var normalizeMaskRows = (mask) => {
234
- if (!mask) return [];
235
- if (Array.isArray(mask)) return mask;
236
- return mask.trim().split("\n").map((row) => row.replace(/\r/g, ""));
237
- };
238
- var buildActionBarData = (player, options) => {
239
- return {
240
- items: (player.items?.() || []).map((item) => {
241
- const id = item.id?.() ?? item.id;
242
- const data = resolveItemData(player, id);
243
- const name = resolveSignal(data?.name) ?? resolveSignal(item.name) ?? id;
244
- const description = resolveSignal(data?.description) ?? resolveSignal(item.description) ?? "";
245
- const icon = resolveSignal(data?.icon) ?? resolveSignal(item.icon);
246
- const quantity = resolveSignal(item.quantity) ?? 1;
247
- const consumable = resolveSignal(data?.consumable);
248
- const itemType = resolveSignal(data?._type);
249
- return {
250
- id,
251
- name,
252
- description,
253
- icon,
254
- quantity,
255
- usable: quantity > 0 && consumable !== false && (itemType ? itemType === "item" : true)
256
- };
257
- }),
258
- skills: (player.skills?.() || []).map((skill) => {
259
- const id = skill.id?.() ?? skill.id;
260
- const data = resolveSkillData(player, id) || skill;
261
- const name = resolveSignal(data?.name) ?? resolveSignal(skill.name) ?? id;
262
- const description = resolveSignal(data?.description) ?? resolveSignal(skill.description) ?? "";
263
- const icon = resolveSignal(data?.icon) ?? resolveSignal(skill.icon);
264
- const spCost = resolveSignal(data?.spCost) ?? resolveSignal(skill.spCost) ?? 0;
265
- const usable = spCost <= player.sp;
266
- const targeting = resolveSkillTargeting(player, id, options);
267
- const skillEntry = {
268
- id,
269
- name,
270
- description,
271
- icon,
272
- spCost,
273
- usable,
274
- range: targeting?.range ?? 0
275
- };
276
- if (targeting) {
277
- const mask = targeting.aoeMask ?? options.skills?.defaultAoeMask;
278
- if (mask) skillEntry.aoeMask = normalizeMaskRows(mask);
279
- }
280
- return skillEntry;
281
- })
282
- };
283
- };
284
- var ensureActionBarGui = (player, options) => {
285
- const gui = player.getGui?.("action-battle-action-bar") || player.gui("action-battle-action-bar");
286
- if (!gui.__actionBattleReady) {
287
- gui.__actionBattleReady = true;
288
- gui.on("useItem", ({ id }) => {
289
- try {
290
- player.useItem(id);
291
- } catch {}
292
- gui.update(buildActionBarData(player, options));
293
- });
294
- gui.on("useSkill", ({ id, target }) => {
295
- handleActionBattleSkillUse(player, id, target, options);
296
- gui.update(buildActionBarData(player, options));
297
- });
298
- gui.on("refresh", () => {
299
- gui.update(buildActionBarData(player, options));
300
- });
38
+ canHit(target) {
39
+ if (this.hitPolicy === "allowRepeatHits") return true;
40
+ const key = getTargetKey(target);
41
+ return !key || !this.hitTargets.has(key);
301
42
  }
302
- return gui;
303
- };
304
- var openActionBattleActionBar = (player, rawOptions = {}) => {
305
- const options = normalizeActionBattleOptions(rawOptions);
306
- ensureActionBarGui(player, options).open(buildActionBarData(player, options));
307
- };
308
- var updateActionBattleActionBar = (player, rawOptions = {}) => {
309
- const options = normalizeActionBattleOptions(rawOptions);
310
- const gui = player.getGui?.(ACTION_BATTLE_ACTION_BAR_GUI_ID);
311
- if (gui) gui.update(buildActionBarData(player, options));
312
- };
313
- var getTileSize = (map) => ({
314
- width: map?.tileWidth ?? 32,
315
- height: map?.tileHeight ?? 32
316
- });
317
- var getEntityTile = (entity, tileSize) => {
318
- const hitbox = entity.hitbox?.() || {
319
- w: tileSize.width,
320
- h: tileSize.height
321
- };
322
- return {
323
- x: Math.floor((entity.x() + hitbox.w / 2) / tileSize.width),
324
- y: Math.floor((entity.y() + hitbox.h / 2) / tileSize.height)
325
- };
326
- };
327
- var handleActionBattleSkillUse = (player, skillId, target, options) => {
328
- const skillData = resolveSkillData(player, skillId);
329
- const map = player.getCurrentMap();
330
- if (!map) {
331
- playActionBattleAnimation("castSkill", player, options.animations, { skill: skillData });
332
- player.useSkill(skillId);
333
- return;
43
+ recordHit(target) {
44
+ const key = getTargetKey(target);
45
+ if (key) this.hitTargets.add(key);
334
46
  }
335
- const targeting = resolveSkillTargeting(player, skillId, options);
336
- if (!targeting || !target) {
337
- playActionBattleAnimation("castSkill", player, options.animations, { skill: skillData });
338
- player.useSkill(skillId);
339
- return;
47
+ tryHit(target) {
48
+ if (!this.canHit(target)) return false;
49
+ this.recordHit(target);
50
+ return true;
340
51
  }
341
- const tileSize = getTileSize(map);
342
- const origin = getEntityTile(player, tileSize);
343
- const targetTile = {
344
- x: target.x,
345
- y: target.y
346
- };
347
- if (manhattanDistance(origin, targetTile) > targeting.range) return;
348
- const mask = parseAoeMask(targeting.aoeMask || options.skills?.defaultAoeMask);
349
- const affected = /* @__PURE__ */ new Set();
350
- mask.cells.forEach((cell) => {
351
- const x = targetTile.x + cell.dx;
352
- const y = targetTile.y + cell.dy;
353
- affected.add(`${x},${y}`);
354
- });
355
- const targets = [];
356
- const affects = options.targeting?.affects || "events";
357
- if (affects === "events" || affects === "both") map.getEvents().forEach((event) => {
358
- const tile = getEntityTile(event, tileSize);
359
- if (affected.has(`${tile.x},${tile.y}`)) targets.push(event);
360
- });
361
- if (affects === "players" || affects === "both") map.getPlayers().forEach((other) => {
362
- if (other.id === player.id) return;
363
- const tile = getEntityTile(other, tileSize);
364
- if (affected.has(`${tile.x},${tile.y}`)) targets.push(other);
365
- });
366
- if (!options.targeting?.allowEmptyTarget && targets.length === 0) return;
367
- playActionBattleAnimation("castSkill", player, options.animations, {
368
- skill: skillData,
369
- target: targets[0]
370
- });
371
- player.useSkill(skillId, targets);
372
- };
373
- var createActionBattleServer = (rawOptions = {}) => {
374
- const options = normalizeActionBattleOptions(rawOptions);
375
- setActionBattleOptions(options);
376
- setActionBattleSystems(options);
377
- return defineModule({
378
- player: {
379
- /**
380
- * Handle player input for combat actions
381
- *
382
- * When a player presses the action key, create an attack hitbox
383
- * that can damage AI enemies within range and knockback the event.
384
- * Knockback force is based on the player's equipped weapon.
385
- * Triggers attack animation and visual effects.
386
- *
387
- * @param player - The player performing the action
388
- * @param input - Input data containing pressed keys
389
- */
390
- onInput(player, input) {
391
- if (input.action == Control.Action) {
392
- const map = player.getCurrentMap();
393
- const hitboxes = resolvePlayerAttackHitboxes(player, player.getDirection(), options);
394
- if (isActionReservedForNormalEvent(player, map, hitboxes)) return;
395
- const lockMovement = options.attack?.lockMovement !== false;
396
- const lockDurationMs = options.attack?.lockDurationMs ?? DEFAULT_ATTACK_LOCK_DURATION_MS;
397
- let movementLocked = false;
398
- if (lockMovement && !beginPlayerAttackLock(player, map, Math.max(0, lockDurationMs))) return;
399
- movementLocked = lockMovement && lockDurationMs > 0;
400
- playActionBattleAnimation("attack", player, options.animations);
401
- if (movementLocked) player.animationFixed = true;
402
- map?.createMovingHitbox(hitboxes, { speed: 3 }).subscribe({ next(hits) {
403
- hits.forEach((hit) => {
404
- if (hit instanceof RpgEvent) {
405
- if (applyPlayerHitToEvent(player, hit)?.defeated) console.log(`Player ${player.id} defeated AI ${hit.id}`);
406
- }
407
- });
408
- } });
409
- }
410
- },
411
- onConnected(player) {
412
- if (options.ui?.actionBar?.enabled && options.ui?.actionBar?.autoOpen) openActionBattleActionBar(player, options);
413
- }
414
- },
415
- event: {
416
- /**
417
- * Handle player detection when entering AI vision
418
- *
419
- * Called when a player enters an AI event's vision range.
420
- * The AI will start pursuing and attacking the player.
421
- *
422
- * @param event - The AI event
423
- * @param player - The player entering vision
424
- * @param shape - The vision shape
425
- */
426
- onDetectInShape(event, player, shape) {
427
- event.battleAi?.onDetectInShape(player, shape);
428
- },
429
- /**
430
- * Handle player leaving AI vision
431
- *
432
- * Called when a player leaves an AI event's vision range.
433
- * The AI will stop pursuing the player.
434
- *
435
- * @param event - The AI event
436
- * @param player - The player leaving vision
437
- * @param shape - The vision shape
438
- */
439
- onDetectOutShape(event, player, shape) {
440
- event.battleAi?.onDetectOutShape(player, shape);
441
- }
442
- }
443
- });
444
52
  };
445
- var server_default = createActionBattleServer();
446
53
  //#endregion
447
- export { ACTION_BATTLE_ACTION_BAR_GUI_ID, DEFAULT_PLAYER_ATTACK_HITBOXES, applyPlayerHitToEvent, createActionBattleServer, server_default as default, getPlayerWeaponKnockbackForce, openActionBattleActionBar, updateActionBattleActionBar };
54
+ export { ACTION_BATTLE_HITBOX_FRAME_MS, ActionBattleHitTracker, createActionBattleAttackId, getNormalizedActionBattleAttackProfile, resolveActionBattleHitboxSpeed, scheduleActionBattleStartup };