## What was done - agents.js: spring physics (stiffness=7, damping=0.8, clamp ±0.44 rad) on timmy.group.rotation.x/z via slapOffset/slapVelocity integrated per-frame with proper dt (capped at 50ms for tab-background safety) - agents.js: applySlap(hitPoint) — computes XZ impulse direction from hit point relative to TIMMY_POS, adds angular velocity, triggers pip startle + crystal flash - agents.js: _playBoing() — lazy AudioContext, sine oscillator 260→90 Hz with exponential gain decay (0.38s) - agents.js: Pip startle — 3s decay timer, random scatter direction offset, 4x spin speed while startled, boosted pip light intensity - agents.js: Crystal ball hit flash — hitFlashTimer=0.5s, intensity spikes to 10 and fades; normal crystalLight/cbMat logic when not flashing - agents.js: getTimmyGroup() export for raycaster target - interaction.js: registerSlapTarget(group, applyFn) — stores targets - interaction.js: _onPointerDown capture-phase listener — raycasts against timmyGroup recursively, calls applySlap on hit, suppresses OrbitControls drag for 220ms via stopImmediatePropagation + controls.enabled toggle - main.js: imports getTimmyGroup, applySlap, registerSlapTarget; wires registerSlapTarget(getTimmyGroup(), applySlap) after initInteraction ## Verification - Vite build: clean, 14 modules, 0 errors - /tower HTTP 200 - Testkit: 27/27 PASS
88 lines
2.8 KiB
JavaScript
88 lines
2.8 KiB
JavaScript
import * as THREE from 'three';
|
|
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
|
|
let controls;
|
|
let _canvas;
|
|
let _camera = null;
|
|
let _timmyGroup = null;
|
|
let _applySlap = null;
|
|
const _raycaster = new THREE.Raycaster();
|
|
const _pointer = new THREE.Vector2();
|
|
const _noCtxMenu = e => e.preventDefault();
|
|
|
|
// ── registerSlapTarget ────────────────────────────────────────────────────────
|
|
// Call after initAgents() with Timmy's group and the applySlap function.
|
|
// The capture-phase pointerdown listener will hit-test against the group and
|
|
// call applySlap(hitPoint) — also suppressing the OrbitControls drag.
|
|
|
|
export function registerSlapTarget(timmyGroup, applyFn) {
|
|
_timmyGroup = timmyGroup;
|
|
_applySlap = applyFn;
|
|
}
|
|
|
|
export function initInteraction(camera, renderer) {
|
|
_camera = camera;
|
|
|
|
controls = new OrbitControls(camera, renderer.domElement);
|
|
controls.enableDamping = true;
|
|
controls.dampingFactor = 0.05;
|
|
controls.screenSpacePanning = false;
|
|
controls.minDistance = 5;
|
|
controls.maxDistance = 80;
|
|
controls.maxPolarAngle = Math.PI / 2.1;
|
|
controls.target.set(0, 0, 0);
|
|
controls.update();
|
|
|
|
_canvas = renderer.domElement;
|
|
_canvas.addEventListener('contextmenu', _noCtxMenu);
|
|
|
|
// Capture phase so we intercept before OrbitControls' bubble-phase handler.
|
|
// If Timmy is hit we call stopImmediatePropagation() to suppress the orbit drag.
|
|
_canvas.addEventListener('pointerdown', _onPointerDown, { capture: true });
|
|
}
|
|
|
|
function _onPointerDown(event) {
|
|
if (!_timmyGroup || !_applySlap || !_camera) return;
|
|
|
|
const rect = _canvas.getBoundingClientRect();
|
|
_pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
|
|
_pointer.y = ((event.clientY - rect.top) / rect.height) * -2 + 1;
|
|
|
|
_raycaster.setFromCamera(_pointer, _camera);
|
|
const hits = _raycaster.intersectObject(_timmyGroup, true);
|
|
|
|
if (hits.length > 0) {
|
|
event.stopImmediatePropagation(); // block OrbitControls drag
|
|
_applySlap(hits[0].point);
|
|
|
|
// Re-enable orbit after a short pause to avoid accidental rotation
|
|
if (controls) {
|
|
controls.enabled = false;
|
|
setTimeout(() => { if (controls) controls.enabled = true; }, 220);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function updateControls() {
|
|
if (controls) controls.update();
|
|
}
|
|
|
|
/**
|
|
* Dispose OrbitControls event listeners.
|
|
* Called before context-loss teardown.
|
|
*/
|
|
export function disposeInteraction() {
|
|
if (_canvas) {
|
|
_canvas.removeEventListener('contextmenu', _noCtxMenu);
|
|
_canvas.removeEventListener('pointerdown', _onPointerDown, { capture: true });
|
|
_canvas = null;
|
|
}
|
|
if (controls) {
|
|
controls.dispose();
|
|
controls = null;
|
|
}
|
|
_camera = null;
|
|
_timmyGroup = null;
|
|
_applySlap = null;
|
|
}
|