Milestone 2: The Garden
Now that you've got a hero who can move around, it's time to put some trees and hedges in its way. We'll do this in a few steps. First, let's just make one hedge and one tree. We'll make a few mistakes, but eventually we'll get them to behave correctly. Then we can use some more clever techniques to make all the hedges and trees, without having to write a lot of code.
Our First Hedge And Tree
Let's start with the basics: two actors, one rectangular, the other circular. They'll represent a hedge and a tree:
// A hedge, which is a box, and a tree, which is a circle, both in the hero's way
new Actor({
appearance: new FilledBox({ width: 0.5, height: 3.2, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
rigidBody: new BoxBody({ cx: 3.5, cy: 4.5, width: 0.5, height: 3.2 }),
});
new Actor({
appearance: new FilledCircle({ radius: 0.8, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
rigidBody: new CircleBody({ cx: 8, cy: 4.5, radius: 0.8 }),
});
When you try to move the hero, you'll notice that it can walk through them.
That's not going to make for a challenging game.
The first thing to do is make them both solid, by giving them each a SolidBehavior:
// A hedge, which is a box, and a tree, which is a circle, both in the hero's way
new Actor({
appearance: new FilledBox({ width: 0.5, height: 3.2, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
rigidBody: new BoxBody({ cx: 3.5, cy: 4.5, width: 0.5, height: 3.2 }),
behaviors: new SolidBehavior(),
});
new Actor({
appearance: new FilledCircle({ radius: 0.8, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
rigidBody: new CircleBody({ cx: 8, cy: 4.5, radius: 0.8 }),
behaviors: new SolidBehavior(),
});
Unfortunately, the hero still can walk through them.
The problem is that they're solid, but the hero has the default (Passive) behavior, meaning it can still go through things, even solid things.
To fix this, the hero needs a behavior too.
We'll use PlayerControlBehavior, because that enables it to pick up items and go to a destination.
Eventually, we'll need both.
// The main character, who starts at the left
const hero = new Actor({
appearance: new FilledCircle({ radius: 0.35, fillColor: "#9ca3af", lineWidth: 0.04, lineColor: "#d1d5db" }),
rigidBody: new CircleBody({ cx: 1.2, cy: 4.5, radius: 0.35 }, { dynamic: true }),
behaviors: new PlayerControlBehavior(),
});
Drawing Lots Of Hedges And Trees
It would be tedious to draw each hedge and tree by hand. Fortunately, we don't have to. The technique we'll use here has two parts. First, you'll set up a pair of arrays that have information about how to draw each hedge and each tree. Then you'll write a pair of loops that go through the arrays and use each entry to draw a hedge or tree.
First, you'll need to set up two arrays. For hedges, we'll use the left/top/right/bottom coordinates.
These are easy to figure out: just press b while the game is playing, open a console, and click where you want them to go. The console will show the coordinates you clicked, which makes the rest easy:
// The garden's hedges, as rectangles (left, top, right, bottom)
const HEDGES = [
{ l: 3.25, t: 0, r: 3.75, b: 3.2 },
{ l: 3.25, t: 5.8, r: 3.75, b: 9 },
{ l: 5.5, t: 2.0, r: 10.5, b: 2.5 },
{ l: 5.5, t: 6.5, r: 10.5, b: 7.0 },
{ l: 12.25, t: 1.5, r: 12.75, b: 4.1 },
{ l: 12.25, t: 4.9, r: 12.75, b: 7.5 },
{ l: 12.75, t: 1.5, r: 15.0, b: 2.0 },
{ l: 12.75, t: 7.0, r: 14.2, b: 7.5 },
{ l: 8.75, t: 0, r: 9.25, b: 1.1 },
{ l: 6.75, t: 7.9, r: 7.25, b: 9 },
{ l: 10.5, t: 3.6, r: 11.0, b: 5.4 },
];
// The garden's trees, as circles (center and radius)
const TREES = [
{ cx: 1.6, cy: 1.4, radius: 0.6 },
{ cx: 1.8, cy: 7.6, radius: 0.7 },
{ cx: 8.0, cy: 4.5, radius: 0.8 },
{ cx: 6.2, cy: 1.0, radius: 0.45 },
{ cx: 10.8, cy: 8.0, radius: 0.5 },
{ cx: 14.8, cy: 0.7, radius: 0.4 },
];
Now that the data is in place, all that remains is to loop over it and use each line to draw a tree or hedge:
// Draw the hedges as solid gray boxes
for (const hedge of HEDGES) {
new Actor({
appearance: new FilledBox({ width: hedge.r - hedge.l, height: hedge.b - hedge.t, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
rigidBody: new BoxBody({ cx: (hedge.l + hedge.r) / 2, cy: (hedge.t + hedge.b) / 2, width: hedge.r - hedge.l, height: hedge.b - hedge.t }),
behaviors: new SolidBehavior(),
});
}
// Draw the trees as solid gray circles
for (const tree of TREES) {
new Actor({
appearance: new FilledCircle({ radius: tree.radius, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
rigidBody: new CircleBody({ cx: tree.cx, cy: tree.cy, radius: tree.radius }),
behaviors: new SolidBehavior(),
});
}
Make Design Easier
To make it a little easier to work on the design of your game, it's a good idea to add text labels to things during development. The text labels help you (or a play-tester) to know what something is supposed to be, even if its graphics aren't implemented yet.
To use text, you'll first need to load a font.
Since you downloaded Arimo to your assets folder earlier, the easiest thing to do is use Arimo.
You'll need to add this inside of your Config class:
resources = {
fonts: ["Arimo-Regular.ttf"],
};
Now it's time to update the loops that draw the hedges and trees.
A nice feature that we can use here is arrays.
More specifically, you can give a single appearance, or an array of appearances, when creating an actor.
Arrays are delimited with square braces: [].
So in the code below, appearance gets an array with two components: a picture and some text.
// Draw the hedges as solid, labeled gray boxes
for (const hedge of HEDGES) {
new Actor({
appearance: [
new FilledBox({ width: hedge.r - hedge.l, height: hedge.b - hedge.t, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
new TextSprite({ center: true, face: "Arimo Regular", size: 14, color: "#f3f4f6" }, "hedge"),
],
rigidBody: new BoxBody({ cx: (hedge.l + hedge.r) / 2, cy: (hedge.t + hedge.b) / 2, width: hedge.r - hedge.l, height: hedge.b - hedge.t }),
behaviors: new SolidBehavior(),
});
}
// Draw the trees as solid, labeled gray circles
for (const tree of TREES) {
new Actor({
appearance: [
new FilledCircle({ radius: tree.radius, fillColor: "#6b7280", lineWidth: 0.04, lineColor: "#9ca3af" }),
new TextSprite({ center: true, face: "Arimo Regular", size: 14, color: "#f3f4f6" }, "tree"),
],
rigidBody: new CircleBody({ cx: tree.cx, cy: tree.cy, radius: tree.radius }),
behaviors: new SolidBehavior(),
});
}
For good measure, let's also put a label on the hero that the player controls:
// The main character, who starts at the left
const hero = new Actor({
appearance: [
new FilledCircle({ radius: 0.35, fillColor: "#9ca3af", lineWidth: 0.04, lineColor: "#d1d5db" }),
new TextSprite({ center: true, face: "Arimo Regular", size: 14, color: "#111827" }, "you"),
],
rigidBody: new CircleBody({ cx: 1.2, cy: 4.5, radius: 0.35 }, { dynamic: true }),
behaviors: new PlayerControlBehavior(),
});
End State
You're done with the second milestone! If you want to compare your code to mine, here's my Firefly Jar Milestone 2 code.
Here's my game, for you to test: