@waica/behaviors 0.6.0 → 0.7.0

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/dist/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  export { PlatformerMotor } from './platformer-motor.js';
2
2
  export { PLAYER_ROLE, PLAYER_STATE_GRAPH, playerUpdate } from './player-states.js';
3
+ export { TopDownMotor, type TopDownFacing } from './topdown-motor.js';
4
+ export { TOPDOWN_PLAYER_ROLE, TOPDOWN_PLAYER_STATE_GRAPH, topdownPlayerUpdate, } from './topdown-player-states.js';
5
+ export { Interactable, interactUpdate } from './interactable.js';
3
6
  export { Collectible } from './collectible.js';
4
7
  export { Patrol, PATROLLER_ROLE, PATROLLER_STATE_GRAPH, type PatrolAxis } from './patrol.js';
5
8
  export { Chaser, CHASER_ROLE, CHASER_STATE_GRAPH, type ChaserMode } from './chaser.js';
package/dist/index.js CHANGED
@@ -1,5 +1,8 @@
1
1
  export { PlatformerMotor } from './platformer-motor.js';
2
2
  export { PLAYER_ROLE, PLAYER_STATE_GRAPH, playerUpdate } from './player-states.js';
3
+ export { TopDownMotor } from './topdown-motor.js';
4
+ export { TOPDOWN_PLAYER_ROLE, TOPDOWN_PLAYER_STATE_GRAPH, topdownPlayerUpdate, } from './topdown-player-states.js';
5
+ export { Interactable, interactUpdate } from './interactable.js';
3
6
  export { Collectible } from './collectible.js';
4
7
  export { Patrol, PATROLLER_ROLE, PATROLLER_STATE_GRAPH } from './patrol.js';
5
8
  export { Chaser, CHASER_ROLE, CHASER_STATE_GRAPH } from './chaser.js';
@@ -0,0 +1,31 @@
1
+ import { Component, type StateContext } from '@waica/engine';
2
+ /**
3
+ * Something the player can talk to or examine: a dialogue line and the
4
+ * radius it can be triggered from. The component is pure data — the
5
+ * player role's always-hook does the lookup (see interactUpdate), so an
6
+ * NPC stays code-free: Interactable + the npc role is a whole villager.
7
+ */
8
+ export declare class Interactable extends Component {
9
+ static componentName: string;
10
+ static params: {
11
+ line: {
12
+ label: string;
13
+ };
14
+ radius: {
15
+ label: string;
16
+ min: number;
17
+ max: number;
18
+ step: number;
19
+ };
20
+ };
21
+ /** What pressing interact within the radius says. */
22
+ line: string;
23
+ radius: number;
24
+ }
25
+ /**
26
+ * The player role's interact lookup, run by its '*' hook in every state:
27
+ * pressing interact near an Interactable publishes its line through the
28
+ * npcLine stat and shows the npc-line UI piece; walking out of every
29
+ * radius hides it again. Nearest one wins when several are in range.
30
+ */
31
+ export declare function interactUpdate({ entity, game }: StateContext): void;
@@ -0,0 +1,49 @@
1
+ import { Component } from '@waica/engine';
2
+ /**
3
+ * Something the player can talk to or examine: a dialogue line and the
4
+ * radius it can be triggered from. The component is pure data — the
5
+ * player role's always-hook does the lookup (see interactUpdate), so an
6
+ * NPC stays code-free: Interactable + the npc role is a whole villager.
7
+ */
8
+ export class Interactable extends Component {
9
+ static componentName = 'Interactable';
10
+ static params = {
11
+ line: { label: 'Line' },
12
+ radius: { label: 'Radius', min: 0.5, max: 10, step: 0.25 },
13
+ };
14
+ /** What pressing interact within the radius says. */
15
+ line = 'Hello, traveler!';
16
+ radius = 1.5;
17
+ }
18
+ /**
19
+ * The player role's interact lookup, run by its '*' hook in every state:
20
+ * pressing interact near an Interactable publishes its line through the
21
+ * npcLine stat and shows the npc-line UI piece; walking out of every
22
+ * radius hides it again. Nearest one wins when several are in range.
23
+ */
24
+ export function interactUpdate({ entity, game }) {
25
+ let nearest = null;
26
+ let nearestDistance = Infinity;
27
+ for (const other of game.entities) {
28
+ if (other === entity)
29
+ continue;
30
+ const interactable = other.get(Interactable);
31
+ if (!interactable)
32
+ continue;
33
+ const distance = Math.hypot(other.position.x - entity.position.x, other.position.y - entity.position.y);
34
+ if (distance <= interactable.radius && distance < nearestDistance) {
35
+ nearest = interactable;
36
+ nearestDistance = distance;
37
+ }
38
+ }
39
+ if (!nearest) {
40
+ game.ui.hide('npc-line');
41
+ return;
42
+ }
43
+ if (game.input.justPressed('interact') && !game.input.consumed('interact')) {
44
+ // The press is spent: an input:interact edge needs a NEW press.
45
+ game.input.consume('interact');
46
+ game.stats.set('npcLine', nearest.line);
47
+ game.ui.show('npc-line');
48
+ }
49
+ }
@@ -1,4 +1,4 @@
1
- import { Component } from '@waica/engine';
1
+ import { Component, type CameraVelocity, type CameraVelocityProvider } from '@waica/engine';
2
2
  /**
3
3
  * Passive platformer motor: tuning params, physical state and movement
4
4
  * methods for state code to call. It has no onUpdate of its own — the
@@ -9,7 +9,7 @@ import { Component } from '@waica/engine';
9
9
  * Per-axis collision against the scene's Solids — deterministic genre
10
10
  * movement (Celeste-style), not "realistic" physics.
11
11
  */
