ECHO GARDEN / STUDY / LEVEL 1 OF 7

One garden,
many messages.

The whole world of Echo Garden is one small object: {seeds: {id: {pos, owner, color}}}. Nobody edits it directly. Every change, yours or your partner's, is written as a message such as {op: 'plant', id, owner, color, pos}, and every headset applies the same messages with the same function, applyGardenMessage. Same messages in, same garden out. This page runs the game's own echoGardenRules.js.

a clickthe trigger, on an empty spot of the panel
→
a message{op:'plant', id, owner, color, pos}
→
applyGardenMessagechecks it, then changes the garden, or refuses
→
the picturedrawn from the garden every frame

The panel, from the front

click to plant · click a note to select it

Bigger notes are pulled toward you (level 3 shows how). The panel is 1.64 m wide, from x = −0.82 to 0.82 m, and 0.88 m tall, from y = 1.18 to 2.06 m above the floor.

Send messages

Messages, newest first: did applyGardenMessage accept them?

window.echoGarden right now

The real code

echoGardenRules.js

The rules file has no graphics, controllers or network: it only knows how a message changes a garden. That is why this page, a test file, and both headsets can all run it. gardenPosition clamps every position into the garden; applyGardenMessage refuses a message that makes no sense and returns false.


      

Where the scene makes messages

echoGarden.js

A click becomes plant(...); dispatch applies the message here and sends it (level 7 follows it across the network). The id joins the owner, the time and a counter, so two players clicking at the same moment never make the same id.


      
    

What you should be able to explain

  1. The state is small and shared. Only what the other player also needs goes into window.echoGarden: where each note is, who planted it and its colour. The 3D objects that draw a note, the sound, and what your hand is holding stay outside it, on each headset.
  2. Messages, not edits. A message describes one change. Every headset runs the same applyGardenMessage on the same messages, so they end up with the same garden. This is the message-passing style of the course's construct.js.
  3. Absolute values. A move says "note 7 is here", not "move note 7 up 10 cm". Applying it twice gives the same result, so a message that arrives twice does no harm. (Press "apply the last message again".) The plant of an existing id is refused for the same reason.
  4. Refuse what makes no sense. The rules check every message: a position is clamped into the panel, the 25th note is refused, an unknown id cannot be moved. A message comes from another computer, so the rules never trust it blindly.

Exercises: ① Plant a note, select it, press "move up" several times, then "apply the last message again". Why does nothing change? ② Imagine moves were sent as "+10 cm". What would go wrong if one arrived twice? ③ Press "plant 30 notes": how many are accepted, and which line of the rules stops the rest?