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-5 changes how the Moblin is built so that how an enemy moves can be swapped without touching when it moves. That’s the Strategy pattern, and it’s what lets the assignment give enemies new ways to move without new states.
The Moblin’s Move state never changes here. Only the strategy plugged into it does, and its movement changes with it.
The Problem
In Zelda-4, the wandering code lives inside MoblinMoveState.update(). That’s fine for one behaviour, but think about what’s coming. One Moblin should stand guard. Another should chase Link. A bat should wander until Link gets close, then dive at him. Writing each as a branch in the move state, or as another state per movement, mixes up two separate questions:
- When does the enemy move? That’s the state’s job: Move, Rest, Attack.
- How does it move while it does? Wander, stand still, chase, follow a path.
The Strategy pattern pulls the second question out into its own objects.
Movement Strategies
src/strategies/MovementStrategy.js defines what every strategy can do:
2export default class MovementStrategy {3 /**4 * Forgets any route or memory so the next reaction starts fresh.5 *6 * @returns {void}7 */8 reset() {}9
10 /**11 * Moves an enemy for one frame and sets its facing.12 *13 * @param {Enemy} _enemy Enemy to move.14 * @param {number} _dt Elapsed seconds.15 * @returns {void}16 */17 update(_enemy, _dt) {}18
19 /**20 * Draws strategy-specific collision debugging.21 *22 * @param {CanvasRenderingContext2D} _context Rendering context.23 * @param {Enemy} _enemy Enemy using this strategy.24 * @returns {void}25 */26 renderDebug(_context, _enemy) {}27}reset()forgets any route or memory, so the strategy starts fresh.update(enemy, dt)moves the enemy for one frame and sets its facing.renderDebug()draws anything useful in debug mode. Most strategies draw nothing.
The enemy is passed in rather than stored. A strategy only keeps its own data, such as a heading or a path, which keeps it simple to reason about.
Two strategies come with the template. Stand inherits every method and does nothing. Wander is the Zelda-4 walking code, moved out of MoblinMoveState with this.moblin swapped for the enemy it’s given:
30update(enemy, dt) {31 if (!this.direction || this.remaining <= 0) {32 this.direction = pickRandomElement([33 Direction.Up,34 Direction.Down,35 Direction.Left,36 Direction.Right,37 ]);38 this.remaining = getRandomPositiveNumber(0.5, 1.5);39 }40
41 const distance = enemy.speed * dt;42 const moved = enemy.move(this.direction, distance);43
44 enemy.setFacing(this.direction);45 this.remaining = moved < distance ? 0 : this.remaining - dt;46}Choosing a Strategy by Name
src/strategies/movementStrategies.js maps names to classes, just like the enemy dictionary in roomBuilder.js:
10export const movementStrategies = {11 stand: StandMovementStrategy,12 wander: WanderMovementStrategy,13};Enemy definitions and spawns gain a movement field. The Moblin’s default is wander, and room 2’s second spawn overrides it:
39enemies: [40 { tileIndex: 43, type: "moblin" },41 { tileIndex: 22, type: "moblin", movement: "stand" },42],The Enemy constructor creates the enemy’s own strategy instance from the spawn’s name, falling back to the type’s default:
95createMovementStrategy(spawn, definition) {96 const name = spawn.movement ?? definition.movement ?? "stand";97
98 return new movementStrategies[name]();99}Each enemy gets a new instance. A wandering strategy remembers its heading, and two Moblins sharing one would steer each other.
The Move State Delegates
MoblinMoveState no longer knows how to move. It resets the strategy when it starts, and hands over every frame:
36update(dt) {37 super.update(dt);38
39 const facing = this.moblin.facing;40
41 this.moblin.movementStrategy.update(this.moblin, dt);42
43 if (this.moblin.facing !== facing) {44 this.restartWalkingAnimation(this.walkingFrames);45 }46}The only thing left in the state is what belongs to it: its animation. It restarts the walk cycle when the strategy turns the Moblin, without caring why it turned.
Enemy.renderDebug() also calls movementStrategy.renderDebug(), so a strategy with something to show, like a chase route, can draw it in debug mode.
Pick the strategy by name
Room 2’s second Moblin should stand still, but it wanders, because createMovementStrategy() in src/objects/Enemy.js always builds a wander strategy.
The finished method is the last code block under Choosing a Strategy by Name above. Replace the stub’s body with its two lines.
Then walk Link through the right-hand passage into room 2 and watch its second Moblin stand still. The Challenge tab gets a ✓ once you’ve seen it work in the game.