micro-game-shell v0.9.0
micro-game-shell
A small game shell. Provides reliable timing events, and manages pointerlock/fullscreen/resize.
This library provides two sets of timing events: tick, which can fire at any specified rate, and render, which fires on browser frame updates (but can be throttled to a lower rate).`
The intention is that game clients can use these events to implement fixed timesteps, where game logic (e.g. physics) runs at a different cadence than rendering.
Usage
npm i --save micro-game-shellimport { MicroGameShell } from 'micro-game-shell'
// instantiate
var document.querySelector('#game') // optional
var pollRate = 10 // optional
var shell = new MicroGameShell(domElement, pollRate)
// settings
shell.tickRate = 30 // ticks/second
shell.maxRenderRate = 0 // fps - 0 means uncapped
shell.stickyPointerLock = false // whether to request pointerLock on click
shell.stickyFullscreen = false // ditto for fullscreen
shell.maxTickProcessingTime = 25 // see below
// add event callbacks
shell.onTick = (tickDur) => { /* ... */ }
shell.onRender = (dt, fp) => { /* ... */ }
// see below for rest of eventsAPI
Consctructor params:
domElement- a DOM element, for pointerLock/fullscreen. If none is specified, shell instance will ignore DOM features, and just issue timing events.pollTime(10ms) - how often to poll for ticks. Set to0if you want to disable polling.
Instance properties:
tickRate- desired tick events per second. Default30maxRenderRate- upper limit for the rate of render events. Default of0means uncapped.pointerLock- check or set whether the DOM element has pointerlockfullscreen- check or set whether the DOM element has fullscreenstickyPointerLock- shell will request pointerLock when the domElement is clicked. defaultfalsestickyFullscreen- shell will request fullscreen when the domElement is clicked. defaultfalsemaxTickTime- max time spent issuing tick events when behind schedule. If the shell spends this long on tick events, it will discard all pending ticks to catch up. Default100ms.
Events:
onTick(tickDur)- tick event handler.tickDuris a fixed constant value (ms), not the actual time elapsed.onRender(dt, framePart, tickTime)- render event handler.dtis actual time since the last render (in ms), andframepartis the fractional number of game ticks (i.e. how far we are into the current tick).onInit()- fires once, when shell is set up (afterDOMReady)onResize()- fires when dom element's window changes sizeonPointerLockChanged(hasPL)- fires on gaining/losing pointerLockonFullscreenChanged(hasFS)- fires on gaining/losing fullscreenonPointerLockError(err)- fires when pointerLock is refused by the browser
Credits
Made with 🍺 by Andy Hall, license is ISC.