Reset this lesson?

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

Angry-Birds-4 gives each level a set number of birds. They line up behind the slingshot and wait their turn, hopping now and then. When one is finished, the next one in line takes its place in the sling, and when they’ve all gone, that’s it.

Queues

A queue is a list where things join at the back and leave from the front, so it’s first in, first out. It’s a line at a shop. A stack is the opposite, last in, first out, like a pile of plates. In a JavaScript array, push() adds to the end, shift() removes from the front, and pop() removes from the end.

Its two main operations are enqueue, which adds to the back, and dequeue, which removes from the front. Peek looks at the front without removing it. Try dequeue and peek on a queue of birds:

BirdQueue keeps its birds in an array, in firing order. The birds go in once, in the constructor, so it never needs a separate enqueue:

src/objects/BirdQueue.js
24
birdTypes.forEach((type, index) => {
25
const queueX = (x / birdTypes.length) * (birdTypes.length - index);
26
27
this.birds.push(createBird(game, type, queueX, GROUND_Y, effects));
28
});

The birds are spread evenly between the left edge and the sling, with the first in line closest to it. Each starts on the ground at GROUND_Y and waits there in the Waiting state.

The Bird Factory

createBird() in src/services/BirdFactory.js is the Factory pattern from Zelda’s enemies. It makes a bird from its type, so nothing else needs to know which class each bird is:

src/services/BirdFactory.js
18
export const createBird = (game, type, x, y, effects) => {
19
const BirdClass = birdClasses[type] ?? Bird;
20
21
return new BirdClass(game, x, y, effects, type);
22
};

birdClasses is empty for now, so every type is a plain Bird. A bird with an ability, like the ones in the assignment, gets a class of its own and one entry here.

Levels as Data

Each level is described by plain data in src/config/levels.js. For now a level is only its list of birds:

src/config/levels.js
4
export const levels = [
5
{
6
birds: ["red", "red", "red"],
7
},
8
];

PlayState builds its Level from levels[0], and the level hands definition.birds to the queue. The next lessons add a fortress to this level, and later more levels.

Phases

Level now keeps a phase, which says where the level has got to. update() reads it, so the whole flow of a level is in one place:

src/objects/Level.js
75
switch (this.phase) {
76
case LevelPhase.Aiming:
77
this.updateAiming();
78
break;
79
case LevelPhase.Flying:
80
this.updateFlying();
81
break;
82
}
  • Aiming, in updateAiming(), waits for a launch, then starts the camera following the bird and moves to Flying.
  • Flying, in updateFlying(), waits for the bird to finish, through isFinished(). If any birds are left, loadNextBird() takes the next one from the queue, puts it in the sling, pans back, and goes back to Aiming.

A phase belongs to the level. Each bird still has its own state, and two of those are also called Aiming and Flying. This is a state machine too, but a lighter one. Each phase is a single method on the level, with nothing to set up or clear away, so an enum and a switch is enough. The next lessons add more phases.

Next in line

Fire Red, and when he’s finished, the sling stays empty. dequeue() in src/objects/BirdQueue.js returns the bird at the front but never takes it out of the queue, so the level keeps getting the same bird back.

  1. Take the first bird out of this.birds. Array.prototype.shift() removes and returns the first element.
  2. If there wasn’t one, throw an Error. The level only asks when it knows a bird is left, so an empty queue here is a bug. The throw also tells CheckJS that past it, the bird can’t be undefined.
  3. Change the bird’s own state, not the level’s phase, to Aiming with bird.stateMachine.change() and BirdStateName.Aiming, so it’s ready for the sling. Then return it.

Then delete the // biome-ignore comment above the @src/enums.js import at the top of the file. It only kept Biome quiet about the unused BirdStateName until you used it.

The Challenge tab gets a ✓ once the queue has handed the sling its last bird.

Replace the return line that’s there now. Keep the bird from shift() in a bird constant, so the rest of dequeue() can use it.

On an empty array, shift() returns undefined, so check if (!bird) before anything uses it.

Replace the TODO and the return under it with:

const bird = this.birds.shift();
if (!bird) {
throw new Error("The bird queue is empty.");
}
bird.stateMachine.change(BirdStateName.Aiming);
return bird;

📚 References

Powered by WebContainers
Files
Preparing Environment