@rpgjs/action-battle 5.0.0-alpha.44 → 5.0.0-beta.10

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 (103) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/LICENSE +19 -0
  3. package/README.md +392 -22
  4. package/dist/{ai.server.d.ts → client/ai.server.d.ts} +90 -28
  5. package/dist/client/animations.d.ts +16 -0
  6. package/dist/{client.d.ts → client/client.d.ts} +3 -2
  7. package/dist/{config.d.ts → client/config.d.ts} +2 -0
  8. package/dist/client/core/attack-profile.d.ts +9 -0
  9. package/dist/client/core/attack-runtime.d.ts +20 -0
  10. package/dist/client/core/context.d.ts +5 -0
  11. package/dist/client/core/defaults.d.ts +81 -0
  12. package/dist/client/core/enemy-attack-profiles.d.ts +6 -0
  13. package/dist/client/core/equipment.d.ts +2 -0
  14. package/dist/client/core/hit-reaction.d.ts +5 -0
  15. package/dist/client/core/hit.d.ts +2 -0
  16. package/dist/client/enemies/factory.d.ts +7 -0
  17. package/dist/client/index.d.ts +21 -0
  18. package/dist/client/index.js +24 -31
  19. package/dist/client/index10.js +61 -0
  20. package/dist/client/index11.js +55 -0
  21. package/dist/client/index12.js +106 -0
  22. package/dist/client/index13.js +143 -0
  23. package/dist/client/index14.js +25 -0
  24. package/dist/client/index15.js +72 -0
  25. package/dist/client/index16.js +1343 -0
  26. package/dist/client/index17.js +13 -0
  27. package/dist/client/index18.js +60 -0
  28. package/dist/client/index19.js +10 -0
  29. package/dist/client/index2.js +30 -45
  30. package/dist/client/index20.js +504 -0
  31. package/dist/client/index3.js +45 -1288
  32. package/dist/client/index4.js +105 -330
  33. package/dist/client/index5.js +84 -291
  34. package/dist/client/index6.js +309 -95
  35. package/dist/client/index7.js +35 -59
  36. package/dist/client/index8.js +101 -54
  37. package/dist/client/index9.js +79 -30
  38. package/dist/{server.d.ts → client/server.d.ts} +12 -4
  39. package/dist/client/ui/state.d.ts +35 -0
  40. package/dist/server/ai.server.d.ts +569 -0
  41. package/dist/server/animations.d.ts +16 -0
  42. package/dist/server/config.d.ts +5 -0
  43. package/dist/server/core/attack-profile.d.ts +9 -0
  44. package/dist/server/core/attack-runtime.d.ts +20 -0
  45. package/dist/server/core/context.d.ts +5 -0
  46. package/dist/server/core/defaults.d.ts +81 -0
  47. package/dist/server/core/enemy-attack-profiles.d.ts +6 -0
  48. package/dist/server/core/equipment.d.ts +2 -0
  49. package/dist/server/core/hit-reaction.d.ts +5 -0
  50. package/dist/server/core/hit.d.ts +2 -0
  51. package/dist/server/enemies/factory.d.ts +7 -0
  52. package/dist/server/index.d.ts +21 -0
  53. package/dist/server/index.js +23 -31
  54. package/dist/server/index10.js +1342 -0
  55. package/dist/server/index11.js +37 -0
  56. package/dist/server/index12.js +60 -0
  57. package/dist/server/index13.js +13 -0
  58. package/dist/server/index14.js +503 -0
  59. package/dist/server/index15.js +10 -0
  60. package/dist/server/index2.js +59 -332
  61. package/dist/server/index3.js +29 -1286
  62. package/dist/server/index4.js +45 -53
  63. package/dist/server/index5.js +107 -29
  64. package/dist/server/index6.js +143 -0
  65. package/dist/server/index7.js +25 -0
  66. package/dist/server/index8.js +72 -0
  67. package/dist/server/index9.js +55 -0
  68. package/dist/server/server.d.ts +106 -0
  69. package/dist/server/targeting.d.ts +19 -0
  70. package/package.json +12 -12
  71. package/src/ai.server.spec.ts +120 -0
  72. package/src/ai.server.ts +515 -91
  73. package/src/animations.ts +149 -0
  74. package/src/canvas-engine-shim.ts +4 -0
  75. package/src/client.ts +130 -2
  76. package/src/components/action-bar.ce +5 -3
  77. package/src/components/attack-preview.ce +90 -0
  78. package/src/config.ts +61 -0
  79. package/src/core/attack-profile.spec.ts +118 -0
  80. package/src/core/attack-profile.ts +100 -0
  81. package/src/core/attack-runtime.spec.ts +103 -0
  82. package/src/core/attack-runtime.ts +83 -0
  83. package/src/core/context.ts +35 -0
  84. package/src/core/contracts.ts +126 -0
  85. package/src/core/defaults.ts +162 -0
  86. package/src/core/enemy-attack-profiles.spec.ts +35 -0
  87. package/src/core/enemy-attack-profiles.ts +103 -0
  88. package/src/core/equipment.spec.ts +37 -0
  89. package/src/core/equipment.ts +17 -0
  90. package/src/core/hit-reaction.spec.ts +43 -0
  91. package/src/core/hit-reaction.ts +70 -0
  92. package/src/core/hit.spec.ts +111 -0
  93. package/src/core/hit.ts +92 -0
  94. package/src/enemies/factory.ts +25 -0
  95. package/src/index.ts +94 -1
  96. package/src/server.ts +427 -93
  97. package/src/targeting.spec.ts +24 -0
  98. package/src/types/canvas-engine.d.ts +4 -0
  99. package/src/types.ts +148 -0
  100. package/src/ui/state.ts +57 -0
  101. package/dist/index.d.ts +0 -11
  102. package/dist/ui/state.d.ts +0 -18
  103. /package/dist/{targeting.d.ts → client/targeting.d.ts} +0 -0
