The Game.onTimePass
callback is the main way to plug logic that must run “all the time”. Each call comes with a dt
variable that holds the fraction of a second that has passed in a given loop. The main way to use it is to multiply any time-dependent value by it. For example:
Game.onTimePass = (dt) => {
for (let playerId of Object.keys(boostingPlayers)) {
let blob = playerBlobs[playerId]
blob.size = blob.size - 0.25 * dt
// ...
}
}
This way, the script decreases the boosting player’s blob size by 0.25 per second — which is simple to reason about and consistent with built-in props related to time like velocity
.
But there’s another way to use dt
— to implement timers in order to track how much time has passed between specific events in the game. Take a look at the following example:
let gameTime = 0
let playerStates = {}
Game.onTimePass = (dt) => {
gameTime = gameTime + dt
for (let player_state of Object.values(playerStates)) do
if (playerState.boost && (gameTime - playerState.boostedAt > 5)) {
playerState.boost = false
playerState.boostedAt = null
playerState.blob.speed = 10
}
}
}
Game.onAction = (player, enabled) => {
if (!enabled) return
let playerState = playerStates[player.id]
playerState.boost = true
playerState.boostedAt = gameTime
playerState.blob.speed = 100
}
First off, we keep the overall game time in gameTime
. Then we use it to measure how much time has passed since players have started the boost. Finally, we act upon this “timer” to stop the boost after 5 seconds.
You could go further and make the blob slow down gradually by mixing the boosting and regular speeds using the time difference. Also, consider adding some easing function to the mix for a more realistic result.