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.
Angry-Birds-7 turns one level into a game. There are three levels now. Each one opens with a look at the fortress before the first shot. Winning shows Victory! and moves on to the next level, and losing shows Game Over and tries the same one again.
Levels
src/config/levels.js now has three levels. They’re the hut, a house on a hill, and two huts with a glass tower across their roofs. The house sits on a mound, a static shape of soil given by its outline:
19mounds: [20 {21 points: [22 { x: 2030, y: 810 },23 { x: 2090, y: 740 },24 { x: 2410, y: 740 },25 { x: 2470, y: 810 },26 ],27 },28],Mound is given. Matter only collides convex shapes, ones with no dents, so it splits the outline into triangles and makes a body from them.
Drawing it is a different problem. The soil art is a small square tile, and a mound can be any shape. renderSoil() in src/entities/Mound.js solves it with the canvas’s clip:
26context.save();27context.beginPath();28corners.forEach(({ x, y }) => {29 context.lineTo(x, y);30});31context.closePath();32context.clip();It traces the outline as a path, a list of points joined by lines, the same way you’d start drawing a shape, but instead of filling it, clip() turns it into a stencil. From then on, the canvas only keeps what’s drawn inside that path. renderSoil() then tiles soil across the whole rectangle around the mound, and every bit that falls outside the outline is thrown away, leaving soil in exactly the mound’s shape. context.restore() at the end removes the clip, so whatever is drawn next isn’t cut off. Step through it, and show the triangles Matter collides with:
Moving On
Level now knows its number, and PlayState builds the level from the number it’s given:
32createLevel({ levelNumber = 1 }) {33 const index = (levelNumber - 1) % levels.length;34
35 return new Level(this.game, index + 1, levels[index]);36}The % wraps the number around, so winning the last level goes back to the first. When the level is won or lost, PlayState changes to VictoryState or GameOverState, passing the level and the number to play next.
With the Preview phase, the opening look at the fortress, a level now goes through every phase:
The End Screens
Victory and Game Over share a parent, LevelEndState in src/states/LevelEndState.js. Neither one clears the level away. They keep updating and drawing it behind a big label, so a tower still falling finishes falling, until the player clicks or presses Enter.
That means PlayState.exit() can’t always destroy its level:
44exit() {45 if (!this.level?.hasEnded()) {46 this.level?.destroy();47 }48
49 super.exit();50}A level that ended is handed on, and LevelEndState.exit() destroys it once the player moves on.
This is the preview you’ll write in the challenge, seen from above:
Show the fortress
Each level now starts at the sling, with no idea where the pigs are. startPreview() in src/objects/Level.js skips straight to aiming.
The fortress starts off-screen, so each level should open on a new phase, Preview. It should wait a moment, pan to the pigs, pan back to the sling, and only then let the player aim. The sling should stay locked the whole time, so a click can’t fire a bird mid-pan.
Panning uses the camera from Angry-Birds-3. this.camera.panTo(x) slides the view over until its left edge is at x in the world, so panTo(0) brings it back to the sling.
startPreview() is already async. await this.timer.delay(seconds) pauses it for that many seconds of game time, without pausing the game. this.timer only ticks inside the level’s own update(), so once the level is gone its waits never finish, and a preview still waiting can’t resume into the next level. Write the steps one after another:
- Lock the sling with
this.slingshot.setLocked(true). - Wait
PREVIEW_HOLDseconds, then pan tothis.getFortressView(). - Wait
PREVIEW_PANseconds, then pan back to the sling. - Wait
PREVIEW_PANseconds again, then unlock the sling withthis.slingshot.setLocked(false)and callstartAiming(), in place of the call that’s there now.
Then delete the two // biome-ignore comments above PREVIEW_HOLD and PREVIEW_PAN at the top of the file. They only kept Biome quiet about the unused constants until you used them.
The Challenge tab gets a ✓ once you’ve watched the opening pan go to the fortress and back.
Write each wait on its own line, starting with await. The code after an await runs only once that wait is over.
If the camera jumps back to the sling at once, an await is missing in front of one of the delays.
Replace the TODO and the startAiming() call under it with:
this.slingshot.setLocked(true);await this.timer.delay(PREVIEW_HOLD);this.camera.panTo(this.getFortressView());await this.timer.delay(PREVIEW_PAN);this.camera.panTo(0);await this.timer.delay(PREVIEW_PAN);this.slingshot.setLocked(false);this.startAiming();