clawmate 1.3.0 → 1.4.1
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/electron-builder.yml +1 -1
- package/index.js +589 -406
- package/main/ai-bridge.js +64 -58
- package/main/ai-connector.js +67 -62
- package/main/autostart.js +7 -7
- package/main/desktop-path.js +4 -4
- package/main/file-command-parser.js +77 -41
- package/main/file-ops.js +27 -27
- package/main/index.js +18 -16
- package/main/ipc-handlers.js +27 -24
- package/main/manifest.js +2 -2
- package/main/platform.js +16 -16
- package/main/smart-file-ops.js +64 -64
- package/main/store.js +1 -1
- package/main/telegram.js +154 -121
- package/main/tray.js +226 -71
- package/main/updater.js +13 -13
- package/openclaw.plugin.json +1 -1
- package/package.json +3 -4
- package/preload/preload.js +18 -18
- package/renderer/css/effects.css +6 -6
- package/renderer/css/pet.css +8 -8
- package/renderer/css/speech.css +5 -5
- package/renderer/first-run.html +15 -15
- package/renderer/index.html +4 -4
- package/renderer/js/ai-controller.js +99 -88
- package/renderer/js/app.js +26 -23
- package/renderer/js/browser-watcher.js +32 -32
- package/renderer/js/character.js +33 -33
- package/renderer/js/interactions.js +57 -14
- package/renderer/js/memory.js +144 -37
- package/renderer/js/metrics.js +141 -141
- package/renderer/js/mode-manager.js +59 -15
- package/renderer/js/pet-engine.js +236 -236
- package/renderer/js/speech.js +19 -19
- package/renderer/js/state-machine.js +23 -23
- package/renderer/js/time-aware.js +15 -15
- package/renderer/launcher.html +9 -9
- package/shared/constants.js +11 -11
- package/shared/messages.js +130 -130
- package/shared/personalities.js +72 -37
- package/skills/launch-pet/index.js +13 -13
- package/skills/launch-pet/skill.json +12 -23
|
@@ -1,72 +1,72 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* requestAnimationFrame
|
|
2
|
+
* Core movement/physics engine (renewed)
|
|
3
|
+
* requestAnimationFrame based -- step movement + jump + rappel + gravity fall
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* crawling
|
|
7
|
-
* jumping
|
|
8
|
-
* falling
|
|
9
|
-
* rappelling
|
|
5
|
+
* Movement modes:
|
|
6
|
+
* crawling -- step-by-step crawling on surfaces
|
|
7
|
+
* jumping -- parabolic trajectory jump
|
|
8
|
+
* falling -- gravity-based free fall
|
|
9
|
+
* rappelling -- pendulum swing descent on a thread
|
|
10
10
|
*/
|
|
11
11
|
const PetEngine = (() => {
|
|
12
|
-
// ---
|
|
13
|
-
const GRAVITY = 0.3; //
|
|
14
|
-
const STEP_SIZE = 4; //
|
|
15
|
-
const JUMP_VX = 3; //
|
|
16
|
-
const JUMP_VY = -7; //
|
|
17
|
-
const BOUNCE_FACTOR = 0.3; //
|
|
18
|
-
const CHAR_SIZE = 64; //
|
|
19
|
-
const ANIM_INTERVAL = 150; //
|
|
20
|
-
const THREAD_SPEED = 0.8; //
|
|
21
|
-
|
|
22
|
-
// ---
|
|
12
|
+
// --- Physics constants ---
|
|
13
|
+
const GRAVITY = 0.3; // Gravity acceleration (px/frame^2)
|
|
14
|
+
const STEP_SIZE = 4; // Step size (px)
|
|
15
|
+
const JUMP_VX = 3; // Jump horizontal initial velocity
|
|
16
|
+
const JUMP_VY = -7; // Jump vertical initial velocity (upward)
|
|
17
|
+
const BOUNCE_FACTOR = 0.3; // Landing bounce factor
|
|
18
|
+
const CHAR_SIZE = 64; // Character size (px)
|
|
19
|
+
const ANIM_INTERVAL = 150; // Animation frame transition interval (ms) -- smooth transition
|
|
20
|
+
const THREAD_SPEED = 0.8; // Rappel descent speed (px/frame)
|
|
21
|
+
|
|
22
|
+
// --- Position and velocity ---
|
|
23
23
|
let x = 0, y = 0;
|
|
24
|
-
let vx = 0, vy = 0; //
|
|
25
|
-
|
|
26
|
-
// ---
|
|
27
|
-
let edge = 'bottom'; //
|
|
28
|
-
let direction = 1; //
|
|
29
|
-
let flipX = false; //
|
|
30
|
-
let prevFlipX = false; //
|
|
31
|
-
let flipTransition = 0; // flipX
|
|
32
|
-
const FLIP_DURATION = 120; // flipX
|
|
33
|
-
let flipStartTime = 0; // flipX
|
|
24
|
+
let vx = 0, vy = 0; // Current velocity vector
|
|
25
|
+
|
|
26
|
+
// --- Surface/direction ---
|
|
27
|
+
let edge = 'bottom'; // Currently attached edge (bottom, left, right, top, surface)
|
|
28
|
+
let direction = 1; // Movement direction: 1=right/down, -1=left/up
|
|
29
|
+
let flipX = false; // Character horizontal flip
|
|
30
|
+
let prevFlipX = false; // Previous frame flipX (for transition detection)
|
|
31
|
+
let flipTransition = 0; // flipX transition progress (0~1, 1 = complete)
|
|
32
|
+
const FLIP_DURATION = 120; // flipX transition duration (ms)
|
|
33
|
+
let flipStartTime = 0; // flipX transition start time
|
|
34
34
|
let screenW, screenH;
|
|
35
35
|
|
|
36
|
-
// ---
|
|
36
|
+
// --- Engine state ---
|
|
37
37
|
let running = false;
|
|
38
38
|
let petContainer = null;
|
|
39
39
|
let speedMultiplier = 1.0;
|
|
40
40
|
let animFrame = 0;
|
|
41
41
|
let lastAnimTime = 0;
|
|
42
|
-
let animFrameChanged = false; //
|
|
42
|
+
let animFrameChanged = false; // Animation frame change flag (synced with movement)
|
|
43
43
|
|
|
44
|
-
// ---
|
|
44
|
+
// --- Movement mode ---
|
|
45
45
|
let movementMode = 'crawling'; // crawling | jumping | falling | rappelling
|
|
46
|
-
let onSurface = true; //
|
|
46
|
+
let onSurface = true; // Whether on a surface
|
|
47
47
|
|
|
48
|
-
// ---
|
|
48
|
+
// --- Step system (animation frame sync) ---
|
|
49
49
|
|
|
50
|
-
// ---
|
|
51
|
-
//
|
|
50
|
+
// --- Rappel (thread) system ---
|
|
51
|
+
// Pendulum swing descent from attachment point
|
|
52
52
|
let thread = null; // { attachX, attachY, length, angle, swingVel } | null
|
|
53
53
|
|
|
54
|
-
// ---
|
|
55
|
-
//
|
|
54
|
+
// --- Window surface list ---
|
|
55
|
+
// Register external window title bars etc. as additional surfaces
|
|
56
56
|
let windowSurfaces = []; // [{ id, x, y, width, height }]
|
|
57
57
|
|
|
58
|
-
// ---
|
|
58
|
+
// --- Reference to current surface when landed ---
|
|
59
59
|
let currentSurface = null;
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
62
|
+
* Initialization: set up container and place at bottom-center of screen
|
|
63
63
|
*/
|
|
64
64
|
function init(container) {
|
|
65
65
|
petContainer = container;
|
|
66
66
|
screenW = window.innerWidth;
|
|
67
67
|
screenH = window.innerHeight;
|
|
68
68
|
|
|
69
|
-
//
|
|
69
|
+
// Start at bottom-center of screen
|
|
70
70
|
x = (screenW - CHAR_SIZE) / 2;
|
|
71
71
|
y = screenH - CHAR_SIZE;
|
|
72
72
|
edge = 'bottom';
|
|
@@ -76,7 +76,7 @@ const PetEngine = (() => {
|
|
|
76
76
|
currentSurface = null;
|
|
77
77
|
updateVisual();
|
|
78
78
|
|
|
79
|
-
//
|
|
79
|
+
// Handle window resize
|
|
80
80
|
window.addEventListener('resize', () => {
|
|
81
81
|
screenW = window.innerWidth;
|
|
82
82
|
screenH = window.innerHeight;
|
|
@@ -86,14 +86,14 @@ const PetEngine = (() => {
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
/**
|
|
89
|
-
*
|
|
89
|
+
* Set speed multiplier (adjusts speed based on personality/evolution stage)
|
|
90
90
|
*/
|
|
91
91
|
function setSpeedMultiplier(mult) {
|
|
92
92
|
speedMultiplier = mult;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
|
-
*
|
|
96
|
+
* Clamp position within screen bounds
|
|
97
97
|
*/
|
|
98
98
|
function clampPosition() {
|
|
99
99
|
x = Math.max(0, Math.min(x, screenW - CHAR_SIZE));
|
|
@@ -101,24 +101,24 @@ const PetEngine = (() => {
|
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
// ===================================
|
|
104
|
-
//
|
|
104
|
+
// Visual update
|
|
105
105
|
// ===================================
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
|
-
*
|
|
109
|
-
*
|
|
108
|
+
* Update container position and rotation/flip
|
|
109
|
+
* Apply transform so character faces correct direction per edge
|
|
110
110
|
*
|
|
111
|
-
* EDGE_OFFSET:
|
|
112
|
-
*
|
|
111
|
+
* EDGE_OFFSET: Compensates for empty pixels (4px) at sprite border
|
|
112
|
+
* so legs render flush against wall/floor/ceiling
|
|
113
113
|
*/
|
|
114
114
|
const EDGE_OFFSET = 4;
|
|
115
115
|
|
|
116
116
|
function updateVisual() {
|
|
117
117
|
if (!petContainer) return;
|
|
118
118
|
|
|
119
|
-
// --- flipX
|
|
119
|
+
// --- Smooth flipX transition handling ---
|
|
120
120
|
if (flipX !== prevFlipX) {
|
|
121
|
-
//
|
|
121
|
+
// Start transition when direction changed
|
|
122
122
|
flipStartTime = Date.now();
|
|
123
123
|
flipTransition = 0;
|
|
124
124
|
prevFlipX = flipX;
|
|
@@ -128,7 +128,7 @@ const PetEngine = (() => {
|
|
|
128
128
|
flipTransition = Math.min(1, elapsed / FLIP_DURATION);
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
//
|
|
131
|
+
// Apply CSS transition during flip
|
|
132
132
|
if (flipTransition < 1) {
|
|
133
133
|
petContainer.style.transition = `transform ${FLIP_DURATION}ms ease-in-out`;
|
|
134
134
|
} else {
|
|
@@ -138,21 +138,21 @@ const PetEngine = (() => {
|
|
|
138
138
|
let renderX = x;
|
|
139
139
|
let renderY = y;
|
|
140
140
|
|
|
141
|
-
//
|
|
141
|
+
// Apply offset only when attached to surface (not needed in mid-air)
|
|
142
142
|
if (onSurface && movementMode === 'crawling') {
|
|
143
143
|
switch (edge) {
|
|
144
144
|
case 'bottom':
|
|
145
145
|
case 'surface':
|
|
146
|
-
renderY += EDGE_OFFSET; //
|
|
146
|
+
renderY += EDGE_OFFSET; // Floor: press legs down
|
|
147
147
|
break;
|
|
148
148
|
case 'top':
|
|
149
|
-
renderY -= EDGE_OFFSET; //
|
|
149
|
+
renderY -= EDGE_OFFSET; // Ceiling: press legs up
|
|
150
150
|
break;
|
|
151
151
|
case 'left':
|
|
152
|
-
renderX -= EDGE_OFFSET; //
|
|
152
|
+
renderX -= EDGE_OFFSET; // Left wall: press legs left
|
|
153
153
|
break;
|
|
154
154
|
case 'right':
|
|
155
|
-
renderX += EDGE_OFFSET; //
|
|
155
|
+
renderX += EDGE_OFFSET; // Right wall: press legs right
|
|
156
156
|
break;
|
|
157
157
|
}
|
|
158
158
|
}
|
|
@@ -163,41 +163,41 @@ const PetEngine = (() => {
|
|
|
163
163
|
let transform = '';
|
|
164
164
|
|
|
165
165
|
if (movementMode === 'rappelling' || movementMode === 'jumping' || movementMode === 'falling') {
|
|
166
|
-
//
|
|
166
|
+
// Mid-air: default floor-based pose (no rotation)
|
|
167
167
|
if (flipX) transform = 'scaleX(-1)';
|
|
168
168
|
} else if (edge === 'left') {
|
|
169
|
-
//
|
|
169
|
+
// Left wall: rotate counter-clockwise so legs face left edge
|
|
170
170
|
transform = 'rotate(-90deg)';
|
|
171
171
|
if (flipX) transform += ' scaleX(-1)';
|
|
172
172
|
} else if (edge === 'right') {
|
|
173
|
-
//
|
|
173
|
+
// Right wall: rotate clockwise so legs face right edge
|
|
174
174
|
transform = 'rotate(90deg)';
|
|
175
175
|
if (flipX) transform += ' scaleX(-1)';
|
|
176
176
|
} else if (edge === 'top') {
|
|
177
|
-
//
|
|
177
|
+
// Ceiling: flip vertically so legs face upward
|
|
178
178
|
transform = 'scaleY(-1)';
|
|
179
179
|
if (flipX) transform += ' scaleX(-1)';
|
|
180
180
|
} else {
|
|
181
|
-
//
|
|
181
|
+
// Floor/surface: default pose
|
|
182
182
|
if (flipX) transform = 'scaleX(-1)';
|
|
183
183
|
}
|
|
184
184
|
|
|
185
185
|
petContainer.style.transform = transform || 'none';
|
|
186
186
|
|
|
187
|
-
//
|
|
187
|
+
// Update rappel thread visualization
|
|
188
188
|
updateThreadVisual();
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
/**
|
|
192
|
-
*
|
|
193
|
-
* thread
|
|
192
|
+
* Visualize rappel thread as SVG line
|
|
193
|
+
* Hide when no thread; when active, connect attachment point to character top
|
|
194
194
|
*/
|
|
195
195
|
function updateThreadVisual() {
|
|
196
196
|
const line = document.getElementById('thread-line');
|
|
197
197
|
if (!line) return;
|
|
198
198
|
|
|
199
199
|
if (!thread) {
|
|
200
|
-
//
|
|
200
|
+
// Hide when no thread
|
|
201
201
|
line.setAttribute('x1', '0');
|
|
202
202
|
line.setAttribute('y1', '0');
|
|
203
203
|
line.setAttribute('x2', '0');
|
|
@@ -206,7 +206,7 @@ const PetEngine = (() => {
|
|
|
206
206
|
return;
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
//
|
|
209
|
+
// Draw thread from attachment point to character top-center
|
|
210
210
|
line.style.display = 'block';
|
|
211
211
|
line.setAttribute('x1', thread.attachX);
|
|
212
212
|
line.setAttribute('y1', thread.attachY);
|
|
@@ -217,56 +217,56 @@ const PetEngine = (() => {
|
|
|
217
217
|
}
|
|
218
218
|
|
|
219
219
|
// ===================================
|
|
220
|
-
//
|
|
220
|
+
// Step-based movement (choppy walking)
|
|
221
221
|
// ===================================
|
|
222
222
|
|
|
223
223
|
/**
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
224
|
+
* Step movement: move one step only when animation frame transitions
|
|
225
|
+
* Leg motion (frame change) and actual position movement are 1:1 synced
|
|
226
|
+
* so the body only moves when legs move, creating natural walking
|
|
227
227
|
*
|
|
228
|
-
* @param {number} stepScale -
|
|
228
|
+
* @param {number} stepScale - Step size multiplier (0.6 = slow with load, 1.0 = default)
|
|
229
229
|
*/
|
|
230
230
|
function stepMove(stepScale) {
|
|
231
|
-
//
|
|
231
|
+
// Do not move if animation frame has not changed
|
|
232
232
|
if (!animFrameChanged) return;
|
|
233
233
|
|
|
234
|
-
//
|
|
234
|
+
// Advance one step
|
|
235
235
|
const stepDist = STEP_SIZE * stepScale * speedMultiplier;
|
|
236
236
|
|
|
237
237
|
if (edge === 'bottom' || edge === 'top' || edge === 'surface') {
|
|
238
|
-
//
|
|
238
|
+
// Horizontal movement (floor, ceiling, window surface)
|
|
239
239
|
x += stepDist * direction;
|
|
240
240
|
flipX = direction < 0;
|
|
241
241
|
} else if (edge === 'left') {
|
|
242
|
-
//
|
|
242
|
+
// Left wall: y-axis movement (direction=1 goes down, -1 goes up)
|
|
243
243
|
y += stepDist * direction;
|
|
244
244
|
} else if (edge === 'right') {
|
|
245
|
-
//
|
|
245
|
+
// Right wall: y-axis movement
|
|
246
246
|
y += stepDist * direction;
|
|
247
247
|
}
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
// ===================================
|
|
251
|
-
//
|
|
251
|
+
// Window surface detection
|
|
252
252
|
// ===================================
|
|
253
253
|
|
|
254
254
|
/**
|
|
255
|
-
*
|
|
256
|
-
*
|
|
255
|
+
* Find window surface below a given position
|
|
256
|
+
* Landable when character is within horizontal range and near surface top
|
|
257
257
|
*
|
|
258
|
-
* @param {number} px -
|
|
259
|
-
* @param {number} py -
|
|
260
|
-
* @returns {object|null}
|
|
258
|
+
* @param {number} px - Character x coordinate
|
|
259
|
+
* @param {number} py - Character bottom y coordinate (y + CHAR_SIZE)
|
|
260
|
+
* @returns {object|null} Landable surface or null
|
|
261
261
|
*/
|
|
262
262
|
function findSurfaceBelow(px, py) {
|
|
263
263
|
let closest = null;
|
|
264
264
|
let closestDist = Infinity;
|
|
265
265
|
|
|
266
266
|
for (const s of windowSurfaces) {
|
|
267
|
-
//
|
|
267
|
+
// Check horizontal range: whether character overlaps surface
|
|
268
268
|
if (px + CHAR_SIZE > s.x && px < s.x + s.width) {
|
|
269
|
-
//
|
|
269
|
+
// Check if near surface top (falling from above)
|
|
270
270
|
if (py >= s.y && py <= s.y + 10) {
|
|
271
271
|
const dist = Math.abs(py - s.y);
|
|
272
272
|
if (dist < closestDist) {
|
|
@@ -280,8 +280,8 @@ const PetEngine = (() => {
|
|
|
280
280
|
}
|
|
281
281
|
|
|
282
282
|
/**
|
|
283
|
-
*
|
|
284
|
-
* (
|
|
283
|
+
* Register window surface list from external source
|
|
284
|
+
* (e.g., register title bars of other windows as walkable surfaces)
|
|
285
285
|
*
|
|
286
286
|
* @param {Array} surfaces - [{ id, x, y, width, height }]
|
|
287
287
|
*/
|
|
@@ -290,28 +290,28 @@ const PetEngine = (() => {
|
|
|
290
290
|
}
|
|
291
291
|
|
|
292
292
|
// ===================================
|
|
293
|
-
//
|
|
293
|
+
// Physics state-based movement handling
|
|
294
294
|
// ===================================
|
|
295
295
|
|
|
296
296
|
/**
|
|
297
|
-
*
|
|
297
|
+
* Main movement logic: perform physics calculations based on movementMode
|
|
298
298
|
*
|
|
299
|
-
* @param {string} state - StateMachine
|
|
299
|
+
* @param {string} state - Current StateMachine state (walking, idle, etc.)
|
|
300
300
|
*/
|
|
301
301
|
function moveForState(state) {
|
|
302
302
|
switch (movementMode) {
|
|
303
303
|
|
|
304
|
-
// ---
|
|
304
|
+
// --- Parabolic jump ---
|
|
305
305
|
case 'jumping':
|
|
306
|
-
vy += GRAVITY; //
|
|
306
|
+
vy += GRAVITY; // Apply gravity
|
|
307
307
|
x += vx;
|
|
308
308
|
y += vy;
|
|
309
309
|
flipX = vx < 0;
|
|
310
310
|
|
|
311
|
-
//
|
|
311
|
+
// Floor landing detection
|
|
312
312
|
if (y >= screenH - CHAR_SIZE) {
|
|
313
313
|
y = screenH - CHAR_SIZE;
|
|
314
|
-
//
|
|
314
|
+
// Bounce effect: slight rebound
|
|
315
315
|
if (Math.abs(vy) > 2) {
|
|
316
316
|
vy = -vy * BOUNCE_FACTOR;
|
|
317
317
|
} else {
|
|
@@ -324,7 +324,7 @@ const PetEngine = (() => {
|
|
|
324
324
|
}
|
|
325
325
|
}
|
|
326
326
|
|
|
327
|
-
//
|
|
327
|
+
// Window surface landing detection (only while falling downward)
|
|
328
328
|
if (vy > 0 && movementMode === 'jumping') {
|
|
329
329
|
const landSurface = findSurfaceBelow(x, y + CHAR_SIZE);
|
|
330
330
|
if (landSurface) {
|
|
@@ -338,7 +338,7 @@ const PetEngine = (() => {
|
|
|
338
338
|
}
|
|
339
339
|
}
|
|
340
340
|
|
|
341
|
-
//
|
|
341
|
+
// Wall/ceiling collision -> attach to that edge
|
|
342
342
|
if (x <= 0 && movementMode === 'jumping') {
|
|
343
343
|
x = 0;
|
|
344
344
|
edge = 'left';
|
|
@@ -347,7 +347,7 @@ const PetEngine = (() => {
|
|
|
347
347
|
currentSurface = null;
|
|
348
348
|
vx = 0;
|
|
349
349
|
vy = 0;
|
|
350
|
-
direction = 1; //
|
|
350
|
+
direction = 1; // Downward direction
|
|
351
351
|
}
|
|
352
352
|
if (x >= screenW - CHAR_SIZE && movementMode === 'jumping') {
|
|
353
353
|
x = screenW - CHAR_SIZE;
|
|
@@ -367,16 +367,16 @@ const PetEngine = (() => {
|
|
|
367
367
|
currentSurface = null;
|
|
368
368
|
vx = 0;
|
|
369
369
|
vy = 0;
|
|
370
|
-
direction = 1; //
|
|
370
|
+
direction = 1; // Rightward direction
|
|
371
371
|
}
|
|
372
372
|
break;
|
|
373
373
|
|
|
374
|
-
// ---
|
|
374
|
+
// --- Free fall (gravity) ---
|
|
375
375
|
case 'falling':
|
|
376
376
|
vy += GRAVITY;
|
|
377
377
|
y += vy;
|
|
378
378
|
|
|
379
|
-
//
|
|
379
|
+
// Window surface landing detection
|
|
380
380
|
if (vy > 0) {
|
|
381
381
|
const fallSurface = findSurfaceBelow(x, y + CHAR_SIZE);
|
|
382
382
|
if (fallSurface) {
|
|
@@ -389,7 +389,7 @@ const PetEngine = (() => {
|
|
|
389
389
|
}
|
|
390
390
|
}
|
|
391
391
|
|
|
392
|
-
//
|
|
392
|
+
// Floor landing
|
|
393
393
|
if (y >= screenH - CHAR_SIZE) {
|
|
394
394
|
y = screenH - CHAR_SIZE;
|
|
395
395
|
edge = 'bottom';
|
|
@@ -400,23 +400,23 @@ const PetEngine = (() => {
|
|
|
400
400
|
}
|
|
401
401
|
break;
|
|
402
402
|
|
|
403
|
-
// ---
|
|
403
|
+
// --- Rappel: pendulum swing descent on thread ---
|
|
404
404
|
case 'rappelling':
|
|
405
405
|
if (thread) {
|
|
406
|
-
//
|
|
406
|
+
// Increase thread length -> descend
|
|
407
407
|
thread.length += THREAD_SPEED * speedMultiplier;
|
|
408
408
|
|
|
409
|
-
//
|
|
409
|
+
// Pendulum swing physics
|
|
410
410
|
thread.swingVel += Math.sin(thread.angle) * 0.01;
|
|
411
|
-
thread.swingVel *= 0.98; //
|
|
411
|
+
thread.swingVel *= 0.98; // Damping
|
|
412
412
|
|
|
413
413
|
thread.angle += thread.swingVel;
|
|
414
414
|
|
|
415
|
-
//
|
|
415
|
+
// Calculate pendulum position from attachment point
|
|
416
416
|
x = thread.attachX + Math.sin(thread.angle) * thread.length - CHAR_SIZE / 2;
|
|
417
417
|
y = thread.attachY + Math.cos(thread.angle) * thread.length;
|
|
418
418
|
|
|
419
|
-
//
|
|
419
|
+
// Bounce off left/right screen edges
|
|
420
420
|
if (x <= 0) {
|
|
421
421
|
x = 0;
|
|
422
422
|
thread.swingVel = Math.abs(thread.swingVel) * 0.5;
|
|
@@ -426,7 +426,7 @@ const PetEngine = (() => {
|
|
|
426
426
|
thread.swingVel = -Math.abs(thread.swingVel) * 0.5;
|
|
427
427
|
}
|
|
428
428
|
|
|
429
|
-
//
|
|
429
|
+
// Window surface landing detection
|
|
430
430
|
const rappelSurface = findSurfaceBelow(x, y + CHAR_SIZE);
|
|
431
431
|
if (rappelSurface) {
|
|
432
432
|
y = rappelSurface.y - CHAR_SIZE;
|
|
@@ -437,7 +437,7 @@ const PetEngine = (() => {
|
|
|
437
437
|
currentSurface = rappelSurface;
|
|
438
438
|
}
|
|
439
439
|
|
|
440
|
-
//
|
|
440
|
+
// Reached floor
|
|
441
441
|
if (y >= screenH - CHAR_SIZE) {
|
|
442
442
|
y = screenH - CHAR_SIZE;
|
|
443
443
|
thread = null;
|
|
@@ -449,7 +449,7 @@ const PetEngine = (() => {
|
|
|
449
449
|
}
|
|
450
450
|
break;
|
|
451
451
|
|
|
452
|
-
// ---
|
|
452
|
+
// --- Surface crawling (step-based) ---
|
|
453
453
|
case 'crawling':
|
|
454
454
|
default:
|
|
455
455
|
switch (state) {
|
|
@@ -457,17 +457,17 @@ const PetEngine = (() => {
|
|
|
457
457
|
case 'ceiling_walk':
|
|
458
458
|
stepMove(1.0);
|
|
459
459
|
|
|
460
|
-
//
|
|
460
|
+
// Boundary handling for horizontal movement
|
|
461
461
|
if (edge === 'bottom' || edge === 'top') {
|
|
462
462
|
if (x <= 0) { x = 0; direction = 1; }
|
|
463
463
|
if (x >= screenW - CHAR_SIZE) { x = screenW - CHAR_SIZE; direction = -1; }
|
|
464
464
|
}
|
|
465
465
|
|
|
466
|
-
//
|
|
466
|
+
// Fall off edge when moving on window surface
|
|
467
467
|
if (edge === 'surface' && currentSurface) {
|
|
468
468
|
if (x <= currentSurface.x - CHAR_SIZE / 2 ||
|
|
469
469
|
x >= currentSurface.x + currentSurface.width - CHAR_SIZE / 2) {
|
|
470
|
-
//
|
|
470
|
+
// Fell off surface edge -> falling mode
|
|
471
471
|
movementMode = 'falling';
|
|
472
472
|
onSurface = false;
|
|
473
473
|
currentSurface = null;
|
|
@@ -479,7 +479,7 @@ const PetEngine = (() => {
|
|
|
479
479
|
|
|
480
480
|
case 'climbing_up':
|
|
481
481
|
if (edge === 'bottom' || edge === 'surface') {
|
|
482
|
-
//
|
|
482
|
+
// Transition from floor/surface to wall
|
|
483
483
|
if (direction > 0) {
|
|
484
484
|
x = screenW - CHAR_SIZE;
|
|
485
485
|
edge = 'right';
|
|
@@ -488,33 +488,33 @@ const PetEngine = (() => {
|
|
|
488
488
|
edge = 'left';
|
|
489
489
|
}
|
|
490
490
|
currentSurface = null;
|
|
491
|
-
direction = -1; //
|
|
491
|
+
direction = -1; // Upward direction on wall
|
|
492
492
|
}
|
|
493
493
|
|
|
494
|
-
//
|
|
494
|
+
// Climbing up wall: y decreases
|
|
495
495
|
if (edge === 'left' || edge === 'right') {
|
|
496
496
|
stepMove(0.7);
|
|
497
|
-
// stepMove
|
|
497
|
+
// stepMove applies direction(-1) so y decreases
|
|
498
498
|
}
|
|
499
499
|
|
|
500
|
-
//
|
|
500
|
+
// Reached ceiling
|
|
501
501
|
if (y <= 0) {
|
|
502
502
|
y = 0;
|
|
503
503
|
edge = 'top';
|
|
504
|
-
direction = 1; //
|
|
504
|
+
direction = 1; // Move rightward on ceiling
|
|
505
505
|
}
|
|
506
506
|
break;
|
|
507
507
|
|
|
508
508
|
case 'climbing_down':
|
|
509
|
-
//
|
|
509
|
+
// Climbing down wall: y increases
|
|
510
510
|
if (edge === 'left' || edge === 'right') {
|
|
511
|
-
// direction
|
|
511
|
+
// Set direction to 1 (down) for stepMove
|
|
512
512
|
const prevDir = direction;
|
|
513
513
|
direction = 1;
|
|
514
514
|
stepMove(0.7);
|
|
515
515
|
direction = prevDir;
|
|
516
516
|
} else if (edge === 'top') {
|
|
517
|
-
//
|
|
517
|
+
// Start descending from ceiling to wall
|
|
518
518
|
if (x < screenW / 2) {
|
|
519
519
|
x = 0;
|
|
520
520
|
edge = 'left';
|
|
@@ -522,19 +522,19 @@ const PetEngine = (() => {
|
|
|
522
522
|
x = screenW - CHAR_SIZE;
|
|
523
523
|
edge = 'right';
|
|
524
524
|
}
|
|
525
|
-
direction = 1; //
|
|
525
|
+
direction = 1; // Downward direction
|
|
526
526
|
}
|
|
527
527
|
|
|
528
|
-
//
|
|
528
|
+
// Reached floor
|
|
529
529
|
if (y >= screenH - CHAR_SIZE) {
|
|
530
530
|
y = screenH - CHAR_SIZE;
|
|
531
531
|
edge = 'bottom';
|
|
532
|
-
direction = Math.random() < 0.5 ? 1 : -1; //
|
|
532
|
+
direction = Math.random() < 0.5 ? 1 : -1; // Random direction
|
|
533
533
|
}
|
|
534
534
|
break;
|
|
535
535
|
|
|
536
536
|
case 'scared':
|
|
537
|
-
//
|
|
537
|
+
// Flee: skip steps, fast continuous movement
|
|
538
538
|
if (edge === 'bottom' || edge === 'top' || edge === 'surface') {
|
|
539
539
|
x += STEP_SIZE * 2.5 * direction * speedMultiplier;
|
|
540
540
|
flipX = direction < 0;
|
|
@@ -544,7 +544,7 @@ const PetEngine = (() => {
|
|
|
544
544
|
break;
|
|
545
545
|
|
|
546
546
|
case 'carrying':
|
|
547
|
-
//
|
|
547
|
+
// Slow movement while carrying
|
|
548
548
|
stepMove(0.6);
|
|
549
549
|
if (edge === 'bottom' || edge === 'top' || edge === 'surface') {
|
|
550
550
|
if (x <= 0) { x = 0; direction = 1; }
|
|
@@ -553,7 +553,7 @@ const PetEngine = (() => {
|
|
|
553
553
|
break;
|
|
554
554
|
|
|
555
555
|
case 'excited':
|
|
556
|
-
//
|
|
556
|
+
// Small jump effect (bouncing in place)
|
|
557
557
|
if (typeof StateMachine !== 'undefined') {
|
|
558
558
|
const elapsed = StateMachine.getElapsed();
|
|
559
559
|
const jumpOffset = Math.sin(elapsed / 150) * 8;
|
|
@@ -565,22 +565,22 @@ const PetEngine = (() => {
|
|
|
565
565
|
}
|
|
566
566
|
break;
|
|
567
567
|
|
|
568
|
-
//
|
|
568
|
+
// Jumping state (physics state transitioned from StateMachine)
|
|
569
569
|
case 'jumping':
|
|
570
|
-
// movementMode
|
|
570
|
+
// Initiate if movementMode is not yet jumping
|
|
571
571
|
if (movementMode === 'crawling') {
|
|
572
572
|
_initiateRandomJump();
|
|
573
573
|
}
|
|
574
574
|
break;
|
|
575
575
|
|
|
576
|
-
//
|
|
576
|
+
// Rappelling state
|
|
577
577
|
case 'rappelling':
|
|
578
578
|
if (movementMode === 'crawling') {
|
|
579
579
|
startRappel();
|
|
580
580
|
}
|
|
581
581
|
break;
|
|
582
582
|
|
|
583
|
-
//
|
|
583
|
+
// Falling state
|
|
584
584
|
case 'falling':
|
|
585
585
|
if (movementMode === 'crawling') {
|
|
586
586
|
movementMode = 'falling';
|
|
@@ -589,7 +589,7 @@ const PetEngine = (() => {
|
|
|
589
589
|
}
|
|
590
590
|
break;
|
|
591
591
|
|
|
592
|
-
//
|
|
592
|
+
// Custom movement pattern in progress
|
|
593
593
|
case 'custom':
|
|
594
594
|
if (activeCustomMovement) {
|
|
595
595
|
updateCustomMovement(now - (updateCustomMovement._lastTime || now));
|
|
@@ -601,7 +601,7 @@ const PetEngine = (() => {
|
|
|
601
601
|
case 'sleeping':
|
|
602
602
|
case 'interacting':
|
|
603
603
|
case 'playing':
|
|
604
|
-
//
|
|
604
|
+
// Stationary or subtle sway (no movement)
|
|
605
605
|
break;
|
|
606
606
|
}
|
|
607
607
|
break;
|
|
@@ -612,26 +612,26 @@ const PetEngine = (() => {
|
|
|
612
612
|
}
|
|
613
613
|
|
|
614
614
|
/**
|
|
615
|
-
*
|
|
616
|
-
*
|
|
615
|
+
* Random jump when StateMachine transitions to jumping state
|
|
616
|
+
* Leap toward screen center or random position from current location
|
|
617
617
|
*/
|
|
618
618
|
function _initiateRandomJump() {
|
|
619
|
-
//
|
|
619
|
+
// Random target point near screen center
|
|
620
620
|
const targetX = screenW * 0.2 + Math.random() * screenW * 0.6;
|
|
621
621
|
const targetY = screenH * 0.3 + Math.random() * screenH * 0.4;
|
|
622
622
|
jumpTo(targetX, targetY);
|
|
623
623
|
}
|
|
624
624
|
|
|
625
625
|
// ===================================
|
|
626
|
-
//
|
|
626
|
+
// Jump commands
|
|
627
627
|
// ===================================
|
|
628
628
|
|
|
629
629
|
/**
|
|
630
|
-
*
|
|
631
|
-
*
|
|
630
|
+
* Start parabolic jump toward target point
|
|
631
|
+
* Calculate initial velocity (vx, vy) to create parabolic trajectory
|
|
632
632
|
*
|
|
633
|
-
* @param {number} targetX -
|
|
634
|
-
* @param {number} targetY -
|
|
633
|
+
* @param {number} targetX - Target x coordinate
|
|
634
|
+
* @param {number} targetY - Target y coordinate
|
|
635
635
|
*/
|
|
636
636
|
function jumpTo(targetX, targetY) {
|
|
637
637
|
if (movementMode !== 'crawling') return;
|
|
@@ -640,14 +640,14 @@ const PetEngine = (() => {
|
|
|
640
640
|
const dy = targetY - y;
|
|
641
641
|
const dist = Math.hypot(dx, dy);
|
|
642
642
|
|
|
643
|
-
//
|
|
643
|
+
// Estimate flight time (distance-based)
|
|
644
644
|
const time = Math.max(20, dist / (JUMP_VX * 2 + 2));
|
|
645
645
|
|
|
646
|
-
//
|
|
646
|
+
// Calculate parabolic initial velocity
|
|
647
647
|
vx = dx / time;
|
|
648
648
|
vy = (dy / time) - (GRAVITY * time) / 2;
|
|
649
649
|
|
|
650
|
-
// vx, vy
|
|
650
|
+
// Clamp vx, vy range (prevent excessive speed)
|
|
651
651
|
const maxV = 8;
|
|
652
652
|
vx = Math.max(-maxV, Math.min(maxV, vx));
|
|
653
653
|
vy = Math.max(-12, Math.min(maxV, vy));
|
|
@@ -662,29 +662,29 @@ const PetEngine = (() => {
|
|
|
662
662
|
}
|
|
663
663
|
|
|
664
664
|
// ===================================
|
|
665
|
-
//
|
|
665
|
+
// Rappel (Thread) system
|
|
666
666
|
// ===================================
|
|
667
667
|
|
|
668
668
|
/**
|
|
669
|
-
*
|
|
670
|
-
*
|
|
669
|
+
* Start rappel: descend by lowering thread from ceiling or wall
|
|
670
|
+
* Set attachment point at current position and begin pendulum swing
|
|
671
671
|
*/
|
|
672
672
|
function startRappel() {
|
|
673
|
-
//
|
|
673
|
+
// Rappel only possible from ceiling, left wall, or right wall
|
|
674
674
|
if (edge !== 'top' && edge !== 'left' && edge !== 'right') return;
|
|
675
675
|
|
|
676
676
|
let attachX, attachY;
|
|
677
677
|
|
|
678
678
|
if (edge === 'top') {
|
|
679
|
-
//
|
|
679
|
+
// Rappel from ceiling: attach directly above current position
|
|
680
680
|
attachX = x + CHAR_SIZE / 2;
|
|
681
681
|
attachY = 0;
|
|
682
682
|
} else if (edge === 'left') {
|
|
683
|
-
//
|
|
683
|
+
// Rappel from left wall: attach at current y position on wall
|
|
684
684
|
attachX = 0;
|
|
685
685
|
attachY = y;
|
|
686
686
|
} else {
|
|
687
|
-
//
|
|
687
|
+
// Rappel from right wall
|
|
688
688
|
attachX = screenW;
|
|
689
689
|
attachY = y;
|
|
690
690
|
}
|
|
@@ -692,9 +692,9 @@ const PetEngine = (() => {
|
|
|
692
692
|
thread = {
|
|
693
693
|
attachX: attachX,
|
|
694
694
|
attachY: attachY,
|
|
695
|
-
length: CHAR_SIZE, //
|
|
696
|
-
angle: 0, //
|
|
697
|
-
swingVel: (Math.random() - 0.5) * 0.05, //
|
|
695
|
+
length: CHAR_SIZE, // Initial thread length
|
|
696
|
+
angle: 0, // Pendulum angle (radians)
|
|
697
|
+
swingVel: (Math.random() - 0.5) * 0.05, // Initial swing velocity
|
|
698
698
|
};
|
|
699
699
|
|
|
700
700
|
movementMode = 'rappelling';
|
|
@@ -707,7 +707,7 @@ const PetEngine = (() => {
|
|
|
707
707
|
}
|
|
708
708
|
|
|
709
709
|
/**
|
|
710
|
-
*
|
|
710
|
+
* Release rappel: let go of thread to transition to free fall
|
|
711
711
|
*/
|
|
712
712
|
function releaseThread() {
|
|
713
713
|
if (!thread) return;
|
|
@@ -722,41 +722,41 @@ const PetEngine = (() => {
|
|
|
722
722
|
}
|
|
723
723
|
|
|
724
724
|
/**
|
|
725
|
-
*
|
|
726
|
-
*
|
|
725
|
+
* Move to screen center
|
|
726
|
+
* Descend via rappel from ceiling, otherwise jump
|
|
727
727
|
*/
|
|
728
728
|
function moveToCenter() {
|
|
729
729
|
const cx = (screenW - CHAR_SIZE) / 2;
|
|
730
730
|
const cy = (screenH - CHAR_SIZE) / 2;
|
|
731
731
|
|
|
732
732
|
if (edge === 'top') {
|
|
733
|
-
//
|
|
733
|
+
// From ceiling, descend via rappel
|
|
734
734
|
startRappel();
|
|
735
735
|
} else {
|
|
736
|
-
//
|
|
736
|
+
// From floor/wall, jump to center
|
|
737
737
|
jumpTo(cx, cy);
|
|
738
738
|
}
|
|
739
739
|
}
|
|
740
740
|
|
|
741
741
|
// ===================================
|
|
742
|
-
//
|
|
742
|
+
// Animation frame update
|
|
743
743
|
// ===================================
|
|
744
744
|
|
|
745
745
|
/**
|
|
746
|
-
*
|
|
747
|
-
*
|
|
746
|
+
* Render animation frame matching current state
|
|
747
|
+
* Reuse existing frameset when in mid-air
|
|
748
748
|
*
|
|
749
|
-
* @param {string} state - StateMachine
|
|
750
|
-
* @param {number} timestamp - requestAnimationFrame
|
|
749
|
+
* @param {string} state - StateMachine state
|
|
750
|
+
* @param {number} timestamp - requestAnimationFrame timestamp
|
|
751
751
|
*/
|
|
752
752
|
function updateAnimation(state, timestamp) {
|
|
753
753
|
if (timestamp - lastAnimTime > ANIM_INTERVAL) {
|
|
754
754
|
animFrame++;
|
|
755
755
|
lastAnimTime = timestamp;
|
|
756
|
-
animFrameChanged = true; //
|
|
756
|
+
animFrameChanged = true; // Notify movement system of frame transition
|
|
757
757
|
}
|
|
758
758
|
|
|
759
|
-
//
|
|
759
|
+
// Map to appropriate frameset based on movement mode
|
|
760
760
|
let effectiveState = state;
|
|
761
761
|
if (movementMode === 'jumping') effectiveState = 'jumping';
|
|
762
762
|
if (movementMode === 'falling') effectiveState = 'falling';
|
|
@@ -768,11 +768,11 @@ const PetEngine = (() => {
|
|
|
768
768
|
}
|
|
769
769
|
|
|
770
770
|
// ===================================
|
|
771
|
-
//
|
|
771
|
+
// Position/state accessors
|
|
772
772
|
// ===================================
|
|
773
773
|
|
|
774
774
|
/**
|
|
775
|
-
*
|
|
775
|
+
* Return current position and state info
|
|
776
776
|
* @returns {{ x, y, edge, direction, flipX, movementMode, onSurface, thread }}
|
|
777
777
|
*/
|
|
778
778
|
function getPosition() {
|
|
@@ -784,7 +784,7 @@ const PetEngine = (() => {
|
|
|
784
784
|
}
|
|
785
785
|
|
|
786
786
|
/**
|
|
787
|
-
*
|
|
787
|
+
* Set position directly (for drag, etc.)
|
|
788
788
|
*/
|
|
789
789
|
function setPosition(nx, ny) {
|
|
790
790
|
x = nx;
|
|
@@ -803,8 +803,8 @@ const PetEngine = (() => {
|
|
|
803
803
|
}
|
|
804
804
|
|
|
805
805
|
/**
|
|
806
|
-
*
|
|
807
|
-
*
|
|
806
|
+
* Snap to nearest edge instantly (after drag)
|
|
807
|
+
* Reset all physics state and attach to surface
|
|
808
808
|
*/
|
|
809
809
|
function snapToNearestEdge() {
|
|
810
810
|
const distBottom = screenH - CHAR_SIZE - y;
|
|
@@ -827,7 +827,7 @@ const PetEngine = (() => {
|
|
|
827
827
|
edge = 'right';
|
|
828
828
|
}
|
|
829
829
|
|
|
830
|
-
//
|
|
830
|
+
// Full physics state reset
|
|
831
831
|
movementMode = 'crawling';
|
|
832
832
|
onSurface = true;
|
|
833
833
|
currentSurface = null;
|
|
@@ -839,8 +839,8 @@ const PetEngine = (() => {
|
|
|
839
839
|
}
|
|
840
840
|
|
|
841
841
|
/**
|
|
842
|
-
*
|
|
843
|
-
*
|
|
842
|
+
* Start free fall (when released near screen center)
|
|
843
|
+
* Fall to floor or nearest surface by gravity
|
|
844
844
|
*/
|
|
845
845
|
function startFalling() {
|
|
846
846
|
movementMode = 'falling';
|
|
@@ -856,7 +856,7 @@ const PetEngine = (() => {
|
|
|
856
856
|
}
|
|
857
857
|
|
|
858
858
|
/**
|
|
859
|
-
*
|
|
859
|
+
* Return rappel thread info
|
|
860
860
|
* @returns {object|null}
|
|
861
861
|
*/
|
|
862
862
|
function getThread() {
|
|
@@ -864,36 +864,36 @@ const PetEngine = (() => {
|
|
|
864
864
|
}
|
|
865
865
|
|
|
866
866
|
// ===================================
|
|
867
|
-
//
|
|
867
|
+
// Custom movement pattern registry
|
|
868
868
|
// ===================================
|
|
869
869
|
|
|
870
|
-
//
|
|
871
|
-
//
|
|
870
|
+
// Registered custom movement pattern store
|
|
871
|
+
// Each handler: { init(params), update(deltaTime), isComplete(), cleanup() }
|
|
872
872
|
let customMovements = {};
|
|
873
|
-
let activeCustomMovement = null; //
|
|
873
|
+
let activeCustomMovement = null; // Currently active custom movement { name, handler, state }
|
|
874
874
|
|
|
875
875
|
/**
|
|
876
|
-
*
|
|
877
|
-
* @param {string} name -
|
|
876
|
+
* Register custom movement pattern
|
|
877
|
+
* @param {string} name - Pattern name (e.g., 'zigzag', 'patrol')
|
|
878
878
|
* @param {object} handler - { init, update, isComplete, cleanup }
|
|
879
879
|
*/
|
|
880
880
|
function registerMovement(name, handler) {
|
|
881
881
|
if (!handler || typeof handler.update !== 'function') {
|
|
882
|
-
console.error(`[PetEngine]
|
|
882
|
+
console.error(`[PetEngine] Failed to register movement pattern '${name}': update function required`);
|
|
883
883
|
return false;
|
|
884
884
|
}
|
|
885
|
-
//
|
|
885
|
+
// Fill in default methods
|
|
886
886
|
handler.init = handler.init || (() => {});
|
|
887
887
|
handler.isComplete = handler.isComplete || (() => false);
|
|
888
888
|
handler.cleanup = handler.cleanup || (() => {});
|
|
889
889
|
customMovements[name] = handler;
|
|
890
|
-
console.log(`[PetEngine]
|
|
890
|
+
console.log(`[PetEngine] Custom movement pattern registered: ${name}`);
|
|
891
891
|
return true;
|
|
892
892
|
}
|
|
893
893
|
|
|
894
894
|
/**
|
|
895
|
-
*
|
|
896
|
-
* @param {string} name -
|
|
895
|
+
* Remove registered custom movement pattern
|
|
896
|
+
* @param {string} name - Pattern name
|
|
897
897
|
*/
|
|
898
898
|
function unregisterMovement(name) {
|
|
899
899
|
if (activeCustomMovement && activeCustomMovement.name === name) {
|
|
@@ -903,24 +903,24 @@ const PetEngine = (() => {
|
|
|
903
903
|
}
|
|
904
904
|
|
|
905
905
|
/**
|
|
906
|
-
*
|
|
907
|
-
* @param {string} name -
|
|
908
|
-
* @param {object} params -
|
|
909
|
-
* @returns {boolean}
|
|
906
|
+
* Execute custom movement pattern
|
|
907
|
+
* @param {string} name - Registered pattern name
|
|
908
|
+
* @param {object} params - Pattern initialization parameters
|
|
909
|
+
* @returns {boolean} Whether execution succeeded
|
|
910
910
|
*/
|
|
911
911
|
function executeCustomMovement(name, params = {}) {
|
|
912
912
|
const handler = customMovements[name];
|
|
913
913
|
if (!handler) {
|
|
914
|
-
console.warn(`[PetEngine]
|
|
914
|
+
console.warn(`[PetEngine] Unregistered movement pattern: ${name}`);
|
|
915
915
|
return false;
|
|
916
916
|
}
|
|
917
917
|
|
|
918
|
-
//
|
|
918
|
+
// Clean up existing custom movement if any
|
|
919
919
|
if (activeCustomMovement) {
|
|
920
920
|
activeCustomMovement.handler.cleanup();
|
|
921
921
|
}
|
|
922
922
|
|
|
923
|
-
//
|
|
923
|
+
// Pass current position/screen info on pattern initialization
|
|
924
924
|
const context = {
|
|
925
925
|
x, y, screenW, screenH,
|
|
926
926
|
charSize: CHAR_SIZE,
|
|
@@ -930,18 +930,18 @@ const PetEngine = (() => {
|
|
|
930
930
|
const state = handler.init(Object.assign({}, params, context)) || {};
|
|
931
931
|
activeCustomMovement = { name, handler, state };
|
|
932
932
|
|
|
933
|
-
// CUSTOM
|
|
933
|
+
// Transition to CUSTOM state
|
|
934
934
|
if (typeof StateMachine !== 'undefined') {
|
|
935
935
|
StateMachine.forceState('custom');
|
|
936
936
|
}
|
|
937
937
|
|
|
938
|
-
console.log(`[PetEngine]
|
|
938
|
+
console.log(`[PetEngine] Executing custom movement: ${name}`);
|
|
939
939
|
return true;
|
|
940
940
|
}
|
|
941
941
|
|
|
942
942
|
/**
|
|
943
|
-
*
|
|
944
|
-
* @param {number} deltaTime -
|
|
943
|
+
* Update custom movement every frame
|
|
944
|
+
* @param {number} deltaTime - Elapsed time between frames (ms)
|
|
945
945
|
*/
|
|
946
946
|
function updateCustomMovement(deltaTime) {
|
|
947
947
|
if (!activeCustomMovement) return;
|
|
@@ -958,17 +958,17 @@ const PetEngine = (() => {
|
|
|
958
958
|
|
|
959
959
|
handler.update(deltaTime, state, context);
|
|
960
960
|
|
|
961
|
-
//
|
|
961
|
+
// Handler may have set position via setPos
|
|
962
962
|
clampPosition();
|
|
963
963
|
|
|
964
|
-
//
|
|
964
|
+
// Check completion
|
|
965
965
|
if (handler.isComplete(state)) {
|
|
966
966
|
stopCustomMovement();
|
|
967
967
|
}
|
|
968
968
|
}
|
|
969
969
|
|
|
970
970
|
/**
|
|
971
|
-
*
|
|
971
|
+
* Force stop current custom movement -> return to IDLE
|
|
972
972
|
*/
|
|
973
973
|
function stopCustomMovement() {
|
|
974
974
|
if (!activeCustomMovement) return;
|
|
@@ -981,24 +981,24 @@ const PetEngine = (() => {
|
|
|
981
981
|
}
|
|
982
982
|
|
|
983
983
|
/**
|
|
984
|
-
*
|
|
984
|
+
* Return list of registered custom movement patterns
|
|
985
985
|
*/
|
|
986
986
|
function getRegisteredMovements() {
|
|
987
987
|
return Object.keys(customMovements);
|
|
988
988
|
}
|
|
989
989
|
|
|
990
|
-
// ---
|
|
990
|
+
// --- Pre-registered movement patterns ---
|
|
991
991
|
|
|
992
|
-
//
|
|
992
|
+
// Zigzag: alternating diagonal movement
|
|
993
993
|
registerMovement('zigzag', {
|
|
994
994
|
init(params) {
|
|
995
995
|
return {
|
|
996
|
-
amplitude: params.amplitude || 40, //
|
|
997
|
-
speed: params.speed || 2, //
|
|
998
|
-
segmentLength: params.segmentLength || 60, //
|
|
996
|
+
amplitude: params.amplitude || 40, // Horizontal amplitude (px)
|
|
997
|
+
speed: params.speed || 2, // Forward speed
|
|
998
|
+
segmentLength: params.segmentLength || 60, // Segment length
|
|
999
999
|
traveled: 0,
|
|
1000
|
-
totalDistance: params.distance || 300, //
|
|
1001
|
-
zigDir: 1, //
|
|
1000
|
+
totalDistance: params.distance || 300, // Total travel distance
|
|
1001
|
+
zigDir: 1, // Zigzag direction
|
|
1002
1002
|
startX: params.x,
|
|
1003
1003
|
startY: params.y,
|
|
1004
1004
|
};
|
|
@@ -1007,9 +1007,9 @@ const PetEngine = (() => {
|
|
|
1007
1007
|
const step = state.speed * (dt / 16);
|
|
1008
1008
|
state.traveled += step;
|
|
1009
1009
|
|
|
1010
|
-
//
|
|
1010
|
+
// Horizontal advance
|
|
1011
1011
|
const moveX = step * (ctx.direction || 1);
|
|
1012
|
-
//
|
|
1012
|
+
// Vertical zigzag
|
|
1013
1013
|
const segProgress = (state.traveled % state.segmentLength) / state.segmentLength;
|
|
1014
1014
|
if (segProgress < 0.05) state.zigDir *= -1;
|
|
1015
1015
|
const moveY = state.zigDir * step * 0.7;
|
|
@@ -1023,14 +1023,14 @@ const PetEngine = (() => {
|
|
|
1023
1023
|
cleanup() {},
|
|
1024
1024
|
});
|
|
1025
1025
|
|
|
1026
|
-
//
|
|
1026
|
+
// Patrol: round-trip between two points
|
|
1027
1027
|
registerMovement('patrol', {
|
|
1028
1028
|
init(params) {
|
|
1029
1029
|
return {
|
|
1030
1030
|
pointA: { x: params.pointAX || 100, y: params.pointAY || params.y },
|
|
1031
1031
|
pointB: { x: params.pointBX || params.screenW - 164, y: params.pointBY || params.y },
|
|
1032
1032
|
speed: params.speed || 1.5,
|
|
1033
|
-
laps: params.laps || 3, //
|
|
1033
|
+
laps: params.laps || 3, // Number of round trips
|
|
1034
1034
|
currentLap: 0,
|
|
1035
1035
|
targetIdx: 0, // 0=A, 1=B
|
|
1036
1036
|
};
|
|
@@ -1042,7 +1042,7 @@ const PetEngine = (() => {
|
|
|
1042
1042
|
const dist = Math.hypot(dx, dy);
|
|
1043
1043
|
|
|
1044
1044
|
if (dist < 5) {
|
|
1045
|
-
//
|
|
1045
|
+
// Reached target -> reverse direction
|
|
1046
1046
|
state.targetIdx = 1 - state.targetIdx;
|
|
1047
1047
|
if (state.targetIdx === 0) state.currentLap++;
|
|
1048
1048
|
return;
|
|
@@ -1059,14 +1059,14 @@ const PetEngine = (() => {
|
|
|
1059
1059
|
cleanup() {},
|
|
1060
1060
|
});
|
|
1061
1061
|
|
|
1062
|
-
//
|
|
1062
|
+
// Circular rotation: revolve around center point
|
|
1063
1063
|
registerMovement('circle', {
|
|
1064
1064
|
init(params) {
|
|
1065
1065
|
return {
|
|
1066
1066
|
centerX: params.centerX || params.x,
|
|
1067
1067
|
centerY: params.centerY || params.y - 50,
|
|
1068
1068
|
radius: params.radius || 50,
|
|
1069
|
-
speed: params.speed || 0.03, //
|
|
1069
|
+
speed: params.speed || 0.03, // Angular velocity (rad/frame)
|
|
1070
1070
|
angle: 0,
|
|
1071
1071
|
totalAngle: params.revolutions ? params.revolutions * Math.PI * 2 : Math.PI * 4,
|
|
1072
1072
|
traveled: 0,
|
|
@@ -1088,12 +1088,12 @@ const PetEngine = (() => {
|
|
|
1088
1088
|
cleanup() {},
|
|
1089
1089
|
});
|
|
1090
1090
|
|
|
1091
|
-
//
|
|
1091
|
+
// Shake: fast horizontal vibration
|
|
1092
1092
|
registerMovement('shake', {
|
|
1093
1093
|
init(params) {
|
|
1094
1094
|
return {
|
|
1095
|
-
intensity: params.intensity || 4, //
|
|
1096
|
-
duration: params.duration || 800, //
|
|
1095
|
+
intensity: params.intensity || 4, // Shake intensity (px)
|
|
1096
|
+
duration: params.duration || 800, // Duration (ms)
|
|
1097
1097
|
elapsed: 0,
|
|
1098
1098
|
originX: params.x,
|
|
1099
1099
|
originY: params.y,
|
|
@@ -1104,7 +1104,7 @@ const PetEngine = (() => {
|
|
|
1104
1104
|
state.elapsed += dt;
|
|
1105
1105
|
state.phase += dt * 0.05;
|
|
1106
1106
|
|
|
1107
|
-
//
|
|
1107
|
+
// Damped sinusoidal vibration
|
|
1108
1108
|
const decay = 1 - (state.elapsed / state.duration);
|
|
1109
1109
|
const offsetX = Math.sin(state.phase) * state.intensity * decay;
|
|
1110
1110
|
ctx.setPos(state.originX + offsetX, state.originY);
|
|
@@ -1115,7 +1115,7 @@ const PetEngine = (() => {
|
|
|
1115
1115
|
cleanup() {},
|
|
1116
1116
|
});
|
|
1117
1117
|
|
|
1118
|
-
//
|
|
1118
|
+
// Dance: sequential combo of moves (jump + spin + shake)
|
|
1119
1119
|
registerMovement('dance', {
|
|
1120
1120
|
init(params) {
|
|
1121
1121
|
return {
|
|
@@ -1132,18 +1132,18 @@ const PetEngine = (() => {
|
|
|
1132
1132
|
|
|
1133
1133
|
const t = state.elapsed / state.duration;
|
|
1134
1134
|
|
|
1135
|
-
//
|
|
1135
|
+
// Different moves per phase
|
|
1136
1136
|
if (t < 0.25) {
|
|
1137
|
-
//
|
|
1137
|
+
// Phase 1: left-right swing
|
|
1138
1138
|
const swingX = Math.sin(state.phase * 8) * 20;
|
|
1139
1139
|
ctx.setPos(state.originX + swingX, state.originY);
|
|
1140
1140
|
ctx.setFlip(swingX < 0);
|
|
1141
1141
|
} else if (t < 0.5) {
|
|
1142
|
-
//
|
|
1142
|
+
// Phase 2: up-down bounce
|
|
1143
1143
|
const bounceY = Math.abs(Math.sin(state.phase * 6)) * -30;
|
|
1144
1144
|
ctx.setPos(state.originX, state.originY + bounceY);
|
|
1145
1145
|
} else if (t < 0.75) {
|
|
1146
|
-
//
|
|
1146
|
+
// Phase 3: small circle
|
|
1147
1147
|
const angle = state.phase * 10;
|
|
1148
1148
|
ctx.setPos(
|
|
1149
1149
|
state.originX + Math.cos(angle) * 15,
|
|
@@ -1151,7 +1151,7 @@ const PetEngine = (() => {
|
|
|
1151
1151
|
);
|
|
1152
1152
|
ctx.setFlip(Math.cos(angle) < 0);
|
|
1153
1153
|
} else {
|
|
1154
|
-
//
|
|
1154
|
+
// Phase 4: fast horizontal shake (finish)
|
|
1155
1155
|
const shake = Math.sin(state.phase * 20) * 6 * (1 - t);
|
|
1156
1156
|
ctx.setPos(state.originX + shake, state.originY);
|
|
1157
1157
|
}
|
|
@@ -1163,15 +1163,15 @@ const PetEngine = (() => {
|
|
|
1163
1163
|
});
|
|
1164
1164
|
|
|
1165
1165
|
// ===================================
|
|
1166
|
-
//
|
|
1166
|
+
// Main loop
|
|
1167
1167
|
// ===================================
|
|
1168
1168
|
|
|
1169
1169
|
let frameId = null;
|
|
1170
1170
|
|
|
1171
1171
|
/**
|
|
1172
|
-
*
|
|
1172
|
+
* Start engine: begin requestAnimationFrame loop
|
|
1173
1173
|
*/
|
|
1174
|
-
let lastLoopTimestamp = 0; //
|
|
1174
|
+
let lastLoopTimestamp = 0; // Previous loop timestamp (for deltaTime calculation)
|
|
1175
1175
|
|
|
1176
1176
|
function start() {
|
|
1177
1177
|
if (running) return;
|
|
@@ -1182,16 +1182,16 @@ const PetEngine = (() => {
|
|
|
1182
1182
|
function loop(timestamp) {
|
|
1183
1183
|
if (!running) return;
|
|
1184
1184
|
|
|
1185
|
-
//
|
|
1185
|
+
// Calculate deltaTime for custom movement
|
|
1186
1186
|
const deltaTime = timestamp - lastLoopTimestamp;
|
|
1187
1187
|
lastLoopTimestamp = timestamp;
|
|
1188
1188
|
|
|
1189
1189
|
const state = StateMachine.update();
|
|
1190
1190
|
|
|
1191
|
-
//
|
|
1191
|
+
// Update animation first -> set animFrameChanged flag
|
|
1192
1192
|
updateAnimation(state, timestamp);
|
|
1193
1193
|
|
|
1194
|
-
//
|
|
1194
|
+
// Run dedicated update if custom movement is active
|
|
1195
1195
|
if (activeCustomMovement && state === 'custom') {
|
|
1196
1196
|
updateCustomMovement(deltaTime);
|
|
1197
1197
|
clampPosition();
|
|
@@ -1200,7 +1200,7 @@ const PetEngine = (() => {
|
|
|
1200
1200
|
moveForState(state);
|
|
1201
1201
|
}
|
|
1202
1202
|
|
|
1203
|
-
//
|
|
1203
|
+
// Reset frame transition flag (wait until next frame)
|
|
1204
1204
|
animFrameChanged = false;
|
|
1205
1205
|
frameId = requestAnimationFrame(loop);
|
|
1206
1206
|
}
|
|
@@ -1208,23 +1208,23 @@ const PetEngine = (() => {
|
|
|
1208
1208
|
}
|
|
1209
1209
|
|
|
1210
1210
|
/**
|
|
1211
|
-
*
|
|
1211
|
+
* Stop engine
|
|
1212
1212
|
*/
|
|
1213
1213
|
function stop() {
|
|
1214
1214
|
running = false;
|
|
1215
1215
|
if (frameId) cancelAnimationFrame(frameId);
|
|
1216
1216
|
}
|
|
1217
1217
|
|
|
1218
|
-
// ---
|
|
1218
|
+
// --- Public API ---
|
|
1219
1219
|
return {
|
|
1220
1220
|
init, start, stop,
|
|
1221
1221
|
getPosition, setPosition, setEdge, setDirection,
|
|
1222
1222
|
snapToNearestEdge, setSpeedMultiplier,
|
|
1223
1223
|
moveForState, updateAnimation,
|
|
1224
|
-
//
|
|
1224
|
+
// Physics-based movement
|
|
1225
1225
|
jumpTo, startRappel, releaseThread, moveToCenter,
|
|
1226
1226
|
setSurfaces, getThread, startFalling,
|
|
1227
|
-
//
|
|
1227
|
+
// Custom movement pattern system
|
|
1228
1228
|
registerMovement, unregisterMovement,
|
|
1229
1229
|
executeCustomMovement, stopCustomMovement,
|
|
1230
1230
|
getRegisteredMovements,
|