@klex/mascot 0.4.0 → 0.6.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.
@@ -0,0 +1,126 @@
1
+ import { emotePose } from './emotes';
2
+
3
+ /** Written lines on the right-hand page before Klex turns it. */
4
+ export const BOOK_LINES = 4;
5
+ const TURN_SECONDS = 0.75;
6
+ const between = (min: number, max: number) => min + Math.random() * (max - min);
7
+
8
+ type Phase = 'writing' | 'reading' | 'pondering' | 'turning';
9
+
10
+ // Each avatar writes at its own uneven pace and pauses to reread or think.
11
+ export function createLearning() {
12
+ let phase: Phase = Math.random() < 0.7 ? 'writing' : 'reading';
13
+ const duration = () =>
14
+ phase === 'writing'
15
+ ? between(3.5, 6.5)
16
+ : phase === 'reading'
17
+ ? between(1.8, 3)
18
+ : phase === 'pondering'
19
+ ? between(1.4, 2.4)
20
+ : TURN_SECONDS;
21
+ let phaseDuration = duration();
22
+ let time = 0;
23
+ let ink = Math.floor(Math.random() * BOOK_LINES);
24
+ let speed = 0;
25
+ let speedIn = 0;
26
+ let blinkIn = between(2.5, 5);
27
+ let blinkTime = Infinity;
28
+ const pen = { x: ink % 1, line: Math.floor(ink), lift: 1 };
29
+ const pose = {
30
+ compression: 0.025,
31
+ headTilt: 0,
32
+ headSway: 0,
33
+ nod: 0.25,
34
+ lookX: 0,
35
+ lookY: 4,
36
+ };
37
+
38
+ const next = () => {
39
+ phase =
40
+ ink >= BOOK_LINES
41
+ ? 'turning'
42
+ : phase === 'writing'
43
+ ? Math.random() < 0.6
44
+ ? 'reading'
45
+ : 'pondering'
46
+ : 'writing';
47
+ phaseDuration = duration();
48
+ time = 0;
49
+ };
50
+
51
+ return {
52
+ get expression() {
53
+ return phase === 'writing' ? ('focused' as const) : ('neutral' as const);
54
+ },
55
+ /** Written share of the right-hand page, in lines. */
56
+ get ink() {
57
+ return ink;
58
+ },
59
+ /** Pen tip on the current line (0 = start, 1 = end) and its lift (1 = up). */
60
+ get pen() {
61
+ return pen;
62
+ },
63
+ /** Page turn progress from 0 to 1 while a full page turns, else 0. */
64
+ get turn() {
65
+ return phase === 'turning' ? Math.min(1, time / TURN_SECONDS) : 0;
66
+ },
67
+ advance(dt: number) {
68
+ time += dt;
69
+ blinkTime += dt;
70
+ blinkIn -= dt;
71
+ if (phase === 'turning' && time >= TURN_SECONDS) {
72
+ ink = 0;
73
+ phase = 'writing';
74
+ phaseDuration = duration();
75
+ time = 0;
76
+ } else if (time >= phaseDuration) next();
77
+ if (blinkIn <= 0) {
78
+ blinkTime = 0;
79
+ blinkIn = between(2.5, 5);
80
+ }
81
+ if (phase === 'writing') {
82
+ speedIn -= dt;
83
+ if (speedIn <= 0) {
84
+ // Words come in bursts with short gaps between them.
85
+ speedIn = between(0.25, 0.6);
86
+ speed = Math.random() < 0.2 ? 0 : between(0.35, 0.85);
87
+ }
88
+ ink = Math.min(BOOK_LINES, ink + speed * dt);
89
+ if (ink >= BOOK_LINES) next();
90
+ }
91
+ const writing = phase === 'writing';
92
+ const line = Math.min(BOOK_LINES - 1, Math.floor(ink));
93
+ const target = writing ? ink - line : pen.x;
94
+ pen.line = line;
95
+ pen.x += (target - pen.x) * (1 - Math.exp(-28 * dt));
96
+ // The pen lifts while it travels back to the start of a line.
97
+ const travelling = Math.min(1, Math.abs(target - pen.x) * 6);
98
+ pen.lift +=
99
+ ((writing && speed > 0 ? travelling : 1) - pen.lift) *
100
+ (1 - Math.exp(-20 * dt));
101
+ const scan = (time * 1.3) % 1;
102
+ const thinking = phase === 'pondering';
103
+ const targetPose = {
104
+ compression: 0.025 + (writing ? (1 - pen.lift) * 0.008 : 0),
105
+ headTilt: thinking ? -5 : writing ? 2 + pen.x * 2 : 0,
106
+ headSway: thinking ? -1 : writing ? (pen.x - 0.5) * 1.2 : 0,
107
+ nod: thinking ? -0.3 : 0.25 + (writing ? 0.1 : 0),
108
+ lookX: thinking
109
+ ? -3
110
+ : writing
111
+ ? -1 + pen.x * 4
112
+ : phase === 'reading'
113
+ ? -1 + scan * 4
114
+ : 1,
115
+ lookY: thinking ? -6 : writing ? 4 + line * 0.5 : 4,
116
+ };
117
+ const blend = 1 - Math.exp(-12 * dt);
118
+ for (const key of Object.keys(pose) as (keyof typeof pose)[])
119
+ pose[key] += (targetPose[key] - pose[key]) * blend;
120
+ },
121
+ pose() {
122
+ const blink = emotePose('blink', blinkTime / 0.3).blinkLeft;
123
+ return { ...pose, blinkLeft: blink, blinkRight: blink };
124
+ },
125
+ };
126
+ }
package/src/presets.ts CHANGED
@@ -1,9 +1,30 @@
1
+ /** Cross-section of the inflated body from its edge inwards. */
2
+ export type InflateProfile = 'pillow' | 'dome' | 'gummy';
3
+
4
+ /** Extra height as a soft 2D blob, e.g. a belly or cheeks. */
5
+ export type InflateBump = {
6
+ /** Rest position in the 160×140 frame of the right-facing outline. */
7
+ x: number;
8
+ y: number;
9
+ radius: number;
10
+ /** Added height in frame units. Negative values dent the surface. */
11
+ height: number;
12
+ /** `face` follows the eyes, `body` the deformed silhouette's bounds. */
13
+ anchor?: 'face' | 'body';
14
+ };
15
+
1
16
  export type BodyShape = {
2
17
  id: string;
3
18
  name: string;
4
19
  outline: readonly (readonly [number, number])[];
5
20
  eyeX: number;
6
21
  eyeY: number;
22
+ /** Inflated look only: 0 is a flat cookie, 1 a round marshmallow. */
23
+ puffiness?: number;
24
+ /** Inflated look only. Defaults to `dome`. */
25
+ edgeProfile?: InflateProfile;
26
+ /** Inflated look only. At most four bumps are drawn. */
27
+ bumps?: readonly InflateBump[];
7
28
  };
