Milestone 1: Walking Around

The first step in making your own version of Firefly Jar is to make the folder where your code will go. In your terminal, navigate to your my-jetlag-tutorials folder. Then type npm run create firefly_jar. Once it completes, type npm run serve firefly_jar to serve the current game, and point a browser at http://localhost:7000/firefly_jar/firefly_jar.html to see your game.

Of course, right now it's just the default game. Keep the server running, and keep the browser open. As you make changes to firefly_jar.ts, you'll see the game automatically re-build, and the browser will auto-refresh.

A Blank Slate

Now open your my-jetlag-tutorials folder in VSCode. Open the src/firefly_jar/firefly_jar.ts file. This is where you'll do all of your work.

As a first step, replace the entire contents of the file with this:

import { DebugToggle, initializeAndLaunch, JetLagGameConfig } from "jetlag2d";

/** Screen dimensions and other game configuration */
class Config implements JetLagGameConfig {
  aspectRatio = { width: 16, height: 9 };
  hitBoxes = true;
}

/**
 * Firefly Jar, milestone 1, step 0: the shell of every game: it runs, and there's nothing in it.
 */
function builder() {
  // Pressing 'b' will show hit boxes
  DebugToggle.enable();
}

// call the function that starts running the game in the `game-player` div
// tag of `index.html`
initializeAndLaunch("game-player", new Config(), builder);

If you read the last "Getting Started" chapter, this code should be familiar. It says your game has a 16x9 aspect ratio (the default for landscape games). The game has nothing in it, but the b button will show and hide hit boxes.

When you save, your browser should auto-refresh, and show a blank game.

Setting The Background Color

Now let's go ahead and add one line inside of the builder, right after the call to DebugToggle.enable. This line will set the initial background color for the game. Remember that the color is an HTML color code (a "hex code").

  // Set the color of the background
  stage.backgroundColor = "#0b1430";

You might find that when you typed stage and pressed the . key, VSCode updated your imports (the top line) to include the stage variable. If it didn't, and there is now an error under the word stage, then right-click it and choose the option that updates your imports. Without doing this, your code won't work.

Adding A Hero

Next, add these lines to draw a hero. The hero's physics shape is a circle, as is its appearance. Be sure to save, run the game, and press b to see the hit boxes. You'll notice that the hit box for a circle has a radial line, to help you see rotation.

  // The main character, who starts at the left
  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 }),
  });

Again, if you see errors related to the words Actor, FilledCircle, or CircleBody, be sure to update your imports. This is an issue that comes up a lot, so it's good to get in the habit of dealing with it now.

Make The Hero Move

Now let's try to make the hero move. You'll recall from "Getting Started" that we can do this with keyboard handlers. Let's start small: Press the right key to start moving rightward at 5 meters per second, and release to stop:

  stage.keyboard.setKeyDownHandler(KeyCodes.KEY_RIGHT, () => (hero.rigidBody.xVelocity = 5));
  stage.keyboard.setKeyUpHandler(KeyCodes.KEY_RIGHT, () => (hero.rigidBody.xVelocity = 0));

When you run this code, nothing happens. Can you think of why? The issue is that we did not change the hero's body type. The default body type is static, which means it cannot move. We could make it kinematic or dynamic. Dynamic is better here, because it can notify us when the hero collides with other actors. Here's how to change the hero to make it dynamic:

  // 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 }),
  });

With that code in place, your hero should be able to walk to the right. Now go ahead and replace those two lines with these 9 lines. They set up a constant for the speed, so it's easy to tune the hero speed from just one place. They also set up all four arrow keys:

  const speed = 5;
  stage.keyboard.setKeyDownHandler(KeyCodes.KEY_RIGHT, () => (hero.rigidBody.xVelocity = speed));
  stage.keyboard.setKeyUpHandler(KeyCodes.KEY_RIGHT, () => (hero.rigidBody.xVelocity = 0));
  stage.keyboard.setKeyDownHandler(KeyCodes.KEY_LEFT, () => (hero.rigidBody.xVelocity = -speed));
  stage.keyboard.setKeyUpHandler(KeyCodes.KEY_LEFT, () => (hero.rigidBody.xVelocity = 0));
  stage.keyboard.setKeyDownHandler(KeyCodes.KEY_UP, () => (hero.rigidBody.yVelocity = -speed));
  stage.keyboard.setKeyUpHandler(KeyCodes.KEY_UP, () => (hero.rigidBody.yVelocity = 0));
  stage.keyboard.setKeyDownHandler(KeyCodes.KEY_DOWN, () => (hero.rigidBody.yVelocity = speed));
  stage.keyboard.setKeyUpHandler(KeyCodes.KEY_DOWN, () => (hero.rigidBody.yVelocity = 0));

(Again, if you have import errors related to KeyCodes, be sure to fix them before testing your code!)

Fixing The Hero's Diagonal Movement

It's never too early to look for gameplay bugs. This code has one. If you only press right or left, the hero moves at 5 meters per second along the X axis. If you only press up or down, the hero moves at speed 5 along the Y axis. But if you press up and left at the same time, the hero moves at speed 5 on the X axis and also at speed 5 on the Y axis, for a total of just over 7 meters per second diagonally. That's not good. We can fix it by replacing those 9 lines with something slightly fancier, which uses the Pythagorean Theorem to figure out the right values of X and Y so that the diagonal is 5, and then uses an in-line function called updateVelocity to actually set the hero's speed:

  // The arrow keys say which way the hero wants to go.  `dx` is -1 (left), 0,
  // or 1 (right), and `dy` is -1 (up), 0, or 1 (down).
  //
  // The key handlers set or clear dx/dy, and a helper function computes the
  // actual velocity.
  const speed = 5;
  let dx = 0;
  let dy = 0;

  // Turn the direction into a velocity whose *length* is 5
  const updateVelocity = () => {
    const length = Math.hypot(dx, dy);
    // If the length is 0, dx == dy == 0, so we can't divide by length, we need
    // to just use 0.
    const scale = length > 0 ? speed / length : 0;
    hero.rigidBody.xVelocity = dx * scale;
    hero.rigidBody.yVelocity = dy * scale;
  };

  stage.keyboard.setKeyDownHandler(KeyCodes.KEY_RIGHT, () => { dx = 1; updateVelocity(); });
  stage.keyboard.setKeyUpHandler(KeyCodes.KEY_RIGHT, () => { dx = 0; updateVelocity(); });
  stage.keyboard.setKeyDownHandler(KeyCodes.KEY_LEFT, () => { dx = -1; updateVelocity(); });
  stage.keyboard.setKeyUpHandler(KeyCodes.KEY_LEFT, () => { dx = 0; updateVelocity(); });
  stage.keyboard.setKeyDownHandler(KeyCodes.KEY_UP, () => { dy = -1; updateVelocity(); });
  stage.keyboard.setKeyUpHandler(KeyCodes.KEY_UP, () => { dy = 0; updateVelocity(); });
  stage.keyboard.setKeyDownHandler(KeyCodes.KEY_DOWN, () => { dy = 1; updateVelocity(); });
  stage.keyboard.setKeyUpHandler(KeyCodes.KEY_DOWN, () => { dy = 0; updateVelocity(); });

Be sure to read the comments for that code. In particular, the code needed to make sure not to accidentally divide by zero!

End State

Congratulations, you've completed the first milestone. If you want to compare your code to mine, here's my Firefly Jar Milestone 1 code.

Your game should play the same as mine. You can test it here: