Sooner or later you’ll need to make your game aware of the passage of time, having to plug some extra logic that executes “all the time” or “once upon a time”.
For instance, our current game has one serious flaw: players may boost forever, making the no-boost movement useless. Let’s force them to plan their boosting more carefully by reducing their size while they boost and disabling it altogether once they hit the minimum size.
Here come the changes:
const boostingPlayers: Set<string> = new Set()
// ...
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.onAction = (player: Player, enabled: boolean) => {
// ...
if (enabled) {
boostingPlayers.add(player.id)
} else {
boostingPlayers.delete(player.id)
}
}
And here’s what they’re all about:
-
We declare a
boostingPlayer
set. -
We define
@heyplay/iokit:Game.onTimePass
that Heyplay engine calls on every loop withdt
being the time that has passed in seconds. In it, we loop boosting players and reduce the size of their blobs by 0.15 per sec and, once it hits the minimum value of 3, we disable the boost. -
We add & remove players from the
boostingPlayer
set as they toggle the action button.
Play the game to see the boosting limits in action.