Reset this lesson?

Your edits to this lesson are deleted and its starting files load again.

Angry-Birds-5 builds the first fortress, a little wooden hut with a ball under its peaked roof. Blocks of wood, glass, and stone stand and topple under Matter’s physics, crack as they take hits, and shatter when they run out of health.

Blocks

A Block is a rectangle body, or a circle for the balls, built from an entry in the level’s data:

src/config/levels.js
9
blocks: [
10
{ type: "wood", size: "medium", x: 1210, y: 729, angle: 1.5708 },
11
{ type: "wood", size: "medium", x: 1350, y: 729, angle: 1.5708 },
12
{ type: "wood", size: "large", x: 1280, y: 639, angle: 0 },
13
{ type: "wood", size: "medium-ball", x: 1280, y: 594, angle: 0 },
14
{ type: "wood", size: "medium", x: 1231, y: 555, angle: -1.0472 },
15
{ type: "wood", size: "medium", x: 1329, y: 555, angle: 1.0472 },
16
],

Each position is the block’s centre, and its angle is in radians, so 1.5708 is a quarter turn, standing the plank on end. blockShapes in config/entities.js gives each size its width and height, and getBlockFrames() finds its frames. The material sets the block’s density, its mass per unit of area, so a stone plank is much heavier than a wooden one the same size.

The Level constructor in src/objects/Level.js now sets camera.arrivalX to the leftmost block’s x, so the opening pan ends at the fortress.

Sleeping

Each step, Matter pushes apart any bodies that overlap, but only approximately. In a stack those errors add up, and a tower jiggles and slides apart by itself.

The engine was created with enableSleeping, so a body that has barely moved for a while goes to sleep, and Matter stops moving it until something hits it. The Fortress constructor, in src/objects/Fortress.js, puts every block to sleep as the level starts, before it can jiggle.

Sleeping has a catch. Matter doesn’t wake a block whose support slides away or vanishes, so it would be left floating. wakeNeighbours() in src/entities/GameEntity.js wakes whatever is next to a moving or vanishing body, and anything still held up dozes off again.

On the left, sleeping is off. On the right, the tower starts asleep. Knock out its bottom brick with wakeNeighbours() on, then off:

Breaking

Anything with health is a Breakable, in src/entities/Breakable.js. Each collision passes through onCollide() to getHitBy(). A Block sets countsOwnSpeed, so onCollide() passes on the faster body’s speed, and landing hurts a block too. getHitBy() turns the hit into damage in four checks:

src/entities/Breakable.js
55
getHitBy(speed, { body, hitScale, materialDamage }) {
56
if (this.hitCooldown > 0 || speed < this.minHitSpeed) {
57
return;
58
}
59
60
const hitMass = Math.min(body.mass, MAX_HIT_MASS) * hitScale;
61
const impact = speed * hitMass * (materialDamage[this.material] ?? 1);
62
const hits = Math.floor(impact / this.toughness);
63
64
if (hits > 0) {
65
this.takeDamage(hits);
66
}
67
}
  1. Is it fast enough? A hit slower than minHitSpeed, which is 4, does nothing.
  2. hitMass is the attacker’s mass, capped at MAX_HIT_MASS, which is 16, times hitScale, which is 1 for every bird.
  3. impact is the speed times hitMass times materialDamage, also 1 for every bird here. So a bird’s impact is its speed times its mass, up to the cap.
  4. hits, the health the block loses, is what the impact buys. Think of the impact as what the hit has to spend, and the block’s toughness as the price of one health. Wood’s toughness is 110, so an impact of 250 buys 2 health with 30 left over, and an impact of 100 buys nothing.

Throw a bird at a block below. Set its speed and mass, and press throw:

Falling Blocks

Blocks weigh a fraction of a bird, so falling blocks would barely mark anything. Block in src/entities/Block.js gives blocks a hitScale of BLOCK_HIT_SCALE, which is 5, and that’s what lets a falling block crack the ones it lands on. It also sets materialDamage to 1/5 against stone, which cancels the scale out, so a block can’t crack the stone it rests on.

If a hit leaves either body isBroken(), onCollisionStart() in src/objects/Level.js now sets pair.isActive to false, so Matter doesn’t push the two apart. A falling boulder keeps its speed through the plank it breaks.

After a hit, takeDamage() ignores new hits for HIT_COOLDOWN, so a tumbling block counts as one hit. chooseFrame() shows more cracks as the block weakens.

The Fortress

Fortress holds the blocks and cleans up the broken ones. It’s also a subject, the side that sends notifications in the Observer pattern. Anything with an onNotify() method can subscribe(). It’s onNotify(), not Zelda’s update(notification), because Effects already has an update(dt). When a block breaks, the fortress calls notify() with where it was and what its shards look like:

src/objects/Fortress.js
38
if (entity.isDead && !wasDead) {
39
this.notify({
40
x: entity.body.position.x,
41
y: entity.body.position.y,
42
shardFrames: entity.shardFrames,
43
});
44
}

Effects subscribes, and bursts the shards, so a block knows nothing about effects. Birds call effects.burst() directly for their feathers, because only Effects cares about a feather.

Spread the word

Break a block, and it vanishes without a single shard. Nobody is listening to the fortress, and it wouldn’t tell them anything if they were. Wire up the Observer pattern by filling in the TODO (challenge) spots:

  1. In src/objects/Fortress.js, have notify() pass the notification to every observer’s onNotify().
  2. In Fortress.update(), have it notify the observers when a block has just broken. wasDead will hold whether the block was already dead, so each block is announced once.
  3. In src/objects/Effects.js, have onNotify() call this.burst() with the notification’s x, y, and shardFrames, then SHARD_COUNT for how many shards to throw.
  4. In the Level constructor in src/objects/Level.js, call this.fortress.subscribe() and pass it this.effects.

Then delete the four // biome-ignore comments, above wasDead and notify() in Fortress.js, and above SHARD_COUNT and onNotify() in Effects.js. They kept Biome quiet until you used those names.

The Challenge tab gets a ✓ once you’ve broken a block and seen its shards fly.

this.observers is a Set, and a Set has forEach() like an array. In notify(), call onNotify(notification) on each observer.

In Fortress.update(), a block has just broken when entity.isDead is true and wasDead is false. Then call this.notify() with x and y from entity.body.position, and shardFrames from entity.

In notify() in Fortress.js:

this.observers.forEach((observer) => {
observer.onNotify(notification);
});

In Fortress.update(), in place of its TODO:

if (entity.isDead && !wasDead) {
this.notify({
x: entity.body.position.x,
y: entity.body.position.y,
shardFrames: entity.shardFrames,
});
}

Effects.onNotify() needs this.burst(x, y, shardFrames, SHARD_COUNT);, and the Level constructor needs this.fortress.subscribe(this.effects);.

📚 References

Powered by WebContainers
Files
Preparing Environment