8
29
 
9
30
  export type KlexPalette = {
@@ -19,6 +40,12 @@ export const BODY_SHAPES = [
19
40
  name: 'Klex',
20
41
  eyeX: 23.294,
21
42
  eyeY: 56.824,
43
+ puffiness: 0.6,
44
+ edgeProfile: 'dome',
45
+ bumps: [
46
+ { x: 90.3, y: 67, radius: 6, height: 1.5, anchor: 'face' },
47
+ { x: 116.3, y: 67, radius: 6, height: 1.5, anchor: 'face' },
48
+ ],
22
49
  // Logo contour from packages/ui/src/components/ui/klex-logo.tsx, scaled
23
50
  // uniformly to 110px high, with three sole points at y=128 for ground contact.
24
51
  outline: [
@@ -125,6 +152,9 @@ export const BODY_SHAPES = [
125
152
  name: 'Blob',
126
153
  eyeX: 23,
127
154
  eyeY: 65,
155
+ puffiness: 0.75,
156
+ edgeProfile: 'pillow',
157
+ bumps: [{ x: 84, y: 100, radius: 24, height: 5, anchor: 'body' }],
128
158
  outline: [
129
159
  [88, 28],
130
160
  [115, 33],
@@ -147,6 +177,8 @@ export const BODY_SHAPES = [
147
177
  name: 'Slime',
148
178
  eyeX: 31,
149
179
  eyeY: 87,
180
+ puffiness: 0.45,
181
+ edgeProfile: 'gummy',
150
182
  outline: [
151
183
  [100, 58],
152
184
  [123, 64],
@@ -169,6 +201,9 @@ export const BODY_SHAPES = [
169
201
  name: 'Drop',
170
202
  eyeX: 18,
171
203
  eyeY: 63,
204
+ puffiness: 0.7,
205
+ edgeProfile: 'dome',
206
+ bumps: [{ x: 86, y: 104, radius: 20, height: 3, anchor: 'body' }],
172
207
  outline: [
173
208
  [92, 12],
174
209
  [108, 26],
@@ -0,0 +1,111 @@
1
+ import { emotePose } from './emotes';
2
+
3
+ const phases = ['pondering', 'searching', 'remembering'] as const;
4
+ const between = (min: number, max: number) => min + Math.random() * (max - min);
5
+
6
+ // Thinking is ambient: it never claims a memory was actually found.
7
+ export function createRecalling() {
8
+ let phase: (typeof phases)[number] =
9
+ Math.random() < 0.6 ? 'pondering' : 'searching';
10
+ const duration = () =>
11
+ phase === 'pondering'
12
+ ? between(3, 5.5)
13
+ : phase === 'searching'
14
+ ? between(2, 3.5)
15
+ : between(1.1, 1.6);
16
+ let phaseDuration = duration();
17
+ let time = between(0, phaseDuration * 0.5);
18
+ let blinkIn = between(2.5, 5);
19
+ let blinkTime = Infinity;
20
+ let glanceIn = 0;
21
+ let glanceX = 0;
22
+ let glanceY = -6;
23
+ const pose = {
24
+ compression: 0.01,
25
+ headTilt: 5,
26
+ headSway: 0,
27
+ nod: -0.2,
28
+ lookX: 3,
29
+ lookY: -6,
30
+ };
31
+
32
+ return {
33
+ get expression() {
34
+ return phase === 'remembering' && time > 0.2
35
+ ? ('happy' as const)
36
+ : ('neutral' as const);
37
+ },
38
+ advance(dt: number) {
39
+ time += dt;
40
+ blinkTime += dt;
41
+ blinkIn -= dt;
42
+ if (time >= phaseDuration) {
43
+ // Remembering always returns to quiet pondering.
44
+ phase =
45
+ phase === 'remembering'
46
+ ? 'pondering'
47
+ : phase === 'pondering'
48
+ ? Math.random() < 0.6
49
+ ? 'searching'
50
+ : 'remembering'
51
+ : 'remembering';
52
+ phaseDuration = duration();
53
+ time = 0;
54
+ glanceIn = 0;
55
+ blinkIn = Math.min(blinkIn, 0.15);
56
+ }
57
+ if (blinkIn <= 0) {
58
+ blinkTime = 0;
59
+ blinkIn = between(2.5, 5);
60
+ }
61
+ if (phase === 'searching') {
62
+ glanceIn -= dt;
63
+ if (glanceIn <= 0) {
64
+ // Quick saccades between places a memory might be.
65
+ glanceIn = between(0.35, 0.8);
66
+ glanceX = between(-4, 4);
67
+ glanceY = between(-7, -3);
68
+ }
69
+ }
70
+ const slow = Math.sin((time * Math.PI * 2) / 4.2);
71
+ const nod =
72
+ phase === 'remembering'
73
+ ? Math.sin(Math.min(time / 0.6, 1) * Math.PI)
74
+ : 0;
75
+ const target =
76
+ phase === 'pondering'
77
+ ? {
78
+ compression: 0.01 + 0.006 * (1 - Math.cos(time * 1.4)),
79
+ headTilt: 6 + 1.5 * slow,
80
+ headSway: 0.8 * slow,
81
+ nod: -0.25,
82
+ lookX: 3 + 0.6 * slow,
83
+ lookY: -6.5,
84
+ }
85
+ : phase === 'searching'
86
+ ? {
87
+ compression: 0.012,
88
+ headTilt: 2 + glanceX * 0.5,
89
+ headSway: glanceX * 0.2,
90
+ nod: -0.15,
91
+ lookX: glanceX,
92
+ lookY: glanceY,
93
+ }
94
+ : {
95
+ compression: 0.015 + nod * 0.03,
96
+ headTilt: 1,
97
+ headSway: 0,
98
+ nod: nod * 0.6,
99
+ lookX: 0.5,
100
+ lookY: 0,
101
+ };
102
+ const blend = 1 - Math.exp((phase === 'searching' ? -16 : -8) * dt);
103
+ for (const key of Object.keys(pose) as (keyof typeof pose)[])
104
+ pose[key] += (target[key] - pose[key]) * blend;
105
+ },
106
+ pose() {
107
+ const blink = emotePose('blink', blinkTime / 0.3).blinkLeft;
108
+ return { ...pose, blinkLeft: blink, blinkRight: blink };
109
+ },
110
+ };
111
+ }