12
- export declare class PlatformerMotor extends Component {
12
+ export declare class PlatformerMotor extends Component implements CameraVelocityProvider {
13
13
  static componentName: string;
14
14
  static displayName: string;
15
15
  static params: {
@@ -103,6 +103,8 @@ export declare class PlatformerMotor extends Component {
103
103
  private bufferTimer;
104
104
  private squashX;
105
105
  private squashY;
106
+ /** The scene camera reads follow velocity through this explicit seam. */
107
+ getCameraVelocity(): CameraVelocity;
106
108
  /**
107
109
  * Per-frame bookkeeping: forgiveness timers, squash decay and the
108
110
  * facing flip. The logic set's '*' hook runs it in every state, so
@@ -1,4 +1,4 @@
1
- import { Component, resolveSolidAxis, THREE } from '@waica/engine';
1
+ import { Component, resolveSolidAxis, THREE, } from '@waica/engine';
2
2
  /**
3
3
  * Passive platformer motor: tuning params, physical state and movement
4
4
  * methods for state code to call. It has no onUpdate of its own — the
@@ -60,6 +60,10 @@ export class PlatformerMotor extends Component {
60
60
  bufferTimer = 0;
61
61
  squashX = 1;
62
62
  squashY = 1;
63
+ /** The scene camera reads follow velocity through this explicit seam. */
64
+ getCameraVelocity() {
65
+ return { vx: this.vx, vy: this.vy };
66
+ }
63
67
  /**
64
68
  * Per-frame bookkeeping: forgiveness timers, squash decay and the
65
69
  * facing flip. The logic set's '*' hook runs it in every state, so
@@ -0,0 +1,72 @@
1
+ import { Component, type AnimationFacingProvider, type CameraVelocity, type CameraVelocityProvider } from '@waica/engine';
2
+ /** The four facings the top-down animation contract declares. */
3
+ export type TopDownFacing = 'n' | 's' | 'e' | 'w';
4
+ /**
5
+ * Passive top-down motor: tuning params, physical state and movement
6
+ * methods for state code to call. It has no onUpdate of its own — the
7
+ * StateMachine is the only owner of the frame; this is the toolbox the
8
+ * active state moves the body with. Eight-direction movement with the
9
+ * input vector normalized so diagonals match cardinal speed, no gravity,
10
+ * and per-axis collision against the scene's Solids — deterministic
11
+ * genre movement (Zelda-style), not "realistic" physics.
12
+ */
13
+ export declare class TopDownMotor extends Component implements CameraVelocityProvider, AnimationFacingProvider {
14
+ static componentName: string;
15
+ static displayName: string;
16
+ static params: {
17
+ moveSpeed: {
18
+ label: string;
19
+ min: number;
20
+ max: number;
21
+ step: number;
22
+ };
23
+ acceleration: {
24
+ label: string;
25
+ min: number;
26
+ max: number;
27
+ step: number;
28
+ };
29
+ deceleration: {
30
+ label: string;
31
+ min: number;
32
+ max: number;
33
+ step: number;
34
+ };
35
+ walkThreshold: {
36
+ label: string;
37
+ min: number;
38
+ max: number;
39
+ step: number;
40
+ };
41
+ };
42
+ static transient: string[];
43
+ moveSpeed: number;
44
+ acceleration: number;
45
+ deceleration: number;
46
+ /** Speed above this reads as walking (the idle ↔ walk edge). */
47
+ walkThreshold: number;
48
+ /** The character's AABB hitbox — a footprint, shorter than the sprite. */
49
+ hitboxWidth: number;
50
+ hitboxHeight: number;
51
+ vx: number;
52
+ vy: number;
53
+ /** Four-direction facing; starts looking at the camera. */
54
+ facing: TopDownFacing;
55
+ /** The scene camera reads follow velocity through this explicit seam. */
56
+ getCameraVelocity(): CameraVelocity;
57
+ /** The StateMachine resolves directional clips through this explicit seam. */
58
+ getAnimationFacing(): string;
59
+ /**
60
+ * Accelerates toward the input vector (each axis -1..1) * moveSpeed.
61
+ * The vector is normalized so diagonals move at cardinal speed, and
62
+ * facing follows the dominant input axis — ties keep the last facing.
63
+ */
64
+ run(inputX: number, inputY: number, dt: number): void;
65
+ /** Live speed, for the idle ↔ walk edge. */
66
+ speed(): number;
67
+ /** Integrates velocity and resolves each axis against Solids. */
68
+ step(dt: number): void;
69
+ halt(): void;
70
+ private resolveAxis;
71
+ private collisionBody;
72
+ }
@@ -0,0 +1,102 @@
1
+ import { Component, resolveSolidAxis, THREE, } from '@waica/engine';
2
+ /**
3
+ * Passive top-down motor: tuning params, physical state and movement
4
+ * methods for state code to call. It has no onUpdate of its own — the
5
+ * StateMachine is the only owner of the frame; this is the toolbox the
6
+ * active state moves the body with. Eight-direction movement with the
7
+ * input vector normalized so diagonals match cardinal speed, no gravity,
8
+ * and per-axis collision against the scene's Solids — deterministic
9
+ * genre movement (Zelda-style), not "realistic" physics.
10
+ */
11
+ export class TopDownMotor extends Component {
12
+ static componentName = 'TopDownMotor';
13
+ static displayName = 'Motor';
14
+ static params = {
15
+ moveSpeed: { label: 'Speed', min: 1, max: 30, step: 0.5 },
16
+ acceleration: { label: 'Acceleration', min: 5, max: 200, step: 5 },
17
+ deceleration: { label: 'Deceleration', min: 5, max: 200, step: 5 },
18
+ walkThreshold: { label: 'Walk threshold', min: 0, max: 5, step: 0.1 },
19
+ };
20
+ static transient = ['vx', 'vy', 'facing'];
21
+ moveSpeed = 6;
22
+ acceleration = 60;
23
+ deceleration = 80;
24
+ /** Speed above this reads as walking (the idle ↔ walk edge). */
25
+ walkThreshold = 0.5;
26
+ /** The character's AABB hitbox — a footprint, shorter than the sprite. */
27
+ hitboxWidth = 0.9;
28
+ hitboxHeight = 0.6;
29
+ vx = 0;
30
+ vy = 0;
31
+ /** Four-direction facing; starts looking at the camera. */
32
+ facing = 's';
33
+ /** The scene camera reads follow velocity through this explicit seam. */
34
+ getCameraVelocity() {
35
+ return { vx: this.vx, vy: this.vy };
36
+ }
37
+ /** The StateMachine resolves directional clips through this explicit seam. */
38
+ getAnimationFacing() {
39
+ return this.facing;
40
+ }
41
+ /**
42
+ * Accelerates toward the input vector (each axis -1..1) * moveSpeed.
43
+ * The vector is normalized so diagonals move at cardinal speed, and
44
+ * facing follows the dominant input axis — ties keep the last facing.
45
+ */
46
+ run(inputX, inputY, dt) {
47
+ let x = inputX;
48
+ let y = inputY;
49
+ const length = Math.hypot(x, y);
50
+ if (length > 1) {
51
+ x /= length;
52
+ y /= length;
53
+ }
54
+ const rateX = x !== 0 ? this.acceleration : this.deceleration;
55
+ const rateY = y !== 0 ? this.acceleration : this.deceleration;
56
+ this.vx = THREE.MathUtils.damp(this.vx, x * this.moveSpeed, rateX / this.moveSpeed, dt);
57
+ this.vy = THREE.MathUtils.damp(this.vy, y * this.moveSpeed, rateY / this.moveSpeed, dt);
58
+ const absX = Math.abs(inputX);
59
+ const absY = Math.abs(inputY);
60
+ if (absX > absY)
61
+ this.facing = inputX < 0 ? 'w' : 'e';
62
+ else if (absY > absX)
63
+ this.facing = inputY < 0 ? 's' : 'n';
64
+ }
65
+ /** Live speed, for the idle ↔ walk edge. */
66
+ speed() {
67
+ return Math.hypot(this.vx, this.vy);
68
+ }
69
+ /** Integrates velocity and resolves each axis against Solids. */
70
+ step(dt) {
71
+ const pos = this.entity.position;
72
+ const previousX = pos.x;
73
+ pos.x += this.vx * dt;
74
+ if (this.resolveAxis('x', previousX))
75
+ this.vx = 0;
76
+ const previousY = pos.y;
77
+ pos.y += this.vy * dt;
78
+ if (this.resolveAxis('y', previousY))
79
+ this.vy = 0;
80
+ }
81
+ halt() {
82
+ this.vx = 0;
83
+ this.vy = 0;
84
+ }
85
+ resolveAxis(axis, previous) {
86
+ return resolveSolidAxis({
87
+ entity: this.entity,
88
+ axis,
89
+ previous,
90
+ body: () => this.collisionBody(),
91
+ });
92
+ }
93
+ collisionBody() {
94
+ return {
95
+ x: this.entity.position.x,
96
+ y: this.entity.position.y,
97
+ width: this.hitboxWidth,
98
+ height: this.hitboxHeight,
99
+ shape: 'rectangle',
100
+ };
101
+ }
102
+ }
@@ -0,0 +1,18 @@
1
+ import { type RoleDefinition, type RoleGraph, type StateContext } from '@waica/engine';
2
+ /**
3
+ * The top-down 'player' role: the character you control from above. Both
4
+ * default states share one body update; they differ only in the edges
5
+ * their prefab data declares. Extending the player is defineStates
6
+ * ('player', { yourState: {...} }) plus a state in the prefab — never a
7
+ * fight with a parallel controller.
8
+ */
9
+ /** The state graph new top-down player characters start with, as prefab data. */
10
+ export declare const TOPDOWN_PLAYER_STATE_GRAPH: RoleGraph;
11
+ /**
12
+ * One body update shared by every default state: read the four-direction
13
+ * actions, accelerate, collide, then report what the body is doing as
14
+ * signals. Unmatched signals are no-ops, so each state only reacts to
15
+ * the edges its data declares.
16
+ */
17
+ export declare function topdownPlayerUpdate({ entity, game, fsm }: StateContext, dt: number): void;
18
+ export declare const TOPDOWN_PLAYER_ROLE: RoleDefinition;
@@ -0,0 +1,86 @@
1
+ import {} from '@waica/engine';
2
+ import { Health } from './health.js';
3
+ import { interactUpdate } from './interactable.js';
4
+ import { Respawnable } from './respawnable.js';
5
+ import { TopDownMotor } from './topdown-motor.js';
6
+ /**
7
+ * The top-down 'player' role: the character you control from above. Both
8
+ * default states share one body update; they differ only in the edges
9
+ * their prefab data declares. Extending the player is defineStates
10
+ * ('player', { yourState: {...} }) plus a state in the prefab — never a
11
+ * fight with a parallel controller.
12
+ */
13
+ /** The state graph new top-down player characters start with, as prefab data. */
14
+ export const TOPDOWN_PLAYER_STATE_GRAPH = {
15
+ initial: 'idle',
16
+ states: {
17
+ idle: {
18
+ transitions: [{ on: 'signal:move', to: 'walk' }],
19
+ },
20
+ walk: {
21
+ transitions: [{ on: 'signal:stop', to: 'idle' }],
22
+ },
23
+ dead: {
24
+ // Freeze on idle (resolved directionally) rather than name a death
25
+ // clip the stock sheets do not have and warn every frame.
26
+ clip: 'idle',
27
+ // A beat before control comes back, so death reads as an event.
28
+ transitions: [{ on: 'timer:0.8', to: 'idle' }],
29
+ },
30
+ // Dying is the same from every state, so the edge lives on '*'. Its
31
+ // presence is also the contract Health checks before signalling: a graph
32
+ // without it gets its entity destroyed instead.
33
+ '*': { transitions: [{ on: 'signal:death', to: 'dead' }] },
34
+ },
35
+ };
36
+ /**
37
+ * One body update shared by every default state: read the four-direction
38
+ * actions, accelerate, collide, then report what the body is doing as
39
+ * signals. Unmatched signals are no-ops, so each state only reacts to
40
+ * the edges its data declares.
41
+ */
42
+ export function topdownPlayerUpdate({ entity, game, fsm }, dt) {
43
+ const motor = entity.get(TopDownMotor);
44
+ if (!motor)
45
+ return;
46
+ motor.run(game.input.axis('left', 'right'), game.input.axis('down', 'up'), dt);
47
+ motor.step(dt);
48
+ fsm.signal(motor.speed() > motor.walkThreshold ? 'move' : 'stop');
49
+ }
50
+ export const TOPDOWN_PLAYER_ROLE = {
51
+ description: 'You control this character with the project controls: eight-direction ' +
52
+ 'top-down movement with normalized diagonals and no gravity. Its ' +
53
+ 'states move the Motor.',
54
+ driver: 'TopDownMotor',
55
+ graph: TOPDOWN_PLAYER_STATE_GRAPH,
56
+ signals: {
57
+ move: 'moving',
58
+ stop: 'standing still',
59
+ },
60
+ states: {
61
+ // Always-hook: the interact lookup must survive custom states like an
62
+ // attack, so it runs in every state.
63
+ '*': {
64
+ onUpdate(ctx) {
65
+ interactUpdate(ctx);
66
+ },
67
+ },
68
+ // Fallback: a custom state without its own onUpdate keeps the full
69
+ // body update — its file only has to say what makes it special.
70
+ default: { onUpdate: topdownPlayerUpdate },
71
+ idle: { onUpdate: topdownPlayerUpdate },
72
+ walk: { onUpdate: topdownPlayerUpdate },
73
+ // Coming back is what leaving death means, so it hangs off onExit: any
74
+ // other way out of this state (a project's own edge) revives too.
75
+ dead: {
76
+ // A state without its own onUpdate falls back to the role's default
77
+ // body update, so without this no-op the player kept walking for the
78
+ // whole death beat instead of the graph taking control away.
79
+ onUpdate() { },
80
+ onExit({ entity }) {
81
+ entity.get(Respawnable)?.respawn();
82
+ entity.get(Health)?.heal(Infinity);
83
+ },
84
+ },
85
+ },
86
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waica/behaviors",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Waica built-in behaviors — the curated game-feel library",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -19,6 +19,6 @@
19
19
  }
20
20
  },
21
21
  "dependencies": {
22
- "@waica/engine": "^0.6.0"
22
+ "@waica/engine": "^0.7.0"
23
23
  }
24
24
  }