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-1 puts Link in the dungeon. He walks in four directions with the arrow keys or WASD, faces the way he moves, and stops at walls, pillars, and closed doors.
Actors and Link
Player extends a new base class, Actor, which holds what anything that walks around a room needs: the room it’s in, its position, which way it faces, and a way to move. Link adds his speed, his health, and a state machine:
13export default class Player extends Actor {14 /** @type {GameServices} */15 services;16
17 /** @type {number} */18 speed = PLAYER_SPEED;19
20 /** @type {number} */21 health = PLAYER_MAX_HEALTH;22
23 /** @type {number} */24 maxHealth = PLAYER_MAX_HEALTH;25
26 /** @type {StateMachine} */27 stateMachine;Health doesn’t do anything yet. Enemies will lower it in Zelda-4, and the HUD will show it in Zelda-7.
Unlike Mario, there’s no gravity. The dungeon is seen from above, so up and down are directions on the floor like left and right.
States Own Their Animation
Link has two states for now, PlayerIdleState and PlayerWalkState. Each state builds its own animation and draws it. Player itself only forwards update() and render() to the state machine:
57update(dt) {58 this.stateMachine.update(dt);59}stateDiagram-v2 [*] --> Idle Idle --> Walk: direction held Walk --> Idle: no direction held
The shared base, PlayerState, advances whatever animation the state set and draws its current frame at Link’s position:
57render(context) {58 const frame = this.animation?.getCurrentFrame();59
60 if (frame) {61 this.player.renderFrame(context, frame);62 }63}It also gives every state a Timer, cancelled in exit(). Link’s first two states don’t use theirs, but the sword in Zelda-3 will.
Four Directions from Four Sprites
Link’s sheet has only four walking sprites: facing down, facing up, and two steps facing left. createWalkingFrames() in frames.js builds all four directions from them. Down and up alternate between a sprite and its mirror image; right mirrors left:
51right: [52 { sprite: leftOne, isMirrored: true },53 { sprite: leftTwo, isMirrored: true },54],Actor.renderFrame() draws a mirrored frame by flipping the canvas with scale(-1, 1) around the sprite. scale(-1, 1) flips everything drawn after it across the canvas origin, not across the sprite. Try it below: flip x on its own, then translate() to the sprite first, then add the width. Only the last one puts Link back in the blue box, because after the flip the sprite extends to the left of the origin, so the origin has to start at its right edge.
Walking
Player.getHeldDirection() turns the arrow keys and WASD into one of four directions. PlayerWalkState follows it each frame, restarts the walk cycle when Link turns, and moves him:
46if (this.player.setFacing(direction)) {47 this.restartWalkingAnimation();48}49
50this.player.move(direction, this.player.speed * dt);When no direction is held, it changes back to Idle.
Walls
src/roomCollision.js has one rule for what blocks movement. Off the map is solid, and so is any tile whose id is in solidTileIds in src/config/tiles.js:
37export function isSolidTileAt(room, column, row) {38 if (column < 0 || column >= ROOM_COLUMNS || row < 0 || row >= ROOM_ROWS) {39 return true;40 }41
42 const tile = room.tiles[row * ROOM_COLUMNS + column];43
44 return tile === undefined || solidTileIds.includes(tile.id);45}solidTileIds lists walls, pillars, torches, and also closed doors and cracked walls, so for now those act as walls too.
The Hurtbox
Link isn’t tested against walls with his whole sprite. Actor.getHurtbox() returns the bottom 8 × 8 of his 16 × 16 tile, roughly his feet:
18hurtboxOffset = new Vector(4, 8);19
20/** @type {Vector} */21hurtboxDimensions = new Vector(8, 8);In a top-down game this is what makes walls feel right. Link’s head can overlap the wall above him, so he can walk right up to it, which reads as standing in front of it.
isAreaOpen() checks the four tiles under the corners of that box, and Player.canOccupy() asks it about a candidate position.
Drag Link around, or move him with the player.x and player.y sliders, and watch which tiles get checked:
One Pixel at a Time
Actor.move() doesn’t jump to the new position. stepUntilBlocked() in directions.js walks there one pixel at a time and stops at the first step that isn’t open:
47while (moved < distance) {48 const step = Math.min(1, distance - moved);49 const next = getPositionInDirection(current, direction, step);50
51 if (!canEnter(next)) {52 break;53 }54
55 current = next;56 moved += step;57}Link always ends flush against a wall, however long the frame was, and he can never skip through one. move() returns how far he actually got, which the enemies in Zelda-4 will use to notice they’re blocked.
Make walls solid
Link walks straight through the pillars, because isSolidTileAt() in src/roomCollision.js only treats a missing tile as solid.
The finished line is in the code block under Walls above. Copy it into the return statement, so a tile whose id is in solidTileIds counts as solid too.
Then walk Link into a wall or pillar. The Challenge tab gets a ✓ once you’ve seen it work in the game.