@@ -0,0 +1,106 @@
1
+ import { normalizeActionBattleOptions } from "./index4.js";
2
+ import { setActionBattleOptions, startAttackPreview, stopAttackPreview } from "./index5.js";
3
+ import __ce_component from "./index6.js";
4
+ import __ce_component$1 from "./index8.js";
5
+ import __ce_component$2 from "./index9.js";
6
+ import { resolveActionBattleAnimation } from "./index10.js";
7
+ import { getNormalizedActionBattleAttackProfile } from "./index11.js";
8
+ import { PrebuiltComponentAnimations, RpgClientEngine, RpgGui, inject } from "@rpgjs/client";
9
+ import { defineModule } from "@rpgjs/common";
10
+ //#region src/client.ts
11
+ var DEFAULT_ATTACK_LOCK_DURATION_MS = 350;
12
+ var beginLocalPlayerAttackLock = (engine, durationMs, locks) => {
13
+ if (durationMs <= 0) return true;
14
+ const player = engine.scene?.getCurrentPlayer?.();
15
+ if (!player) return true;
16
+ const runtimePlayer = player;
17
+ const now = Date.now();
18
+ if (typeof runtimePlayer.__actionBattleAttackLockedUntil === "number" && runtimePlayer.__actionBattleAttackLockedUntil > now) return false;
19
+ const lockId = (runtimePlayer.__actionBattleAttackLockId ?? 0) + 1;
20
+ runtimePlayer.__actionBattleAttackLockId = lockId;
21
+ runtimePlayer.__actionBattleAttackLockedUntil = now + durationMs;
22
+ const previousCanMove = player.canMove;
23
+ const previousDirectionFixed = player.directionFixed;
24
+ const previousAnimationFixed = player.animationFixed;
25
+ if (locks.movement) {
26
+ if (typeof engine.interruptCurrentPlayerMovement === "function") engine.interruptCurrentPlayerMovement(player);
27
+ else engine.scene?.stopMovement?.(player);
28
+ player.canMove = false;
29
+ }
30
+ if (locks.direction) player.directionFixed = true;
31
+ player.animationFixed = true;
32
+ setTimeout(() => {
33
+ if (runtimePlayer.__actionBattleAttackLockId !== lockId) return;
34
+ runtimePlayer.__actionBattleAttackLockedUntil = 0;
35
+ player.canMove = previousCanMove;
36
+ player.directionFixed = previousDirectionFixed;
37
+ player.animationFixed = previousAnimationFixed;
38
+ }, durationMs);
39
+ return true;
40
+ };
41
+ var resolveLocalPlayerDirection = (player) => {
42
+ if (typeof player.getDirection === "function") return player.getDirection();
43
+ if (typeof player.direction === "function") return player.direction();
44
+ return player.direction ?? "down";
45
+ };
46
+ var playLocalPlayerAttackAnimation = (player, options) => {
47
+ if (!player || typeof player.setAnimation !== "function") return;
48
+ const animation = resolveActionBattleAnimation("attack", player, options.animations);
49
+ if (!animation) return;
50
+ if (animation.graphic !== void 0) {
51
+ player.setAnimation(animation.animationName, animation.graphic, animation.repeat);
52
+ return;
53
+ }
54
+ player.setAnimation(animation.animationName, animation.repeat);
55
+ };
56
+ var showLocalAttackPreview = (player, options) => {
57
+ if (!player || options.attack?.showPreview === false) return;
58
+ const durationMs = Math.max(1, options.attack?.previewDurationMs ?? 180);
59
+ const previewId = startAttackPreview({
60
+ direction: resolveLocalPlayerDirection(player),
61
+ durationMs,
62
+ color: options.attack?.previewColor,
63
+ accentColor: options.attack?.previewAccentColor
64
+ });
65
+ setTimeout(() => stopAttackPreview(previewId), durationMs);
66
+ };
67
+ var createActionBattleClient = (options = {}) => {
68
+ const normalized = normalizeActionBattleOptions(options);
69
+ setActionBattleOptions(normalized);
70
+ const actionBarEnabled = normalized.ui?.actionBar?.enabled;
71
+ const componentsInFront = [...normalized.ui?.targeting?.enabled ? [__ce_component$1] : [], __ce_component$2];
72
+ const hitComponent = PrebuiltComponentAnimations?.Hit;
73
+ return defineModule({
74
+ componentAnimations: hitComponent ? [{
75
+ id: "hit",
76
+ component: hitComponent
77
+ }] : [],
78
+ gui: actionBarEnabled ? [{
79
+ id: "action-battle-action-bar",
80
+ component: __ce_component,
81
+ dependencies: () => {
82
+ return [inject(RpgClientEngine).scene.currentPlayer];
83
+ }
84
+ }] : [],
85
+ sprite: { componentsInFront },
86
+ sceneMap: { onAfterLoading() {
87
+ if (actionBarEnabled && normalized.ui?.actionBar?.autoOpen) inject(RpgGui).display("action-battle-action-bar");
88
+ } },
89
+ engine: { onInput(engine, { input }) {
90
+ if (input !== "action") return;
91
+ const player = engine.scene?.getCurrentPlayer?.();
92
+ if (!player) return;
93
+ const attackProfile = getNormalizedActionBattleAttackProfile(normalized);
94
+ const lockDurationMs = Math.max(0, attackProfile.totalDurationMs ?? DEFAULT_ATTACK_LOCK_DURATION_MS);
95
+ if (attackProfile.movementLock || attackProfile.directionLock) beginLocalPlayerAttackLock(engine, lockDurationMs, {
96
+ movement: attackProfile.movementLock,
97
+ direction: attackProfile.directionLock
98
+ });
99
+ playLocalPlayerAttackAnimation(player, normalized);
100
+ showLocalAttackPreview(player, normalized);
101
+ } }
102
+ });
103
+ };
104
+ var client_default = createActionBattleClient();
105
+ //#endregion
106
+ export { createActionBattleClient, client_default as default };
@@ -0,0 +1,143 @@
1
+ //#region src/core/defaults.ts
2
+ var DEFAULT_CORE_KNOCKBACK = {
3
+ force: 50,
4
+ duration: 300
5
+ };
6
+ var CoreAttackPattern = {
7
+ Melee: "melee",
8
+ Combo: "combo",
9
+ Charged: "charged",
10
+ Zone: "zone",
11
+ DashAttack: "dashAttack"
12
+ };
13
+ var CoreEnemyType = {
14
+ Aggressive: "aggressive",
15
+ Defensive: "defensive",
16
+ Ranged: "ranged",
17
+ Tank: "tank",
18
+ Berserker: "berserker"
19
+ };
20
+ var DEFAULT_ZELDA_PLAYER_HITBOXES = {
21
+ up: {
22
+ offsetX: -16,
23
+ offsetY: -48,
24
+ width: 32,
25
+ height: 32
26
+ },
27
+ down: {
28
+ offsetX: -16,
29
+ offsetY: 16,
30
+ width: 32,
31
+ height: 32
32
+ },
33
+ left: {
34
+ offsetX: -48,
35
+ offsetY: -16,
36
+ width: 32,
37
+ height: 32
38
+ },
39
+ right: {
40
+ offsetX: 16,
41
+ offsetY: -16,
42
+ width: 32,
43
+ height: 32
44
+ },
45
+ default: {
46
+ offsetX: 0,
47
+ offsetY: -32,
48
+ width: 32,
49
+ height: 32
50
+ }
51
+ };
52
+ var resolveEquippedWeapon = (entity) => {
53
+ const equipments = entity?.equipments?.() || [];
54
+ for (const item of equipments) {
55
+ const itemId = item?.id?.() ?? item?.id;
56
+ const itemData = entity?.databaseById?.(itemId);
57
+ if (itemData?._type === "weapon") return itemData;
58
+ }
59
+ return null;
60
+ };
61
+ var resolveDirection = (attacker, target) => {
62
+ const dx = target.x() - attacker.x();
63
+ const dy = target.y() - attacker.y();
64
+ const distance = Math.sqrt(dx * dx + dy * dy);
65
+ if (distance <= 0) return void 0;
66
+ return {
67
+ x: dx / distance,
68
+ y: dy / distance
69
+ };
70
+ };
71
+ var createDefaultPlayerHitboxResolver = (hitboxes = DEFAULT_ZELDA_PLAYER_HITBOXES) => (context) => {
72
+ const attacker = context.attacker;
73
+ const config = hitboxes[context.direction ?? (typeof attacker.getDirection === "function" ? attacker.getDirection() : "default")] || hitboxes.default;
74
+ return [{
75
+ x: attacker.x() + config.offsetX,
76
+ y: attacker.y() + config.offsetY,
77
+ width: config.width,
78
+ height: config.height
79
+ }];
80
+ };
81
+ var defaultRpgjsDamageResolver = (context) => {
82
+ const target = context.target;
83
+ const raw = target.applyDamage(context.attacker, context.skill);
84
+ return {
85
+ damage: raw?.damage ?? 0,
86
+ defeated: target.hp <= 0,
87
+ raw
88
+ };
89
+ };
90
+ var defaultKnockbackResolver = (context) => {
91
+ const weapon = context.weapon ?? resolveEquippedWeapon(context.attacker);
92
+ return {
93
+ force: weapon?.knockbackForce ?? DEFAULT_CORE_KNOCKBACK.force,
94
+ duration: weapon?.knockbackDuration ?? DEFAULT_CORE_KNOCKBACK.duration,
95
+ direction: resolveDirection(context.attacker, context.target)
96
+ };
97
+ };
98
+ var defaultCombatSystem = {
99
+ resolveHitboxes: createDefaultPlayerHitboxResolver(),
100
+ resolveDamage: defaultRpgjsDamageResolver,
101
+ resolveKnockback: defaultKnockbackResolver
102
+ };
103
+ var defaultEnemyBehaviors = {
104
+ [CoreEnemyType.Aggressive]: ({ hpPercent }) => ({
105
+ mode: hpPercent !== null && hpPercent < .15 ? "retreat" : "assault",
106
+ attackPatterns: [
107
+ CoreAttackPattern.Melee,
108
+ CoreAttackPattern.Combo,
109
+ CoreAttackPattern.DashAttack
110
+ ]
111
+ }),
112
+ [CoreEnemyType.Defensive]: ({ hpPercent }) => ({
113
+ mode: hpPercent !== null && hpPercent < .3 ? "retreat" : "tactical",
114
+ attackPatterns: [CoreAttackPattern.Melee, CoreAttackPattern.Charged]
115
+ }),
116
+ [CoreEnemyType.Ranged]: ({ distance }) => ({
117
+ mode: distance !== null && distance < 80 ? "retreat" : "tactical",
118
+ attackPatterns: [CoreAttackPattern.Melee, CoreAttackPattern.Zone]
119
+ }),
120
+ [CoreEnemyType.Tank]: () => ({
121
+ mode: "assault",
122
+ attackPatterns: [
123
+ CoreAttackPattern.Melee,
124
+ CoreAttackPattern.Charged,
125
+ CoreAttackPattern.Zone
126
+ ]
127
+ }),
128
+ [CoreEnemyType.Berserker]: ({ hpPercent }) => ({
129
+ mode: "assault",
130
+ attackCooldown: hpPercent === null ? void 0 : Math.max(250, 800 * Math.max(.3, hpPercent)),
131
+ attackPatterns: [
132
+ CoreAttackPattern.Melee,
133
+ CoreAttackPattern.Combo,
134
+ CoreAttackPattern.DashAttack
135
+ ]
136
+ })
137
+ };
138
+ var defaultActionBattleSystems = {
139
+ combat: defaultCombatSystem,
140
+ ai: { behaviors: defaultEnemyBehaviors }
141
+ };
142
+ //#endregion
143
+ export { DEFAULT_ZELDA_PLAYER_HITBOXES, createDefaultPlayerHitboxResolver, defaultActionBattleSystems, defaultCombatSystem, defaultEnemyBehaviors, defaultKnockbackResolver, defaultRpgjsDamageResolver };
@@ -0,0 +1,25 @@
1
+ import { defaultActionBattleSystems } from "./index13.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
11
+ }
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);
21
+ };
22
+ var getActionBattleSystems = () => currentActionBattleSystems;
23
+ var createActionBattleSystems = mergeSystems;
24
+ //#endregion
25
+ export { createActionBattleSystems, getActionBattleSystems, setActionBattleSystems };
@@ -0,0 +1,72 @@
1
+ import { normalizeActionBattleAttackProfile } from "./index3.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
+ }
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
+ }
71
+ //#endregion
72
+ export { DEFAULT_ACTION_BATTLE_ENEMY_ATTACK_PROFILES, normalizeActionBattleEnemyAttackProfiles };