castle-web-cli 0.4.231 → 0.4.232
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/serverBundle.js +5 -3
- package/kits/hub-game/CLAUDE.md +26 -4
- package/kits/hub-game/code/client/avatar.js +39 -0
- package/kits/hub-game/code/client/chat.js +12 -0
- package/kits/hub-game/code/client/chrome.js +194 -4
- package/kits/hub-game/code/client/grab.js +31 -7
- package/kits/hub-game/code/client/hats.js +26 -0
- package/kits/hub-game/code/client/hookLayer.js +30 -3
- package/kits/hub-game/code/client/hudClear.js +126 -0
- package/kits/hub-game/code/client/state.js +22 -0
- package/kits/hub-game/code/client/tiers.js +31 -6
- package/kits/hub-game/code/client/together.js +66 -1
- package/kits/hub-game/code/client/wear.js +96 -14
- package/kits/hub-game/code/server/actors.js +5 -0
- package/kits/hub-game/code/server/grab.js +296 -11
- package/kits/hub-game/code/server/hub.js +63 -2
- package/kits/hub-game/code/server/manifest.js +48 -3
- package/kits/hub-game/code/server/party.js +11 -0
- package/kits/hub-game/code/server/say.js +8 -2
- package/kits/hub-game/code/server/wear.js +116 -11
- package/kits/hub-game/code/systems/hub.js +253 -5
- package/kits/hub-game/docs/contract.md +204 -8
- package/kits/hub-game/package.json +1 -1
- package/kits/hub-game/scripts/test-chat.mjs +32 -0
- package/kits/hub-game/scripts/test-grab-client.mjs +186 -3
- package/kits/hub-game/scripts/test-grab.mjs +857 -3
- package/kits/hub-game/scripts/test-hats.mjs +229 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +513 -0
- package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
- package/kits/hub-game/scripts/test-manifest.mjs +75 -0
- package/kits/hub-game/scripts/test-party.mjs +22 -1
- package/kits/hub-game/scripts/test-say.mjs +59 -2
- package/kits/hub-game/scripts/test-tiers.mjs +190 -2
- package/kits/hub-game/scripts/test-try-on.mjs +940 -0
- package/kits/physics-2d/castle.json +1 -1
- package/kits/physics-2d/engine/scene.js +35 -0
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +44 -0
- package/kits/physics-3d/CLAUDE.md +252 -0
- package/kits/physics-3d/behaviors/Player.jsx +363 -2
- package/kits/physics-3d/castle.json +2 -2
- package/kits/physics-3d/docs/pxmodel-format.md +4 -0
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -0
- package/kits/physics-3d/engine3d/optimize.js +49 -0
- package/kits/physics-3d/engine3d/orbitinput.js +277 -0
- package/kits/physics-3d/engine3d/world3d.js +81 -25
- package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
- package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
- package/kits/physics-3d/scripts/test-camera-follow.mjs +12 -2
- package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
- package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
- package/kits/physics-3d/scripts/test-static-actors.mjs +6 -1
- package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
- package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
- package/kits/physics-3d/systems/physics3d.js +91 -6
- package/package.json +1 -1
|
@@ -0,0 +1,2105 @@
|
|
|
1
|
+
// The camera the player controls: `"engine": { "camera": { "follow": "rigid",
|
|
2
|
+
// "control": "orbit" } }` (behaviors/Player.jsx from `updateOrbiting` on,
|
|
3
|
+
// engine3d/orbitinput.js, `castRayFixed` in systems/physics3d.js).
|
|
4
|
+
//
|
|
5
|
+
// - Without the key, and with `"follow": "rigid"` alone, the Player is the
|
|
6
|
+
// Player it was: every drawn frame of a scripted run (keys, stick, taps, a
|
|
7
|
+
// teleport, frames before physics is ready, uneven frame lengths) has the
|
|
8
|
+
// camera, the player, what is asked of `scene.physics3d`, the hud and
|
|
9
|
+
// `scene.persistent` of `Player.before-orbit.mjs`, number for number.
|
|
10
|
+
// That file is behaviors/Player.jsx as it was on the commit before the
|
|
11
|
+
// key, copied whole.
|
|
12
|
+
// - The same through the runtime itself: physics-2d's SceneRuntime (loaded
|
|
13
|
+
// by itself, as its test-skip-unchanged.mjs loads it), Scene3DPlayer's
|
|
14
|
+
// own `attachInput` (its source, cut out of the .jsx and run against a
|
|
15
|
+
// stand-in canvas) and the physics system's `reset` on each load. There a
|
|
16
|
+
// keyless runtime never makes the orbit input; a scene without the key
|
|
17
|
+
// loaded after one with it, with fingers still down, is the Player before
|
|
18
|
+
// the key from its first frame; fingers held across any load are let go
|
|
19
|
+
// of; a Player turned off and on, and a locked look input, replay no look.
|
|
20
|
+
// - With it: walking is camera-relative and the character turns to face its
|
|
21
|
+
// walk, or the way something else turned the camera; the camera turns,
|
|
22
|
+
// tilts and zooms as asked, within its limits; it is held in front of
|
|
23
|
+
// what is in the way at once and let out slowly, and never goes through
|
|
24
|
+
// the player; its tilt and distance are never in `scene.persistent`; a
|
|
25
|
+
// teleport snaps it and a camera something else placed is eased on from
|
|
26
|
+
// where it was left.
|
|
27
|
+
// - The pointers' roles (stick, look, pinch, tap), the wheel, the lock, and
|
|
28
|
+
// the listeners that are only there under the key.
|
|
29
|
+
// - `castRayFixed` on rapier itself, when it is installed here.
|
|
30
|
+
//
|
|
31
|
+
// test-static-actors.mjs runs it (`npm run check`).
|
|
32
|
+
// node scripts/test-camera-orbit.mjs
|
|
33
|
+
import assert from 'node:assert/strict';
|
|
34
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
35
|
+
import { register } from 'node:module';
|
|
36
|
+
import { dirname, join } from 'node:path';
|
|
37
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
38
|
+
import * as THREE from 'three';
|
|
39
|
+
|
|
40
|
+
const stub = `
|
|
41
|
+
const any = () => new Proxy(function () {}, {
|
|
42
|
+
get: (t, k) => (k === 'then' ? undefined : k === Symbol.toPrimitive ? () => 0 : any()),
|
|
43
|
+
apply: () => any(),
|
|
44
|
+
construct: () => any(),
|
|
45
|
+
});
|
|
46
|
+
export const init = async () => {};
|
|
47
|
+
export const World = any();
|
|
48
|
+
export const EventQueue = any();
|
|
49
|
+
export const RigidBodyDesc = any();
|
|
50
|
+
export const ColliderDesc = any();
|
|
51
|
+
export const RigidBodyType = { Dynamic: 0, Fixed: 1, KinematicPositionBased: 2, KinematicVelocityBased: 3 };
|
|
52
|
+
export const ActiveEvents = any();
|
|
53
|
+
export const ActiveCollisionTypes = any();
|
|
54
|
+
export const Ray = any();
|
|
55
|
+
export const Quaternion = any();
|
|
56
|
+
export const Vector3 = any();
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
// The nearest installed rapier, from this kit's directory upwards.
|
|
60
|
+
function findRapier() {
|
|
61
|
+
let dir = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
62
|
+
for (;;) {
|
|
63
|
+
const file = join(dir, 'node_modules', '@dimforge', 'rapier3d-compat', 'rapier.mjs');
|
|
64
|
+
if (existsSync(file)) return pathToFileURL(file).href;
|
|
65
|
+
const up = dirname(dir);
|
|
66
|
+
if (up === dir) return null;
|
|
67
|
+
dir = up;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const realRapier = findRapier();
|
|
71
|
+
|
|
72
|
+
// What physics-2d's runtime imports that needs the bundler, stood in for as
|
|
73
|
+
// its test-skip-unchanged.mjs stands it in.
|
|
74
|
+
const engineStubs = {
|
|
75
|
+
'castle-web-sdk': `export const resolveDeckFile = (name) => String(name).replace(/^\\.\\//, '');
|
|
76
|
+
export const deckPrefixOf = () => ''; export const IMPORTS_DIR = 'imports'; export const IMPORTS_PREFIX = 'imports/';`,
|
|
77
|
+
'./files': `export const initialFiles = {}; export const parseJsonFile = (path, text) => ({ value: JSON.parse(text) });
|
|
78
|
+
export const formatJson = (value) => JSON.stringify(value);`,
|
|
79
|
+
'./blueprint': `export const actorUsesBlueprint = () => false; export const getBlueprintTemplate = () => null;
|
|
80
|
+
export const mergeComponents = (template, own) => ({ ...(template ?? {}), ...(own ?? {}) });`,
|
|
81
|
+
'./collider': `export const getColliderRect = () => null; export const intersects = () => false; export const spriteIsEmpty = () => false;`,
|
|
82
|
+
'./systemRegistry': `export const systemInstallers = [];`,
|
|
83
|
+
'./fonts': `export const deckFontFamily = 'sans-serif';`,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const hooks = `
|
|
87
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
88
|
+
import { fileURLToPath } from 'node:url';
|
|
89
|
+
const real = ${JSON.stringify(realRapier)};
|
|
90
|
+
const engineStubs = ${JSON.stringify(engineStubs)};
|
|
91
|
+
export async function resolve(specifier, context, next) {
|
|
92
|
+
const parent = context.parentURL ?? '';
|
|
93
|
+
if (parent.includes('/physics-2d/engine/') && engineStubs[specifier]) {
|
|
94
|
+
return { url: 'data:text/javascript,' + encodeURIComponent(engineStubs[specifier]) + '#orbit-' + encodeURIComponent(specifier), shortCircuit: true };
|
|
95
|
+
}
|
|
96
|
+
if (specifier === '@dimforge/rapier3d-compat') {
|
|
97
|
+
if (parent.endsWith('?orbit-real')) return { url: real, shortCircuit: true };
|
|
98
|
+
return { url: 'data:text/javascript,' + encodeURIComponent(${JSON.stringify(stub)}), shortCircuit: true };
|
|
99
|
+
}
|
|
100
|
+
// This file's own copy of the physics system on rapier itself; settled
|
|
101
|
+
// here so no other test's hooks rewrite the address.
|
|
102
|
+
if (specifier.endsWith('?orbit-real')) return { url: new URL(specifier, parent).href, shortCircuit: true };
|
|
103
|
+
if (specifier.startsWith('@imports/castle.physics-2d/')) {
|
|
104
|
+
specifier = parent.replace(/\\/kits\\/.*/, '/kits/physics-2d/') + specifier.slice(27);
|
|
105
|
+
}
|
|
106
|
+
if ((specifier.startsWith('.') || specifier.startsWith('file:')) && parent.includes('/kits/') && !/\\.(m?js|jsx|json)(\\?.*)?$/.test(specifier)) {
|
|
107
|
+
const base = new URL(specifier, parent).href;
|
|
108
|
+
for (const ext of ['.js', '.jsx']) if (existsSync(fileURLToPath(base + ext))) return next(base + ext, context);
|
|
109
|
+
}
|
|
110
|
+
return next(specifier, context);
|
|
111
|
+
}
|
|
112
|
+
export async function load(url, context, next) {
|
|
113
|
+
if (url.endsWith('/behaviors/Player.jsx')) {
|
|
114
|
+
return { format: 'module', source: readFileSync(fileURLToPath(url), 'utf8'), shortCircuit: true };
|
|
115
|
+
}
|
|
116
|
+
return next(url, context);
|
|
117
|
+
}`;
|
|
118
|
+
register(`data:text/javascript,${encodeURIComponent(hooks)}`);
|
|
119
|
+
const { Player } = await import('../behaviors/Player.jsx');
|
|
120
|
+
const { Player: PlayerBefore } = await import('./Player.before-orbit.mjs');
|
|
121
|
+
const { placeFollowCamera, Physics3DSystem } = await import('../systems/physics3d.js');
|
|
122
|
+
const { makeLookGestures, attachOrbitInput, stickFrom, LOOK_RAD_PER_PX, WHEEL_ZOOM_PER_NOTCH } = await import('../engine3d/orbitinput.js');
|
|
123
|
+
const { SceneRuntime } = await import('../../physics-2d/engine/scene.js');
|
|
124
|
+
|
|
125
|
+
const DEG = Math.PI / 180;
|
|
126
|
+
const FIXED = 1 / 60;
|
|
127
|
+
const PROPS = { ...Player.defaultProps };
|
|
128
|
+
const FLOOR = 0.7;
|
|
129
|
+
const TUNING = { stickRadius: 56, tapSlop: 12, tapMs: 300 };
|
|
130
|
+
// The Player's own numbers, restated: a change there is a change here.
|
|
131
|
+
const LOOK_HEIGHT = 0.8;
|
|
132
|
+
const RANGE0 = Math.hypot(PROPS.camDistance, PROPS.camHeight - LOOK_HEIGHT);
|
|
133
|
+
const PITCH0 = Math.atan2(PROPS.camHeight - LOOK_HEIGHT, PROPS.camDistance);
|
|
134
|
+
|
|
135
|
+
// A world of one capsule on flat ground, stepped as systems/physics3d.js
|
|
136
|
+
// steps (test-camera-follow.mjs's, with the scene's latched keys settable
|
|
137
|
+
// and a scene load). `mode` is 'off', 'rigid' or 'orbit'.
|
|
138
|
+
function makeWorld(mode, PlayerClass = Player) {
|
|
139
|
+
const camera = new THREE.PerspectiveCamera(50, 1280 / 800, 0.1, 1000);
|
|
140
|
+
camera.position.set(8, 8, 8);
|
|
141
|
+
camera.lookAt(0, 0, 0);
|
|
142
|
+
const body = { pos: { x: 0, y: 1, z: 0 }, vel: { x: 0, y: 0, z: 0 }, prev: null, acc: 0, force: 0 };
|
|
143
|
+
const calls = [];
|
|
144
|
+
const world = { camera, body, calls, frames: [], ready: true, wall: null, casts: 0 };
|
|
145
|
+
const logged = (name, fn) => (...args) => {
|
|
146
|
+
calls.push([name, ...args.slice(1).map((a) => (a && typeof a === 'object' ? { ...a } : a))]);
|
|
147
|
+
return fn(...args);
|
|
148
|
+
};
|
|
149
|
+
const phys = {
|
|
150
|
+
ready: () => world.ready,
|
|
151
|
+
mass: () => 1,
|
|
152
|
+
castRay: logged('castRay', (from, dir, max) => (from.y <= max ? { distance: from.y } : null)),
|
|
153
|
+
getVelocity: () => ({ ...body.vel }),
|
|
154
|
+
setVelocity: logged('setVelocity', (_, v) => Object.assign(body.vel, v)),
|
|
155
|
+
applyImpulse: logged('applyImpulse', (_, i) => {
|
|
156
|
+
for (const k of ['x', 'y', 'z']) body.vel[k] += i[k];
|
|
157
|
+
}),
|
|
158
|
+
addForce: logged('addForce', (_, f) => {
|
|
159
|
+
body.force += f.y;
|
|
160
|
+
}),
|
|
161
|
+
resetForces: logged('resetForces', () => {
|
|
162
|
+
body.force = 0;
|
|
163
|
+
}),
|
|
164
|
+
// What stands between the camera and the player: `world.wall` metres
|
|
165
|
+
// from the point the camera looks at, or nothing.
|
|
166
|
+
castRayFixed: (from, dir, max) => {
|
|
167
|
+
world.casts += 1;
|
|
168
|
+
world.lastCast = { from: { ...from }, dir: { ...dir }, max };
|
|
169
|
+
return world.wall !== null && world.wall <= max ? { distance: world.wall, actorId: 'wall' } : null;
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
const stick = {
|
|
173
|
+
x: 0,
|
|
174
|
+
y: 0,
|
|
175
|
+
active: false,
|
|
176
|
+
taps: 0,
|
|
177
|
+
consumeJump() {
|
|
178
|
+
const had = stick.taps > 0;
|
|
179
|
+
stick.taps = 0;
|
|
180
|
+
return had;
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
const gestures = makeLookGestures(stick, () => (stick.taps += 1), TUNING);
|
|
184
|
+
let listens = 0;
|
|
185
|
+
let consumes = 0;
|
|
186
|
+
gestures.look.listen = () => (listens += 1);
|
|
187
|
+
const consume = gestures.look.consume;
|
|
188
|
+
gestures.look.consume = () => {
|
|
189
|
+
consumes += 1;
|
|
190
|
+
return consume();
|
|
191
|
+
};
|
|
192
|
+
world.gestures = gestures;
|
|
193
|
+
world.stick = stick;
|
|
194
|
+
world.lookCalls = () => ({ listens, consumes });
|
|
195
|
+
const scene = {
|
|
196
|
+
physics3d: phys,
|
|
197
|
+
keys: new Set(),
|
|
198
|
+
moveInput: stick,
|
|
199
|
+
lookInput: gestures.look,
|
|
200
|
+
persistent: {},
|
|
201
|
+
three: { camera },
|
|
202
|
+
time: 0,
|
|
203
|
+
actorsWith: () => [],
|
|
204
|
+
getActor: (id) => scene.actors.find((a) => a.id === id),
|
|
205
|
+
};
|
|
206
|
+
world.scene = scene;
|
|
207
|
+
// What `load` does to the runtime: new scene data, the keys latched from
|
|
208
|
+
// it, every actor's runtime rebuilt, a pending camera request dropped.
|
|
209
|
+
world.load = (next, options = {}) => {
|
|
210
|
+
world.mode = next;
|
|
211
|
+
scene.data = {};
|
|
212
|
+
scene.cameraFollowsRigidly = next !== 'off';
|
|
213
|
+
scene.cameraControl = next === 'orbit' ? 'orbit' : null;
|
|
214
|
+
scene.followCamera = null;
|
|
215
|
+
world.actor = {
|
|
216
|
+
id: 'player',
|
|
217
|
+
components: { Transform: { x: body.pos.x, y: body.pos.y, z: body.pos.z, rotationY: options.rotationY ?? 0 }, Shape: { radius: 0.35, height: 1.4 }, Player: PROPS },
|
|
218
|
+
runtime: {},
|
|
219
|
+
};
|
|
220
|
+
scene.actors = [world.actor];
|
|
221
|
+
};
|
|
222
|
+
world.load(mode);
|
|
223
|
+
const stepPhysics = (dt) => {
|
|
224
|
+
const t = world.actor.components.Transform;
|
|
225
|
+
body.acc += dt;
|
|
226
|
+
let steps = 0;
|
|
227
|
+
while (body.acc >= FIXED && steps < 4) {
|
|
228
|
+
body.prev = { ...body.pos };
|
|
229
|
+
body.vel.y += (-9.81 * 2 + body.force) * FIXED;
|
|
230
|
+
for (const k of ['x', 'y', 'z']) body.pos[k] += body.vel[k] * FIXED;
|
|
231
|
+
if (body.pos.y < FLOOR) {
|
|
232
|
+
body.pos.y = FLOOR;
|
|
233
|
+
body.vel.y = Math.max(0, body.vel.y);
|
|
234
|
+
}
|
|
235
|
+
body.acc -= FIXED;
|
|
236
|
+
steps += 1;
|
|
237
|
+
}
|
|
238
|
+
if (steps === 4) body.acc = 0;
|
|
239
|
+
body.force = 0;
|
|
240
|
+
const alpha = Math.max(0, Math.min(1, body.acc / FIXED));
|
|
241
|
+
const prev = body.prev ?? body.pos;
|
|
242
|
+
for (const k of ['x', 'y', 'z']) t[k] = prev[k] + (body.pos[k] - prev[k]) * alpha;
|
|
243
|
+
};
|
|
244
|
+
world.frame = (dt = FIXED, keys = []) => {
|
|
245
|
+
const t = world.actor.components.Transform;
|
|
246
|
+
scene.keys = new Set(keys);
|
|
247
|
+
scene.time += dt;
|
|
248
|
+
new PlayerClass(PROPS).update(world.actor, scene, dt);
|
|
249
|
+
if (world.ready) stepPhysics(dt);
|
|
250
|
+
else body.force = 0;
|
|
251
|
+
placeFollowCamera(scene);
|
|
252
|
+
const p = camera.position;
|
|
253
|
+
world.frames.push({
|
|
254
|
+
cam: { x: p.x, y: p.y, z: p.z },
|
|
255
|
+
q: camera.quaternion.toArray(),
|
|
256
|
+
player: { x: t.x, y: t.y, z: t.z, rotationY: t.rotationY },
|
|
257
|
+
vel: { ...body.vel },
|
|
258
|
+
persistent: JSON.stringify(scene.persistent),
|
|
259
|
+
hud: JSON.stringify(scene.hud ?? null),
|
|
260
|
+
calls: JSON.stringify(calls.splice(0)),
|
|
261
|
+
});
|
|
262
|
+
return world.frames.at(-1);
|
|
263
|
+
};
|
|
264
|
+
world.run = (n, keys = [], dt = FIXED) => {
|
|
265
|
+
for (let i = 0; i < n; i += 1) world.frame(typeof dt === 'function' ? dt(i) : dt, keys);
|
|
266
|
+
return world.frames.at(-1);
|
|
267
|
+
};
|
|
268
|
+
world.teleport = (dx, dz) => {
|
|
269
|
+
body.pos.x += dx;
|
|
270
|
+
body.pos.z += dz;
|
|
271
|
+
body.prev = null;
|
|
272
|
+
};
|
|
273
|
+
// Where the orbit camera rests for a yaw, tilt and distance.
|
|
274
|
+
world.rest = (yaw, pitch, range) => {
|
|
275
|
+
const t = world.actor.components.Transform;
|
|
276
|
+
return {
|
|
277
|
+
x: t.x - range * Math.cos(pitch) * Math.sin(yaw),
|
|
278
|
+
y: t.y + LOOK_HEIGHT + range * Math.sin(pitch),
|
|
279
|
+
z: t.z + range * Math.cos(pitch) * Math.cos(yaw),
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
world.off = (want) => Math.hypot(camera.position.x - want.x, camera.position.y - want.y, camera.position.z - want.z);
|
|
283
|
+
return world;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const UNEVEN = (i) => (i % 2 === 0 ? 1 / 60 : 1 / 30);
|
|
287
|
+
|
|
288
|
+
// A run that uses everything the Player reads: keys, the stick, taps, the
|
|
289
|
+
// frames before physics is ready, a teleport, uneven frames; and has a
|
|
290
|
+
// finger on the camera and a wheel turning throughout, which a scene
|
|
291
|
+
// without the key must not notice.
|
|
292
|
+
function scripted(world) {
|
|
293
|
+
let pointer = 100;
|
|
294
|
+
const step = (i) => {
|
|
295
|
+
const keys = [];
|
|
296
|
+
const phase = Math.floor(i / 40) % 9;
|
|
297
|
+
if (phase === 1 || phase === 2 || phase === 6) keys.push('KeyW');
|
|
298
|
+
if (phase === 2) keys.push('KeyD');
|
|
299
|
+
if (phase === 3) keys.push('KeyA');
|
|
300
|
+
if (phase === 4) keys.push('ArrowLeft', 'ArrowUp');
|
|
301
|
+
if (phase === 7) keys.push('KeyS', 'ArrowRight');
|
|
302
|
+
if (i % 53 === 7) keys.push('Space');
|
|
303
|
+
world.stick.active = phase === 5 || phase === 8;
|
|
304
|
+
world.stick.x = world.stick.active ? Math.sin(i / 9) * 0.9 : 0;
|
|
305
|
+
world.stick.y = world.stick.active ? -Math.cos(i / 13) : 0;
|
|
306
|
+
if (i % 47 === 11) world.stick.taps += 1;
|
|
307
|
+
if (i === 200) world.teleport(40, -25);
|
|
308
|
+
world.ready = i >= 5;
|
|
309
|
+
// The camera's input, live the whole time.
|
|
310
|
+
if (i % 30 === 0) {
|
|
311
|
+
world.gestures.up(pointer, i * 16, false);
|
|
312
|
+
pointer += 1;
|
|
313
|
+
world.gestures.down(pointer, { x: 900, y: 300, time: i * 16, left: false });
|
|
314
|
+
}
|
|
315
|
+
world.gestures.move(pointer, 900 + (i % 30) * 9, 300 + (i % 30) * 5, i * 16);
|
|
316
|
+
if (i % 17 === 0) world.gestures.wheel(i % 2 ? 100 : -100, 0, i * 16);
|
|
317
|
+
world.frame(UNEVEN(i), keys);
|
|
318
|
+
};
|
|
319
|
+
return step;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const same = (a, b, what) => {
|
|
323
|
+
assert.equal(a.frames.length, b.frames.length, what);
|
|
324
|
+
for (let i = 0; i < a.frames.length; i += 1) assert.deepEqual(a.frames[i], b.frames[i], `${what}: frame ${i}`);
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
function unkeyedIsUnchanged() {
|
|
328
|
+
let checks = 0;
|
|
329
|
+
for (const mode of ['off', 'rigid']) {
|
|
330
|
+
const now = makeWorld(mode, Player);
|
|
331
|
+
const before = makeWorld(mode, PlayerBefore);
|
|
332
|
+
const stepNow = scripted(now);
|
|
333
|
+
const stepBefore = scripted(before);
|
|
334
|
+
for (let i = 0; i < 720; i += 1) {
|
|
335
|
+
stepNow(i);
|
|
336
|
+
stepBefore(i);
|
|
337
|
+
}
|
|
338
|
+
same(now, before, `${mode}: the Player is the one before the key`);
|
|
339
|
+
assert.ok(now.frames.some((f) => f.calls.includes(mode === 'rigid' ? 'addForce' : 'applyImpulse')), 'and the run did something');
|
|
340
|
+
assert.equal(now.casts, 0, `${mode}: nothing is asked of castRayFixed`);
|
|
341
|
+
assert.deepEqual(now.lookCalls(), { listens: 0, consumes: 0 }, `${mode}: the camera's input is not touched`);
|
|
342
|
+
assert.equal(now.scene.cameraOrbit, undefined, `${mode}: nothing is kept on the runtime`);
|
|
343
|
+
assert.equal(now.actor.runtime.orbit, undefined, `${mode}: nor on the actor`);
|
|
344
|
+
checks += 1;
|
|
345
|
+
}
|
|
346
|
+
return checks;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// The key on in one scene, off in the next, on again: the scene without it
|
|
350
|
+
// is the Player before the key from its first frame, whatever the keyed
|
|
351
|
+
// scene left behind, and the keyed scene after it starts clean.
|
|
352
|
+
// A stand-in canvas: listeners by type, events fired by hand.
|
|
353
|
+
function makeCanvas() {
|
|
354
|
+
const on = [];
|
|
355
|
+
return {
|
|
356
|
+
on,
|
|
357
|
+
focused: 0,
|
|
358
|
+
captured: [],
|
|
359
|
+
addEventListener: (type, fn) => on.push([type, fn]),
|
|
360
|
+
removeEventListener: (type, fn) => {
|
|
361
|
+
const i = on.findIndex(([t, f]) => t === type && f === fn);
|
|
362
|
+
if (i >= 0) on.splice(i, 1);
|
|
363
|
+
},
|
|
364
|
+
focus() {
|
|
365
|
+
this.focused += 1;
|
|
366
|
+
},
|
|
367
|
+
setPointerCapture(id) {
|
|
368
|
+
this.captured.push(id);
|
|
369
|
+
},
|
|
370
|
+
getBoundingClientRect: () => ({ left: 100, top: 0, width: 800, height: 600 }),
|
|
371
|
+
fire(type, event = {}) {
|
|
372
|
+
let prevented = false;
|
|
373
|
+
const e = { type, preventDefault: () => (prevented = true), ...event };
|
|
374
|
+
for (const [t, fn] of [...on]) if (t === type) fn(e);
|
|
375
|
+
return prevented;
|
|
376
|
+
},
|
|
377
|
+
types() {
|
|
378
|
+
return on.map(([t]) => t).sort();
|
|
379
|
+
},
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Scene3DPlayer's `attachInput`, its own source: cut out of the .jsx (it
|
|
384
|
+
// holds no JSX) and run with a stand-in window, document and clock. The
|
|
385
|
+
// one before the key is cut out of Scene3DPlayer.before-orbit.jsx, that
|
|
386
|
+
// file as it was on the commit before the key, copied whole.
|
|
387
|
+
const cutAttachInput = (file) => {
|
|
388
|
+
const source = readFileSync(new URL(file, import.meta.url), 'utf8');
|
|
389
|
+
const cut = source.slice(source.indexOf('function attachInput('), source.indexOf('\nfunction attachResize('));
|
|
390
|
+
assert.ok(cut.length > 1000 && cut.trimEnd().endsWith('\n}') && !/<[A-Za-z]/.test(cut), `attachInput is cut out of ${file} whole, with no JSX in it`);
|
|
391
|
+
return cut;
|
|
392
|
+
};
|
|
393
|
+
const ATTACH_NOW = cutAttachInput('../engine3d/Scene3DPlayer.jsx');
|
|
394
|
+
const ATTACH_BEFORE = cutAttachInput('./Scene3DPlayer.before-orbit.jsx');
|
|
395
|
+
assert.ok(!ATTACH_BEFORE.includes('orbit') && ATTACH_NOW.includes('orbit'), 'the two are the two');
|
|
396
|
+
// A stand-in window whose listeners are kept, as the canvas's are.
|
|
397
|
+
function makeWindow() {
|
|
398
|
+
const window = makeCanvas();
|
|
399
|
+
window.parent = window;
|
|
400
|
+
return window;
|
|
401
|
+
}
|
|
402
|
+
function makeAttachInput(clock, source = ATTACH_NOW, window = makeWindow()) {
|
|
403
|
+
const document = { hasFocus: () => true };
|
|
404
|
+
return new Function('attachOrbitInput', 'window', 'document', 'performance', `${source}\nreturn attachInput;`)(attachOrbitInput, window, document, clock);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const sceneData = (mode, at) => ({
|
|
408
|
+
...(mode === 'off' ? {} : { engine: { camera: mode === 'orbit' ? { follow: 'rigid', control: 'orbit' } : { follow: 'rigid' } } }),
|
|
409
|
+
actors: [
|
|
410
|
+
{
|
|
411
|
+
id: 'player',
|
|
412
|
+
components: {
|
|
413
|
+
Transform: { x: at.x, y: at.y, z: at.z, rotationX: 0, rotationY: 0, rotationZ: 0 },
|
|
414
|
+
Shape: { radius: 0.35, height: 1.4 },
|
|
415
|
+
Player: { ...PROPS },
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
],
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
// The runtime as Scene3DPlayer makes it, with the real behavior dispatch,
|
|
422
|
+
// the real pointer handlers and the physics system's real `reset` on every
|
|
423
|
+
// load; the physics itself is makeWorld's capsule on flat ground, placing
|
|
424
|
+
// the follow camera last as afterBehaviors does. `PlayerClass` is this
|
|
425
|
+
// kit's Player or the one before the key.
|
|
426
|
+
function makeRuntime(mode, PlayerClass = Player, attachSource = PlayerClass === Player ? ATTACH_NOW : ATTACH_BEFORE) {
|
|
427
|
+
const camera = new THREE.PerspectiveCamera(50, 1280 / 800, 0.1, 1000);
|
|
428
|
+
camera.position.set(8, 8, 8);
|
|
429
|
+
camera.lookAt(0, 0, 0);
|
|
430
|
+
const body = { pos: { x: 0, y: 1, z: 0 }, vel: { x: 0, y: 0, z: 0 }, prev: null, acc: 0, force: 0 };
|
|
431
|
+
const calls = [];
|
|
432
|
+
const clock = { t: 1000, now: () => clock.t };
|
|
433
|
+
const logged = (name, fn) => (...args) => {
|
|
434
|
+
calls.push([name, ...args.slice(1).map((a) => (a && typeof a === 'object' ? { ...a } : a))]);
|
|
435
|
+
return fn(...args);
|
|
436
|
+
};
|
|
437
|
+
const harness = { camera, body, calls, clock, frames: [], casts: 0 };
|
|
438
|
+
const runtime = new SceneRuntime(sceneData(mode, body.pos), [PlayerClass], {}, {});
|
|
439
|
+
runtime.three = { camera };
|
|
440
|
+
runtime.persistent = {};
|
|
441
|
+
runtime.physics3d = {
|
|
442
|
+
ready: () => true,
|
|
443
|
+
mass: () => 1,
|
|
444
|
+
castRay: logged('castRay', (from, dir, max) => (from.y <= max ? { distance: from.y } : null)),
|
|
445
|
+
getVelocity: () => ({ ...body.vel }),
|
|
446
|
+
setVelocity: logged('setVelocity', (_, v) => Object.assign(body.vel, v)),
|
|
447
|
+
applyImpulse: logged('applyImpulse', (_, i) => {
|
|
448
|
+
for (const k of ['x', 'y', 'z']) body.vel[k] += i[k];
|
|
449
|
+
}),
|
|
450
|
+
addForce: logged('addForce', (_, f) => {
|
|
451
|
+
body.force += f.y;
|
|
452
|
+
}),
|
|
453
|
+
resetForces: logged('resetForces', () => {
|
|
454
|
+
body.force = 0;
|
|
455
|
+
}),
|
|
456
|
+
castRayFixed: () => {
|
|
457
|
+
harness.casts += 1;
|
|
458
|
+
return null;
|
|
459
|
+
},
|
|
460
|
+
};
|
|
461
|
+
const physics = new Physics3DSystem();
|
|
462
|
+
runtime.registerSystem({
|
|
463
|
+
reset: (rt) => physics.reset(rt),
|
|
464
|
+
afterBehaviors(scene, dt) {
|
|
465
|
+
const t = scene.getActor('player').components.Transform;
|
|
466
|
+
body.acc += dt;
|
|
467
|
+
let steps = 0;
|
|
468
|
+
while (body.acc >= FIXED && steps < 4) {
|
|
469
|
+
body.prev = { ...body.pos };
|
|
470
|
+
body.vel.y += (-9.81 * 2 + body.force) * FIXED;
|
|
471
|
+
for (const k of ['x', 'y', 'z']) body.pos[k] += body.vel[k] * FIXED;
|
|
472
|
+
if (body.pos.y < FLOOR) {
|
|
473
|
+
body.pos.y = FLOOR;
|
|
474
|
+
body.vel.y = Math.max(0, body.vel.y);
|
|
475
|
+
}
|
|
476
|
+
body.acc -= FIXED;
|
|
477
|
+
steps += 1;
|
|
478
|
+
}
|
|
479
|
+
if (steps === 4) body.acc = 0;
|
|
480
|
+
body.force = 0;
|
|
481
|
+
const alpha = Math.max(0, Math.min(1, body.acc / FIXED));
|
|
482
|
+
const prev = body.prev ?? body.pos;
|
|
483
|
+
for (const k of ['x', 'y', 'z']) t[k] = prev[k] + (body.pos[k] - prev[k]) * alpha;
|
|
484
|
+
// Where collision callbacks run, before the camera is placed.
|
|
485
|
+
harness.callback?.(scene);
|
|
486
|
+
placeFollowCamera(scene);
|
|
487
|
+
},
|
|
488
|
+
});
|
|
489
|
+
const canvas = makeCanvas();
|
|
490
|
+
harness.canvas = canvas;
|
|
491
|
+
harness.runtime = runtime;
|
|
492
|
+
harness.detach = makeAttachInput(clock, attachSource)(canvas, runtime, { debugLines: { visible: false } });
|
|
493
|
+
harness.player = () => runtime.getActor('player');
|
|
494
|
+
harness.stick = () => ({ x: runtime.moveInput.x, y: runtime.moveInput.y, active: runtime.moveInput.active });
|
|
495
|
+
// A scene load, as the play loop's loadFromFile ends in `load`: the player
|
|
496
|
+
// where the body is.
|
|
497
|
+
harness.load = (next) => runtime.load(sceneData(next, body.pos));
|
|
498
|
+
harness.frame = (dt = FIXED, keys = []) => {
|
|
499
|
+
clock.t += dt * 1000;
|
|
500
|
+
runtime.keys.clear();
|
|
501
|
+
for (const key of keys) runtime.keys.add(key);
|
|
502
|
+
runtime.update(dt);
|
|
503
|
+
const t = harness.player().components.Transform;
|
|
504
|
+
const p = camera.position;
|
|
505
|
+
harness.frames.push({
|
|
506
|
+
cam: { x: p.x, y: p.y, z: p.z },
|
|
507
|
+
q: camera.quaternion.toArray(),
|
|
508
|
+
player: { x: t.x, y: t.y, z: t.z, rotationY: t.rotationY },
|
|
509
|
+
vel: { ...body.vel },
|
|
510
|
+
stick: harness.stick(),
|
|
511
|
+
persistent: JSON.stringify(runtime.persistent),
|
|
512
|
+
hud: JSON.stringify(runtime.hud ?? null),
|
|
513
|
+
calls: JSON.stringify(calls.splice(0)),
|
|
514
|
+
});
|
|
515
|
+
return harness.frames.at(-1);
|
|
516
|
+
};
|
|
517
|
+
harness.run = (n, keys = [], dt = FIXED) => {
|
|
518
|
+
for (let i = 0; i < n; i += 1) harness.frame(typeof dt === 'function' ? dt(i) : dt, keys);
|
|
519
|
+
return harness.frames.at(-1);
|
|
520
|
+
};
|
|
521
|
+
const pointer = (type) => (id, x, y, more = {}) => canvas.fire(type, { pointerId: id, clientX: x, clientY: y, pointerType: 'touch', button: 0, ...more });
|
|
522
|
+
harness.down = pointer('pointerdown');
|
|
523
|
+
harness.move = pointer('pointermove');
|
|
524
|
+
harness.up = pointer('pointerup');
|
|
525
|
+
harness.wheel = (deltaY) => canvas.fire('wheel', { deltaY, deltaMode: 0 });
|
|
526
|
+
harness.jumped = () => runtime.moveInput.consumeJump();
|
|
527
|
+
// Another runtime brought to this one's state: body, camera, persistent
|
|
528
|
+
// store and scene time.
|
|
529
|
+
harness.copyTo = (other) => {
|
|
530
|
+
Object.assign(other.body, structuredClone({ pos: body.pos, vel: body.vel, prev: body.prev, acc: body.acc, force: body.force }));
|
|
531
|
+
other.runtime.persistent = structuredClone(runtime.persistent);
|
|
532
|
+
other.runtime.time = runtime.time;
|
|
533
|
+
other.clock.t = clock.t;
|
|
534
|
+
other.camera.position.copy(camera.position);
|
|
535
|
+
other.camera.quaternion.copy(camera.quaternion);
|
|
536
|
+
};
|
|
537
|
+
harness.rest = (yaw, pitch, range) => {
|
|
538
|
+
const t = harness.player().components.Transform;
|
|
539
|
+
return {
|
|
540
|
+
x: t.x - range * Math.cos(pitch) * Math.sin(yaw),
|
|
541
|
+
y: t.y + LOOK_HEIGHT + range * Math.sin(pitch),
|
|
542
|
+
z: t.z + range * Math.cos(pitch) * Math.cos(yaw),
|
|
543
|
+
};
|
|
544
|
+
};
|
|
545
|
+
harness.off = (want) => Math.hypot(camera.position.x - want.x, camera.position.y - want.y, camera.position.z - want.z);
|
|
546
|
+
return harness;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Pointer and key input for a keyless scene, through the canvas: drags (the
|
|
550
|
+
// stick), taps (jumps), a second finger, keys; fired at each harness alike.
|
|
551
|
+
function keylessInput(...harnesses) {
|
|
552
|
+
return (i) => {
|
|
553
|
+
const each = (fn) => harnesses.forEach(fn);
|
|
554
|
+
const phase = i % 60;
|
|
555
|
+
if (phase === 0) each((h) => h.down(500 + i, 300, 400));
|
|
556
|
+
if (phase > 0 && phase < 40) each((h) => h.move(500 + i - phase, 300 + phase * 3, 400 - phase * 2));
|
|
557
|
+
if (phase === 40) each((h) => h.up(500 + i - phase, 420, 320));
|
|
558
|
+
if (phase === 45) each((h) => h.down(900 + i, 700, 300));
|
|
559
|
+
if (phase === 47) each((h) => h.up(900 + i - 2, 701, 300));
|
|
560
|
+
if (phase === 10) each((h) => h.wheel(-100));
|
|
561
|
+
const keys = i % 200 > 150 ? ['KeyW', 'KeyA'] : [];
|
|
562
|
+
return keys;
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const sameFrames = (a, b, what) => {
|
|
567
|
+
assert.equal(a.length, b.length, what);
|
|
568
|
+
for (let i = 0; i < a.length; i += 1) assert.deepEqual(a[i], b[i], `${what}: frame ${i}`);
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
// Through the runtime itself. A keyless runtime: the Player and attachInput
|
|
572
|
+
// before the key, frame for frame, and no orbit input made.
|
|
573
|
+
function coldKeyless() {
|
|
574
|
+
let checks = 0;
|
|
575
|
+
for (const mode of ['off', 'rigid']) {
|
|
576
|
+
const now = makeRuntime(mode, Player);
|
|
577
|
+
const before = makeRuntime(mode, PlayerBefore);
|
|
578
|
+
const input = keylessInput(now, before);
|
|
579
|
+
for (let i = 0; i < 400; i += 1) {
|
|
580
|
+
const keys = input(i);
|
|
581
|
+
now.frame(UNEVEN(i), keys);
|
|
582
|
+
before.frame(UNEVEN(i), keys);
|
|
583
|
+
}
|
|
584
|
+
sameFrames(now.frames, before.frames, `${mode}: through the runtime, the Player before the key`);
|
|
585
|
+
assert.ok(now.frames.some((f) => f.stick.active) && now.frames.some((f) => f.calls.includes('setVelocity')), 'and the run walked and jumped');
|
|
586
|
+
assert.equal(now.runtime.lookInput, undefined, `${mode}: no look input is made`);
|
|
587
|
+
assert.equal(typeof now.runtime.orbitInput, 'function');
|
|
588
|
+
assert.deepEqual(now.canvas.types(), ['pointercancel', 'pointerdown', 'pointermove', 'pointerup'], `${mode}: the listeners the canvas always had`);
|
|
589
|
+
assert.equal(now.casts, 0);
|
|
590
|
+
now.detach();
|
|
591
|
+
assert.deepEqual(now.canvas.types(), [], 'and detaching takes them off, with no orbit input to detach');
|
|
592
|
+
now.runtime.load(sceneData(mode, now.body.pos));
|
|
593
|
+
now.runtime.dispose?.();
|
|
594
|
+
assert.equal(now.runtime.lookInput, undefined, 'a load and a dispose cope with there being none');
|
|
595
|
+
checks += 1;
|
|
596
|
+
}
|
|
597
|
+
return checks;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// Fingers down in a keyed scene, then a scene without the key: everything
|
|
601
|
+
// the orbit input held is gone at the load, with no event, and from the
|
|
602
|
+
// first frame the Player is the one before the key, whatever those fingers
|
|
603
|
+
// go on doing. Then the key again, with no event between.
|
|
604
|
+
function acrossLoads() {
|
|
605
|
+
let checks = 0;
|
|
606
|
+
for (const mode of ['off', 'rigid']) {
|
|
607
|
+
const h = makeRuntime('orbit');
|
|
608
|
+
h.run(60);
|
|
609
|
+
assert.deepEqual(h.canvas.types().filter((t) => t === 'wheel' || t === 'contextmenu'), ['contextmenu', 'wheel'], 'the keyed Player asks for the wheel');
|
|
610
|
+
// The stick held in the left half, a look in the right, a wheel turned.
|
|
611
|
+
h.down(1, 300, 400);
|
|
612
|
+
h.move(1, 300, 330);
|
|
613
|
+
h.down(2, 800, 300);
|
|
614
|
+
h.move(2, 850, 340);
|
|
615
|
+
h.wheel(-300);
|
|
616
|
+
h.run(90, [], UNEVEN);
|
|
617
|
+
const chosen = { ...h.runtime.cameraOrbit };
|
|
618
|
+
assert.notEqual(chosen.pitch, PITCH0);
|
|
619
|
+
assert.notEqual(chosen.range, RANGE0);
|
|
620
|
+
h.move(2, 900, 300);
|
|
621
|
+
h.wheel(-100);
|
|
622
|
+
assert.equal(h.stick().active, true);
|
|
623
|
+
|
|
624
|
+
h.load(mode);
|
|
625
|
+
assert.deepEqual(h.stick(), { x: 0, y: 0, active: false }, `orbit then ${mode}: the stick is let go of at the load`);
|
|
626
|
+
assert.deepEqual(h.canvas.types(), ['pointercancel', 'pointerdown', 'pointermove', 'pointerup'], 'and the wheel and context menu listeners are gone');
|
|
627
|
+
assert.deepEqual({ ...h.runtime.lookInput.consume() }, { yaw: 0, pitch: 0, zoom: 1, locked: false }, 'and nothing asked of the camera is left');
|
|
628
|
+
assert.equal(h.canvas.fire('contextmenu'), false, 'the context menu opens');
|
|
629
|
+
|
|
630
|
+
const twin = makeRuntime(mode, PlayerBefore);
|
|
631
|
+
h.copyTo(twin);
|
|
632
|
+
twin.load(mode);
|
|
633
|
+
h.frames.length = 0;
|
|
634
|
+
h.calls.length = 0;
|
|
635
|
+
const input = keylessInput(h, twin);
|
|
636
|
+
for (let i = 1; i < 300; i += 1) {
|
|
637
|
+
// The fingers from the keyed scene go on dragging, here only.
|
|
638
|
+
if (i < 100) {
|
|
639
|
+
h.move(1, 300 + i, 300);
|
|
640
|
+
h.move(2, 900 - i, 300 + i);
|
|
641
|
+
}
|
|
642
|
+
if (i === 100) h.up(1, 400, 300);
|
|
643
|
+
if (i === 101) h.up(2, 800, 401);
|
|
644
|
+
const keys = input(i);
|
|
645
|
+
h.frame(UNEVEN(i), keys);
|
|
646
|
+
twin.frame(UNEVEN(i), keys);
|
|
647
|
+
}
|
|
648
|
+
sameFrames(h.frames, twin.frames, `orbit then ${mode}: the Player before the key, from the first frame`);
|
|
649
|
+
assert.deepEqual(h.runtime.cameraOrbit, chosen, 'what the player chose is kept, untouched');
|
|
650
|
+
checks += 1;
|
|
651
|
+
|
|
652
|
+
// The key again, with no event between: the listeners are back from the
|
|
653
|
+
// first frame, the tilt and distance chosen before are kept, and the
|
|
654
|
+
// camera goes on from where the other scene left it.
|
|
655
|
+
const yaw = h.runtime.persistent.camAngle;
|
|
656
|
+
h.load('orbit');
|
|
657
|
+
const left = { ...h.camera.position };
|
|
658
|
+
h.frame();
|
|
659
|
+
assert.deepEqual(h.canvas.types().filter((t) => t === 'wheel' || t === 'contextmenu'), ['contextmenu', 'wheel'], 'the wheel is listened to again');
|
|
660
|
+
assert.equal(h.runtime.persistent.camAngle, yaw);
|
|
661
|
+
assert.deepEqual(h.runtime.cameraOrbit, chosen, 'tilt and distance as chosen');
|
|
662
|
+
const moved = Math.hypot(h.camera.position.x - left.x, h.camera.position.y - left.y, h.camera.position.z - left.z);
|
|
663
|
+
const far = h.off(h.rest(yaw, chosen.pitch, chosen.range));
|
|
664
|
+
assert.ok(far > 0.5 && moved < far, `eased on from where it was left (moved ${moved}, ${far} to go)`);
|
|
665
|
+
h.run(120);
|
|
666
|
+
assert.ok(h.off(h.rest(yaw, chosen.pitch, chosen.range)) < 1e-3, 'to the tilt and distance chosen before');
|
|
667
|
+
checks += 1;
|
|
668
|
+
}
|
|
669
|
+
// Keyed to keyed: a finger held across the load is let go of too, and its
|
|
670
|
+
// lift is no tap.
|
|
671
|
+
{
|
|
672
|
+
const h = makeRuntime('orbit');
|
|
673
|
+
h.run(30);
|
|
674
|
+
h.down(1, 300, 400);
|
|
675
|
+
h.move(1, 300, 330);
|
|
676
|
+
h.down(2, 800, 300);
|
|
677
|
+
h.frame();
|
|
678
|
+
assert.equal(h.stick().active, true);
|
|
679
|
+
h.load('orbit');
|
|
680
|
+
assert.deepEqual(h.stick(), { x: 0, y: 0, active: false }, 'keyed to keyed: the stick is let go of');
|
|
681
|
+
h.move(1, 300, 300);
|
|
682
|
+
h.frame();
|
|
683
|
+
assert.equal(h.stick().active, false, 'and the finger does not take it again');
|
|
684
|
+
h.up(1, 300, 300);
|
|
685
|
+
h.up(2, 800, 300);
|
|
686
|
+
assert.equal(h.jumped(), false, 'its lift is no tap, nor the other\'s');
|
|
687
|
+
checks += 1;
|
|
688
|
+
}
|
|
689
|
+
// Keyless to keyed with the stick held: the old handlers keep it until a
|
|
690
|
+
// pointer goes down under the key, which lets go of everything they held;
|
|
691
|
+
// only one of the two writes the stick at any time.
|
|
692
|
+
{
|
|
693
|
+
const h = makeRuntime('off');
|
|
694
|
+
h.run(10);
|
|
695
|
+
h.down(1, 300, 400);
|
|
696
|
+
h.move(1, 300, 330);
|
|
697
|
+
assert.equal(h.stick().active, true);
|
|
698
|
+
h.load('orbit');
|
|
699
|
+
h.move(1, 330, 330);
|
|
700
|
+
assert.ok(h.stick().active && h.stick().x > 0, 'the old handlers still have the finger they had');
|
|
701
|
+
h.down(2, 800, 300);
|
|
702
|
+
assert.deepEqual(h.stick(), { x: 0, y: 0, active: false }, 'a pointer down under the key lets go of it');
|
|
703
|
+
h.move(1, 300, 300);
|
|
704
|
+
assert.deepEqual(h.stick(), { x: 0, y: 0, active: false }, 'and the old handlers ignore it from then on');
|
|
705
|
+
h.up(1, 300, 300);
|
|
706
|
+
assert.equal(h.jumped(), false, 'its lift is no tap');
|
|
707
|
+
h.up(2, 800, 300);
|
|
708
|
+
assert.equal(h.jumped(), true, 'the new finger\'s tap is');
|
|
709
|
+
h.down(3, 300, 400);
|
|
710
|
+
h.move(3, 300, 344);
|
|
711
|
+
assert.deepEqual(h.stick(), { x: 0, y: -1, active: true }, 'and the stick is the orbit input\'s now');
|
|
712
|
+
checks += 1;
|
|
713
|
+
}
|
|
714
|
+
return checks;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// A Player turned off and on again, through the runtime's own dispatch:
|
|
718
|
+
// what was asked of the camera while it was off is not replayed, for a
|
|
719
|
+
// tenth of a second off and for one frame off.
|
|
720
|
+
function offAndOn() {
|
|
721
|
+
let checks = 0;
|
|
722
|
+
for (const frames of [6, 1]) {
|
|
723
|
+
const h = makeRuntime('orbit');
|
|
724
|
+
h.run(90);
|
|
725
|
+
const yaw = h.runtime.persistent.camAngle;
|
|
726
|
+
const chosen = { ...h.runtime.cameraOrbit };
|
|
727
|
+
const props = h.player().components.Player;
|
|
728
|
+
props.$enabled = false;
|
|
729
|
+
const at = { ...h.camera.position };
|
|
730
|
+
for (let i = 0; i < frames; i += 1) {
|
|
731
|
+
h.down(1 + i, 800, 300);
|
|
732
|
+
h.move(1 + i, 900, 400);
|
|
733
|
+
h.up(1 + i, 900, 400);
|
|
734
|
+
h.wheel(-200);
|
|
735
|
+
h.frame();
|
|
736
|
+
}
|
|
737
|
+
assert.deepEqual({ ...h.camera.position }, at, 'a Player turned off places no camera');
|
|
738
|
+
props.$enabled = true;
|
|
739
|
+
h.frame();
|
|
740
|
+
assert.equal(h.runtime.persistent.camAngle, yaw, `off ${frames} frame(s): a drag made meanwhile is dropped`);
|
|
741
|
+
assert.deepEqual(h.runtime.cameraOrbit, chosen, 'and so is a wheel');
|
|
742
|
+
h.run(60);
|
|
743
|
+
assert.ok(h.off(h.rest(yaw, chosen.pitch, chosen.range)) < 1e-3, 'the camera settles where it was');
|
|
744
|
+
// The player has it again.
|
|
745
|
+
h.wheel(-100);
|
|
746
|
+
h.frame();
|
|
747
|
+
assert.ok(h.runtime.cameraOrbit.range < chosen.range);
|
|
748
|
+
checks += 1;
|
|
749
|
+
}
|
|
750
|
+
return checks;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
// The hub's door trip locks the look input each frame of the trip, from a
|
|
754
|
+
// system that runs after physics (`scene.lookInput?.lock?.()` beside its
|
|
755
|
+
// `lockInput`). Drags and wheels during it move nothing, and are not
|
|
756
|
+
// replayed after it, even with the camera left where the Player put it.
|
|
757
|
+
function lockedTrip() {
|
|
758
|
+
let checks = 0;
|
|
759
|
+
const h = makeRuntime('orbit');
|
|
760
|
+
const hub = { trip: false };
|
|
761
|
+
h.runtime.registerSystem({
|
|
762
|
+
afterBehaviors(scene) {
|
|
763
|
+
if (hub.trip) scene.lookInput?.lock?.();
|
|
764
|
+
},
|
|
765
|
+
});
|
|
766
|
+
h.run(90);
|
|
767
|
+
const yaw = h.runtime.persistent.camAngle;
|
|
768
|
+
const chosen = { ...h.runtime.cameraOrbit };
|
|
769
|
+
const at = { ...h.camera.position };
|
|
770
|
+
hub.trip = true;
|
|
771
|
+
h.frame();
|
|
772
|
+
h.down(1, 800, 300);
|
|
773
|
+
for (let i = 0; i < 30; i += 1) {
|
|
774
|
+
h.move(1, 820 + i * 10, 300 + i * 5);
|
|
775
|
+
h.wheel(-100);
|
|
776
|
+
h.frame();
|
|
777
|
+
}
|
|
778
|
+
hub.trip = false;
|
|
779
|
+
h.move(1, 1200, 600);
|
|
780
|
+
h.wheel(-100);
|
|
781
|
+
h.frame();
|
|
782
|
+
assert.equal(h.runtime.persistent.camAngle, yaw, 'a drag during the trip does not turn the camera');
|
|
783
|
+
assert.deepEqual(h.runtime.cameraOrbit, chosen, 'nor tilt or zoom it, up to the frame after it');
|
|
784
|
+
assert.ok(h.off(at) < 1e-3, `and the camera stays where it was (${h.off(at)} m off)`);
|
|
785
|
+
h.up(1, 1200, 600);
|
|
786
|
+
h.frame();
|
|
787
|
+
assert.equal(h.runtime.persistent.camAngle, yaw, 'nothing from the trip is replayed after it');
|
|
788
|
+
assert.deepEqual(h.runtime.cameraOrbit, chosen);
|
|
789
|
+
h.down(2, 800, 300);
|
|
790
|
+
h.move(2, 840, 300);
|
|
791
|
+
h.move(2, 900, 300);
|
|
792
|
+
h.frame();
|
|
793
|
+
assert.ok(Math.abs(h.runtime.persistent.camAngle - (yaw + 100 * LOOK_RAD_PER_PX)) < 1e-12, 'a drag after it turns the camera by that drag');
|
|
794
|
+
// Without the key a lock is never asked for: there is no look input.
|
|
795
|
+
const off = makeRuntime('off');
|
|
796
|
+
off.runtime.registerSystem({ afterBehaviors: (scene) => scene.lookInput?.lock?.() });
|
|
797
|
+
off.run(10);
|
|
798
|
+
assert.equal(off.runtime.lookInput, undefined);
|
|
799
|
+
checks += 1;
|
|
800
|
+
return checks;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
function walking() {
|
|
804
|
+
let checks = 0;
|
|
805
|
+
const heading = (world, n, keys) => {
|
|
806
|
+
const from = { ...world.actor.components.Transform };
|
|
807
|
+
world.run(n, keys);
|
|
808
|
+
const t = world.actor.components.Transform;
|
|
809
|
+
const d = Math.hypot(t.x - from.x, t.z - from.z);
|
|
810
|
+
return { x: (t.x - from.x) / d, z: (t.z - from.z) / d, d };
|
|
811
|
+
};
|
|
812
|
+
// Stick up, and W: away from the camera, along the ground.
|
|
813
|
+
for (const input of ['stick', 'KeyW', 'ArrowUp']) {
|
|
814
|
+
const world = makeWorld('orbit');
|
|
815
|
+
world.run(60);
|
|
816
|
+
if (input === 'stick') Object.assign(world.stick, { active: true, x: 0, y: -1 });
|
|
817
|
+
const a = world.scene.persistent.camAngle;
|
|
818
|
+
const went = heading(world, 60, input === 'stick' ? [] : [input]);
|
|
819
|
+
assert.ok(went.x * Math.sin(a) - went.z * Math.cos(a) > 1 - 1e-9, `${input}: away from the camera`);
|
|
820
|
+
const f = world.frames.at(-1);
|
|
821
|
+
const away = { x: f.player.x - f.cam.x, z: f.player.z - f.cam.z };
|
|
822
|
+
assert.ok((away.x * went.x + away.z * went.z) / Math.hypot(away.x, away.z) > 1 - 1e-6, `${input}: the camera is behind the walk`);
|
|
823
|
+
assert.equal(world.scene.persistent.camAngle, a, `${input}: walking does not turn the camera`);
|
|
824
|
+
// The character faces the walk: with the camera behind, as it always did.
|
|
825
|
+
assert.ok(Math.abs(Math.sin((f.player.rotationY * DEG - (Math.PI - a)) / 2)) < 1e-4, `${input}: and the character faces it`);
|
|
826
|
+
checks += 1;
|
|
827
|
+
}
|
|
828
|
+
// Strafing: across the view, the camera's angle unchanged, and the
|
|
829
|
+
// character turns to face the walk by the shortest way, not at once.
|
|
830
|
+
for (const [input, side] of [
|
|
831
|
+
['KeyD', 1],
|
|
832
|
+
['KeyA', -1],
|
|
833
|
+
['stick', 1],
|
|
834
|
+
]) {
|
|
835
|
+
const world = makeWorld('orbit');
|
|
836
|
+
world.run(60);
|
|
837
|
+
if (input === 'stick') Object.assign(world.stick, { active: true, x: 1, y: 0 });
|
|
838
|
+
const keys = input === 'stick' ? [] : [input];
|
|
839
|
+
const a = world.scene.persistent.camAngle;
|
|
840
|
+
const start = world.actor.components.Transform.rotationY * DEG;
|
|
841
|
+
const want = Math.atan2(side * Math.cos(a), side * Math.sin(a));
|
|
842
|
+
const gap = (facing) => Math.abs(Math.atan2(Math.sin(want - facing), Math.cos(want - facing)));
|
|
843
|
+
const first = world.frame(FIXED, keys);
|
|
844
|
+
assert.ok(gap(first.player.rotationY * DEG) > 0.5 * gap(start) && gap(first.player.rotationY * DEG) < gap(start), `${input}: the turn is eased`);
|
|
845
|
+
let last = gap(first.player.rotationY * DEG);
|
|
846
|
+
for (let i = 0; i < 59; i += 1) {
|
|
847
|
+
const now = gap(world.frame(FIXED, keys).player.rotationY * DEG);
|
|
848
|
+
assert.ok(now <= last + 1e-12, `${input}: and never turns away`);
|
|
849
|
+
last = now;
|
|
850
|
+
}
|
|
851
|
+
assert.ok(last < 1e-3, `${input}: the character faces its walk (${last})`);
|
|
852
|
+
const went = heading(world, 30, keys);
|
|
853
|
+
assert.ok(went.x * side * Math.cos(a) + went.z * side * Math.sin(a) > 1 - 1e-9, `${input}: walks across the view`);
|
|
854
|
+
assert.equal(world.scene.persistent.camAngle, a, `${input}: the camera does not turn`);
|
|
855
|
+
checks += 1;
|
|
856
|
+
}
|
|
857
|
+
// The facing starts as the keyless Player's, PI - camAngle, whatever the
|
|
858
|
+
// Transform said, and standing still it stays.
|
|
859
|
+
{
|
|
860
|
+
const world = makeWorld('orbit');
|
|
861
|
+
world.load('orbit', { rotationY: 123 });
|
|
862
|
+
world.run(60);
|
|
863
|
+
assert.ok(Math.abs(world.actor.components.Transform.rotationY - (Math.PI - Math.PI / 4) / DEG) < 1e-9, 'facing starts behind the camera and stays');
|
|
864
|
+
checks += 1;
|
|
865
|
+
}
|
|
866
|
+
// A half-pushed stick walks at half speed; two keys at once no faster
|
|
867
|
+
// than one.
|
|
868
|
+
{
|
|
869
|
+
const speed = (set, keys) => {
|
|
870
|
+
const world = makeWorld('orbit');
|
|
871
|
+
world.run(60);
|
|
872
|
+
Object.assign(world.stick, set);
|
|
873
|
+
world.run(120, keys);
|
|
874
|
+
return Math.hypot(world.body.vel.x, world.body.vel.z);
|
|
875
|
+
};
|
|
876
|
+
assert.ok(Math.abs(speed({ active: true, x: 0, y: -1 }, []) - PROPS.speed) < 1e-3);
|
|
877
|
+
assert.ok(Math.abs(speed({ active: true, x: 0.5, y: 0 }, []) - PROPS.speed / 2) < 1e-3, 'the stick is analogue');
|
|
878
|
+
assert.ok(Math.abs(speed({ active: true, x: 1, y: -1 }, []) - PROPS.speed) < 1e-3, 'and no longer than 1');
|
|
879
|
+
assert.ok(Math.abs(speed({}, ['KeyW', 'KeyD']) - PROPS.speed) < 1e-3, 'two keys walk no faster than one');
|
|
880
|
+
assert.ok(speed({ active: true, x: 0.1, y: 0.1 }, []) < 1e-6, 'inside the dead zone nothing moves');
|
|
881
|
+
checks += 1;
|
|
882
|
+
}
|
|
883
|
+
// Left and right swing the camera at turnSpeed, and do not walk.
|
|
884
|
+
{
|
|
885
|
+
const world = makeWorld('orbit');
|
|
886
|
+
world.run(60);
|
|
887
|
+
const a = world.scene.persistent.camAngle;
|
|
888
|
+
const from = { ...world.body.pos };
|
|
889
|
+
world.run(30, ['ArrowRight']);
|
|
890
|
+
assert.ok(Math.abs(world.scene.persistent.camAngle - (a + 30 * FIXED * PROPS.turnSpeed)) < 1e-9, 'right swings the view right');
|
|
891
|
+
world.run(60, ['ArrowLeft']);
|
|
892
|
+
assert.ok(Math.abs(world.scene.persistent.camAngle - (a - 30 * FIXED * PROPS.turnSpeed)) < 1e-9, 'left swings it left');
|
|
893
|
+
assert.ok(Math.hypot(world.body.pos.x - from.x, world.body.pos.z - from.z) < 1e-9, 'and neither walks');
|
|
894
|
+
checks += 1;
|
|
895
|
+
}
|
|
896
|
+
// The jump, the hover and the pull are the rigid camera's: forces, a tap
|
|
897
|
+
// or Space, the lift dropped on the frame of a jump.
|
|
898
|
+
{
|
|
899
|
+
const world = makeWorld('orbit');
|
|
900
|
+
world.run(60);
|
|
901
|
+
assert.ok(world.frames.at(-1).calls.includes('addForce') && !/"applyImpulse",\{"x":[^,]*,"y":(?!0,)/.test(world.frames.at(-1).calls), 'the hover is a force');
|
|
902
|
+
world.stick.taps = 1;
|
|
903
|
+
const f = world.frame(FIXED);
|
|
904
|
+
assert.ok(f.calls.includes(`["setVelocity",{"x":0,"y":${PROPS.jumpSpeed},"z":0}],["resetForces"]`), `a tap jumps (${f.calls})`);
|
|
905
|
+
assert.equal(world.stick.taps, 0, 'and is taken once');
|
|
906
|
+
world.run(8);
|
|
907
|
+
assert.ok(world.body.pos.y > 1.2, 'off the ground');
|
|
908
|
+
world.run(120);
|
|
909
|
+
const g = world.frame(FIXED, ['Space']);
|
|
910
|
+
assert.ok(g.calls.includes('setVelocity'), 'Space jumps');
|
|
911
|
+
checks += 1;
|
|
912
|
+
}
|
|
913
|
+
return checks;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
function looking() {
|
|
917
|
+
let checks = 0;
|
|
918
|
+
const settled = (options) => {
|
|
919
|
+
const world = makeWorld('orbit', Player, options);
|
|
920
|
+
world.run(90);
|
|
921
|
+
return world;
|
|
922
|
+
};
|
|
923
|
+
// It starts where the fixed camera sits: camDistance behind, camHeight up.
|
|
924
|
+
{
|
|
925
|
+
const world = settled();
|
|
926
|
+
const t = world.actor.components.Transform;
|
|
927
|
+
const a = world.scene.persistent.camAngle;
|
|
928
|
+
const fixed = { x: t.x - PROPS.camDistance * Math.sin(a), y: t.y + PROPS.camHeight, z: t.z + PROPS.camDistance * Math.cos(a) };
|
|
929
|
+
assert.ok(world.off(fixed) < 1e-3, `starts where the fixed camera sits (${world.off(fixed)} m off)`);
|
|
930
|
+
assert.ok(world.off(world.rest(a, PITCH0, RANGE0)) < 1e-3);
|
|
931
|
+
// And looks at the player, LOOK_HEIGHT up.
|
|
932
|
+
const look = new THREE.Vector3(0, 0, -1).applyQuaternion(world.camera.quaternion);
|
|
933
|
+
const to = new THREE.Vector3(t.x - world.camera.position.x, t.y + LOOK_HEIGHT - world.camera.position.y, t.z - world.camera.position.z).normalize();
|
|
934
|
+
assert.ok(look.dot(to) > 1 - 1e-9, 'looking at the player');
|
|
935
|
+
checks += 1;
|
|
936
|
+
}
|
|
937
|
+
// A drag to the right swings the view right (the yaw grows, as turning
|
|
938
|
+
// right always grew it), by LOOK_RAD_PER_PX a pixel; a drag up looks up
|
|
939
|
+
// (the camera goes lower). Eased, not at once.
|
|
940
|
+
{
|
|
941
|
+
const world = settled();
|
|
942
|
+
const a = world.scene.persistent.camAngle;
|
|
943
|
+
world.gestures.down(1, { x: 900, y: 300, time: 0, left: false });
|
|
944
|
+
world.gestures.move(1, 920, 300, 5);
|
|
945
|
+
world.gestures.move(1, 1020, 260, 10);
|
|
946
|
+
const dx = 1020 - 900;
|
|
947
|
+
const dy = 260 - 300;
|
|
948
|
+
const before = world.off(world.rest(a + dx * LOOK_RAD_PER_PX, PITCH0 + dy * LOOK_RAD_PER_PX, RANGE0));
|
|
949
|
+
world.frame();
|
|
950
|
+
assert.ok(Math.abs(world.scene.persistent.camAngle - (a + dx * LOOK_RAD_PER_PX)) < 1e-12, 'the yaw is the persistent camAngle');
|
|
951
|
+
assert.ok(Math.abs(world.scene.cameraOrbit.pitch - (PITCH0 + dy * LOOK_RAD_PER_PX)) < 1e-12, 'the tilt is on the runtime');
|
|
952
|
+
const after = world.off(world.rest(world.scene.persistent.camAngle, world.scene.cameraOrbit.pitch, RANGE0));
|
|
953
|
+
assert.ok(after > 0.5 * before && after < 0.8 * before, `eased: ${after} of ${before} left after a frame`);
|
|
954
|
+
const lower = world.camera.position.y;
|
|
955
|
+
world.run(60);
|
|
956
|
+
assert.ok(world.off(world.rest(world.scene.persistent.camAngle, world.scene.cameraOrbit.pitch, RANGE0)) < 1e-4, 'and gets there');
|
|
957
|
+
assert.ok(world.camera.position.y < lower, 'a drag up puts the camera lower');
|
|
958
|
+
// The view swung right: what was straight ahead is now left of centre.
|
|
959
|
+
const ahead = new THREE.Vector3(Math.sin(a) * 50, 0, -Math.cos(a) * 50).project(world.camera);
|
|
960
|
+
assert.ok(ahead.x < -0.05, `the view swung right (${ahead.x})`);
|
|
961
|
+
checks += 1;
|
|
962
|
+
}
|
|
963
|
+
// The tilt stays between 5 and 70 degrees, the distance between 3 and 12 m.
|
|
964
|
+
{
|
|
965
|
+
const world = settled();
|
|
966
|
+
const drag = (dy) => {
|
|
967
|
+
world.gestures.down(2, { x: 900, y: 400, time: 0, left: false });
|
|
968
|
+
world.gestures.move(2, 900, 400 + Math.sign(dy) * 20, 1);
|
|
969
|
+
world.gestures.move(2, 900, 400 + dy, 2);
|
|
970
|
+
world.gestures.up(2, 500, false);
|
|
971
|
+
world.run(90);
|
|
972
|
+
};
|
|
973
|
+
drag(2000);
|
|
974
|
+
assert.equal(world.scene.cameraOrbit.pitch, 70 * DEG, 'no steeper than 70 degrees');
|
|
975
|
+
assert.ok(world.off(world.rest(world.scene.persistent.camAngle, 70 * DEG, RANGE0)) < 1e-4);
|
|
976
|
+
drag(-4000);
|
|
977
|
+
assert.equal(world.scene.cameraOrbit.pitch, 5 * DEG, 'no flatter than 5');
|
|
978
|
+
assert.ok(world.camera.position.y > world.actor.components.Transform.y + LOOK_HEIGHT, 'and still above the point it looks at');
|
|
979
|
+
for (let i = 0; i < 40; i += 1) world.gestures.wheel(-100, 0, i);
|
|
980
|
+
world.run(90);
|
|
981
|
+
assert.equal(world.scene.cameraOrbit.range, 3, 'no nearer than 3 m');
|
|
982
|
+
assert.ok(world.off(world.rest(world.scene.persistent.camAngle, 5 * DEG, 3)) < 1e-4);
|
|
983
|
+
for (let i = 0; i < 40; i += 1) {
|
|
984
|
+
world.gestures.wheel(100, 0, i);
|
|
985
|
+
world.frame();
|
|
986
|
+
}
|
|
987
|
+
world.run(90);
|
|
988
|
+
assert.equal(world.scene.cameraOrbit.range, 12, 'no farther than 12 m');
|
|
989
|
+
assert.ok(world.off(world.rest(world.scene.persistent.camAngle, 5 * DEG, 12)) < 1e-4);
|
|
990
|
+
// One notch is a tenth.
|
|
991
|
+
world.gestures.wheel(-100, 0, 0);
|
|
992
|
+
world.frame();
|
|
993
|
+
assert.ok(Math.abs(world.scene.cameraOrbit.range - 12 / WHEEL_ZOOM_PER_NOTCH) < 1e-9, 'a notch of the wheel is a tenth nearer');
|
|
994
|
+
// A pinch: fingers twice as far apart, half the distance.
|
|
995
|
+
world.gestures.down(5, { x: 500, y: 300, time: 0, left: false });
|
|
996
|
+
world.gestures.down(6, { x: 700, y: 300, time: 0, left: false });
|
|
997
|
+
world.gestures.move(6, 900, 300, 1);
|
|
998
|
+
const from = 12 / WHEEL_ZOOM_PER_NOTCH;
|
|
999
|
+
world.frame();
|
|
1000
|
+
assert.ok(Math.abs(world.scene.cameraOrbit.range - from / 2) < 1e-9, 'fingers apart bring it nearer');
|
|
1001
|
+
world.gestures.move(5, 880, 300, 2);
|
|
1002
|
+
world.frame();
|
|
1003
|
+
assert.equal(world.scene.cameraOrbit.range, 12, 'and a pinch stops at the limits too');
|
|
1004
|
+
checks += 1;
|
|
1005
|
+
}
|
|
1006
|
+
// A Player whose camera starts outside the limits has room to where it
|
|
1007
|
+
// starts; one with other numbers starts from its own.
|
|
1008
|
+
{
|
|
1009
|
+
const world = makeWorld('orbit');
|
|
1010
|
+
world.actor.components.Player = { ...PROPS, camDistance: 20, camHeight: 30 };
|
|
1011
|
+
world.run(5);
|
|
1012
|
+
const o = world.scene.cameraOrbit;
|
|
1013
|
+
assert.equal(o.range, Math.hypot(20, 30 - LOOK_HEIGHT));
|
|
1014
|
+
assert.equal(o.farthest, o.range, 'room out to where it starts');
|
|
1015
|
+
assert.equal(o.nearest, 3);
|
|
1016
|
+
assert.equal(o.pitch, Math.atan2(30 - LOOK_HEIGHT, 20), 'a tilt inside the limits is kept');
|
|
1017
|
+
world.actor.components.Player = { ...PROPS, camDistance: 1, camHeight: 1 };
|
|
1018
|
+
world.run(5);
|
|
1019
|
+
assert.equal(world.scene.cameraOrbit.nearest, Math.hypot(1, 1 - LOOK_HEIGHT), 'and in to it');
|
|
1020
|
+
assert.equal(world.scene.cameraOrbit.farthest, 12);
|
|
1021
|
+
world.actor.components.Player = { ...PROPS, camDistance: 0, camHeight: 20 };
|
|
1022
|
+
world.run(5);
|
|
1023
|
+
assert.equal(world.scene.cameraOrbit.pitch, 70 * DEG, 'a tilt outside them is brought in');
|
|
1024
|
+
checks += 1;
|
|
1025
|
+
}
|
|
1026
|
+
// What the player chose is on the runtime and nowhere that is sent.
|
|
1027
|
+
{
|
|
1028
|
+
const world = settled();
|
|
1029
|
+
world.gestures.down(1, { x: 900, y: 300, time: 0, left: false });
|
|
1030
|
+
world.gestures.move(1, 1000, 420, 5);
|
|
1031
|
+
world.gestures.wheel(-200, 0, 6);
|
|
1032
|
+
world.run(60, ['KeyW']);
|
|
1033
|
+
world.wall = 2;
|
|
1034
|
+
world.run(10, ['KeyW']);
|
|
1035
|
+
assert.deepEqual(Object.keys(world.scene.persistent).sort(), ['camAngle', 'camLastAt', 'score'], 'scene.persistent holds what it held');
|
|
1036
|
+
assert.deepEqual(Object.keys(world.scene.persistent.camLastAt), ['x', 'z']);
|
|
1037
|
+
assert.ok(world.frames.every((f) => !/pitch|range|zoom|orbit/i.test(f.persistent)), 'no tilt or distance in it, on any frame');
|
|
1038
|
+
assert.notEqual(world.scene.cameraOrbit.pitch, PITCH0);
|
|
1039
|
+
assert.notEqual(world.scene.cameraOrbit.range, RANGE0);
|
|
1040
|
+
checks += 1;
|
|
1041
|
+
}
|
|
1042
|
+
return checks;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
function blocked() {
|
|
1046
|
+
let checks = 0;
|
|
1047
|
+
const world = makeWorld('orbit');
|
|
1048
|
+
world.run(90);
|
|
1049
|
+
const a = world.scene.persistent.camAngle;
|
|
1050
|
+
const reach = () => {
|
|
1051
|
+
const t = world.actor.components.Transform;
|
|
1052
|
+
return Math.hypot(world.camera.position.x - t.x, world.camera.position.y - (t.y + LOOK_HEIGHT), world.camera.position.z - t.z);
|
|
1053
|
+
};
|
|
1054
|
+
// While the first frame's takeover blend lasts its drawn point is tested
|
|
1055
|
+
// too; settled, one ray a frame.
|
|
1056
|
+
assert.ok(world.casts > 90, 'a second ray while a blend lasts');
|
|
1057
|
+
world.casts = 0;
|
|
1058
|
+
world.run(30);
|
|
1059
|
+
assert.equal(world.casts, 30, 'one ray a frame');
|
|
1060
|
+
// The ray goes from the point the camera is placed from (the player's
|
|
1061
|
+
// eased height, LOOK_HEIGHT up) toward where the camera wants to be, as
|
|
1062
|
+
// far as it wants to be and the margin past that.
|
|
1063
|
+
const t = world.actor.components.Transform;
|
|
1064
|
+
const eye = { x: world.camera.position.x - world.lastCast.dir.x * RANGE0, y: world.camera.position.y - world.lastCast.dir.y * RANGE0, z: world.camera.position.z - world.lastCast.dir.z * RANGE0 };
|
|
1065
|
+
for (const k of ['x', 'y', 'z']) assert.ok(Math.abs(world.lastCast.from[k] - eye[k]) < 1e-4, `the ray starts where the camera is placed from (${k})`);
|
|
1066
|
+
assert.ok(Math.abs(world.lastCast.from.y - (t.y + LOOK_HEIGHT)) < 1e-3);
|
|
1067
|
+
assert.ok(Math.abs(world.lastCast.max - (RANGE0 + 0.25)) < 1e-6);
|
|
1068
|
+
const want = world.rest(a, PITCH0, 1);
|
|
1069
|
+
for (const k of ['x', 'y', 'z']) assert.ok(Math.abs(world.lastCast.dir[k] - (want[k] - world.lastCast.from[k])) < 1e-6, `ray ${k}`);
|
|
1070
|
+
assert.ok(Math.abs(Math.hypot(world.lastCast.dir.x, world.lastCast.dir.y, world.lastCast.dir.z) - 1) < 1e-12, 'a unit direction');
|
|
1071
|
+
checks += 1;
|
|
1072
|
+
// Something in the way: in front of it at once, a margin clear.
|
|
1073
|
+
world.wall = 4;
|
|
1074
|
+
world.frame();
|
|
1075
|
+
assert.ok(Math.abs(reach() - 3.75) < 1e-5, `pulled in at once, 0.25 m clear (${reach()})`);
|
|
1076
|
+
assert.ok(world.off(world.rest(a, PITCH0, 3.75)) < 1e-5, 'along the same line');
|
|
1077
|
+
world.wall = 2;
|
|
1078
|
+
world.frame();
|
|
1079
|
+
assert.ok(Math.abs(reach() - 1.75) < 1e-5, 'and in again at once');
|
|
1080
|
+
// Never nearer than 0.6 m.
|
|
1081
|
+
world.wall = 0.3;
|
|
1082
|
+
world.frame();
|
|
1083
|
+
assert.ok(Math.abs(reach() - 0.6) < 1e-5, `no nearer than 0.6 m (${reach()})`);
|
|
1084
|
+
world.wall = 0;
|
|
1085
|
+
world.frame();
|
|
1086
|
+
assert.ok(Math.abs(reach() - 0.6) < 1e-5);
|
|
1087
|
+
checks += 1;
|
|
1088
|
+
// Out of the way: back out slowly, never past where it wants to be.
|
|
1089
|
+
world.wall = null;
|
|
1090
|
+
let last = reach();
|
|
1091
|
+
world.frame();
|
|
1092
|
+
assert.ok(reach() > last && reach() < last + (RANGE0 - last) * 0.15, `let out slowly (${reach()} from ${last})`);
|
|
1093
|
+
for (let i = 0; i < 240; i += 1) {
|
|
1094
|
+
last = reach();
|
|
1095
|
+
world.frame(UNEVEN(i));
|
|
1096
|
+
assert.ok(reach() >= last - 1e-12 && reach() <= RANGE0 + 1e-9, 'outward only');
|
|
1097
|
+
}
|
|
1098
|
+
assert.ok(Math.abs(reach() - RANGE0) < 1e-3, 'all the way');
|
|
1099
|
+
checks += 1;
|
|
1100
|
+
// Half way out, something nearer again: in at once.
|
|
1101
|
+
world.wall = 3;
|
|
1102
|
+
world.frame();
|
|
1103
|
+
world.wall = null;
|
|
1104
|
+
world.run(10);
|
|
1105
|
+
const half = reach();
|
|
1106
|
+
world.wall = half + 2;
|
|
1107
|
+
world.frame();
|
|
1108
|
+
assert.ok(reach() > half && reach() <= half + 2 - 0.25 + 1e-9, 'still easing out behind a farther wall, inside it');
|
|
1109
|
+
world.wall = 1.5;
|
|
1110
|
+
world.frame();
|
|
1111
|
+
assert.ok(Math.abs(reach() - 1.25) < 1e-5);
|
|
1112
|
+
checks += 1;
|
|
1113
|
+
// Zooming in while held in front of a wall: the camera stays in front of
|
|
1114
|
+
// the wall, on the player's side of the point it looks at, never through
|
|
1115
|
+
// the player, and comes nearer only once the distance asked is nearer
|
|
1116
|
+
// than the wall.
|
|
1117
|
+
{
|
|
1118
|
+
const held = makeWorld('orbit');
|
|
1119
|
+
held.run(90);
|
|
1120
|
+
held.wall = 4;
|
|
1121
|
+
held.frame();
|
|
1122
|
+
const at = () => {
|
|
1123
|
+
const tt = held.actor.components.Transform;
|
|
1124
|
+
const c = held.camera.position;
|
|
1125
|
+
const d = held.lastCast.dir;
|
|
1126
|
+
// Along the ray from the point the camera is placed from: positive is
|
|
1127
|
+
// behind the player, where the camera belongs.
|
|
1128
|
+
return (c.x - held.lastCast.from.x) * d.x + (c.y - held.lastCast.from.y) * d.y + (c.z - held.lastCast.from.z) * d.z + 0 * tt.x;
|
|
1129
|
+
};
|
|
1130
|
+
assert.ok(Math.abs(at() - 3.75) < 1e-5);
|
|
1131
|
+
let last = at();
|
|
1132
|
+
for (let i = 0; i < 90; i += 1) {
|
|
1133
|
+
if (i < 20) held.gestures.wheel(-100, 0);
|
|
1134
|
+
held.frame();
|
|
1135
|
+
const now = at();
|
|
1136
|
+
assert.ok(now > 0, `frame ${i}: the camera is not through the player (${now})`);
|
|
1137
|
+
assert.ok(now <= 3.75 + 1e-5, `frame ${i}: nor past the wall (${now})`);
|
|
1138
|
+
assert.ok(now <= last + 1e-5, `frame ${i}: and only comes nearer (${now} after ${last})`);
|
|
1139
|
+
assert.ok(now >= Math.min(0.6, held.scene.cameraOrbit.range) - 1e-9, `frame ${i}: no nearer than 0.6 m`);
|
|
1140
|
+
last = now;
|
|
1141
|
+
}
|
|
1142
|
+
assert.equal(held.scene.cameraOrbit.range, 3, 'zoomed all the way in');
|
|
1143
|
+
assert.ok(Math.abs(at() - 3) < 1e-3, `at the distance asked, nearer than the wall (${at()})`);
|
|
1144
|
+
// Back out with the wall still there: no farther than in front of it.
|
|
1145
|
+
for (let i = 0; i < 20; i += 1) held.gestures.wheel(100, 0);
|
|
1146
|
+
held.run(90);
|
|
1147
|
+
assert.ok(Math.abs(at() - 3.75) < 1e-5, `held in front of the wall again (${at()})`);
|
|
1148
|
+
checks += 1;
|
|
1149
|
+
}
|
|
1150
|
+
// With nothing in the way, zooming in and out is at the zoom's own pace,
|
|
1151
|
+
// not the easing out's.
|
|
1152
|
+
{
|
|
1153
|
+
const free = makeWorld('orbit');
|
|
1154
|
+
free.run(90);
|
|
1155
|
+
for (let i = 0; i < 40; i += 1) free.gestures.wheel(-100, 0);
|
|
1156
|
+
free.run(30);
|
|
1157
|
+
const tt = free.actor.components.Transform;
|
|
1158
|
+
const dist = () => Math.hypot(free.camera.position.x - tt.x, free.camera.position.y - (tt.y + LOOK_HEIGHT), free.camera.position.z - tt.z);
|
|
1159
|
+
assert.ok(Math.abs(dist() - 3) < 1e-2, `zooming in (${dist()})`);
|
|
1160
|
+
for (let i = 0; i < 40; i += 1) free.gestures.wheel(100, 0);
|
|
1161
|
+
free.run(30);
|
|
1162
|
+
assert.ok(Math.abs(dist() - 12) < 5e-2, `and out (${dist()})`);
|
|
1163
|
+
checks += 1;
|
|
1164
|
+
}
|
|
1165
|
+
// No physics world: no ray, and the camera is placed all the same.
|
|
1166
|
+
{
|
|
1167
|
+
const unready = makeWorld('orbit');
|
|
1168
|
+
unready.ready = false;
|
|
1169
|
+
const t0 = unready.actor.components.Transform;
|
|
1170
|
+
for (let i = 0; i < 90; i += 1) {
|
|
1171
|
+
t0.x += 0.05;
|
|
1172
|
+
unready.frame();
|
|
1173
|
+
}
|
|
1174
|
+
assert.equal(unready.casts, 0, 'no ray without a world');
|
|
1175
|
+
assert.ok(unready.off(unready.rest(Math.PI / 4, PITCH0, RANGE0)) < 1e-2, 'placed before physics is ready');
|
|
1176
|
+
checks += 1;
|
|
1177
|
+
}
|
|
1178
|
+
// An api without castRayFixed (an older physics system): no collision.
|
|
1179
|
+
{
|
|
1180
|
+
const old = makeWorld('orbit');
|
|
1181
|
+
delete old.scene.physics3d.castRayFixed;
|
|
1182
|
+
old.run(90);
|
|
1183
|
+
assert.ok(old.off(old.rest(Math.PI / 4, PITCH0, RANGE0)) < 1e-3);
|
|
1184
|
+
checks += 1;
|
|
1185
|
+
}
|
|
1186
|
+
return checks;
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
// Without the key the character always faces PI - camAngle, so code that
|
|
1190
|
+
// turns it sets camAngle. Under the key turning the character is asked for
|
|
1191
|
+
// (`scene.faceRequest`, as the hub asks beside its camAngle write); a
|
|
1192
|
+
// camAngle written without a request turns only the camera, and the
|
|
1193
|
+
// Player's own swing and look never turn the character.
|
|
1194
|
+
function facing() {
|
|
1195
|
+
let checks = 0;
|
|
1196
|
+
const world = makeWorld('orbit');
|
|
1197
|
+
world.run(60);
|
|
1198
|
+
const rotation = () => world.actor.components.Transform.rotationY;
|
|
1199
|
+
const start = rotation();
|
|
1200
|
+
world.run(20, ['ArrowRight']);
|
|
1201
|
+
assert.equal(rotation(), start, 'left/right turn the camera, not the character');
|
|
1202
|
+
world.gestures.down(1, { x: 900, y: 300, left: false });
|
|
1203
|
+
world.gestures.move(1, 960, 300);
|
|
1204
|
+
world.gestures.move(1, 1100, 300);
|
|
1205
|
+
world.gestures.up(1, 0, false);
|
|
1206
|
+
world.run(20);
|
|
1207
|
+
assert.equal(rotation(), start, 'a drag turns the camera, not the character');
|
|
1208
|
+
// A camAngle written with no request: the camera turns, the character not.
|
|
1209
|
+
world.scene.persistent.camAngle = 0.3;
|
|
1210
|
+
world.run(5);
|
|
1211
|
+
assert.equal(rotation(), start, 'a camAngle written without a request does not turn the character');
|
|
1212
|
+
// The hub's arrival: camAngle and the request. The character faces that
|
|
1213
|
+
// way that frame, standing still, and stays so.
|
|
1214
|
+
world.scene.persistent.camAngle = Math.PI - 1.1;
|
|
1215
|
+
world.scene.faceRequest = 1.1;
|
|
1216
|
+
world.frame();
|
|
1217
|
+
assert.equal(rotation(), 1.1 / DEG, 'a request turns the character that frame');
|
|
1218
|
+
assert.equal(world.scene.faceRequest, undefined, 'and is taken');
|
|
1219
|
+
world.run(20);
|
|
1220
|
+
assert.equal(rotation(), 1.1 / DEG, 'and it stays so');
|
|
1221
|
+
// An arrival that writes the yaw that is already there still turns it.
|
|
1222
|
+
world.run(30, ['KeyD']);
|
|
1223
|
+
assert.notEqual(rotation(), 1.1 / DEG);
|
|
1224
|
+
const yaw = world.scene.persistent.camAngle;
|
|
1225
|
+
world.scene.persistent.camAngle = yaw;
|
|
1226
|
+
world.scene.faceRequest = Math.PI - yaw;
|
|
1227
|
+
world.frame();
|
|
1228
|
+
assert.equal(rotation(), (Math.PI - yaw) / DEG, 'an arrival writing the same yaw turns it');
|
|
1229
|
+
// A request while a drag waits to be taken: the facing is the one asked,
|
|
1230
|
+
// exactly, and the drag turns the camera.
|
|
1231
|
+
world.gestures.down(2, { x: 900, y: 300, left: false });
|
|
1232
|
+
world.gestures.move(2, 960, 300);
|
|
1233
|
+
world.gestures.move(2, 1000, 300);
|
|
1234
|
+
const before = world.scene.persistent.camAngle;
|
|
1235
|
+
world.scene.faceRequest = -0.7;
|
|
1236
|
+
world.frame();
|
|
1237
|
+
assert.equal(rotation(), -0.7 / DEG, 'a request beside a pending drag is applied exactly');
|
|
1238
|
+
assert.ok(world.scene.persistent.camAngle > before, 'and the drag turns the camera');
|
|
1239
|
+
world.gestures.up(2, 0, false);
|
|
1240
|
+
// A request that is not a finite number is dropped, nothing turned.
|
|
1241
|
+
world.scene.faceRequest = NaN;
|
|
1242
|
+
world.frame();
|
|
1243
|
+
assert.equal(rotation(), -0.7 / DEG);
|
|
1244
|
+
assert.equal(world.scene.faceRequest, undefined);
|
|
1245
|
+
// A walk turns the character from there.
|
|
1246
|
+
world.run(60, ['KeyD']);
|
|
1247
|
+
const a = world.scene.persistent.camAngle;
|
|
1248
|
+
assert.ok(Math.abs(rotation() - Math.atan2(Math.cos(a), Math.sin(a)) / DEG) < 1e-2, 'strafing turns it to the walk');
|
|
1249
|
+
checks += 1;
|
|
1250
|
+
return checks;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
function takenOver() {
|
|
1254
|
+
let checks = 0;
|
|
1255
|
+
// A teleport snaps.
|
|
1256
|
+
{
|
|
1257
|
+
const world = makeWorld('orbit');
|
|
1258
|
+
world.run(60);
|
|
1259
|
+
world.run(30, ['KeyW'], UNEVEN);
|
|
1260
|
+
world.teleport(40, -25);
|
|
1261
|
+
world.frame(FIXED, ['KeyW']);
|
|
1262
|
+
const err = world.off(world.rest(world.scene.persistent.camAngle, PITCH0, RANGE0));
|
|
1263
|
+
assert.ok(err < 1e-9, `a teleport snaps the camera (${err} m off)`);
|
|
1264
|
+
checks += 1;
|
|
1265
|
+
}
|
|
1266
|
+
// Something else places the camera (a hub trip writes it every frame of
|
|
1267
|
+
// the trip, after this): what the player drags meanwhile is dropped, and
|
|
1268
|
+
// afterwards the camera is eased on from where it was left, in about 0.3 s.
|
|
1269
|
+
{
|
|
1270
|
+
const world = makeWorld('orbit');
|
|
1271
|
+
world.run(90);
|
|
1272
|
+
const a = world.scene.persistent.camAngle;
|
|
1273
|
+
const rest = world.rest(a, PITCH0, RANGE0);
|
|
1274
|
+
world.gestures.down(1, { x: 900, y: 300, time: 0, left: false });
|
|
1275
|
+
world.camera.position.set(rest.x + 3, rest.y + 2, rest.z - 1);
|
|
1276
|
+
for (let i = 0; i < 40; i += 1) {
|
|
1277
|
+
world.gestures.move(1, 930 + i * 10, 300 + i * 4, i);
|
|
1278
|
+
world.gestures.wheel(-100, 0, i);
|
|
1279
|
+
world.frame();
|
|
1280
|
+
world.camera.position.set(rest.x + 3 + i * 0.01, rest.y + 2, rest.z - 1);
|
|
1281
|
+
}
|
|
1282
|
+
world.gestures.up(1, 1000, false);
|
|
1283
|
+
assert.equal(world.scene.persistent.camAngle, a, 'a drag during a trip does not turn the camera');
|
|
1284
|
+
assert.equal(world.scene.cameraOrbit.pitch, PITCH0);
|
|
1285
|
+
assert.equal(world.scene.cameraOrbit.range, RANGE0, 'nor zoom it');
|
|
1286
|
+
const put = { ...world.camera.position };
|
|
1287
|
+
world.frame();
|
|
1288
|
+
const first = world.off(put);
|
|
1289
|
+
const whole = Math.hypot(put.x - rest.x, put.y - rest.y, put.z - rest.z);
|
|
1290
|
+
assert.ok(first > 0 && first < whole * 0.2, `not popped back: ${first} of ${whole} m in the first frame`);
|
|
1291
|
+
world.run(17);
|
|
1292
|
+
assert.ok(world.off(rest) < whole * 0.06, `about there in 0.3 s (${world.off(rest)} m left)`);
|
|
1293
|
+
world.run(60);
|
|
1294
|
+
assert.ok(world.off(rest) < 1e-4);
|
|
1295
|
+
// The player has it again.
|
|
1296
|
+
world.gestures.wheel(-100, 0, 0);
|
|
1297
|
+
world.frame();
|
|
1298
|
+
assert.ok(world.scene.cameraOrbit.range < RANGE0);
|
|
1299
|
+
checks += 1;
|
|
1300
|
+
}
|
|
1301
|
+
// More than 20 m off: snapped, as the others are.
|
|
1302
|
+
{
|
|
1303
|
+
const world = makeWorld('orbit');
|
|
1304
|
+
world.run(90);
|
|
1305
|
+
const rest = world.rest(world.scene.persistent.camAngle, PITCH0, RANGE0);
|
|
1306
|
+
world.camera.position.set(rest.x + 30, rest.y, rest.z);
|
|
1307
|
+
world.frame();
|
|
1308
|
+
assert.ok(world.off(world.rest(world.scene.persistent.camAngle, PITCH0, RANGE0)) < 1e-9, 'a camera left far away is snapped');
|
|
1309
|
+
checks += 1;
|
|
1310
|
+
}
|
|
1311
|
+
// A despawned player: nothing placed, nothing thrown.
|
|
1312
|
+
{
|
|
1313
|
+
const world = makeWorld('orbit');
|
|
1314
|
+
world.run(30);
|
|
1315
|
+
const at = { ...world.camera.position };
|
|
1316
|
+
new Player(PROPS).update(world.actor, world.scene, FIXED);
|
|
1317
|
+
world.scene.actors = [];
|
|
1318
|
+
placeFollowCamera(world.scene);
|
|
1319
|
+
assert.deepEqual({ ...world.camera.position }, at);
|
|
1320
|
+
checks += 1;
|
|
1321
|
+
}
|
|
1322
|
+
// The hub's arrival sets camAngle to put the camera behind the player; a
|
|
1323
|
+
// full turn away is not swung through.
|
|
1324
|
+
{
|
|
1325
|
+
const world = makeWorld('orbit');
|
|
1326
|
+
world.run(90);
|
|
1327
|
+
world.scene.persistent.camAngle += 2 * Math.PI + 0.3;
|
|
1328
|
+
const before = { ...world.camera.position };
|
|
1329
|
+
world.frame();
|
|
1330
|
+
const step = world.off(before);
|
|
1331
|
+
assert.ok(step < 1.2, `the short way round (${step} m in a frame)`);
|
|
1332
|
+
world.run(60);
|
|
1333
|
+
assert.ok(world.off(world.rest(world.scene.persistent.camAngle, PITCH0, RANGE0)) < 1e-4);
|
|
1334
|
+
checks += 1;
|
|
1335
|
+
}
|
|
1336
|
+
return checks;
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
// engine3d/orbitinput.js: what each pointer is doing.
|
|
1340
|
+
function pointerRoles() {
|
|
1341
|
+
let checks = 0;
|
|
1342
|
+
const make = () => {
|
|
1343
|
+
const stick = { x: 0, y: 0, active: false };
|
|
1344
|
+
let taps = 0;
|
|
1345
|
+
const g = makeLookGestures(stick, () => (taps += 1), TUNING);
|
|
1346
|
+
return { g, stick, taps: () => taps, take: () => ({ ...g.look.consume() }) };
|
|
1347
|
+
};
|
|
1348
|
+
const LEFT = { x: 200, y: 400, time: 0, left: true };
|
|
1349
|
+
const RIGHT = { x: 900, y: 400, time: 0, left: false };
|
|
1350
|
+
const nothing = { yaw: 0, pitch: 0, zoom: 1, locked: false };
|
|
1351
|
+
// A drag that starts in the left half is the stick, by attachInput's
|
|
1352
|
+
// sums: the offset from where it started over the radius, no longer than 1.
|
|
1353
|
+
{
|
|
1354
|
+
const { g, stick, taps, take } = make();
|
|
1355
|
+
g.down(1, LEFT);
|
|
1356
|
+
assert.equal(g.busy, true);
|
|
1357
|
+
assert.equal(g.roleOf(1), 'tap');
|
|
1358
|
+
g.move(1, 205, 395, 10);
|
|
1359
|
+
assert.equal(g.roleOf(1), 'tap', 'inside the slop it is still a tap');
|
|
1360
|
+
assert.equal(stick.active, false);
|
|
1361
|
+
g.move(1, 228, 379, 20);
|
|
1362
|
+
assert.equal(g.roleOf(1), 'stick');
|
|
1363
|
+
assert.deepEqual({ ...stick }, { x: 28 / 56, y: -21 / 56, active: true });
|
|
1364
|
+
g.move(1, 200 + 300, 400 - 400, 30);
|
|
1365
|
+
assert.ok(Math.abs(stick.x - 0.6) < 1e-12 && Math.abs(stick.y + 0.8) < 1e-12, 'no longer than 1');
|
|
1366
|
+
assert.deepEqual(stickFrom(300, -400, 56), { x: stick.x, y: stick.y });
|
|
1367
|
+
// It stays the stick wherever it goes.
|
|
1368
|
+
g.move(1, 1100, 400, 40);
|
|
1369
|
+
assert.equal(g.roleOf(1), 'stick');
|
|
1370
|
+
assert.deepEqual(take(), nothing, 'the stick does not touch the camera');
|
|
1371
|
+
g.up(1, 2000, false);
|
|
1372
|
+
assert.deepEqual({ ...stick }, { x: 0, y: 0, active: false });
|
|
1373
|
+
assert.equal(taps(), 0);
|
|
1374
|
+
assert.equal(g.busy, false);
|
|
1375
|
+
assert.equal(g.roleOf(1), null);
|
|
1376
|
+
checks += 1;
|
|
1377
|
+
}
|
|
1378
|
+
// A drag that starts in the right half turns and tilts; the stick is not
|
|
1379
|
+
// touched. With the stick held too, each does its own.
|
|
1380
|
+
{
|
|
1381
|
+
const { g, stick, take } = make();
|
|
1382
|
+
g.down(1, LEFT);
|
|
1383
|
+
g.move(1, 200, 340, 5);
|
|
1384
|
+
g.down(2, RIGHT);
|
|
1385
|
+
assert.equal(g.roleOf(2), 'tap', 'a finger beside the stick is not a pinch');
|
|
1386
|
+
g.move(2, 920, 400, 10);
|
|
1387
|
+
assert.equal(g.roleOf(2), 'look');
|
|
1388
|
+
g.move(2, 970, 370, 20);
|
|
1389
|
+
const got = take();
|
|
1390
|
+
// Each move adds what it moved by.
|
|
1391
|
+
assert.ok(Math.abs(got.yaw - 70 * LOOK_RAD_PER_PX) < 1e-12 && Math.abs(got.pitch + 30 * LOOK_RAD_PER_PX) < 1e-12, JSON.stringify(got));
|
|
1392
|
+
assert.deepEqual(take(), nothing, 'taken once');
|
|
1393
|
+
assert.deepEqual({ ...stick }, { x: 0, y: -1, active: true });
|
|
1394
|
+
g.up(2, 2000, false);
|
|
1395
|
+
assert.deepEqual(take(), nothing);
|
|
1396
|
+
assert.equal(stick.active, true, 'the stick is still held');
|
|
1397
|
+
// A second drag in the left half while the stick is held does nothing.
|
|
1398
|
+
g.down(3, { ...LEFT, x: 100 });
|
|
1399
|
+
g.move(3, 100, 300, 2010);
|
|
1400
|
+
assert.equal(g.roleOf(3), 'spent');
|
|
1401
|
+
assert.deepEqual({ ...stick }, { x: 0, y: -1, active: true });
|
|
1402
|
+
assert.deepEqual(take(), nothing);
|
|
1403
|
+
checks += 1;
|
|
1404
|
+
}
|
|
1405
|
+
// The left half is the stick's. The right thumb looks, then the left
|
|
1406
|
+
// thumb goes down to walk: a look and a stick, and no zoom.
|
|
1407
|
+
{
|
|
1408
|
+
const { g, stick, take } = make();
|
|
1409
|
+
g.down(1, RIGHT);
|
|
1410
|
+
g.move(1, 940, 400, 5);
|
|
1411
|
+
assert.equal(g.roleOf(1), 'look');
|
|
1412
|
+
g.down(2, LEFT);
|
|
1413
|
+
assert.deepEqual([g.roleOf(1), g.roleOf(2)], ['look', 'tap'], 'a finger in the left half does not make a pinch');
|
|
1414
|
+
g.move(2, 200, 344, 10);
|
|
1415
|
+
g.move(1, 990, 380, 15);
|
|
1416
|
+
assert.deepEqual([g.roleOf(1), g.roleOf(2)], ['look', 'stick']);
|
|
1417
|
+
const got = take();
|
|
1418
|
+
assert.equal(got.zoom, 1, 'no zoom');
|
|
1419
|
+
assert.ok(Math.abs(got.yaw - 90 * LOOK_RAD_PER_PX) < 1e-12 && Math.abs(got.pitch + 20 * LOOK_RAD_PER_PX) < 1e-12, 'the look goes on');
|
|
1420
|
+
assert.deepEqual({ ...stick }, { x: 0, y: -1, active: true });
|
|
1421
|
+
checks += 1;
|
|
1422
|
+
}
|
|
1423
|
+
// Both thumbs down at the same instant, in either order, before either
|
|
1424
|
+
// has moved: the stick and a look.
|
|
1425
|
+
for (const order of [
|
|
1426
|
+
[LEFT, RIGHT],
|
|
1427
|
+
[RIGHT, LEFT],
|
|
1428
|
+
]) {
|
|
1429
|
+
const { g, stick, take, taps } = make();
|
|
1430
|
+
order.forEach((at, i) => g.down(at.left ? 'l' : 'r', { ...at, time: i }));
|
|
1431
|
+
assert.deepEqual([g.roleOf('l'), g.roleOf('r')], ['tap', 'tap']);
|
|
1432
|
+
g.move('r', 960, 430, 10);
|
|
1433
|
+
g.move('l', 172, 400, 11);
|
|
1434
|
+
assert.deepEqual([g.roleOf('l'), g.roleOf('r')], ['stick', 'look']);
|
|
1435
|
+
g.move('r', 1000, 430, 12);
|
|
1436
|
+
const got = take();
|
|
1437
|
+
assert.equal(got.zoom, 1, 'no zoom');
|
|
1438
|
+
assert.ok(got.yaw > 0 && got.pitch > 0);
|
|
1439
|
+
assert.deepEqual({ ...stick }, { x: -0.5, y: 0, active: true });
|
|
1440
|
+
g.up('l', 900, false);
|
|
1441
|
+
g.up('r', 910, false);
|
|
1442
|
+
assert.equal(taps(), 0);
|
|
1443
|
+
assert.equal(stick.active, false);
|
|
1444
|
+
checks += 1;
|
|
1445
|
+
}
|
|
1446
|
+
// Two fingers in the left half: the first to drag is the stick, the
|
|
1447
|
+
// other does nothing. Never a pinch.
|
|
1448
|
+
{
|
|
1449
|
+
const { g, stick, take } = make();
|
|
1450
|
+
g.down(1, LEFT);
|
|
1451
|
+
g.down(2, { ...LEFT, x: 300 });
|
|
1452
|
+
assert.deepEqual([g.roleOf(1), g.roleOf(2)], ['tap', 'tap']);
|
|
1453
|
+
g.move(2, 356, 400, 5);
|
|
1454
|
+
g.move(1, 100, 400, 6);
|
|
1455
|
+
assert.deepEqual([g.roleOf(1), g.roleOf(2)], ['spent', 'stick']);
|
|
1456
|
+
assert.deepEqual({ ...stick }, { x: 1, y: 0, active: true });
|
|
1457
|
+
assert.deepEqual(take(), nothing);
|
|
1458
|
+
checks += 1;
|
|
1459
|
+
}
|
|
1460
|
+
// Two fingers that both landed in the right half are a pinch: no turn,
|
|
1461
|
+
// no stick. One lifts: the other does nothing more.
|
|
1462
|
+
for (const [a, b] of [
|
|
1463
|
+
[RIGHT, { ...RIGHT, x: 1000 }],
|
|
1464
|
+
[{ ...RIGHT, x: 700 }, { ...RIGHT, x: 1100, y: 200 }],
|
|
1465
|
+
]) {
|
|
1466
|
+
const { g, stick, take, taps } = make();
|
|
1467
|
+
g.down(1, a);
|
|
1468
|
+
g.down(2, b);
|
|
1469
|
+
assert.deepEqual([g.roleOf(1), g.roleOf(2)], ['pinch', 'pinch']);
|
|
1470
|
+
// Each finger moves straight away from the other, by one and then two
|
|
1471
|
+
// times how far apart they started.
|
|
1472
|
+
const d0 = Math.hypot(b.x - a.x, b.y - a.y);
|
|
1473
|
+
const ux = (b.x - a.x) / d0;
|
|
1474
|
+
const uy = (b.y - a.y) / d0;
|
|
1475
|
+
g.move(2, b.x + ux * d0, b.y + uy * d0, 10);
|
|
1476
|
+
g.move(1, a.x - 2 * ux * d0, a.y - 2 * uy * d0, 20);
|
|
1477
|
+
const got = take();
|
|
1478
|
+
assert.ok(Math.abs(got.zoom - 1 / 4) < 1e-12, `four times as far apart: a quarter the distance (${got.zoom})`);
|
|
1479
|
+
assert.equal(got.yaw, 0);
|
|
1480
|
+
assert.equal(got.pitch, 0);
|
|
1481
|
+
assert.equal(stick.active, false, 'no stick while pinching');
|
|
1482
|
+
g.up(2, 2000, false);
|
|
1483
|
+
assert.equal(g.roleOf(1), 'spent');
|
|
1484
|
+
g.move(1, a.x + 400, a.y + 200, 2010);
|
|
1485
|
+
assert.deepEqual(take(), nothing, 'the finger left does not turn the camera');
|
|
1486
|
+
assert.equal(stick.active, false, 'or become the stick');
|
|
1487
|
+
g.up(1, 3000, false);
|
|
1488
|
+
assert.equal(taps(), 0);
|
|
1489
|
+
checks += 1;
|
|
1490
|
+
}
|
|
1491
|
+
// A finger already turning the camera, joined by another: a pinch.
|
|
1492
|
+
{
|
|
1493
|
+
const { g, take, stick } = make();
|
|
1494
|
+
g.down(1, RIGHT);
|
|
1495
|
+
g.move(1, 960, 400, 5);
|
|
1496
|
+
assert.equal(g.roleOf(1), 'look');
|
|
1497
|
+
take();
|
|
1498
|
+
g.down(2, { ...RIGHT, x: 760 });
|
|
1499
|
+
assert.deepEqual([g.roleOf(1), g.roleOf(2)], ['pinch', 'pinch']);
|
|
1500
|
+
g.move(1, 860, 400, 10);
|
|
1501
|
+
const got = take();
|
|
1502
|
+
assert.equal(got.yaw, 0, 'no turn from a pinching finger');
|
|
1503
|
+
assert.ok(Math.abs(got.zoom - 2) < 1e-12, 'fingers together: farther');
|
|
1504
|
+
// A finger in the left half while they pinch takes the stick and
|
|
1505
|
+
// walks, and the pinch goes on.
|
|
1506
|
+
g.down(3, LEFT);
|
|
1507
|
+
assert.deepEqual([g.roleOf(1), g.roleOf(2), g.roleOf(3)], ['pinch', 'pinch', 'tap']);
|
|
1508
|
+
g.move(3, 200, 300, 20);
|
|
1509
|
+
assert.equal(g.roleOf(3), 'stick');
|
|
1510
|
+
assert.deepEqual({ ...stick }, { x: 0, y: -1, active: true });
|
|
1511
|
+
assert.equal(take().zoom, 1, 'the stick does not zoom');
|
|
1512
|
+
g.move(1, 960, 400, 25);
|
|
1513
|
+
assert.ok(Math.abs(take().zoom - 1 / 2) < 1e-12, 'and the pinch still does');
|
|
1514
|
+
assert.deepEqual({ ...stick }, { x: 0, y: -1, active: true });
|
|
1515
|
+
g.down(4, { ...RIGHT, y: 100 });
|
|
1516
|
+
g.move(4, 900, 200, 30);
|
|
1517
|
+
assert.equal(g.roleOf(4), 'spent', 'no turning while a pinch is on');
|
|
1518
|
+
checks += 1;
|
|
1519
|
+
}
|
|
1520
|
+
// Fingers that close to nothing do not zoom without bound.
|
|
1521
|
+
{
|
|
1522
|
+
const { g, take } = make();
|
|
1523
|
+
g.down(1, RIGHT);
|
|
1524
|
+
g.down(2, { ...RIGHT, x: 1000 });
|
|
1525
|
+
g.move(2, 900, 400, 5);
|
|
1526
|
+
assert.ok(take().zoom <= 100 / 24 + 1e-9);
|
|
1527
|
+
for (let i = 0; i < 50; i += 1) g.move(2, i % 2 ? 900 : 1900, 400, 10 + i);
|
|
1528
|
+
const { zoom } = take();
|
|
1529
|
+
assert.ok(Number.isFinite(zoom) && zoom > 0 && zoom <= 16);
|
|
1530
|
+
checks += 1;
|
|
1531
|
+
}
|
|
1532
|
+
// A short tap that did not move jumps, anywhere, whatever else is down;
|
|
1533
|
+
// a long one, a moved one and a cancelled one do not.
|
|
1534
|
+
{
|
|
1535
|
+
const { g, taps, stick } = make();
|
|
1536
|
+
g.down(1, RIGHT);
|
|
1537
|
+
g.up(1, 120, false);
|
|
1538
|
+
assert.equal(taps(), 1, 'a tap on the right');
|
|
1539
|
+
g.down(1, LEFT);
|
|
1540
|
+
g.move(1, 204, 403, 150);
|
|
1541
|
+
g.up(1, 250, false);
|
|
1542
|
+
assert.equal(taps(), 2, 'a tap on the left, moved inside the slop');
|
|
1543
|
+
g.down(1, { ...LEFT, time: 1000 });
|
|
1544
|
+
g.up(1, 1300, false);
|
|
1545
|
+
assert.equal(taps(), 2, 'held too long');
|
|
1546
|
+
g.down(1, { ...LEFT, time: 2000 });
|
|
1547
|
+
g.move(1, 200, 340, 2010);
|
|
1548
|
+
g.down(2, { ...RIGHT, time: 2020 });
|
|
1549
|
+
g.up(2, 2100, false);
|
|
1550
|
+
assert.equal(taps(), 3, 'a tap while the stick is held');
|
|
1551
|
+
assert.equal(stick.active, true);
|
|
1552
|
+
g.up(1, 2110, false);
|
|
1553
|
+
assert.equal(taps(), 3, 'a drag is not a tap');
|
|
1554
|
+
g.down(1, { ...RIGHT, time: 3000 });
|
|
1555
|
+
g.up(1, 3050, true);
|
|
1556
|
+
assert.equal(taps(), 3, 'a cancelled pointer is not a tap');
|
|
1557
|
+
// Two fingers tapped together.
|
|
1558
|
+
g.down(1, { ...LEFT, time: 4000 });
|
|
1559
|
+
g.down(2, { ...RIGHT, time: 4010 });
|
|
1560
|
+
g.up(1, 4100, false);
|
|
1561
|
+
g.up(2, 4110, false);
|
|
1562
|
+
assert.equal(taps(), 5);
|
|
1563
|
+
assert.equal(g.busy, false);
|
|
1564
|
+
checks += 1;
|
|
1565
|
+
}
|
|
1566
|
+
// A pinch made with one finger still and the other moving, both lifted
|
|
1567
|
+
// inside the tap time: a zoom, and no jump. Nor from a spent finger.
|
|
1568
|
+
{
|
|
1569
|
+
const { g, taps, take } = make();
|
|
1570
|
+
g.down(1, { ...RIGHT, time: 0 });
|
|
1571
|
+
g.down(2, { ...RIGHT, x: 1000, time: 10 });
|
|
1572
|
+
g.move(2, 1100, 400, 50);
|
|
1573
|
+
g.up(1, 120, false);
|
|
1574
|
+
g.up(2, 150, false);
|
|
1575
|
+
assert.ok(take().zoom < 1, 'a zoom');
|
|
1576
|
+
assert.equal(taps(), 0, 'and no jump from the still finger');
|
|
1577
|
+
g.down(3, { ...RIGHT, time: 200 });
|
|
1578
|
+
g.down(4, { ...RIGHT, x: 1000, time: 210 });
|
|
1579
|
+
g.up(4, 230, false);
|
|
1580
|
+
assert.equal(g.roleOf(3), 'spent');
|
|
1581
|
+
g.up(3, 260, false);
|
|
1582
|
+
assert.equal(taps(), 0, 'nor from the finger a pinch left spent, or the other');
|
|
1583
|
+
checks += 1;
|
|
1584
|
+
}
|
|
1585
|
+
// pointercancel lets go of the stick and ends a pinch.
|
|
1586
|
+
{
|
|
1587
|
+
const { g, stick, take } = make();
|
|
1588
|
+
g.down(1, LEFT);
|
|
1589
|
+
g.move(1, 260, 400, 5);
|
|
1590
|
+
assert.equal(stick.active, true);
|
|
1591
|
+
assert.equal(g.up(1, 10, true), true);
|
|
1592
|
+
assert.deepEqual({ ...stick }, { x: 0, y: 0, active: false });
|
|
1593
|
+
g.down(1, RIGHT);
|
|
1594
|
+
g.down(2, { ...RIGHT, x: 1000 });
|
|
1595
|
+
g.up(1, 20, true);
|
|
1596
|
+
assert.equal(g.roleOf(2), 'spent');
|
|
1597
|
+
g.up(2, 30, true);
|
|
1598
|
+
take();
|
|
1599
|
+
assert.deepEqual(take(), nothing, 'nothing is held after a cancel');
|
|
1600
|
+
// The stick is free again.
|
|
1601
|
+
g.down(3, LEFT);
|
|
1602
|
+
g.move(3, 200, 340, 40);
|
|
1603
|
+
assert.equal(g.roleOf(3), 'stick');
|
|
1604
|
+
checks += 1;
|
|
1605
|
+
}
|
|
1606
|
+
// A mouse: a drag with any button turns and tilts and is never the stick,
|
|
1607
|
+
// wherever it starts; only a left click jumps.
|
|
1608
|
+
{
|
|
1609
|
+
const { g, stick, taps, take } = make();
|
|
1610
|
+
for (const button of [0, 1, 2]) {
|
|
1611
|
+
g.down(1, { ...LEFT, mouse: true, button });
|
|
1612
|
+
g.move(1, 260, 400, 5);
|
|
1613
|
+
assert.equal(g.roleOf(1), 'look', `button ${button}`);
|
|
1614
|
+
g.move(1, 300, 430, 6);
|
|
1615
|
+
const got = take();
|
|
1616
|
+
assert.ok(got.yaw > 0 && got.pitch > 0);
|
|
1617
|
+
assert.equal(stick.active, false);
|
|
1618
|
+
g.up(1, 50, false);
|
|
1619
|
+
assert.equal(taps(), 0, 'a drag is not a click');
|
|
1620
|
+
}
|
|
1621
|
+
g.down(1, { ...RIGHT, mouse: true, button: 2 });
|
|
1622
|
+
g.up(1, 50, false);
|
|
1623
|
+
assert.equal(taps(), 0, 'a right click does not jump');
|
|
1624
|
+
g.down(1, { ...RIGHT, mouse: true, button: 0 });
|
|
1625
|
+
g.up(1, 50, false);
|
|
1626
|
+
assert.equal(taps(), 1, 'a left click does');
|
|
1627
|
+
// A mouse and a finger are not a pinch.
|
|
1628
|
+
g.down(1, { ...RIGHT, mouse: true, button: 0 });
|
|
1629
|
+
g.down(2, RIGHT);
|
|
1630
|
+
assert.deepEqual([g.roleOf(1), g.roleOf(2)], ['tap', 'tap']);
|
|
1631
|
+
checks += 1;
|
|
1632
|
+
}
|
|
1633
|
+
// The wheel: a notch is a tenth, whichever way the browser counts it.
|
|
1634
|
+
{
|
|
1635
|
+
const { g, take } = make();
|
|
1636
|
+
g.wheel(100, 0, 7);
|
|
1637
|
+
assert.ok(Math.abs(take().zoom - WHEEL_ZOOM_PER_NOTCH) < 1e-12, 'toward the player: farther');
|
|
1638
|
+
g.wheel(-100, 0, 8);
|
|
1639
|
+
assert.ok(Math.abs(take().zoom - 1 / WHEEL_ZOOM_PER_NOTCH) < 1e-12);
|
|
1640
|
+
g.wheel(3, 1, 9);
|
|
1641
|
+
assert.ok(Math.abs(take().zoom - WHEEL_ZOOM_PER_NOTCH) < 1e-12, 'three lines are a notch');
|
|
1642
|
+
g.wheel(-1, 2, 10);
|
|
1643
|
+
assert.ok(Math.abs(take().zoom - 1 / WHEEL_ZOOM_PER_NOTCH) < 1e-12, 'a page is a notch');
|
|
1644
|
+
g.wheel(100000, 0, 11);
|
|
1645
|
+
assert.equal(take().zoom, 1.5, 'one event zooms so much and no more');
|
|
1646
|
+
g.wheel(NaN, 0, 12);
|
|
1647
|
+
g.wheel(0, 0, 13);
|
|
1648
|
+
assert.deepEqual(take(), nothing);
|
|
1649
|
+
checks += 1;
|
|
1650
|
+
}
|
|
1651
|
+
// `cancel`: every pointer let go of at once (a scene without the key
|
|
1652
|
+
// loaded with fingers down). The stick is released if one of them held
|
|
1653
|
+
// it, nothing asked of the camera is kept, no tap, and their later moves
|
|
1654
|
+
// and lifts are not this file's.
|
|
1655
|
+
{
|
|
1656
|
+
const { g, stick, taps, take } = make();
|
|
1657
|
+
g.down(1, LEFT);
|
|
1658
|
+
g.move(1, 256, 400, 5);
|
|
1659
|
+
g.down(2, RIGHT);
|
|
1660
|
+
g.move(2, 990, 450, 6);
|
|
1661
|
+
g.down(3, { ...RIGHT, x: 1000, time: 7 });
|
|
1662
|
+
g.wheel(-100, 0, 8);
|
|
1663
|
+
assert.deepEqual({ ...stick }, { x: 1, y: 0, active: true });
|
|
1664
|
+
g.cancel();
|
|
1665
|
+
assert.equal(g.busy, false);
|
|
1666
|
+
assert.deepEqual([g.roleOf(1), g.roleOf(2), g.roleOf(3)], [null, null, null]);
|
|
1667
|
+
assert.deepEqual({ ...stick }, { x: 0, y: 0, active: false }, 'the stick is released');
|
|
1668
|
+
assert.deepEqual(take(), nothing, 'what was asked of the camera is dropped, and nothing is held on it');
|
|
1669
|
+
assert.equal(g.move(1, 300, 400, 9), false);
|
|
1670
|
+
assert.equal(g.move(2, 1100, 400, 9), false);
|
|
1671
|
+
assert.deepEqual({ ...stick }, { x: 0, y: 0, active: false }, 'a cancelled finger does not walk');
|
|
1672
|
+
assert.deepEqual(take(), nothing, 'or turn the camera');
|
|
1673
|
+
for (const id of [1, 2, 3]) assert.equal(g.up(id, 20, false), false);
|
|
1674
|
+
assert.equal(taps(), 0, 'no tap');
|
|
1675
|
+
// A stick somebody else holds (no pointer here has it) is left alone.
|
|
1676
|
+
Object.assign(stick, { x: 0.5, y: 0.25, active: true });
|
|
1677
|
+
g.down(4, RIGHT);
|
|
1678
|
+
g.cancel();
|
|
1679
|
+
assert.deepEqual({ ...stick }, { x: 0.5, y: 0.25, active: true }, "a stick it does not hold is not its to release");
|
|
1680
|
+
Object.assign(stick, { x: 0, y: 0, active: false });
|
|
1681
|
+
// And it starts again clean: the stick is free.
|
|
1682
|
+
g.down(5, LEFT);
|
|
1683
|
+
g.move(5, 200, 344, 30);
|
|
1684
|
+
assert.equal(g.roleOf(5), 'stick');
|
|
1685
|
+
checks += 1;
|
|
1686
|
+
}
|
|
1687
|
+
// A pointer that did not go down here is not this file's.
|
|
1688
|
+
{
|
|
1689
|
+
const { g } = make();
|
|
1690
|
+
assert.equal(g.move(9, 1, 1, 0), false);
|
|
1691
|
+
assert.equal(g.up(9, 0, false), false);
|
|
1692
|
+
assert.equal(g.busy, false);
|
|
1693
|
+
checks += 1;
|
|
1694
|
+
}
|
|
1695
|
+
return checks;
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
// The listeners on the canvas: there under the key only.
|
|
1699
|
+
// The lock: the next `consume` takes nothing and says it is locked; what
|
|
1700
|
+
// was asked before it is dropped, not kept for after.
|
|
1701
|
+
function locking() {
|
|
1702
|
+
let checks = 0;
|
|
1703
|
+
const stick = { x: 0, y: 0, active: false };
|
|
1704
|
+
const g = makeLookGestures(stick, () => {}, TUNING);
|
|
1705
|
+
g.down(1, { x: 900, y: 300, time: 0, left: false });
|
|
1706
|
+
g.move(1, 960, 300);
|
|
1707
|
+
g.wheel(-100, 0);
|
|
1708
|
+
g.look.lock();
|
|
1709
|
+
g.move(1, 1000, 340);
|
|
1710
|
+
assert.deepEqual({ ...g.look.consume() }, { yaw: 0, pitch: 0, zoom: 1, locked: true }, 'a locked take takes nothing');
|
|
1711
|
+
assert.deepEqual({ ...g.look.consume() }, { yaw: 0, pitch: 0, zoom: 1, locked: false }, 'and nothing from before it is left');
|
|
1712
|
+
assert.equal(g.roleOf(1), 'look', 'the finger keeps its role');
|
|
1713
|
+
g.move(1, 1010, 340);
|
|
1714
|
+
assert.ok(Math.abs(g.look.consume().yaw - 10 * LOOK_RAD_PER_PX) < 1e-12, 'and turns the camera once unlocked');
|
|
1715
|
+
g.look.lock();
|
|
1716
|
+
g.cancel();
|
|
1717
|
+
assert.equal(g.look.consume().locked, false, 'a cancel drops a lock too');
|
|
1718
|
+
checks += 1;
|
|
1719
|
+
return checks;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
// The listeners on the canvas: there under the key only, and taken off by
|
|
1723
|
+
// `reset` (each scene load) and `detach`.
|
|
1724
|
+
function listeners() {
|
|
1725
|
+
let checks = 0;
|
|
1726
|
+
const attach = (canvas, runtime) => {
|
|
1727
|
+
const stick = { x: 0, y: 0, active: false };
|
|
1728
|
+
const orbit = attachOrbitInput(canvas, runtime, stick, () => (runtime.taps = (runtime.taps ?? 0) + 1), TUNING);
|
|
1729
|
+
return { orbit, stick };
|
|
1730
|
+
};
|
|
1731
|
+
{
|
|
1732
|
+
const canvas = makeCanvas();
|
|
1733
|
+
const runtime = { cameraControl: null };
|
|
1734
|
+
const { orbit } = attach(canvas, runtime);
|
|
1735
|
+
assert.deepEqual(canvas.types(), [], 'attaching adds no listener');
|
|
1736
|
+
assert.equal(typeof runtime.lookInput.consume, 'function');
|
|
1737
|
+
assert.equal(orbit.busy, false);
|
|
1738
|
+
runtime.lookInput.listen();
|
|
1739
|
+
assert.deepEqual(canvas.types(), [], 'nor does asking without the key');
|
|
1740
|
+
runtime.cameraControl = 'orbit';
|
|
1741
|
+
runtime.lookInput.listen();
|
|
1742
|
+
runtime.lookInput.listen();
|
|
1743
|
+
assert.deepEqual(canvas.types(), ['contextmenu', 'wheel'], 'under the key: one of each, however often asked');
|
|
1744
|
+
assert.equal(canvas.fire('wheel', { deltaY: -100, deltaMode: 0 }), true, 'the wheel is taken');
|
|
1745
|
+
assert.ok(Math.abs(runtime.lookInput.consume().zoom - 1 / WHEEL_ZOOM_PER_NOTCH) < 1e-12);
|
|
1746
|
+
assert.equal(canvas.fire('contextmenu'), true, 'and the context menu kept away');
|
|
1747
|
+
// A load: reset takes both off at once, and lets go of every pointer.
|
|
1748
|
+
orbit.down({ pointerId: 1, clientX: 300, clientY: 300, pointerType: 'touch', button: 0 });
|
|
1749
|
+
canvas.fire('wheel', { deltaY: -100, deltaMode: 0 });
|
|
1750
|
+
runtime.lookInput.reset();
|
|
1751
|
+
assert.deepEqual(canvas.types(), [], 'reset takes the listeners off');
|
|
1752
|
+
assert.equal(orbit.busy, false, 'and lets go of the pointers');
|
|
1753
|
+
assert.equal(runtime.lookInput.consume().zoom, 1, 'and drops what was asked');
|
|
1754
|
+
assert.equal(orbit.up({ pointerId: 1, type: 'pointerup' }), false, 'so a lift after it is not this file\'s');
|
|
1755
|
+
assert.equal(runtime.taps, undefined);
|
|
1756
|
+
runtime.lookInput.listen();
|
|
1757
|
+
assert.deepEqual(canvas.types(), ['contextmenu', 'wheel'], 'and the next keyed frame puts them back');
|
|
1758
|
+
orbit.detach();
|
|
1759
|
+
assert.deepEqual(canvas.types(), [], 'detaching takes them off');
|
|
1760
|
+
orbit.detach();
|
|
1761
|
+
runtime.lookInput.reset();
|
|
1762
|
+
checks += 1;
|
|
1763
|
+
}
|
|
1764
|
+
// A new runtime on the same canvas (the play loop restarted): each has
|
|
1765
|
+
// its own, and the old one's detach leaves the new one's alone.
|
|
1766
|
+
{
|
|
1767
|
+
const canvas = makeCanvas();
|
|
1768
|
+
const first = { cameraControl: 'orbit' };
|
|
1769
|
+
const a = attach(canvas, first);
|
|
1770
|
+
first.lookInput.listen();
|
|
1771
|
+
a.orbit.down({ pointerId: 1, clientX: 700, clientY: 300, pointerType: 'touch', button: 0 });
|
|
1772
|
+
a.orbit.detach();
|
|
1773
|
+
const second = { cameraControl: 'orbit' };
|
|
1774
|
+
const b = attach(canvas, second);
|
|
1775
|
+
assert.equal(b.orbit.busy, false, 'a new runtime starts with no pointers');
|
|
1776
|
+
second.lookInput.listen();
|
|
1777
|
+
assert.deepEqual(canvas.types(), ['contextmenu', 'wheel']);
|
|
1778
|
+
a.orbit.detach();
|
|
1779
|
+
assert.deepEqual(canvas.types(), ['contextmenu', 'wheel'], "the old runtime's detach leaves them");
|
|
1780
|
+
canvas.fire('wheel', { deltaY: 100, deltaMode: 0 });
|
|
1781
|
+
assert.equal(first.lookInput.consume().zoom, 1);
|
|
1782
|
+
assert.ok(second.lookInput.consume().zoom > 1);
|
|
1783
|
+
checks += 1;
|
|
1784
|
+
}
|
|
1785
|
+
// The halves are the canvas's own, wherever it is on the page.
|
|
1786
|
+
{
|
|
1787
|
+
const canvas = makeCanvas();
|
|
1788
|
+
const runtime = { cameraControl: 'orbit' };
|
|
1789
|
+
const { orbit, stick } = attach(canvas, runtime);
|
|
1790
|
+
orbit.down({ pointerId: 1, clientX: 480, clientY: 300, pointerType: 'touch', button: 0 });
|
|
1791
|
+
assert.equal(orbit.busy, true);
|
|
1792
|
+
assert.equal(canvas.focused, 1);
|
|
1793
|
+
assert.deepEqual(canvas.captured, [1]);
|
|
1794
|
+
assert.equal(orbit.move({ pointerId: 1, clientX: 480, clientY: 240 }), true);
|
|
1795
|
+
assert.equal(stick.active, true, 'x 480 is left of the middle of a canvas from 100 to 900');
|
|
1796
|
+
orbit.down({ pointerId: 2, clientX: 520, clientY: 300, pointerType: 'touch', button: 0 });
|
|
1797
|
+
orbit.move({ pointerId: 2, clientX: 560, clientY: 300 });
|
|
1798
|
+
assert.ok(runtime.lookInput.consume().yaw > 0, 'and 520 is right of it');
|
|
1799
|
+
assert.equal(orbit.move({ pointerId: 7, clientX: 0, clientY: 0 }), false, "a pointer that did not go down here is not this file's");
|
|
1800
|
+
assert.equal(orbit.up({ pointerId: 7, type: 'pointerup' }), false);
|
|
1801
|
+
assert.equal(orbit.up({ pointerId: 1, type: 'pointerup' }), true);
|
|
1802
|
+
assert.equal(stick.active, false);
|
|
1803
|
+
assert.equal(orbit.up({ pointerId: 2, type: 'pointercancel' }), true);
|
|
1804
|
+
assert.equal(orbit.busy, false, 'nothing is left held');
|
|
1805
|
+
assert.equal(runtime.taps, undefined);
|
|
1806
|
+
checks += 1;
|
|
1807
|
+
}
|
|
1808
|
+
return checks;
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
// attachInput before the key and now, keyless and with `"follow": "rigid"`
|
|
1812
|
+
// alone: the same pointer and key events, one by one, leave the same stick,
|
|
1813
|
+
// the same jumps, the same keys, and the same listeners on the canvas and
|
|
1814
|
+
// the window, attaching and detaching.
|
|
1815
|
+
function inputUnchanged() {
|
|
1816
|
+
let checks = 0;
|
|
1817
|
+
for (const rigid of [false, true]) {
|
|
1818
|
+
const make = (source) => {
|
|
1819
|
+
const clock = { t: 5000, now: () => clock.t };
|
|
1820
|
+
const canvas = makeCanvas();
|
|
1821
|
+
const window = makeWindow();
|
|
1822
|
+
const runtime = { keys: new Set(), cameraFollowsRigidly: rigid, cameraControl: null };
|
|
1823
|
+
const detach = makeAttachInput(clock, source, window)(canvas, runtime, { debugLines: { visible: false } });
|
|
1824
|
+
return { clock, canvas, window, runtime, detach };
|
|
1825
|
+
};
|
|
1826
|
+
const now = make(ATTACH_NOW);
|
|
1827
|
+
const before = make(ATTACH_BEFORE);
|
|
1828
|
+
const seen = (h) => ({
|
|
1829
|
+
stick: { x: h.runtime.moveInput.x, y: h.runtime.moveInput.y, active: h.runtime.moveInput.active },
|
|
1830
|
+
jumped: h.runtime.moveInput.consumeJump(),
|
|
1831
|
+
keys: [...h.runtime.keys].sort(),
|
|
1832
|
+
canvas: h.canvas.types(),
|
|
1833
|
+
window: h.window.types(),
|
|
1834
|
+
focused: h.canvas.focused,
|
|
1835
|
+
captured: [...h.canvas.captured],
|
|
1836
|
+
lookInput: h.runtime.lookInput,
|
|
1837
|
+
});
|
|
1838
|
+
assert.deepEqual(seen(now), seen(before), 'attached');
|
|
1839
|
+
let seed = 7;
|
|
1840
|
+
const rnd = () => (seed = (seed * 1103515245 + 12345) & 0x7fffffff) / 0x7fffffff;
|
|
1841
|
+
const down = new Map();
|
|
1842
|
+
for (let i = 0; i < 6000; i += 1) {
|
|
1843
|
+
const r = rnd();
|
|
1844
|
+
let type;
|
|
1845
|
+
let event;
|
|
1846
|
+
const id = 1 + Math.floor(rnd() * 4);
|
|
1847
|
+
if (r < 0.06) {
|
|
1848
|
+
const code = ['KeyW', 'KeyA', 'Space', 'KeyP', 'ArrowLeft'][Math.floor(rnd() * 5)];
|
|
1849
|
+
type = rnd() < 0.5 ? 'keydown' : 'keyup';
|
|
1850
|
+
event = { code, key: code.toLowerCase(), repeat: rnd() < 0.2, target: rnd() < 0.5 ? 'canvas' : null };
|
|
1851
|
+
} else if (r < 0.2 && !down.has(id)) {
|
|
1852
|
+
type = 'pointerdown';
|
|
1853
|
+
down.set(id, { x: 100 + rnd() * 800, y: 50 + rnd() * 500 });
|
|
1854
|
+
} else if (r < 0.3 && down.has(id)) {
|
|
1855
|
+
type = rnd() < 0.15 ? 'pointercancel' : 'pointerup';
|
|
1856
|
+
} else {
|
|
1857
|
+
type = 'pointermove';
|
|
1858
|
+
}
|
|
1859
|
+
// Half the moves stay within 20 px of where the pointer went down, so
|
|
1860
|
+
// the tap slop and the stick's radius are crossed both ways.
|
|
1861
|
+
const at = down.get(id) ?? { x: 500, y: 300 };
|
|
1862
|
+
const near = rnd() < 0.5;
|
|
1863
|
+
const x = near ? at.x + (rnd() - 0.5) * 40 : 100 + rnd() * 800;
|
|
1864
|
+
const y = near ? at.y + (rnd() - 0.5) * 40 : 50 + rnd() * 500;
|
|
1865
|
+
event ??= { pointerId: id, clientX: type === 'pointerdown' ? at.x : x, clientY: type === 'pointerdown' ? at.y : y, pointerType: rnd() < 0.2 ? 'mouse' : 'touch', button: rnd() < 0.8 ? 0 : 2 };
|
|
1866
|
+
if (type === 'pointerup' || type === 'pointercancel') down.delete(id);
|
|
1867
|
+
const step = rnd() * 400;
|
|
1868
|
+
for (const h of [now, before]) {
|
|
1869
|
+
h.clock.t += step;
|
|
1870
|
+
const target = type.startsWith('key') ? h.window : h.canvas;
|
|
1871
|
+
target.fire(type, { ...event, target: event.target === 'canvas' ? h.canvas : null });
|
|
1872
|
+
}
|
|
1873
|
+
assert.deepEqual(seen(now), seen(before), `${rigid ? 'rigid' : 'no key'}: event ${i} (${type})`);
|
|
1874
|
+
}
|
|
1875
|
+
now.detach();
|
|
1876
|
+
before.detach();
|
|
1877
|
+
assert.deepEqual(seen(now), seen(before), 'detached');
|
|
1878
|
+
assert.deepEqual(now.canvas.types(), []);
|
|
1879
|
+
assert.deepEqual(now.window.types(), []);
|
|
1880
|
+
checks += 1;
|
|
1881
|
+
}
|
|
1882
|
+
return checks;
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
// A tap taken as a jump and not yet used carries across a load, with the key
|
|
1886
|
+
// or without it, as it always has; and a runtime that has met the key keeps
|
|
1887
|
+
// that for its later keyless loads too.
|
|
1888
|
+
function tapAcrossLoad() {
|
|
1889
|
+
let checks = 0;
|
|
1890
|
+
{
|
|
1891
|
+
const h = makeRuntime('orbit');
|
|
1892
|
+
h.run(30);
|
|
1893
|
+
h.down(1, 800, 300);
|
|
1894
|
+
h.up(1, 800, 300);
|
|
1895
|
+
h.load('orbit');
|
|
1896
|
+
assert.equal(h.jumped(), true, 'orbit: the tap is kept across the load');
|
|
1897
|
+
checks += 1;
|
|
1898
|
+
}
|
|
1899
|
+
for (const mode of ['off', 'rigid']) {
|
|
1900
|
+
const h = makeRuntime(mode);
|
|
1901
|
+
h.run(30);
|
|
1902
|
+
h.down(1, 300, 300);
|
|
1903
|
+
h.up(1, 300, 300);
|
|
1904
|
+
h.load(mode);
|
|
1905
|
+
assert.equal(h.jumped(), true, `${mode}: a keyless load keeps the tap`);
|
|
1906
|
+
checks += 1;
|
|
1907
|
+
}
|
|
1908
|
+
for (const mode of ['off', 'rigid']) {
|
|
1909
|
+
const h = makeRuntime('orbit');
|
|
1910
|
+
h.run(10);
|
|
1911
|
+
h.down(1, 800, 300);
|
|
1912
|
+
h.move(1, 900, 300);
|
|
1913
|
+
h.up(1, 900, 300);
|
|
1914
|
+
h.load(mode);
|
|
1915
|
+
h.run(10);
|
|
1916
|
+
h.down(2, 300, 300);
|
|
1917
|
+
h.up(2, 300, 300);
|
|
1918
|
+
h.load(mode);
|
|
1919
|
+
assert.equal(h.jumped(), true, `orbit then ${mode}: a later keyless load keeps the tap`);
|
|
1920
|
+
checks += 1;
|
|
1921
|
+
}
|
|
1922
|
+
return checks;
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
// The engine resets only the look input orbitinput.js made; something else
|
|
1926
|
+
// on `runtime.lookInput` is left alone. A facing asked for before a load is
|
|
1927
|
+
// dropped on every load, whether or not orbit input was ever made.
|
|
1928
|
+
function ownInputOnly() {
|
|
1929
|
+
let checks = 0;
|
|
1930
|
+
const system = new Physics3DSystem();
|
|
1931
|
+
let reset = 0;
|
|
1932
|
+
const foreign = { reset: () => (reset += 1) };
|
|
1933
|
+
const runtime = { lookInput: foreign };
|
|
1934
|
+
system.reset(runtime);
|
|
1935
|
+
assert.equal(reset, 0, "a look input of someone else's is not reset");
|
|
1936
|
+
checks += 1;
|
|
1937
|
+
const bare = { faceRequest: 1.1 };
|
|
1938
|
+
system.reset(bare);
|
|
1939
|
+
assert.equal(bare.faceRequest, undefined, 'a request is dropped with no orbit input made');
|
|
1940
|
+
checks += 1;
|
|
1941
|
+
const h = makeRuntime('orbit');
|
|
1942
|
+
h.run(5);
|
|
1943
|
+
h.runtime.faceRequest = 0.5;
|
|
1944
|
+
h.load('orbit');
|
|
1945
|
+
assert.equal(h.runtime.faceRequest, undefined, 'a request made before a load is dropped');
|
|
1946
|
+
h.frame();
|
|
1947
|
+
assert.ok(Math.abs(h.player().components.Transform.rotationY - (Math.PI - h.runtime.persistent.camAngle) / DEG) < 1e-9, 'and the new scene faces away from the camera');
|
|
1948
|
+
checks += 1;
|
|
1949
|
+
const k = makeRuntime('orbit');
|
|
1950
|
+
k.run(5);
|
|
1951
|
+
k.runtime.faceRequest = 1.1;
|
|
1952
|
+
k.load('off');
|
|
1953
|
+
k.load('orbit');
|
|
1954
|
+
k.frame();
|
|
1955
|
+
assert.ok(Math.abs(k.player().components.Transform.rotationY - (Math.PI - k.runtime.persistent.camAngle) / DEG) < 1e-9, 'orbit, off, orbit: a stale request does not turn the later player');
|
|
1956
|
+
checks += 1;
|
|
1957
|
+
return checks;
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
// A Player turned off by a collision callback, after its update and before
|
|
1961
|
+
// the camera is placed, and on again the next frame: no placement on that
|
|
1962
|
+
// frame, so a drag made meanwhile is not replayed.
|
|
1963
|
+
function offByCallback() {
|
|
1964
|
+
let checks = 0;
|
|
1965
|
+
const h = makeRuntime('orbit');
|
|
1966
|
+
h.run(90);
|
|
1967
|
+
const yaw = h.runtime.persistent.camAngle;
|
|
1968
|
+
const props = h.player().components.Player;
|
|
1969
|
+
h.down(1, 800, 300);
|
|
1970
|
+
h.move(1, 840, 300);
|
|
1971
|
+
h.move(1, 960, 300);
|
|
1972
|
+
h.callback = () => {
|
|
1973
|
+
props.$enabled = false;
|
|
1974
|
+
h.callback = null;
|
|
1975
|
+
};
|
|
1976
|
+
h.frame();
|
|
1977
|
+
props.$enabled = true;
|
|
1978
|
+
h.frame();
|
|
1979
|
+
assert.equal(h.runtime.persistent.camAngle, yaw, 'the drag made before the callback is dropped');
|
|
1980
|
+
h.move(1, 1000, 300);
|
|
1981
|
+
h.frame();
|
|
1982
|
+
assert.ok(Math.abs(h.runtime.persistent.camAngle - (yaw + 40 * LOOK_RAD_PER_PX)) < 1e-12, 'and the next drag turns the camera by itself');
|
|
1983
|
+
checks += 1;
|
|
1984
|
+
return checks;
|
|
1985
|
+
}
|
|
1986
|
+
|
|
1987
|
+
// The point drawn while a takeover blend lasts is collided too: a camera
|
|
1988
|
+
// left 8 m out, behind a wall 4 m from the look point, is never drawn past
|
|
1989
|
+
// the wall, and eases in from where it is drawn without a pop.
|
|
1990
|
+
function blendHeld() {
|
|
1991
|
+
let checks = 0;
|
|
1992
|
+
const world = makeWorld('orbit');
|
|
1993
|
+
world.run(90);
|
|
1994
|
+
world.frame();
|
|
1995
|
+
const from = { ...world.lastCast.from };
|
|
1996
|
+
const dir = { ...world.lastCast.dir };
|
|
1997
|
+
world.wall = 4;
|
|
1998
|
+
world.camera.position.set(from.x + dir.x * 8, from.y + dir.y * 8, from.z + dir.z * 8);
|
|
1999
|
+
const out = () => {
|
|
2000
|
+
const c = world.camera.position;
|
|
2001
|
+
const f = world.lastCast.from;
|
|
2002
|
+
return Math.hypot(c.x - f.x, c.y - f.y, c.z - f.z);
|
|
2003
|
+
};
|
|
2004
|
+
let last = Infinity;
|
|
2005
|
+
for (let i = 0; i < 60; i += 1) {
|
|
2006
|
+
world.frame(UNEVEN(i));
|
|
2007
|
+
const now = out();
|
|
2008
|
+
assert.ok(now <= 3.75 + 1e-9, `frame ${i}: drawn no farther than the wall allows (${now})`);
|
|
2009
|
+
assert.ok(now <= last + 1e-9, `frame ${i}: and only nearer (${now} after ${last})`);
|
|
2010
|
+
last = now;
|
|
2011
|
+
}
|
|
2012
|
+
assert.ok(Math.abs(out() - 3.75) < 1e-3, 'settled in front of the wall');
|
|
2013
|
+
// A blend from beside the wall's ray, not along it, is held too.
|
|
2014
|
+
world.camera.position.set(from.x + 6, from.y + 3, from.z - 5);
|
|
2015
|
+
for (let i = 0; i < 30; i += 1) {
|
|
2016
|
+
world.frame();
|
|
2017
|
+
assert.ok(out() <= 3.75 + 1e-9, `beside: frame ${i} (${out()})`);
|
|
2018
|
+
}
|
|
2019
|
+
checks += 1;
|
|
2020
|
+
return checks;
|
|
2021
|
+
}
|
|
2022
|
+
|
|
2023
|
+
// `castRayFixed` on rapier itself: fixed bodies' solid colliders only.
|
|
2024
|
+
async function fixedRay() {
|
|
2025
|
+
if (!realRapier) return 0;
|
|
2026
|
+
const { Physics3DSystem } = await import('../systems/physics3d.js?orbit-real');
|
|
2027
|
+
const box = (id, x, more = {}) => ({
|
|
2028
|
+
id,
|
|
2029
|
+
components: {
|
|
2030
|
+
Transform: { x, y: 5, z: 0, rotationX: 0, rotationY: 0, rotationZ: 0 },
|
|
2031
|
+
Shape: { kind: 'box', width: 1, height: 4, depth: 4, solid: true, ...more.shape },
|
|
2032
|
+
...(more.body ? { Body: { gravityScale: 0, lockRotations: true, ...more.body } } : {}),
|
|
2033
|
+
},
|
|
2034
|
+
runtime: {},
|
|
2035
|
+
});
|
|
2036
|
+
const actors = [
|
|
2037
|
+
box('sensor', 2, { shape: { solid: false } }),
|
|
2038
|
+
box('dynamic', 4, { body: { type: 'dynamic' } }),
|
|
2039
|
+
box('kinematic', 6, { body: { type: 'kinematic' } }),
|
|
2040
|
+
box('wall', 8),
|
|
2041
|
+
box('behind', 12),
|
|
2042
|
+
];
|
|
2043
|
+
const scene = { time: 0, data: {}, getActors: () => actors, getActor: (id) => actors.find((a) => a.id === id), forEachBehavior: () => {} };
|
|
2044
|
+
const system = new Physics3DSystem();
|
|
2045
|
+
system.attach(scene);
|
|
2046
|
+
assert.equal(scene.physics3d.castRayFixed({ x: 0, y: 5, z: 0 }, { x: 1, y: 0, z: 0 }, 20), null, 'null before there is a world');
|
|
2047
|
+
for (let i = 0; i < 200 && !scene.physics3d.ready(); i += 1) {
|
|
2048
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
2049
|
+
scene.time += FIXED;
|
|
2050
|
+
system.afterBehaviors(scene, FIXED);
|
|
2051
|
+
}
|
|
2052
|
+
assert.ok(scene.physics3d.ready(), 'rapier loaded');
|
|
2053
|
+
for (let i = 0; i < 3; i += 1) system.afterBehaviors(scene, FIXED);
|
|
2054
|
+
const from = { x: 0, y: 5, z: 0 };
|
|
2055
|
+
const dir = { x: 1, y: 0, z: 0 };
|
|
2056
|
+
const any = scene.physics3d.castRay(from, dir, 20);
|
|
2057
|
+
assert.equal(any.actorId, 'dynamic', 'castRay stops at the first solid, as it did');
|
|
2058
|
+
assert.ok(Math.abs(any.distance - 3.5) < 1e-3);
|
|
2059
|
+
const hit = scene.physics3d.castRayFixed(from, dir, 20);
|
|
2060
|
+
assert.equal(hit.actorId, 'wall', 'castRayFixed passes the sensor, the dynamic and the kinematic body');
|
|
2061
|
+
assert.ok(Math.abs(hit.distance - 7.5) < 1e-3, `at the wall's face (${hit.distance})`);
|
|
2062
|
+
assert.equal(scene.physics3d.castRayFixed(from, dir, 7), null, 'nothing within reach');
|
|
2063
|
+
assert.equal(scene.physics3d.castRayFixed(from, { x: -1, y: 0, z: 0 }, 20), null, 'nothing that way');
|
|
2064
|
+
// The ground is fixed and solid.
|
|
2065
|
+
const down = scene.physics3d.castRayFixed(from, { x: 0, y: -1, z: 0 }, 20);
|
|
2066
|
+
assert.ok(down && down.actorId === null && Math.abs(down.distance - 5) < 1e-3, 'the ground blocks');
|
|
2067
|
+
// From inside a wall: at once.
|
|
2068
|
+
assert.equal(scene.physics3d.castRayFixed({ x: 8, y: 5, z: 0 }, dir, 20).distance, 0);
|
|
2069
|
+
system.dispose(scene);
|
|
2070
|
+
assert.equal(scene.physics3d.castRayFixed(from, dir, 20), null, 'null after the world is gone');
|
|
2071
|
+
return 1;
|
|
2072
|
+
}
|
|
2073
|
+
|
|
2074
|
+
export async function run() {
|
|
2075
|
+
const parts = {
|
|
2076
|
+
unchanged: unkeyedIsUnchanged(),
|
|
2077
|
+
cold: coldKeyless(),
|
|
2078
|
+
loads: acrossLoads(),
|
|
2079
|
+
offAndOn: offAndOn(),
|
|
2080
|
+
lockedTrip: lockedTrip(),
|
|
2081
|
+
locking: locking(),
|
|
2082
|
+
inputUnchanged: inputUnchanged(),
|
|
2083
|
+
tapAcrossLoad: tapAcrossLoad(),
|
|
2084
|
+
ownInputOnly: ownInputOnly(),
|
|
2085
|
+
offByCallback: offByCallback(),
|
|
2086
|
+
blendHeld: blendHeld(),
|
|
2087
|
+
walking: walking(),
|
|
2088
|
+
looking: looking(),
|
|
2089
|
+
blocked: blocked(),
|
|
2090
|
+
facing: facing(),
|
|
2091
|
+
takenOver: takenOver(),
|
|
2092
|
+
roles: pointerRoles(),
|
|
2093
|
+
listeners: listeners(),
|
|
2094
|
+
rapier: await fixedRay(),
|
|
2095
|
+
};
|
|
2096
|
+
const checks = Object.values(parts).reduce((a, b) => a + b, 0);
|
|
2097
|
+
return { checks, real: parts.rapier > 0 };
|
|
2098
|
+
}
|
|
2099
|
+
|
|
2100
|
+
export const summary = ({ checks, real }) =>
|
|
2101
|
+
`${checks} orbit-camera checks (${real ? 'castRayFixed on rapier itself' : 'rapier itself is not installed here, so castRayFixed was not run on it'})`;
|
|
2102
|
+
|
|
2103
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
2104
|
+
console.log(`camera orbit: ${summary(await run())}`);
|
|
2105
|
+
}
|