Skip to content

Assignment 4: Zelda

  • 💯 Worth: 6%
  • 📅 Due: See due date on Moodle.
  • 🚫 Penalty: Late submissions lose 10% per day to a maximum of 3 days. Nothing is accepted after 3 days and a grade of 0% will be given.
  • Read and understand all of the Zelda tutorial lessons.
  • Extend an existing codebase by reading its conventions and building new features that fit into them.
  • Use state machines to model entity behaviour, with each state owning its own animation, timers, and drawing.
  • Swap how an entity moves at runtime with the Strategy pattern, and keep the HUD in sync with the Observer pattern.
  • Detect and resolve collisions using hitboxes, hurtboxes, and tile data in a top-down world.
  • Give the player feedback through sound effects, music, and visual effects.
  1. Fork (do not download as a zip) the starter repository from GitHub. Make sure you use the link from Moodle to fork the proper repository for this assignment.
  2. Follow the environment setup instructions if you haven’t already done so from a previous assignment.
  3. Start Live Server in VSC by right-clicking on index.html and selecting Open with Live Server. This will open a new tab in your browser with the game running.

To complete this assignment, you should read and understand all of the Zelda tutorial lessons, and be familiar with the following:

  • State machines: Player, Enemy, and every enemy type register states that own their animation, Timer, and drawing. Entities only call stateMachine.update(dt) and stateMachine.render(context).
  • Hitboxes vs. hurtboxes: A hitbox is what deals damage (the sword, an enemy’s body) and a hurtbox is what receives it. Press Q in game to toggle debug mode and see them.
  • Strategy pattern: An enemy’s movementStrategy decides how it moves, while its states decide when. Strategies are registered by name in src/strategies/movementStrategies.js.
  • Observer pattern: UserInterface subscribes to Player and only redraws values when the player notifies it of a change.

Welcome to your fifth assignment! We’ve built the core of a top-down adventure in the style of Link’s Awakening: rooms to explore, a sword to swing, Moblins to fight, and hearts to lose. Let’s add a few pieces to pay homage to the classic Zelda titles and give Link a fighting chance in the dungeon!

The starter’s four rooms are already laid out so that you can test every feature:

Room What’s there
room1 The starting room. Its Moblin sits beside a cluster of blocks for testing ⭐ A* Moblin.
room2 📦 A chest (drawn as a plain solid tile until you handle it) and two Moblins.
room3 👹 The boss arena, through the opening in room1’s top wall.
room4 🔒 Behind the locked door in room1’s left wall. Unreachable until you implement the 🔑 Locks & Keys major.

Room definitions live in src/config/rooms.js. Rooms rebuild from config every time Link enters them, so nothing persists between visits unless you make it.

The cracked walls in room1’s bottom wall and the ornate chest in room4 are just decoration. They’re not part of any feature, so leave them as solid tiles.

The rooms are already laid out in a way that you can test any feature you choose to implement, so you don’t have to edit them. However, if you’re interested and want to make your own, here’s what to do:

  1. Download the Tiled map editor from here.
  2. Open assets/maps/zelda.tiled-project in Tiled, then open assets/maps/world.world to see every room laid out side by side. dungeon.tmx is room1, dungeon2.tmx is room2, dungeon3.tmx is room3, and dungeon4.tmx is room4. You can add tiles by clicking the sprite in the lower right corner and then clicking on the map to place them. You can remove tiles by using the eraser tool. Save once you’re done.
  3. Open the .tmx file you edited in VS Code, copy the array of numbers between the <data> tags, and replace the tiles array of the matching room in src/config/rooms.js. That’s it! You can call yourself a cartographer now.

Every room must stay 10 tiles wide by 8 tiles tall (80 numbers). Tiled only stores the tiles, so enemies, exits, and passages are still set by hand in each room’s entry in src/config/rooms.js. If you add a wall or obstacle tile that Link walks through, add its ID to solidTileIds in src/config/tiles.js.

