@rpgjs/action-battle 5.0.0-beta.3 → 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 (52) hide show
  1. package/README.md +137 -0
  2. package/dist/ai.server.d.ts +8 -1
  3. package/dist/client/index.js +8 -4
  4. package/dist/client/index10.js +97 -330
  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 +30 -0
  11. package/dist/client/index3.js +33 -1
  12. package/dist/client/index4.js +7 -3
  13. package/dist/client/index7.js +76 -32
  14. package/dist/client/index8.js +24 -4
  15. package/dist/client/index9.js +94 -1165
  16. package/dist/core/context.d.ts +5 -0
  17. package/dist/core/defaults.d.ts +81 -0
  18. package/dist/core/hit.d.ts +2 -0
  19. package/dist/enemies/factory.d.ts +7 -0
  20. package/dist/index.d.ts +6 -1
  21. package/dist/server/index.js +7 -3
  22. package/dist/server/index10.js +10 -0
  23. package/dist/server/index2.js +23 -3
  24. package/dist/server/index3.js +30 -0
  25. package/dist/server/index4.js +137 -1163
  26. package/dist/server/index5.js +22 -34
  27. package/dist/server/index6.js +1190 -345
  28. package/dist/server/index7.js +37 -0
  29. package/dist/server/index8.js +46 -0
  30. package/dist/server/index9.js +447 -0
  31. package/dist/server.d.ts +2 -0
  32. package/dist/ui/state.d.ts +17 -0
  33. package/package.json +5 -5
  34. package/src/ai.server.ts +91 -24
  35. package/src/animations.ts +43 -4
  36. package/src/canvas-engine-shim.ts +4 -0
  37. package/src/client.ts +122 -2
  38. package/src/components/action-bar.ce +5 -3
  39. package/src/components/attack-preview.ce +90 -0
  40. package/src/config.ts +30 -0
  41. package/src/core/context.ts +35 -0
  42. package/src/core/contracts.ts +123 -0
  43. package/src/core/defaults.ts +162 -0
  44. package/src/core/hit.spec.ts +58 -0
  45. package/src/core/hit.ts +66 -0
  46. package/src/enemies/factory.ts +25 -0
  47. package/src/index.ts +40 -0
  48. package/src/server.ts +235 -71
  49. package/src/targeting.spec.ts +24 -0
  50. package/src/types/canvas-engine.d.ts +4 -0
  51. package/src/types.ts +46 -1
  52. package/src/ui/state.ts +57 -0
