@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.
Files changed (55) hide show
  1. package/README.md +137 -0
  2. package/dist/ai.server.d.ts +8 -1
  3. package/dist/client/index.js +19 -31
  4. package/dist/client/index10.js +142 -29
  5. package/dist/client/index11.js +25 -0
  6. package/dist/client/index12.js +1222 -0
  7. package/dist/client/index13.js +46 -0
  8. package/dist/client/index14.js +10 -0
  9. package/dist/client/index15.js +448 -0
  10. package/dist/client/index2.js +93 -46
  11. package/dist/client/index3.js +82 -1329
  12. package/dist/client/index4.js +305 -344
  13. package/dist/client/index5.js +36 -291
  14. package/dist/client/index6.js +99 -95
  15. package/dist/client/index7.js +78 -61
  16. package/dist/client/index8.js +57 -65
  17. package/dist/client/index9.js +97 -62
  18. package/dist/client.d.ts +3 -2
  19. package/dist/core/context.d.ts +5 -0
  20. package/dist/core/defaults.d.ts +81 -0
  21. package/dist/core/hit.d.ts +2 -0
  22. package/dist/enemies/factory.d.ts +7 -0
  23. package/dist/index.d.ts +9 -4
  24. package/dist/server/index.js +18 -31
  25. package/dist/server/index10.js +10 -0
  26. package/dist/server/index2.js +59 -345
  27. package/dist/server/index3.js +92 -1329
  28. package/dist/server/index4.js +141 -67
  29. package/dist/server/index5.js +24 -29
  30. package/dist/server/index6.js +1219 -62
  31. package/dist/server/index7.js +37 -0
  32. package/dist/server/index8.js +46 -0
  33. package/dist/server/index9.js +447 -0
  34. package/dist/server.d.ts +5 -3
  35. package/dist/ui/state.d.ts +20 -3
  36. package/package.json +5 -5
  37. package/src/ai.server.ts +91 -24
  38. package/src/animations.ts +43 -4
  39. package/src/canvas-engine-shim.ts +4 -0
  40. package/src/client.ts +122 -2
  41. package/src/components/action-bar.ce +5 -3
  42. package/src/components/attack-preview.ce +90 -0
  43. package/src/config.ts +30 -0
  44. package/src/core/context.ts +35 -0
  45. package/src/core/contracts.ts +123 -0
  46. package/src/core/defaults.ts +162 -0
  47. package/src/core/hit.spec.ts +58 -0
  48. package/src/core/hit.ts +66 -0
  49. package/src/enemies/factory.ts +25 -0
  50. package/src/index.ts +40 -0
  51. package/src/server.ts +235 -71
  52. package/src/targeting.spec.ts +24 -0
  53. package/src/types/canvas-engine.d.ts +4 -0
  54. package/src/types.ts +46 -1
  55. package/src/ui/state.ts +57 -0
