PF 102 · Event-Driven Programming · Pre-final
Discussion
01 / 14
edp-lab/js/events.js
Enter the Building
Pre-final 01 — Midterm made the island yours. Now you step inside it.
PF 102 · Pre-final Discussion Same edp-lab repo Instructor: John Micheal Acera
Where you are
Same island. Next wall.
PRELIM
Fire → Listen → Handle
addEventListener, event names, the event object, a first look at bubbling.
MIDTERM
Make the island yours
Click selects, hover previews, R resets, B night mode, your own buildings and colors.
PRE-FINAL
Step inside something you made
Double-click a building → you are in it. Esc → back to the island.
You do not restart. You keep your midterm island and teach it one new trick.
Live demo
Try it with me
You doYou see
Click a buildingIt turns gold, HUD shows its name (same as midterm)
Double-click the Cafe (it has a door)You are inside · badge says Inside: Cafe
Move the mouseYou look around the room
Click a tableHUD names the prop
Press EscBack on the island · badge says Island
Double-click the Church → click Exit buildingDifferent room · same way out
The new problem
Where am I — and which listeners still make sense?
Inside the Cafe, should R reset island buildings? Should the camera keep orbiting? Should hover paint buildings you can't see?
  • The browser keeps firing every event, no matter where you are
  • So your handlers have to know where you are
  • That "where am I" is called state
The big picture
One app · two modes

mode = 'island'

Camera orbits · hover highlights · click selects · R reset · B night

dblclick a door → enterInterior()

mode = 'inside'

Mouse looks around · click names a prop · R/B do nothing

Esc or Exit button → exitInterior()

One page, one scene, one camera. "Entering" just moves the camera into a room built far away.
The whole trick
One variable decides everything
let mode = 'island'; // 'island' | 'inside' let currentInterior = null; // which room you are in
  • let because the value changes when you enter and leave
  • A word ('inside') reads like English — if (mode === 'inside')
  • Every handler reads mode first, then decides what to do
Fire → Listen → Handle
Three event paths change the mode
Fire (event)Listen (main.js)Handle (events.js)World state
dblclick a building with a doorcanvas 'dblclick'enterInterior(building)mode = 'inside'
keydown Escapewindow 'keydown'exitInterior()mode = 'island'
click Exit button#exit-btn 'click'exitInterior()mode = 'island'
The Exit button is an HTML button on top of the canvas, not part of the 3D scene. Remember that for Pre-final 03.
Listeners that respect the mode
Same handler · different job
HandlerOn the islandInside a building
onClickselect + show namename the prop you clicked
onDoubleClickenter if it has a door, else detailsdoes nothing
onKeyDownR reset · B nightonly Esc works
animate()orbit + hoverlook around with the mouse
onMouseMovesame both ways — it only records where the mouse is
Pattern · Guard clause
State guards the handler
function enterInterior(building) { if (mode !== 'island') return; // already inside? do nothing // ... switch the world }
  • The event still fires — you cannot stop a user from clicking
  • The handler checks the state and leaves early with return
  • Check the bad case first, one line, done
Watch the Event trace
A double-click is really three events
click→ click→ dblclick
  • First two clicks run onClick → the Cafe gets selected (gold)
  • Then dblclick runs onDoubleClick → enterInterior()
  • enterInterior cleans up that selection before you go in
  • That's the browser's rule, not a bug
Rule
No second HTML page
Linking to index-interior.html is not "entering". It reloads the page and skips every event.
  • No handler, no mode, no guard = no event-driven programming
  • Enter and exit must be handlers + a mode flag in the same page
  • Boxes + a light + a sign are enough. We grade events, not 3D art
Your pre-final scope
One building. Done well.
  • ✓Minimum: 1 enterable building of yours · enter on double-click · exit on Esc · mode visible in the badge/HUD
  • +Stretch: 2nd building with a different room · Exit button that doesn't break picking · a prop that reacts to click
  • ✗Not graded: photoreal rooms, textures, Blender
Campus ideas: ferry terminal, canteen, gym, chapel, library, clinic.
Quick check
Answer out loud
  • 1Which three events can change mode?
  • 2What happens if you call enterInterior() while already inside?
  • 3Why doesn't onMouseMove need to check mode?
Next
Pre-final 02 — Wire the doors
  • →Pick your building to make enterable
  • →Sketch 3–6 props for its room (boxes are fine)
  • →Make sure your midterm island still works
  • →Read the guide: EXAMPLE-enter-building-walkthrough.md
One app. Two modes. Listeners that respect the mode. Questions?
note //