Last active
December 21, 2017 04:58
-
-
Save doughsay/5a6c72590c62ec0d4186edc8f193efaf to your computer and use it in GitHub Desktop.
node.js event based game loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bigInt = require('big-integer') | |
function getCurrentMonoTime () { | |
const [s, n] = process.hrtime() | |
return bigInt(s).times(1000000000).plus(n) | |
} | |
const bootGameTime = 0 | |
const bootMonoTime = getCurrentMonoTime() | |
function getCurrentGameTime () { | |
const msSinceBoot = getCurrentMonoTime().minus(bootMonoTime).divide(1000000).toJSNumber() | |
return bootGameTime + msSinceBoot | |
} | |
let watchdog = null | |
let events = [] | |
function spawnWatchdog (targetTime) { | |
let now = getCurrentGameTime() | |
watchdog = setTimeout(function () { | |
watchdogCb(targetTime) | |
}, targetTime - now) | |
} | |
function killWatchdog () { | |
clearTimeout(watchdog) | |
} | |
function refreshWatchdog () { | |
if (events.length > 0) { | |
const minT = Math.min(...events.map(e => e.t)) | |
spawnWatchdog(minT) | |
} | |
} | |
function watchdogCb (t) { | |
const currentGameTime = getCurrentGameTime() | |
console.log(`woke up! currentGameTime: ${currentGameTime}`) | |
events = events.filter(e => { | |
if (currentGameTime >= e.t) { | |
// run it and remove it | |
e.fn(currentGameTime) | |
return false | |
} else { | |
// keep it | |
return true | |
} | |
}) | |
refreshWatchdog() | |
} | |
function schedule (fn, t) { | |
killWatchdog() | |
events.push({ fn, t }) | |
refreshWatchdog() | |
} | |
// test it | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 0 and it is currently ${t}`) | |
}, 0) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 0 and it is currently ${t}`) | |
}, 0) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 0 and it is currently ${t}`) | |
}, 0) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 0 and it is currently ${t}`) | |
}, 0) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 1000 and it is currently ${t}`) | |
}, 1000) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 1010 and it is currently ${t}`) | |
}, 1010) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 1020 and it is currently ${t}`) | |
}, 1020) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 1030 and it is currently ${t}`) | |
}, 1030) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 2000 and it is currently ${t}`) | |
}, 2000) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 3000 and it is currently ${t}`) | |
}, 3000) | |
schedule(function (t) { | |
console.log(`I was scheduled for t = 10000 and it is currently ${t}`) | |
}, 10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment