Whoa, we’ve created a complete game with custom stage setup, objects, animation, input handling, collisions and feedback. Plus — like all games powered by Heyplay — it’s massively multiplayer by default and it works on all popular desktop & mobile platforms with all possible input devices.
Best of all, it’s just ~100 lines of code! Here’s the complete script in case you’ve missed something along the way or just want to grab the whole thing:
import { Blob, Game, Message, Player, Vector } from "@heyplay/iokit"
const playerBlobs: { [key: string]: Blob } = {}
const blobPlayers: { [key: string]: Player } = {}
const boostingPlayers: Set<string> = new Set()
Game.onStart = () => {
for (let i = 1; i <= 5000; i++) {
Blob.create({
position: new Vector({ x: Math.random() * 1000 - 500, y: Math.random() * 1000 - 500 }),
size: 1,
color: "yellow",
category: "food"
})
}
}
Game.onTimePass = (dt: number) => {
for (let playerId of boostingPlayers) {
let blob = playerBlobs[playerId]
blob.size = Math.max(3, blob.size - 0.25 * dt)
if (blob.size <= 3) {
blob.speed = 5
boostingPlayers.delete(playerId)
}
}
}
Player.onJoin = (player: Player) => {
let blob = Blob.create({
position: new Vector({ x: (parseInt(player.id) - 1) * 20, y: 0 }),
size: 3,
velocity: new Vector({ x: 0, y: 5 }),
category: "player"
})
player.camera.follow(blob)
playerBlobs[player.id] = blob
blobPlayers[blob.id] = player
}
Player.onSteer = (player: Player, dir: number) => {
playerBlobs[player.id].dir = dir
}
Player.onAction = (player, enabled) => {
playerBlobs[player.id].speed = enabled ? 20 : 5
if (enabled) {
boostingPlayers.add(player.id)
} else {
boostingPlayers.delete(player.id)
}
}
Player.onDeath = (player: Player) => {
let blob = playerBlobs[player.id]
blob.destroy()
delete playerBlobs[player.id]
delete blobPlayers[blob.id]
boostingPlayers.delete(player.id)
}
Blob.onCollision = (blobA: Blob, blobB: Blob) => {
if (blobA.category === "food" && blobB.category === "player") {
onCollisionFoodVsPlayer(blobA, blobB);
}
if (blobA.category === "player" && blobB.category === "player") {
onCollisionPlayerVsPlayer(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()
let player = blobPlayers[playerBlob.id]
player.score = player.score + 10
Message.print("yummy!", { player })
}
const onCollisionPlayerVsPlayer = (winnerBlob: Blob, loserBlob: Blob) => {
if (winnerBlob.size < loserBlob.size) {
let tempBlob = winnerBlob
winnerBlob = loserBlob
loserBlob = tempBlob
}
let winner = blobPlayers[winnerBlob.id]
winnerBlob.size = Math.sqrt(Math.pow(winnerBlob.size/2, 2) + Math.pow(loserBlob.size/2, 2)) * 2
winner.score = winner.score + 100
Message.print("super yummy!", { player: winner })
let loser = blobPlayers[loserBlob.id]
loser.kill()
Message.print("you got killed!", { player: loser })
}
You can now release the game by clicking the rocket button on the toolbar. Once you do, the status of the game will change from WIP (work in progress) to Released, which means that this is now the version that Heyplay will use when players hit Play on the game’s page.
Note that you may open the Versions pane in order to switch between the released script (to take a look as it’s not editable) and the next WIP — which you may edit and test without affecting or breaking the released game.