Milestone 4: Dawn and the Jar
The game is starting to be fun, but there's a problem: we have no way of losing or winning. Both are important. If there's no way to lose, then there are no stakes. If there's no way to win, then there's no point.
Losing The Game
The game will have a timer that runs out after 45 seconds. At the end of those 45 seconds, we'll end the game.
Let's start by just getting the lose-and-restart behavior to work.
We will do it in three steps.
First, we'll put the number 45 into a place where we can easily adjust it to lengthen or shorten the game:
// How many seconds until dawn
const DAWN = 45;
In JetLag, whenever a builder is called, a stopwatch starts running. We can use it to figure out how much time is left. Of course, this is going to be a useful bit of code, so we'll put it in a function, so it's easier to re-use:
/**
* Report how many seconds are left until dawn. JetLag has a built-in counter,
* `ScoreService.STOPWATCH`, that goes up by one every second that the game is
* running.
*
* @param dawn How many seconds the night lasts
*
* @returns The seconds until dawn, but never less than zero
*/
function timeLeft(dawn: number) {
return Math.max(0, dawn - stage.score.counters.get(ScoreService.STOPWATCH));
}
With both of those pieces in place, all that remains is to tell JetLag what condition causes the game to be lost.
We can do that by adding a "lose condition", which is a simple function that returns true when the time expires, and false otherwise:
// The game is lost if dawn comes first
stage.score.addLoseCondition(() => timeLeft(DAWN) <= 0);
Giving Feedback And Restarting
When time ran out, the game did not exactly end nicely. Making it nice is going to require us writing some code that is a bit confusing. That code will be easier to understand after you've finished a few more chapters. That code is also something you'll want to use over and over again.
With that being the case, let's not actually write that code right now.
Instead, create a folder called src/common.
It should not be inside of your firefly_jar folder, but instead, it should be a sibling.
That is, you want a folder called src/common and another called src/firefly_jar.
Once you've got that folder, you can copy my common.ts and save it as src/common/common.ts.
Inside of common.ts, there is a function called endScreen, which we can use to give nice feedback when a game ends (either winning or losing).
It needs three colors: a color for the background, a color for the title text, and a color for the other text.
The easiest thing to do is put these all in one place, so we can easily adjust them and fine-tune them:
// Colors for this game's end screens
const END_STYLE = { background: "#000000cc", titleColor: "#fff27a", detailColor: "#ffffff" };
Back in the previous section, we made use of JetLag's automatic score counters, and said that every time a firefly was caught, we'd increment a counter called "fireflies".
Let's write a quick function for reading the value of that counter:
/**
* Report how many fireflies the player has caught. Any time a firefly is
* caught, the "fireflies" score counter goes up by one.
*
* @returns The player's score
*/
function caught() {
return stage.score.counters.get("fireflies");
}
With that in place, we can write code that nicely ends the game when time runs out.
We do this by providing a function that makes a "lose scene".
The "lose scene" is an overlay that sits on top of your game.
JetLag understands that these overlays can be used for a welcome screen, a pause screen, or for winning/losing.
In this case, we'll say "the way to make a lose scene is by calling endScreen".
endScreen needs a title, a description, and a message to guide the player about restarting the level.
It also needs to know how to restart the level.
stage.switchTo(builder) says "restart the game", because builder is the name of the function that made our game.
If we had several builders, for different levels of the game, then we could use the name of a specific builder to switch to a specific level.
With that in place, we can tell JetLag how to display an overlay when the game is lost, and how to restart the game.
// Provide a nice message when the game is lost. When the player clicks,
// `stage.switchTo(builder)` starts a fresh game: it wipes out the old actors,
// timers, and score, and then calls `builder()` again.
stage.score.loseSceneBuilder = (overlay: Scene) => {
endScreen(overlay, {
title: "The sun came up...",
detail: `...and the fireflies went to sleep. You caught ${caught()}.`,
prompt: "Click or press Enter to play again",
next: () => stage.switchTo(builder),
background: END_STYLE.background,
titleColor: END_STYLE.titleColor,
detailColor: END_STYLE.detailColor,
});
};
You might find that VSCode does not recognize endScreen.
If it doesn't, be sure to right click and add an import.
The import should look like this:
import { endScreen } from "../common/common";
The file has a few other helpers in it, too. We'll explore them in later chapters.
Changing The Background Color
Telling someone why they lost is nice, but it's probably not enough. We should give them visual cues that time is running out. Later, we'll put a timer right on the screen. But if we also make the background change over time, it will look like the sun is rising. That will help the player to know that they're close to losing, and need to hurry up.
To make this work, we'll start by defining two colors, for night and dawn:
// The night sky color
const NIGHT_COLOR = "#0b1430";
// The color at dawn
const DAWN_COLOR = "#b86a7c";
Next, let's make a function that finds a blend between these two colors.
The code below is longer than it needs to be.
In fact, common.ts has a different version of it, which is much more brief.
However, it's useful to see this version, because it is very explicit.
It chops the colors into three parts, turns each part into a number, and uses the percentage of night that remains to blend the red, green, and blue parts of the color:
/**
* Blend two "#rrggbb" colors
*
* @param color1 The first color as a hex string
* @param color2 The second color as a hex string
* @param ratio A ratio: 0 means "all first color", 1 means "all second color",
* fractions blend.
* @returns The blended color as a hex string
*/
function mixColor(color1: string, color2: string, ratio: number) {
// Keep the ratio between 0 and 1, even if the caller gives something else
ratio = Math.min(1, Math.max(0, ratio));
// A color like "#0b1430" is three two-digit hex numbers: red ("0b"), green
// ("14"), and blue ("30"). `slice` cuts out each pair of digits, and
// `parseInt(..., 16)` turns it into a regular number from 0 to 255.
const red1 = parseInt(color1.slice(1, 3), 16);
const green1 = parseInt(color1.slice(3, 5), 16);
const blue1 = parseInt(color1.slice(5, 7), 16);
const red2 = parseInt(color2.slice(1, 3), 16);
const green2 = parseInt(color2.slice(3, 5), 16);
const blue2 = parseInt(color2.slice(5, 7), 16);
// Blend each part on its own. When ratio is 0, this is all color1. When
// it's 1, it's all color2. When it's 0.25, it's 75% color1 and 25% color2.
const red = Math.round(red1 * (1 - ratio) + red2 * ratio);
const green = Math.round(green1 * (1 - ratio) + green2 * ratio);
const blue = Math.round(blue1 * (1 - ratio) + blue2 * ratio);
// Turn each number back into two hex digits. `toString(16)` writes a number
// in hex, and `padStart(2, "0")` adds a leading zero when the number is small
// (so 11 becomes "0b", not "b").
const redHex = red.toString(16).padStart(2, "0");
const greenHex = green.toString(16).padStart(2, "0");
const blueHex = blue.toString(16).padStart(2, "0");
return "#" + redHex + greenHex + blueHex;
}
Since we have that function, we can set up a timer that runs four times per second and adjusts the background color accordingly:
// The sky slowly brightens toward dawn
stage.world.timer.addEvent(new TimedEvent(0.25, true, () => {
stage.backgroundColor = mixColor(NIGHT_COLOR, DAWN_COLOR, stage.score.counters.get(ScoreService.STOPWATCH) / DAWN);
}));
Winning
Our next step is to make it possible to win the game.
We'll do this by having a jar that the player needs to reach.
We might want to adjust the jar location, so let's put it in a const variable, so it's easy to find and adjust.
// The location and size of the jar
const JAR = { cx: 14.2, cy: 4.5, width: 0.9, height: 1.1 };
Since the jar is an actor, it needs a rigidBody and an appearance.
Like before, we'll have an array with a shape and some text that reminds us that it's the jar.
We also need a behavior.
In this case, we'll use the GoalBehavior, which takes an onArrival function.
The function we'll provide says "when a PlayerBehavior collides with this, return true, indicating that the PlayerBehavior actor should disappear. Also add one to an arrivals score counter."
// The jar. When the hero gets to it, it sets "arrivals", so that the game will end in victory.
new Actor({
appearance: [
new FilledBox({ width: JAR.width, height: JAR.height, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
new TextSprite({ center: true, face: "Arimo Regular", size: 14, color: "#f3f4f6" }, "jar"),
],
rigidBody: new BoxBody({ cx: JAR.cx, cy: JAR.cy, width: JAR.width, height: JAR.height }),
behaviors: new GoalBehavior({
onArrival: () => {
stage.score.counters.add("arrivals", 1);
return true;
},
}),
});
With that in place, we can tell JetLag to win the game when the arrivals counter is greater than zero:
// Describe the condition for winning
stage.score.addWinCondition(() => stage.score.counters.get("arrivals") >= 1);
And we can set up an overlay to show when the player wins.
It uses the same endScreen function as the losing overlay.
It also just restarts the game.
// A nice message for when the game is won
stage.score.winSceneBuilder = (overlay: Scene) => {
endScreen(overlay, {
title: "The jar is glowing!",
detail: `${caught()} fireflies, with ${Math.floor(timeLeft(DAWN))} seconds to spare`,
prompt: "Click or press Enter to play again",
next: () => stage.switchTo(builder), ...END_STYLE });
};
One thing to notice: this code is a bit simpler than for the loseSceneBuilder.
We're using the ... operator (the "spread" operator) to automatically turn the END_STYLE into the parts that we provided explicitly for losing.
Making Victory A Challenge
You probably noticed a small error in the game: you can win without collecting any fireflies! To fix that we'll start by setting up a variable that says how many fireflies need to be caught. Just like all the other important numbers in the game, we put this in a variable so it's easy to find and easy to adjust:
// How many fireflies it takes to fill the jar
const GOAL = 10;
Next, let's adjust the jar.
In its onArrival function, if we return false, the PlayerControl actor won't disappear.
That's exactly what we need!
If the firefly count isn't high enough, then we return false, and don't increment arrivals.
If the count is high enough, we increment arrivals and return true, which is enough to win the game:
// The jar. When the hero gets to it, it sets "arrivals", so that the game will end in victory.
new Actor({
appearance: [
new FilledBox({ width: JAR.width, height: JAR.height, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
new TextSprite({ center: true, face: "Arimo Regular", size: 14, color: "#f3f4f6" }, "jar"),
],
rigidBody: new BoxBody({ cx: JAR.cx, cy: JAR.cy, width: JAR.width, height: JAR.height }),
behaviors: new GoalBehavior({
onArrival: () => {
// The jar only accepts the hero once it has enough fireflies
if (caught() < GOAL) return false;
stage.score.counters.add("arrivals", 1);
return true;
},
}),
});
End State
You've now got a game that can be won and lost. Here's my Firefly Jar Milestone 4 code, for you to compare against.
If you want to play my version of the game, you can find it here: