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-6 adds the pigs. src/config/levels.js rebuilds the level as a plainer hut, two posts and a plank at about x = 2250, with one pig standing under the plank. Knock every pig out and the level is won. Run out of birds with any left, and it’s lost. There are no end screens yet. The level just stops, and Angry-Birds-7 adds them.
Pigs
Pig in src/entities/Pig.js is the second kind of Breakable. It’s a circle, with its own density, PIG_DENSITY, and a bounce from pigDefinitions, and it differs from a block in two ways:
- However hard the hit, a pig loses one health.
maxHitDamagecaps the damage from one hit, and blocks leave it at infinity. A regular pig has one health, so any hit that buys one health pops it. - Pigs bruise easily.
minHitSpeedis 1.5, against a block’s 4, so a block that’s barely moving still hurts one.
onCollide() in src/entities/Breakable.js gives a block the faster of the two bodies’ speeds, because a block sets countsOwnSpeed, as Angry-Birds-5 showed. A pig leaves countsOwnSpeed false and takes only the other body’s speed, because one that took damage from landing would die from a drop of a few pixels.
The Fortress now holds pigs as well as blocks, and they share the same update, so a broken pig bursts into shards like a block. Every few seconds, updateOinks() in src/objects/Fortress.js plays an oink.
Settling
A shot isn’t over when the bird is. A tower can take seconds to come down, and a pig it lands on is just as dead. So once a bird is finished, the level moves to a new phase, Settling, and waits for three things before deciding what happens next:
240if (241 this.effects.isDone() &&242 this.camera.hasArrived() &&243 (this.stillTime >= SETTLE_HOLD || this.settleTime >= SETTLE_TIMEOUT)244) {245 this.moveOn();246}- The shards and smoke have finished.
- The camera has finished panning.
- Nothing in the fortress has moved faster than
STOPPED_SPEEDforSETTLE_HOLD, orSETTLE_TIMEOUThas passed in case something never stops rolling.
moveOn() then ends the level or loads the next bird. Here, the tower is still coming down long after Red is gone:
If the last bird is gone and pigs are left out of view, showFortress() pans to them while the level settles, so the player sees what they missed.
A Tower Can Win
A tower knocked over by one bird can still be falling as the next is aimed. If it kills the last pig then, updateAiming() in Level.js locks the sling and starts settling, so the player can’t fire a bird at an empty fortress. setLocked() in src/objects/Slingshot.js keeps the mouse off the bird.
Here’s every phase so far, with that shortcut from Aiming to Settling:
Win or lose
Pop every pig, and the level just carries on loading birds until it runs out, and never says you won. moveOn() in src/objects/Level.js only loads the next bird.
Decide, in this order:
- If every pig is dead, set
phasetoLevelPhase.Won. Check this first, so the last bird can still win. - Otherwise, if there are no birds left, set
phasetoLevelPhase.Lost. - Otherwise, load the next bird with
this.loadNextBird().
To answer those questions, call this.fortress.areAllPigsDead(), in src/objects/Fortress.js, and this.birdQueue.areNoBirdsLeft(), in src/objects/BirdQueue.js.
The Challenge tab gets a ✓ once you’ve won the level.
updateAiming() in the same file already asks the fortress whether every pig is dead.
Use one if, else if, else chain, so only one of the three runs.
The whole of moveOn():
moveOn() { if (this.fortress.areAllPigsDead()) { this.phase = LevelPhase.Won; } else if (this.birdQueue.areNoBirdsLeft()) { this.phase = LevelPhase.Lost; } else { this.loadNextBird(); }}