@@ -1,34 +1,78 @@
1
- import { normalizeActionBattleOptions } from "./index2.js";
2
- import { setActionBattleOptions } from "./index3.js";
3
- import component from "./index4.js";
4
- import component$1 from "./index6.js";
5
- import { PrebuiltComponentAnimations, RpgClientEngine, RpgGui, inject } from "@rpgjs/client";
6
- import { defineModule } from "@rpgjs/common";
7
- //#region src/client.ts
8
- var createActionBattleClient = (options = {}) => {
9
- const normalized = normalizeActionBattleOptions(options);
10
- setActionBattleOptions(normalized);
11
- const actionBarEnabled = normalized.ui?.actionBar?.enabled;
12
- const targetingEnabled = normalized.ui?.targeting?.enabled;
13
- const hitComponent = PrebuiltComponentAnimations?.Hit;
14
- return defineModule({
15
- componentAnimations: hitComponent ? [{
16
- id: "hit",
17
- component: hitComponent
18
- }] : [],
19
- gui: actionBarEnabled ? [{
20
- id: "action-battle-action-bar",
21
- component,
22
- dependencies: () => {
23
- return [inject(RpgClientEngine).scene.currentPlayer];
24
- }
25
- }] : [],
26
- sprite: { componentsInFront: targetingEnabled ? [component$1] : [] },
27
- sceneMap: { onAfterLoading() {
28
- if (actionBarEnabled && normalized.ui?.actionBar?.autoOpen) inject(RpgGui).display("action-battle-action-bar");
29
- } }
1
+ import { actionBattleAttackPreviewState } from "./index3.js";
2
+ import { RpgClientEngine, inject } from "@rpgjs/client";
3
+ import { Container, Graphics, computed, cond, h, signal, tick, useDefineProps, useProps } from "canvasengine";
4
+ //#region src/components/attack-preview.ce
5
+ function component($$props) {
6
+ useProps($$props);
7
+ const { object } = useDefineProps($$props)();
8
+ const engine = inject(RpgClientEngine);
9
+ const now = signal(Date.now());
10
+ tick(() => {
11
+ if (actionBattleAttackPreviewState().active) now.set(Date.now());
30
12
  });
31
- };
32
- var client_default = createActionBattleClient();
13
+ const isCurrentPlayer = computed(() => {
14
+ if (!object?.id) return false;
15
+ return (typeof object.id === "function" ? object.id() : object.id) === engine.playerId;
16
+ });
17
+ const preview = computed(() => actionBattleAttackPreviewState());
18
+ const progress = computed(() => {
19
+ const state = preview();
20
+ if (!state.active) return 1;
21
+ const elapsed = now() - state.startedAt;
22
+ return Math.max(0, Math.min(1, elapsed / state.durationMs));
23
+ });
24
+ const shouldRender = computed(() => {
25
+ const state = preview();
26
+ return isCurrentPlayer() && state.active && progress() < 1;
27
+ });
28
+ const getHitbox = () => object.hitbox?.() || {
29
+ w: 32,
30
+ h: 32
31
+ };
32
+ const drawRect = (g, x, y, width, height, color, alpha) => {
33
+ g.rect(x, y, width, height);
34
+ g.fill({
35
+ color,
36
+ alpha
37
+ });
38
+ };
39
+ const drawSlash = (g) => {
40
+ g.clear();
41
+ if (!shouldRender()) return;
42
+ const state = preview();
43
+ const p = progress();
44
+ const alpha = Math.sin(Math.PI * p);
45
+ if (alpha <= 0) return;
46
+ const hitbox = getHitbox();
47
+ const width = hitbox.w || 32;
48
+ const height = hitbox.h || 32;
49
+ const reach = 16 + 18 * p;
50
+ const thickness = 4 + 3 * (1 - p);
51
+ const color = state.color;
52
+ const accent = state.accentColor;
53
+ if (state.direction === "left") {
54
+ drawRect(g, -reach - 6, height * .24, reach, thickness, accent, alpha * .55);
55
+ drawRect(g, -reach - 10, height * .46, reach + 4, thickness + 2, color, alpha);
56
+ drawRect(g, -reach - 6, height * .7, reach, thickness, accent, alpha * .4);
57
+ return;
58
+ }
59
+ if (state.direction === "right") {
60
+ drawRect(g, width + 6, height * .24, reach, thickness, accent, alpha * .55);
61
+ drawRect(g, width + 6, height * .46, reach + 4, thickness + 2, color, alpha);
62
+ drawRect(g, width + 6, height * .7, reach, thickness, accent, alpha * .4);
63
+ return;
64
+ }
65
+ if (state.direction === "up") {
66
+ drawRect(g, width * .24, -reach - 6, thickness, reach, accent, alpha * .55);
67
+ drawRect(g, width * .46, -reach - 10, thickness + 2, reach + 4, color, alpha);
68
+ drawRect(g, width * .7, -reach - 6, thickness, reach, accent, alpha * .4);
69
+ return;
70
+ }
71
+ drawRect(g, width * .24, height + 6, thickness, reach, accent, alpha * .55);
72
+ drawRect(g, width * .46, height + 6, thickness + 2, reach + 4, color, alpha);
73
+ drawRect(g, width * .7, height + 6, thickness, reach, accent, alpha * .4);
74
+ };
75
+ return h(Container, null, cond(shouldRender, () => h(Graphics, { draw: drawSlash })));
76
+ }
33
77
  //#endregion
34
- export { createActionBattleClient, client_default as default };
78
+ export { component as default };
@@ -2,14 +2,34 @@ var DEFAULT_ANIMATION_BY_KEY = {
2
2
  attack: "attack",
3
3
  hurt: "hurt",
4
4
  die: "die",
5
- castSkill: "skill"
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
+ };
6
26
  };
7
27
  function resolveActionBattleAnimation(key, entity, animations, context, defaults = {}) {
8
28
  const defaultAnimationName = defaults.animationName ?? DEFAULT_ANIMATION_BY_KEY[key];
9
29
  const defaultRepeat = defaults.repeat ?? 1;
10
- const hasConfiguredAnimation = animations ? Object.prototype.hasOwnProperty.call(animations, key) : false;
30
+ const { hasConfiguredAnimation, configured: configuredAnimation } = getConfiguredAnimation(key, animations);
11
31
  if (!hasConfiguredAnimation && key !== "attack") return null;
12
- const configured = hasConfiguredAnimation ? animations?.[key] : defaultAnimationName;
32
+ const configured = hasConfiguredAnimation ? configuredAnimation : defaultAnimationName;
13
33
  const result = typeof configured === "function" ? configured(entity, context) : configured;
14
34
  if (result == null) return null;
15
35
  if (typeof result === "string") return {
@@ -38,4 +58,4 @@ function getActionBattleAnimationRemovalDelay(animation) {
38
58
  return animation.waitEnd ? 500 : 0;
39
59
  }
40
60
  //#endregion
41
- export { getActionBattleAnimationRemovalDelay, playActionBattleAnimation };
61
+ export { getActionBattleAnimationRemovalDelay, playActionBattleAnimation, resolveActionBattleAnimation };