@umicat/three-sdk 0.2.0 → 0.4.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/README.md CHANGED
@@ -96,6 +96,27 @@ One real bug was fixed while chasing it and is unrelated but worth having: a
96
96
  bones, so three.js culls a character against a stale volume once it moves.
97
97
  `loadScene3D` sets `frustumCulled = false` on skinned meshes.
98
98
 
99
+ ## 0.3.0 — actions, not just locomotion
100
+
101
+ `CharacterAnimator` owns the two things every game was writing by hand, and
102
+ both are character behaviour rather than game logic (ADR-034).
103
+
104
+ **Locomotion follows `character.state`**, not the keys held — a clip chosen
105
+ from input leaves a character walking in mid-air.
106
+
107
+ **An action is a one-shot that interrupts and returns.** Attacking is not a
108
+ state you enter, it is a thing that happens: `play('attack')` runs the clip
109
+ once, holds its last frame rather than snapping to the bind pose in the gap,
110
+ and hands control back. `busy` is there so a game can refuse a second swing
111
+ from one press — firing twice is the usual reason an attack feels broken.
112
+
113
+ Changing locomotion mid-action does **not** blend a walk cycle into the middle
114
+ of a sword swing: the new locomotion clip is swapped in underneath at zero
115
+ weight and faded up only when the action releases.
116
+
117
+ Names resolve through the manifest's map first (`attack` -> `attack-melee-right`)
118
+ and then by raw clip name, so a game can reach a clip nobody mapped.
119
+
99
120
  ## 0.2.0 — the character's jump and controls belong to the platform
100
121
 
101
122
  ADR-034 makes the platform the owner of the character, so two things that used
