Editor documentation

Heyplay engine provides a powerful built-in collision detection & physics, so let’s give it a try and provide players with a purpose by letting them collect the yellow blobs.

Make following changes to the script from the previous chapter:

Game.onStart = () => {
  for (let i = 1; i <= 5000; i++) {
    Blob.create({
      // ...
      category: "food"
    })
  }
}

Player.onJoin = (player: Player) => {
  let blob = Blob.create({
    // ...
    category: "player"
  })

  // ...
}

Blob.onCollision = (blobA: Blob, blobB: Blob) => {
  if (blobA.category === "food" && blobB.category === "player") {
    onCollisionFoodVsPlayer(blobA, blobB);
  }
}

const onCollisionFoodVsPlayer = (foodBlob: Blob, playerBlob: Blob) => {
  playerBlob.size = Math.sqrt(Math.pow(playerBlob.size/2, 2) + Math.pow(foodBlob.size/2, 2)) * 2
  foodBlob.destroy()
}

This time we’ve made following changes:

  1. We tag those 5K yellow blobs from before with the "food" category.

  2. We tag player blobs with the "player" category.

  3. We define @heyplay/iokit:Blob.onCollision to handle collisions between player & food.

Note that food blob comes first because they come back sorted by category in ascending order.

Now, food will get destroyed as player hits it, making player’s blob absorb its area (literally, hence that fancy equation that relies on Math.sqrt).

Here’s another set of changes to put some highly needed final touches:

const blobPlayers: { [key: string]: Player } = {}

// ...

Player.onJoin = (player: Player) => {
  // ...

  blobPlayers[blob.id] = player
}

const onCollisionFoodVsPlayer = (foodBlob: Blob, playerBlob: Blob) => {
  // ...

  let player = blobPlayers[playerBlob.id]
  player.score = player.score + 10
  Message.print("yummy!", { player })
}

And explanation:

  1. We define the blobPlayers map to hold each blob’s player (reverse to playerBlobs that we’ve already added before).

  2. We store each blob’s player in the table after creating it.

  3. We look it up to update the player score and present a happy message to that player.

Next chapter: Always.