@@ -1,347 +1,61 @@
1
- import { RpgEvent } from "@rpgjs/server";
2
- import { defineModule, Control } from "@rpgjs/common";
3
- import { DEFAULT_KNOCKBACK } from "./index3.js";
4
- import { normalizeActionBattleOptions, setActionBattleOptions } from "./index4.js";
5
- import { manhattanDistance, parseAoeMask } from "./index5.js";
6
- import { playActionBattleAnimation } from "./index6.js";
7
- const ACTION_BATTLE_ACTION_BAR_GUI_ID = "action-battle-action-bar";
8
- const DEFAULT_PLAYER_ATTACK_HITBOXES = {
9
- up: { offsetX: -16, offsetY: -48, width: 32, height: 32 },
10
- down: { offsetX: -16, offsetY: 16, width: 32, height: 32 },
11
- left: { offsetX: -48, offsetY: -16, width: 32, height: 32 },
12
- right: { offsetX: 16, offsetY: -16, width: 32, height: 32 },
13
- default: { offsetX: 0, offsetY: -32, width: 32, height: 32 }
14
- };
15
- function getPlayerWeaponKnockbackForce(player) {
16
- try {
17
- const equipments = player.equipments?.() || [];
18
- for (const item of equipments) {
19
- const itemData = player.databaseById?.(item.id());
20
- if (itemData?._type === "weapon" && itemData.knockbackForce !== void 0) {
21
- return itemData.knockbackForce;
22
- }
23
- }
24
- } catch {
25
- }
26
- return DEFAULT_KNOCKBACK.force;
1
+ var DEFAULT_ANIMATION_BY_KEY = {
2
+ attack: "attack",
3
+ hurt: "hurt",
4
+ die: "die",
5
+ castSkill: "skill",
6
+ castSpell: "skill"
7
+ };
8
+ var getConfiguredAnimation = (key, animations) => {
9
+ if (!animations) return {
10
+ hasConfiguredAnimation: false,
11
+ configured: void 0
12
+ };
13
+ const hasConfiguredAnimation = Object.prototype.hasOwnProperty.call(animations, key);
14
+ if (hasConfiguredAnimation) return {
15
+ hasConfiguredAnimation,
16
+ configured: animations[key]
17
+ };
18
+ if (key === "castSkill") return {
19
+ hasConfiguredAnimation: Object.prototype.hasOwnProperty.call(animations, "castSpell"),
20
+ configured: animations.castSpell
21
+ };
22
+ return {
23
+ hasConfiguredAnimation: false,
24
+ configured: void 0
25
+ };
26
+ };
27
+ function resolveActionBattleAnimation(key, entity, animations, context, defaults = {}) {
28
+ const defaultAnimationName = defaults.animationName ?? DEFAULT_ANIMATION_BY_KEY[key];
29
+ const defaultRepeat = defaults.repeat ?? 1;
30
+ const { hasConfiguredAnimation, configured: configuredAnimation } = getConfiguredAnimation(key, animations);
31
+ if (!hasConfiguredAnimation && key !== "attack") return null;
32
+ const configured = hasConfiguredAnimation ? configuredAnimation : defaultAnimationName;
33
+ const result = typeof configured === "function" ? configured(entity, context) : configured;
34
+ if (result == null) return null;
35
+ if (typeof result === "string") return {
36
+ animationName: result,
37
+ repeat: defaultRepeat,
38
+ waitEnd: false
39
+ };
40
+ return {
41
+ animationName: result.animationName ?? defaultAnimationName,
42
+ graphic: result.graphic,
43
+ repeat: result.repeat ?? defaultRepeat,
44
+ waitEnd: result.waitEnd ?? false,
45
+ delayMs: result.delayMs
46
+ };
27
47
  }
28
- function applyPlayerHitToEvent(player, target, hooks) {
29
- const ai = target.battleAi;
30
- if (!ai) return void 0;
31
- const knockbackForce = getPlayerWeaponKnockbackForce(player);
32
- const defeated = ai.takeDamage(player);
33
- const dx = target.x() - player.x();
34
- const dy = target.y() - player.y();
35
- const distance = Math.sqrt(dx * dx + dy * dy);
36
- let hitResult = {
37
- damage: 0,
38
- // Will be set by takeDamage internally
39
- knockbackForce,
40
- knockbackDuration: DEFAULT_KNOCKBACK.duration,
41
- defeated,
42
- attacker: player,
43
- target
44
- };
45
- if (hooks?.onBeforeHit) {
46
- const modified = hooks.onBeforeHit(hitResult);
47
- if (modified) {
48
- hitResult = modified;
49
- }
50
- }
51
- if (!hitResult.defeated && hitResult.knockbackForce > 0 && distance > 0) {
52
- const knockbackDirection = {
53
- x: dx / distance,
54
- y: dy / distance
55
- };
56
- target.knockback(knockbackDirection, hitResult.knockbackForce, hitResult.knockbackDuration);
57
- }
58
- if (hooks?.onAfterHit) {
59
- hooks.onAfterHit(hitResult);
60
- }
61
- return hitResult;
48
+ function playActionBattleAnimation(key, entity, animations, context, defaults = {}) {
49
+ const animation = resolveActionBattleAnimation(key, entity, animations, context, defaults);
50
+ if (!animation) return null;
51
+ if (animation.graphic !== void 0) entity.setGraphicAnimation(animation.animationName, animation.graphic, animation.repeat);
52
+ else entity.setGraphicAnimation(animation.animationName, animation.repeat);
53
+ return animation;
62
54
  }
63
- const resolveSignal = (value) => typeof value === "function" ? value() : value;
64
- const resolveItemData = (player, itemId) => {
65
- try {
66
- return player.databaseById?.(itemId);
67
- } catch {
68
- return null;
69
- }
70
- };
71
- const resolveSkillData = (player, skillId) => {
72
- try {
73
- return player.databaseById?.(skillId);
74
- } catch {
75
- return null;
76
- }
77
- };
78
- const resolveSkillTargeting = (player, skillId, options) => {
79
- const skillsOptions = options.skills;
80
- const skillData = resolveSkillData(player, skillId);
81
- if (skillsOptions?.getTargeting) {
82
- return skillsOptions.getTargeting(skillData);
83
- }
84
- const range = skillData?.range ?? skillData?.targeting?.range ?? skillData?.targeting?.distance;
85
- const aoeMask = skillData?.aoeMask ?? skillData?.targeting?.aoeMask ?? skillData?.targeting?.mask;
86
- if (range === void 0 && aoeMask === void 0) {
87
- return null;
88
- }
89
- return {
90
- range: range ?? 0,
91
- aoeMask
92
- };
93
- };
94
- const normalizeMaskRows = (mask) => {
95
- if (!mask) return [];
96
- if (Array.isArray(mask)) return mask;
97
- return mask.trim().split("\n").map((row) => row.replace(/\r/g, ""));
98
- };
99
- const buildActionBarData = (player, options) => {
100
- const items = (player.items?.() || []).map((item) => {
101
- const id = item.id?.() ?? item.id;
102
- const data = resolveItemData(player, id);
103
- const name = resolveSignal(data?.name) ?? resolveSignal(item.name) ?? id;
104
- const description = resolveSignal(data?.description) ?? resolveSignal(item.description) ?? "";
105
- const icon = resolveSignal(data?.icon) ?? resolveSignal(item.icon);
106
- const quantity = resolveSignal(item.quantity) ?? 1;
107
- const consumable = resolveSignal(data?.consumable);
108
- const itemType = resolveSignal(data?._type);
109
- const usable = quantity > 0 && consumable !== false && (itemType ? itemType === "item" : true);
110
- return {
111
- id,
112
- name,
113
- description,
114
- icon,
115
- quantity,
116
- usable
117
- };
118
- });
119
- const skills = (player.skills?.() || []).map((skill) => {
120
- const id = skill.id?.() ?? skill.id;
121
- const data = resolveSkillData(player, id) || skill;
122
- const name = resolveSignal(data?.name) ?? resolveSignal(skill.name) ?? id;
123
- const description = resolveSignal(data?.description) ?? resolveSignal(skill.description) ?? "";
124
- const icon = resolveSignal(data?.icon) ?? resolveSignal(skill.icon);
125
- const spCost = resolveSignal(data?.spCost) ?? resolveSignal(skill.spCost) ?? 0;
126
- const usable = spCost <= player.sp;
127
- const targeting = resolveSkillTargeting(player, id, options);
128
- const skillEntry = {
129
- id,
130
- name,
131
- description,
132
- icon,
133
- spCost,
134
- usable,
135
- range: targeting?.range ?? 0
136
- };
137
- if (targeting) {
138
- const mask = targeting.aoeMask ?? options.skills?.defaultAoeMask;
139
- if (mask) {
140
- skillEntry.aoeMask = normalizeMaskRows(mask);
141
- }
142
- }
143
- return skillEntry;
144
- });
145
- return { items, skills };
146
- };
147
- const ensureActionBarGui = (player, options) => {
148
- const existing = player.getGui?.(ACTION_BATTLE_ACTION_BAR_GUI_ID);
149
- const gui = existing || player.gui(ACTION_BATTLE_ACTION_BAR_GUI_ID);
150
- if (!gui.__actionBattleReady) {
151
- gui.__actionBattleReady = true;
152
- gui.on("useItem", ({ id }) => {
153
- try {
154
- player.useItem(id);
155
- } catch {
156
- }
157
- gui.update(buildActionBarData(player, options));
158
- });
159
- gui.on("useSkill", ({ id, target }) => {
160
- handleActionBattleSkillUse(player, id, target, options);
161
- gui.update(buildActionBarData(player, options));
162
- });
163
- gui.on("refresh", () => {
164
- gui.update(buildActionBarData(player, options));
165
- });
166
- }
167
- return gui;
168
- };
169
- const openActionBattleActionBar = (player, rawOptions = {}) => {
170
- const options = normalizeActionBattleOptions(rawOptions);
171
- const gui = ensureActionBarGui(player, options);
172
- gui.open(buildActionBarData(player, options));
173
- };
174
- const updateActionBattleActionBar = (player, rawOptions = {}) => {
175
- const options = normalizeActionBattleOptions(rawOptions);
176
- const gui = player.getGui?.(ACTION_BATTLE_ACTION_BAR_GUI_ID);
177
- if (gui) {
178
- gui.update(buildActionBarData(player, options));
179
- }
180
- };
181
- const getTileSize = (map) => ({
182
- width: map?.tileWidth ?? 32,
183
- height: map?.tileHeight ?? 32
184
- });
185
- const getEntityTile = (entity, tileSize) => {
186
- const hitbox = entity.hitbox?.() || { w: tileSize.width, h: tileSize.height };
187
- const x = Math.floor((entity.x() + hitbox.w / 2) / tileSize.width);
188
- const y = Math.floor((entity.y() + hitbox.h / 2) / tileSize.height);
189
- return { x, y };
190
- };
191
- const handleActionBattleSkillUse = (player, skillId, target, options) => {
192
- const skillData = resolveSkillData(player, skillId);
193
- const map = player.getCurrentMap();
194
- if (!map) {
195
- playActionBattleAnimation("castSkill", player, options.animations, {
196
- skill: skillData
197
- });
198
- player.useSkill(skillId);
199
- return;
200
- }
201
- const targeting = resolveSkillTargeting(player, skillId, options);
202
- if (!targeting || !target) {
203
- playActionBattleAnimation("castSkill", player, options.animations, {
204
- skill: skillData
205
- });
206
- player.useSkill(skillId);
207
- return;
208
- }
209
- const tileSize = getTileSize(map);
210
- const origin = getEntityTile(player, tileSize);
211
- const targetTile = { x: target.x, y: target.y };
212
- if (manhattanDistance(origin, targetTile) > targeting.range) {
213
- return;
214
- }
215
- const mask = parseAoeMask(
216
- targeting.aoeMask || options.skills?.defaultAoeMask
217
- );
218
- const affected = /* @__PURE__ */ new Set();
219
- mask.cells.forEach((cell) => {
220
- const x = targetTile.x + cell.dx;
221
- const y = targetTile.y + cell.dy;
222
- affected.add(`${x},${y}`);
223
- });
224
- const targets = [];
225
- const affects = options.targeting?.affects || "events";
226
- if (affects === "events" || affects === "both") {
227
- map.getEvents().forEach((event) => {
228
- const tile = getEntityTile(event, tileSize);
229
- if (affected.has(`${tile.x},${tile.y}`)) {
230
- targets.push(event);
231
- }
232
- });
233
- }
234
- if (affects === "players" || affects === "both") {
235
- map.getPlayers().forEach((other) => {
236
- if (other.id === player.id) return;
237
- const tile = getEntityTile(other, tileSize);
238
- if (affected.has(`${tile.x},${tile.y}`)) {
239
- targets.push(other);
240
- }
241
- });
242
- }
243
- if (!options.targeting?.allowEmptyTarget && targets.length === 0) {
244
- return;
245
- }
246
- playActionBattleAnimation("castSkill", player, options.animations, {
247
- skill: skillData,
248
- target: targets[0]
249
- });
250
- player.useSkill(skillId, targets);
251
- };
252
- const createActionBattleServer = (rawOptions = {}) => {
253
- const options = normalizeActionBattleOptions(rawOptions);
254
- setActionBattleOptions(options);
255
- return defineModule({
256
- player: {
257
- /**
258
- * Handle player input for combat actions
259
- *
260
- * When a player presses the action key, create an attack hitbox
261
- * that can damage AI enemies within range and knockback the event.
262
- * Knockback force is based on the player's equipped weapon.
263
- * Triggers attack animation and visual effects.
264
- *
265
- * @param player - The player performing the action
266
- * @param input - Input data containing pressed keys
267
- */
268
- onInput(player, input) {
269
- if (input.action == Control.Action) {
270
- playActionBattleAnimation("attack", player, options.animations);
271
- const playerX = player.x();
272
- const playerY = player.y();
273
- const direction = player.getDirection();
274
- const directionKey = direction;
275
- const hitboxConfig = DEFAULT_PLAYER_ATTACK_HITBOXES[directionKey] || DEFAULT_PLAYER_ATTACK_HITBOXES.default;
276
- const hitboxes = [
277
- {
278
- x: playerX + hitboxConfig.offsetX,
279
- y: playerY + hitboxConfig.offsetY,
280
- width: hitboxConfig.width,
281
- height: hitboxConfig.height
282
- }
283
- ];
284
- const map = player.getCurrentMap();
285
- map?.createMovingHitbox(hitboxes, { speed: 3 }).subscribe({
286
- next(hits) {
287
- hits.forEach((hit) => {
288
- if (hit instanceof RpgEvent) {
289
- const result = applyPlayerHitToEvent(player, hit);
290
- if (result?.defeated) {
291
- console.log(`Player ${player.id} defeated AI ${hit.id}`);
292
- }
293
- }
294
- });
295
- }
296
- });
297
- }
298
- },
299
- onConnected(player) {
300
- if (options.ui?.actionBar?.enabled && options.ui?.actionBar?.autoOpen) {
301
- openActionBattleActionBar(player, options);
302
- }
303
- }
304
- },
305
- event: {
306
- /**
307
- * Handle player detection when entering AI vision
308
- *
309
- * Called when a player enters an AI event's vision range.
310
- * The AI will start pursuing and attacking the player.
311
- *
312
- * @param event - The AI event
313
- * @param player - The player entering vision
314
- * @param shape - The vision shape
315
- */
316
- onDetectInShape(event, player, shape) {
317
- const ai = event.battleAi;
318
- ai?.onDetectInShape(player, shape);
319
- },
320
- /**
321
- * Handle player leaving AI vision
322
- *
323
- * Called when a player leaves an AI event's vision range.
324
- * The AI will stop pursuing the player.
325
- *
326
- * @param event - The AI event
327
- * @param player - The player leaving vision
328
- * @param shape - The vision shape
329
- */
330
- onDetectOutShape(event, player, shape) {
331
- const ai = event.battleAi;
332
- ai?.onDetectOutShape(player, shape);
333
- }
334
- }
335
- });
336
- };
337
- const server = createActionBattleServer();
338
- export {
339
- ACTION_BATTLE_ACTION_BAR_GUI_ID,
340
- DEFAULT_PLAYER_ATTACK_HITBOXES,
341
- applyPlayerHitToEvent,
342
- createActionBattleServer,
343
- server as default,
344
- getPlayerWeaponKnockbackForce,
345
- openActionBattleActionBar,
346
- updateActionBattleActionBar
347
- };
55
+ function getActionBattleAnimationRemovalDelay(animation) {
56
+ if (!animation) return 0;
57
+ if (animation.delayMs !== void 0) return animation.delayMs;
58
+ return animation.waitEnd ? 500 : 0;
59
+ }
60
+ //#endregion
61
+ export { getActionBattleAnimationRemovalDelay, playActionBattleAnimation };