@@ -0,0 +1,58 @@
1
+ import * as THREE from 'three';
2
+ import type { CharacterState } from './CharacterController3D.js';
3
+ /** Semantic name -> the clip actually inside the model, as authored in the
4
+ * manifest. Never guess a clip name: this character calls its run `sprint`. */
5
+ export type ClipMap = Record<string, string>;
6
+ export interface CharacterAnimatorOptions {
7
+ /** Cross-fade between locomotion clips, seconds. */
8
+ blend?: number;
9
+ /** Cross-fade in and out of a one-shot action, seconds. */
10
+ actionBlend?: number;
11
+ }
12
+ /**
13
+ * Locomotion plus one-shot actions, for a character whose clips all sit on one
14
+ * rig.
15
+ *
16
+ * Two things were being written by hand in every game, and both are character
17
+ * behaviour rather than game logic (ADR-034), so they live here.
18
+ *
19
+ * **Locomotion follows the CONTROLLER's state, not the keys held.** A clip
20
+ * chosen from input leaves a character walking in mid-air.
21
+ *
22
+ * **An action is a one-shot that interrupts and returns.** Attacking is not a
23
+ * state you enter, it is a thing that happens: it plays once, holds its last
24
+ * frame rather than snapping, and hands control back. Without that, a game
25
+ * either cannot attack at all or leaves the character stuck mid-swing.
26
+ */
27
+ export declare class CharacterAnimator {
28
+ private readonly mixer;
29
+ private readonly map;
30
+ private readonly actions;
31
+ private locomotion;
32
+ private locomotionName;
33
+ private oneShot;
34
+ private oneShotName;
35
+ private readonly blend;
36
+ private readonly actionBlend;
37
+ private readonly onFinished;
38
+ constructor(mixer: THREE.AnimationMixer, clips: THREE.AnimationClip[], map?: ClipMap, options?: CharacterAnimatorOptions);
39
+ /** Is a one-shot still playing? Games gate input on this — swinging twice
40
+ * from one press is the usual reason an attack feels broken. */
41
+ get busy(): boolean;
42
+ /** Which one-shot, if any. */
43
+ get action(): string;
44
+ /** Look a semantic name up through the manifest's map, then by raw clip name
45
+ * so a game can reach a clip the manifest never mapped. */
46
+ private find;
47
+ has(name: string): boolean;
48
+ /** Drive locomotion. Pass `character.state` every frame. */
49
+ update(state: CharacterState | string): void;
50
+ /**
51
+ * Play a one-shot. Returns false if there is no such clip, or if one is
52
+ * already running and `interrupt` was not asked for.
53
+ */
54
+ play(name: string, opts?: {
55
+ interrupt?: boolean;
56
+ }): boolean;
57
+ dispose(): void;
58
+ }
@@ -0,0 +1,103 @@
1
+ import * as THREE from 'three';
2
+ /**
3
+ * Locomotion plus one-shot actions, for a character whose clips all sit on one
4
+ * rig.
5
+ *
6
+ * Two things were being written by hand in every game, and both are character
7
+ * behaviour rather than game logic (ADR-034), so they live here.
8
+ *
9
+ * **Locomotion follows the CONTROLLER's state, not the keys held.** A clip
10
+ * chosen from input leaves a character walking in mid-air.
11
+ *
12
+ * **An action is a one-shot that interrupts and returns.** Attacking is not a
13
+ * state you enter, it is a thing that happens: it plays once, holds its last
14
+ * frame rather than snapping, and hands control back. Without that, a game
15
+ * either cannot attack at all or leaves the character stuck mid-swing.
16
+ */
17
+ export class CharacterAnimator {
18
+ constructor(mixer, clips, map = {}, options = {}) {
19
+ this.mixer = mixer;
20
+ this.map = map;
21
+ this.actions = new Map();
22
+ this.locomotion = null;
23
+ this.locomotionName = '';
24
+ this.oneShot = null;
25
+ this.oneShotName = '';
26
+ this.blend = options.blend ?? 0.15;
27
+ this.actionBlend = options.actionBlend ?? 0.08;
28
+ for (const clip of clips)
29
+ this.actions.set(clip.name, mixer.clipAction(clip));
30
+ this.onFinished = (e) => {
31
+ if (this.oneShot && e.action === this.oneShot) {
32
+ this.oneShot.fadeOut(this.actionBlend);
33
+ this.oneShot = null;
34
+ this.oneShotName = '';
35
+ // Bring locomotion back explicitly — it was faded down, not stopped,
36
+ // so it is still running underneath at weight ~0.
37
+ this.locomotion?.reset().fadeIn(this.actionBlend).play();
38
+ }
39
+ };
40
+ mixer.addEventListener('finished', this.onFinished);
41
+ }
42
+ /** Is a one-shot still playing? Games gate input on this — swinging twice
43
+ * from one press is the usual reason an attack feels broken. */
44
+ get busy() { return this.oneShot !== null; }
45
+ /** Which one-shot, if any. */
46
+ get action() { return this.oneShotName; }
47
+ /** Look a semantic name up through the manifest's map, then by raw clip name
48
+ * so a game can reach a clip the manifest never mapped. */
49
+ find(name) {
50
+ return this.actions.get(this.map[name] ?? name) ?? this.actions.get(name) ?? null;
51
+ }
52
+ has(name) { return this.find(name) !== null; }
53
+ /** Drive locomotion. Pass `character.state` every frame. */
54
+ update(state) {
55
+ if (state === this.locomotionName)
56
+ return;
57
+ const next = this.find(state) ?? this.find('idle');
58
+ if (!next)
59
+ return;
60
+ this.locomotionName = state;
61
+ if (next === this.locomotion)
62
+ return;
63
+ next.reset().setLoop(THREE.LoopRepeat, Infinity).setEffectiveWeight(1);
64
+ // Under a one-shot, swap the underlying clip silently — fading it in here
65
+ // would blend a walk cycle into the middle of a sword swing.
66
+ if (this.busy) {
67
+ next.play().setEffectiveWeight(0);
68
+ }
69
+ else {
70
+ next.fadeIn(this.blend).play();
71
+ this.locomotion?.fadeOut(this.blend);
72
+ }
73
+ this.locomotion = next;
74
+ }
75
+ /**
76
+ * Play a one-shot. Returns false if there is no such clip, or if one is
77
+ * already running and `interrupt` was not asked for.
78
+ */
79
+ play(name, opts = {}) {
80
+ const next = this.find(name);
81
+ if (!next)
82
+ return false;
83
+ if (this.busy && !opts.interrupt)
84
+ return false;
85
+ if (this.oneShot && this.oneShot !== next)
86
+ this.oneShot.fadeOut(this.actionBlend);
87
+ this.locomotion?.fadeOut(this.actionBlend);
88
+ next.reset()
89
+ .setLoop(THREE.LoopOnce, 1)
90
+ .setEffectiveWeight(1)
91
+ .fadeIn(this.actionBlend)
92
+ .play();
93
+ // Hold the last frame instead of snapping back to the bind pose in the
94
+ // gap before locomotion fades in.
95
+ next.clampWhenFinished = true;
96
+ this.oneShot = next;
97
+ this.oneShotName = name;
98
+ return true;
99
+ }
100
+ dispose() {
101
+ this.mixer.removeEventListener('finished', this.onFinished);
102
+ }
103
+ }
@@ -14,6 +14,9 @@ export interface CharacterOptions {
14
14
  stepHeight?: number;
15
15
  /** Steeper than this and they slide instead of climbing. Degrees. */
16
16
  maxSlopeDegrees?: number;
17
+ /** Fall acceleration, in world units. Defaults to the world's own gravity;
18
+ * set it only to make this character deliberately heavier or floatier than
19
+ * its surroundings. */
17
20
  gravity?: number;
18
21
  /** Start position. */
19
22
  position?: {
@@ -83,6 +86,21 @@ export declare class CharacterController3D {
83
86
  get state(): CharacterState;
84
87
  /** Rising under its own power, as opposed to merely off the ground. */
85
88
  get airborne(): boolean;
89
+ /** How high a HELD jump rises, in world units.
90
+ *
91
+ * Level geometry is authored against these two numbers, so they are exposed
92
+ * rather than left to be re-derived. Getting the algebra wrong is not
93
+ * hypothetical: a course laid out against a mis-computed apex produced a
94
+ * first step the character could not clear, which reads to a player as
95
+ * "that platform is unreachable" rather than as a bug. */
96
+ get maxJumpRise(): number;
97
+ /** How high a TAPPED jump rises — the button released as early as the
98
+ * variable-height cut can see it.
99
+ *
100
+ * **Author rises against this one, not `maxJumpRise`.** A jump is a range,
101
+ * not a number: releasing early cuts it deliberately, so a gap only the
102
+ * held jump clears is a gap half your players will believe is closed. */
103
+ get minJumpRise(): number;
86
104
  /** Put the character somewhere, cancelling any fall. The respawn primitive:
87
105
  * without clearing the velocity, a character teleported out of a long drop
88
106
  * arrives still travelling at the speed it fell. */
@@ -29,7 +29,14 @@ export class CharacterController3D {
29
29
  speed: options.speed ?? 4,
30
30
  stepHeight: options.stepHeight ?? 0.4,
31
31
  maxSlopeDegrees: options.maxSlopeDegrees ?? 50,
32
- gravity: options.gravity ?? 9.81,
32
+ // Default to the WORLD's gravity, not to Earth's. A kinematic character
33
+ // integrates its own fall, so a hard-coded 9.81 put a second gravity in
34
+ // the scene: in a world authored in Kenney's unit (gravity -4.17) the
35
+ // character fell 2.35x faster than everything around it, and every jump
36
+ // came out less than half the height the docs promised. One world, one
37
+ // gravity — pass the option only to give this character a deliberately
38
+ // different feel.
39
+ gravity: options.gravity ?? (Math.abs(world.gravity.y) || 9.81),
33
40
  jumpSpeed: options.jumpSpeed ?? 0,
34
41
  coyoteTime: options.coyoteTime ?? 0.12,
35
42
  jumpBuffer: options.jumpBuffer ?? 0.12,
@@ -53,6 +60,30 @@ export class CharacterController3D {
53
60
  get state() { return this._state; }
54
61
  /** Rising under its own power, as opposed to merely off the ground. */
55
62
  get airborne() { return !this.grounded; }
63
+ /** How high a HELD jump rises, in world units.
64
+ *
65
+ * Level geometry is authored against these two numbers, so they are exposed
66
+ * rather than left to be re-derived. Getting the algebra wrong is not
67
+ * hypothetical: a course laid out against a mis-computed apex produced a
68
+ * first step the character could not clear, which reads to a player as
69
+ * "that platform is unreachable" rather than as a bug. */
70
+ get maxJumpRise() {
71
+ if (this.opts.jumpSpeed <= 0)
72
+ return 0;
73
+ return (this.opts.jumpSpeed * this.opts.jumpSpeed) / (2 * this.opts.gravity);
74
+ }
75
+ /** How high a TAPPED jump rises — the button released as early as the
76
+ * variable-height cut can see it.
77
+ *
78
+ * **Author rises against this one, not `maxJumpRise`.** A jump is a range,
79
+ * not a number: releasing early cuts it deliberately, so a gap only the
80
+ * held jump clears is a gap half your players will believe is closed. */
81
+ get minJumpRise() {
82
+ if (this.opts.jumpSpeed <= 0)
83
+ return 0;
84
+ const cut = this.opts.jumpSpeed * this.opts.jumpCut;
85
+ return (cut * cut) / (2 * this.opts.gravity);
86
+ }
56
87
  /** Put the character somewhere, cancelling any fall. The respawn primitive:
57
88
  * without clearing the velocity, a character teleported out of a long drop
58
89
  * arrives still travelling at the speed it fell. */
package/dist/index.d.ts CHANGED
@@ -4,6 +4,8 @@ export * from './scene3d.js';
4
4
  export { loadScene3D } from './SceneLoader3D.js';
5
5
  export { CharacterController3D } from './CharacterController3D.js';
6
6
  export type { CharacterOptions, CharacterState, CharacterInput } from './CharacterController3D.js';
7
+ export { CharacterAnimator } from './CharacterAnimator.js';
8
+ export type { ClipMap, CharacterAnimatorOptions } from './CharacterAnimator.js';
7
9
  export { Input3D } from './Input3D.js';
8
10
  export type { Input3DOptions } from './Input3D.js';
9
11
  export type { LoadedScene3D, LoadSceneOptions } from './SceneLoader3D.js';
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ export { ThreeUmicat } from './ThreeUmicat.js';
7
7
  export * from './scene3d.js';
8
8
  export { loadScene3D } from './SceneLoader3D.js';
9
9
  export { CharacterController3D } from './CharacterController3D.js';
10
+ export { CharacterAnimator } from './CharacterAnimator.js';
10
11
  export { Input3D } from './Input3D.js';
11
12
  // Re-exported so a game imports one package for the common case. A game should
12
13
  // not have to know that identity and saves come from a different package than
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umicat/three-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Three.js runtime for Umicat games: the scene3d design format, its loader with physics, a kinematic character controller, and the Umicat platform via @umicat/platform-sdk.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",