This lesson was updated. Reset to the new version?
This browser isn't keeping your edits, so they're lost when you leave the page.
Zelda-2 joins four rooms into a small dungeon. Walk into a doorway and the camera pans to the next room, the way it does in the original games, while Link walks in through the far side.
Exits
Rooms 2–4 join room 1 in src/config/rooms.js, and every room gains an exits dictionary. Each exit names the room it leads to, where Link should end up, and the passage: the floor tiles in the gap in the wall.
12exits: {13 right: {14 room: "room2",15 spawnX: 16,16 spawnY: 32,17 passage: { x: 144, y: 48, width: 16, height: 32 },18 },19 left: { room: "room4", spawnX: 128, spawnY: 48 },The passage is a rectangle in room pixels, one tile deep, covering the doorway. Room 1’s left exit has no passage, so that wall stays solid: the room it leads to is ready for a feature that opens it later.
Dungeon.getExitReachedBy() decides whether Link is leaving. He has to be facing the exit and his hurtbox has to be completely inside its passage:
119const exit = this.currentRoom.definition.exits[direction];120const passage = getPassage(this.currentRoom, direction);121
122if (!exit || !passage || !isInside(player.getHurtbox(), passage)) {123 return undefined;124}Only Idle and Walk let Link leave. PlayerState adds a canLeaveRoom getter that returns false, and those two states override it to true, so any action added later can’t wander out of the room by accident.
Panning
When Link reaches an exit, Dungeon.tryPan() builds the next room and places it one room away on that side, just off screen:
24const ROOM_OFFSET = {25 left: { x: -ROOM_WIDTH, y: 0 },26 right: { x: ROOM_WIDTH, y: 0 },27 up: { x: 0, y: -ROOM_HEIGHT },28 down: { x: 0, y: ROOM_HEIGHT },29};Then it tweens both rooms at once. The current room slides off one way while the next slides to the origin:
183this.services.timer.tween(184 this.currentRoom.position,185 { x: -roomOffset.x, y: -roomOffset.y },186 ROOM_PAN_DURATION,187);188this.services.timer.tween(189 nextRoom.position,190 { x: 0, y: 0 },191 ROOM_PAN_DURATION,192 {193 callback: () => {194 this.currentRoom = nextRoom;195 this.nextRoom = undefined;196 this.isPanning = false;197 onFinish();198 },199 },200);Every room has a position, and tiles and actors draw relative to it. That’s the only change drawing needed: during a pan, visibleRooms returns both rooms, and each draws itself wherever it currently is.
PlayState stops updating Link while isPanning is true, so input can’t move him mid-pan.
Link Rides Along
Link changes room the moment the pan starts. Player.enterRoom() moves him into the incoming room at the spot that matches where he is on screen, then a third tween slides him to the doorway on the far edge:
91player.enterRoom(92 nextRoom,93 player.position.x - roomOffset.x,94 player.position.y - roomOffset.y,95);96this.services.timer.tween(97 player.position,98 { x: doorway.x, y: doorway.y },99 ROOM_PAN_DURATION,100);Because his position is relative to the incoming room, he moves with it, so he never jumps on screen.
Here’s that moment slowed down, with the world drawn wider than the screen so you can see room 2 waiting off to the right:
Walking In
Once the pan finishes, Link stands in the doorway, half inside the wall. PlayerEnterRoomState walks him to the exit’s spawn point in a straight line:
59if (step < remaining) {60 this.player.position = getPositionInDirection(position, facing, step);61 return;62}63
64this.player.position = new Vector(this.target.x, this.target.y);It ignores input and walls on purpose, since the doorway itself is a wall tile in the new room. When he arrives, it hands over to Walk if a direction is held, or Idle otherwise.
stateDiagram-v2 [*] --> Idle Idle --> Walk: direction held Walk --> Idle: no direction held Idle --> EnterRoom: pan finished Walk --> EnterRoom: pan finished EnterRoom --> Idle: reached spawn EnterRoom --> Walk: reached spawn, direction held
Rooms Start Fresh
Dungeon.createRoom() builds a new Room from its config every time Link enters. Nothing carries over between visits. That’s fine now, because rooms hold only tiles, but it means anything a room gains later, like enemies, comes back each time you return.
Only leave through a passage
Link pans to the next room as soon as he faces any wall that has an exit, even room 1’s walled-off left side, because getExitReachedBy() in src/objects/Dungeon.js only checks that the exit exists.
The finished check is in the second code block under Exits above. Use it in place of the stub’s if, and add the passage line from the same block.
Then walk Link through a passage into the next room. The Challenge tab gets a ✓ once you’ve seen it work in the game.