edp-lab/js/events.js
Wire the Doors
Pre-final 02 — double-click to enter, Esc to leave. You type the events.
PF 102 · Pre-final Hands-on
Your edp-lab fork
~90 minutes
Today
Nine small steps
0 · Provided parts→
1–2 · Tag + room→
3–6 · Enter→
7–8 · Exit→
9 · Look around
- Follow along on the hands-on page — every step has code you can copy and a checkbox
- Save → refresh → test after every step. Don't write five steps then refresh
- F12 Console open the whole time — red text means stop and fix
Step 0 · Copy the provided parts
Boxes are provided. Events are yours.
| File | Copy this in | Why it's provided |
index.html | mode badge, Exit button, Event trace panel | Plain HTML |
css/style.css | the PRE-FINAL block at the bottom | Styling, not EDP |
js/scene.js | makeInterior() helper + 3 element lookups | Three.js boxes, not EDP |
Everything in events.js and main.js today you type yourself.
Step 1 · scene.js
Tag a building as enterable
{ name: 'Cafe', color: 0xfbbf24, x: -10, z: 8, w: 3, h: 2.8, d: 3, interior: 'cafe' },
- Add
interior: 'yourKey' to one entry in spots
- The loop stores it in
mesh.userData.interior and adds a dark door
- Refresh → you should see a door on that building. Nothing else changes yet
Step 2 · scene.js
Build a crude room
makeInterior('cafe', {
name: 'Cafe',
x: 400, // far away — a different x for every room (400, -400, 800…)
wall: 0xfde68a,
floor: 0x92400e,
props: [
{ name: 'Counter', color: 0x78350f, x: 0, z: -5, w: 8, h: 1.2, d: 1.4 },
{ name: 'Table 1', color: 0xf59e0b, x: -4, z: 0, w: 2.4, h: 0.9, d: 2.4 },
],
});
- The key
'cafe' must match the interior: tag from Step 1 exactly
- Props use room coordinates:
x and z from about −7 to 7
- Room map, free slots, prop recipes: EXAMPLE-add-interior-walkthrough.md
Step 3 · events.js
Add the mode flag
let mode = 'island'; // 'island' | 'inside'
let currentInterior = null;
Put it near the top, under let selected and let hovered, so every handler below can read it.
Step 4 · events.js
Write enterInterior()
function enterInterior(building) {
if (mode !== 'island') return; // guard
const interior = edpInteriors[building.userData.interior];
if (!interior) return; // typo in the key?
// clean up midterm selection / hover, then:
mode = 'inside';
currentInterior = interior;
edpCamera.position.copy(interior.eye); // step into the room
edpCamera.lookAt(interior.lookAt);
edpSunSprite.visible = false; // no sun through the walls
edpModeBadge.textContent = 'Inside: ' + interior.name;
edpExitButton.hidden = false;
}
Step 5 · events.js · dblclick
Double-click a door → enter
function onDoubleClick(event) {
logEvent(event, 'onDoubleClick');
if (mode === 'inside') return;
edpRaycaster.setFromCamera(edpMouse, edpCamera);
const hits = edpRaycaster.intersectObjects(edpBuildings, false);
if (!hits.length) return;
const building = hits[0].object;
if (building.userData.interior) {
enterInterior(building);
return;
}
edpHud.innerHTML =
'<strong>' + building.userData.name + ' · details</strong>' +
'Double-click → <code>dblclick</code> listener → handler → HUD update.<br>' +
'<em>Same Fire → Listen → Handle pattern as click.</em>';
}
globalThis.onDoubleClick = onDoubleClick;
- Type the whole function — replace your midterm version
logEvent is Lesson 3 — comment that line until then if the console complains
Refresh → double-click your building. You should be inside. ✔ Commit: "Enter on double-click"
Step 6 · events.js · click
Single click: same listener, new job inside
function onClick(event) {
if (mode === 'inside') {
inspectInteriorProp(); // raycast against currentInterior.props
return;
}
// ... midterm select + name, unchanged ...
}
- On the island, click still selects and shows the name — don't break it
- Inside, the laser (raycaster) tests props instead of buildings
Step 7 · events.js · keydown
exitInterior() + Escape
function exitInterior() {
if (mode !== 'inside') return;
mode = 'island';
currentInterior = null;
edpSunSprite.visible = !nightMode;
edpModeBadge.textContent = 'Island';
edpExitButton.hidden = true;
}
// top of onKeyDown
if (event.code === 'Escape') {
event.preventDefault();
exitInterior();
return;
}
if (mode === 'inside') return; // R, B = island only
Step 8 · main.js
Listen to the Exit button
edpExitButton.addEventListener('click', onExitButtonClick);
function onExitButtonClick(event) { // in events.js
exitInterior();
}
- Pass
onExitButtonClick with no () — with () it runs once at page load
- No raycasting: the browser already knows which HTML element you clicked
- ✔ Commit: "Exit interior on Escape and HUD button"
Step 9 · main.js · animate()
Stop orbiting while inside
function animate() {
requestAnimationFrame(animate); // next frame is booked first
if (mode === 'inside') {
lookAroundInterior();
edpRenderer.render(edpScene, edpCamera);
return; // skip orbit + hover
}
// ... orbit, hover, birds (midterm) ...
}
Step 9 · events.js
Turn your head with the mouse
function lookAroundInterior() {
const yaw = edpMouse.x * 0.7; // left/right, about ±40°
const pitch = edpMouse.y * 0.25; // up/down
const eye = currentInterior.eye;
edpCamera.position.copy(eye); // stay standing in the room
edpCamera.lookAt(
eye.x + Math.sin(yaw) * 10,
eye.y + pitch * 10,
eye.z - Math.cos(yaw) * 10
);
}
- Type the whole function —
eye and position.copy are required, not optional
onMouseMove only records the mouse — animate() calls this
- sin/cos give a point on a circle around your eyes; the camera looks at it
Real bug · one character
"The camera doesn't follow my mouse"
Buggy
const yaw = -edpMouse.x * 0.7;
Mouse left → camera turns right by the same angle. They cancel. The cursor stays stuck on the same spot on the wall.
Fixed
const yaw = edpMouse.x * 0.7;
Mouse left → camera turns left. At the edge you can see the side walls.
Test by moving slowly and asking: "does it go where I point?" Measure, don't guess.
Test checklist
Can a classmate do all of this?
- ✓Single click on the island still selects + names
- ✓Double-click your door → inside · badge shows the room name
- ✓Double-click inside does not re-enter · R and B do nothing inside
- ✓Esc and the Exit button both bring you back · camera orbits again
- ✓Console has no red errors
Next
Pre-final 03 — Who ate the Exit click?
- →Push your commits: enter path, exit path
- →Keep the Event trace panel — we break the Exit button on purpose next time
- →Confused by a line? CODE-EXPLAINED-enter-building.md
You wired two doors: one in, two out. Questions?