npm.io
2.0.0 • Published 5 years ago

accurate-game-loop

Licence
ISC
Version
2.0.0
Deps
0
Size
17 kB
Vulns
0
Weekly
0
Stars
6

Accurate Game Loop

A library for nodejs game loops (not meant for browser), this package is pretty much the most accurate you can get with nodejs

Install

npm install accurate-game-loop

Usage / Documentation

const Loop = require('accurate-game-loop');

Creating a loop

const timesPerSecond = 60;
const update = () => {
    // do something smart
};
const loop = new Loop(update, timesPerSecond);

Starting a loop

loop.start();
// or
const loop = new Loop(...params).start();

Stopping a loop

loop.stop();

Options:

new Loop(
    update,
    timesPerSecond,
    {...options}
)

Logs

This automatically enables every type of log internally (Useful for debug)

Implementation:
new Loop(
    update,
    timesPerSecond,
    { logs: true }
)
Example:

Screenshot

Delta Log

This automatically logs the time differences between the update calls
Implementation:
new Loop(
    update,
    timesPerSecond,
    { delta_log: true }
)
Example:

Screenshot

Difference Log

This automatically logs the time shifts that the code makes internally
Implementation:
new Loop(
    update,
    timesPerSecond,
    { dif_log: true }
)
Example:

Screenshot

Time Function

This allows you to specify the time function you want (expects it to return in nanoseconds)
Example / Implementation:
new Loop(
    update,
    timesPerSecond,
    { time_fn: () => Date.now() * 1e6 }
)
If you have any questions, you can make a github issue or dm me on discord at ZeroTix#6300 (I respond quick)

Keywords