Milestone 6: Golden Fireflies
The last piece of gameplay for Firefly Jar is to add golden fireflies. These fireflies are rare, and worth more points. To get them to work, we'll mostly just need to make a few adjustments to our existing code.
A Second Counter
We could say "golden fireflies add three instead of one" and just use a single counter.
However, it's usually nice to know not just the final score, but how that score was achieved.
To do that, we'll introduce a second counter called "golden".
JetLag makes this easy: we just need to use a different name in the onCollect field of the firefly's CollectableBehavior.
Then JetLag will make sure the counter exists, and will manage it accordingly.
In the end, all that means is that we need to change the makeFirefly function.
We'll do this in two parts.
First, change the signature of the function, to say that it takes another argument: the name of the counter.
While we're at it, we'll add an argument for the label to put on the firefly:
/**
* Make one firefly at (cx, cy). It has a CollectableBehavior, so when the hero touches it, it disappears, and the
* "fireflies" counter goes up by one. It doesn't wait around forever: it blinks out on its own after `lifetime` seconds. For the
* last `flickerTime` seconds of its life it flickers, so the player gets a warning that it's about to go.
*
* @param cx The X coordinate of the firefly's center
* @param cy The Y coordinate of the firefly's center
* @param counter The name of the counter to bump when it's caught ("fireflies" or "golden")
* @param label The text to write on the firefly's gray box
* @param lifetime How many seconds before it blinks out on its own
* @param flickerTime How many seconds, at the end of its life, it flickers
*/
function makeFirefly(cx: number, cy: number, counter: string, label: string, lifetime: number, flickerTime: number) {
Inside the function, we need to make a small edit to how we make the actor. It needs to use one of the new arguments to choose the counter to increment, and another of the arguments to know what label to use:
const firefly = new Actor({
appearance: [box, new TextSprite({ center: true, face: "Arimo Regular", size: 11, color: "#111827" }, label)],
rigidBody: new CircleBody({ cx, cy, radius: 0.3 }),
behaviors: new CollectableBehavior({ onCollect: counter }),
});
Then, in order for our existing fireflies to work, we need to fix the code in two places, so that makeFirefly gets the two extra arguments:
// A new firefly blinks on every so often, somewhere in the open
stage.world.timer.addEvent(new TimedEvent(SPAWN_FREQ, true, () => {
const spot = randomOpenSpot(GLOW_RADIUS);
makeFirefly(spot.cx, spot.cy, "fireflies", "firefly", FIREFLY_LIFETIME, FLICKER_TIME);
}));
// One firefly is already lit when the game starts
makeFirefly(5.5, 4.5, "fireflies", "firefly", FIREFLY_LIFETIME, FLICKER_TIME);
Golden Fireflies
Now we can add the golden fireflies! We'll start by declaring the constants that we'll use: the hard-coded locations where a firefly can be placed, the rate at which they spawn, and how long they last:
/** The hard-to-reach nooks where a golden firefly might appear */
const GOLDEN_SPOTS = [{ cx: 4.6, cy: 1.2 }, { cx: 15.5, cy: 8.5 }, { cx: 11.5, cy: 0.5 }, { cx: 4.4, cy: 8.5 }];
// How often a golden firefly blinks on
const GOLDEN_SPAWN_FREQ = 8;
// How long a golden firefly stays lit
const GOLDEN_FIREFLY_LIFETIME = 3;
Then all it takes is to add a new timer for the golden fireflies!
// Golden fireflies are rarer, hide in corners, and blink out faster
stage.world.timer.addEvent(new TimedEvent(GOLDEN_SPAWN_FREQ, true, () => {
const spot = GOLDEN_SPOTS[Math.floor(Math.random() * GOLDEN_SPOTS.length)];
makeFirefly(spot.cx, spot.cy, "golden", "gold", GOLDEN_FIREFLY_LIFETIME, FLICKER_TIME);
}));
Unfortunately, these fireflies aren't affecting the score yet.
We can fix that in two steps.
First, we'll add a constant that lets us tune how much a golden firefly is worth.
Then we'll update the caught function to compute the score based on both counters: fireflies and golden:
/** How many ordinary fireflies a golden firefly is worth */
const GOLDEN_FIREFLY_VALUE = 3;
/**
* Report how many fireflies the player has caught. Any time a firefly is
* caught, the "fireflies" score counter goes up by one, and any time a golden
* firefly is caught, the "golden" score counter goes up by one. A golden
* firefly is worth `GOLDEN_FIREFLY_VALUE` ordinary ones.
*
* @returns The player's score
*/
function caught() {
return stage.score.counters.get("fireflies") + GOLDEN_FIREFLY_VALUE * stage.score.counters.get("golden");
}
End State
Here's my Firefly Jar Milestone 6 code, for your reference. Below, you'll also find a playable version of the game. Before you play it, there's something important to think about. We're being very disciplined in our development. We're putting numbers into constants, to make them easy to tune. We're putting logic into little helper functions, so that we don't have to duplicate it in many places. That's not just a stylistic preference. It makes us more productive. If you don't count the comments, we added golden fireflies by changing five lines, and adding eight. That's not a lot of work, yet the game is much more interesting.