Each feature’s table lists everything it’s graded on. Each row is worth 2 points: 2 if it works fully, 1 if it partially works, and 0 if it doesn’t work or wasn’t attempted. Only the first two minor and first two major features listed in your README are graded, so if you implement all three of either kind, list the two you want graded first. Additional deductions may be given for other issues, which will be detailed in comments.

Defeated enemies sometimes leave something behind: a heart if Link is hurt, otherwise a rupee.

Requirement How it’s tested
A defeated enemy sometimes, but not always, drops one pickup where it died. Defeat several Moblins, leaving and re-entering rooms to respawn them.
The drop is a heart when Link is injured and a rupee when he’s at full health. Defeat enemies at full health, then again after taking a hit.
Collecting a heart heals one full heart without going past Link’s maximum health. Take one hit, collect a heart, then take another hit and count the hearts.
Collecting a rupee increases a rupee count shown in the HUD. Collect several rupees and watch the HUD.
Pickups are collected on touch, play heart.wav or rupee.wav, and vanish with the room. Collect each kind, and leave a room with an uncollected pickup in it.

Link raises his shield while X is held, blocking any hit that comes from the direction he’s facing.

Requirement How it’s tested
Holding X while idle or walking raises the shield: Link stops, keeps his facing, and shows the shield frame. Hold X facing each of the four directions.
Releasing X lowers the shield and returns Link to normal movement. Release X and walk away.
A hit from the front is blocked: no damage and no hurt blink. Shield toward a Moblin and let it walk into you from each side.
A blocked hit plays shield.wav and pushes Link back a short distance without entering walls. Block a Moblin with Link’s back to a wall.
Hits from behind or the sides land normally. Shield facing away from a Moblin and let it walk into you.

A new flying enemy that flutters around at random and swoops straight at Link once he gets close.

To spawn one, add this to room1’s enemies list in src/config/rooms.js:

{ tileIndex: 12, type: "keese" },
Requirement How it’s tested
The keese alternates between resting (wings folded) and flying (wings flapping). Watch a keese from a distance.
While Link is out of range, the keese flies in random directions. Stay on the far side of the room.
Once Link is in range, the keese flies straight at him, and goes back to wandering when he leaves. Walk toward the keese, then back away.
The keese flies over blocks and other solid tiles but never leaves the room through its outer walls. Stand behind the blocks in room1, then near each wall.
The keese damages Link on contact and can be killed with the sword. Let it hit you, then hit it with the sword.

The chest in room2 holds a key that opens the locked door to room4.

Requirement How it’s tested
Pressing Enter while facing the closed chest opens it and plays treasure-small.wav. Face the chest from each side and press Enter.
Opening the chest shows the key above Link in his raised-item pose. Open the chest.
The key count in the HUD goes up by one, updated through the Observer pattern. Watch the HUD as you open the chest.
The chest can only be opened from directly in front of it, and only once. Press Enter beside the chest, and again after opening it.
Walking into the locked door without a key plays bump.wav and the door stays shut. Walk into the door before opening the chest.
Walking into the locked door with a key spends the key, plays door-unlock.wav, and animates the door open. Walk into the door after opening the chest.
Link can walk through the open door into room4 and back, but enemies can’t. Walk through the doorway both ways.
After leaving and returning, the chest stays open, the door stays unlocked, and the key count is still correct. Walk back and forth between room1, room2, and room4 several times.

Moblins chase Link once he gets close, using A* pathfinding to find a way around obstacles instead of getting stuck on them.

Requirement How it’s tested
A Moblin wanders until Link is in range, then chases him. Approach room1’s Moblin.
The Moblin goes back to wandering when Link leaves its range. Walk away from a chasing Moblin.
The Moblin reaches Link by going around the blocks instead of walking straight into them. Stand on the opposite side of room1’s blocks from the Moblin.
The Moblin never clips into or through a solid tile. Lead the Moblin around the blocks and along the walls.
The Moblin moves smoothly from tile to tile, one axis at a time. Watch the Moblin take corners.
The Moblin finds a new route when Link moves to a different tile, without dropping frames. Circle the blocks while being chased.
Debug mode (Q) draws the Moblin’s current route, and the route matches its movement. Toggle debug mode while being chased.
findPath() uses A*: it always expands the open node with the lowest f = g + h, where h is the Manhattan distance to the goal. Read src/pathfinding.js.
📖 How A* works (start here!)

Watch this first. It’s the best explanation of A* I’ve found:

A* searches the room one tile at a time, starting from the Moblin’s tile, until it reaches Link’s tile. Every tile it looks at becomes a node that remembers three numbers and where it came from:

  • g: the cost of the cheapest route found so far from the start to this node.
  • h: a guess at the cost from this node to the goal, called the heuristic.
  • f = g + h: the estimated cost of the whole route if it goes through this node.
  • parent: the node it was reached from. Following parents back from the goal gives you the route.

The search keeps an open list of nodes it has found but not yet expanded, and a closed set of nodes it’s finished with. Each step it picks the open node with the lowest f, which is the most promising one. A plain BFS (breadth-first search) expands nodes in the order it found them, while A* expands the ones that look closest to the goal first.

Here’s the shape of the algorithm. Filling in the details is up to you:

put the start node in open
while open is not empty:
current = the node in open with the lowest f
if current is the goal:
return the route
move current from open to closed
for each walkable neighbour of current that isn't in closed:
work out its g, h, and f through current
add it to open, or update it if this route is shorter
return no route

Build it in this order, and check each step works before starting the next one. If you try everything at once and the Moblin misbehaves, you won’t know whether the search or the movement is broken.

  1. Write a function that returns the open tiles above, below, left, and right of a given column and row, using isSolidTileAt(). Log it for a few tiles in room1 and check the answers against the map.
  2. Write findPath() in a new src/pathfinding.js from the pseudocode above. Create AStarMovementStrategy as a new MovementStrategy that only calls findPath() and stores the route, and override renderDebug() to draw it. The Moblin doesn’t move along it yet, but in debug mode you should see a route around the blocks to Link that updates as he walks.
  3. Move the Moblin along the route one node at a time, lining up its column before its row so it never cuts a corner. Set its facing as it goes.
  4. Register the strategy in movementStrategies.js. Swap the Moblin from wandering to A* when Link is within range, and back when he leaves. Only recalculate the route when Link enters a different tile or the Moblin reaches its next node.

Design your own boss for the arena in room3. Here’s mine, the Moblin King, for inspiration.

Requirement How it’s tested
The boss spawns in room3, and miniboss-battle.mp3 replaces the dungeon music while it’s alive. Enter room3.
The boss has a first attack that can hurt Link. Let the first attack hit you.
The boss has a second, different attack that can hurt Link. Let the second attack hit you.
The attacks take turns in a repeating loop. Survive and watch the boss for a few cycles.
Each attack is telegraphed (a pose, flash, or sound) before it can hurt Link. Watch for the warning before each attack.
The boss can only be hurt during a stun window, and a non-lethal hit knocks it back. Swing at it during and outside the stun window.
A boss health bar in the HUD drops with each hit, updated through the Observer pattern. Hit the boss several times.
A lethal hit plays a death animation and sound, the boss is removed, and the dungeon music resumes. Defeat the boss.

Read the assignment policies and make sure you include a proper .devlog.md file (5%). There will be a peer evaluation component (5%) to this assignment as well as a chance to be randomly selected to do a code walkthrough at my discretion.

  1. Inside README.md, list the 2 minor and 2 major features you want graded, in that order, then any known bugs to look out for, anything you want your peer evaluators to know, then do your final git push to GitHub.
  2. Go to Gradescope, click the link for this assignment, select the correct repository and branch from the dropdown menus, and click Upload.
  3. Assess your peers starting the day after the three day late submission deadline.