https://github.com/abrie/gameloop
A typed and testable game loop for NodeJS or Browser environments.
https://github.com/abrie/gameloop
browser games javascript nodejs
Last synced: 3 months ago
JSON representation
A typed and testable game loop for NodeJS or Browser environments.
- Host: GitHub
- URL: https://github.com/abrie/gameloop
- Owner: abrie
- License: mit
- Created: 2020-08-11T15:37:47.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-12T16:54:56.000Z (almost 5 years ago)
- Last Synced: 2025-02-06T14:51:05.724Z (4 months ago)
- Topics: browser, games, javascript, nodejs
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@eirba/gameloop
- Size: 102 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Reusable Gameloop Package
Provides a lean, typed, and testable gameloop for use in browsers and node environments. This is implementation follows the ['fix-your-timestep'](https://gafferongames.com/post/fix_your_timestep/) pattern popularized by [Gaffer on Games](https://gafferongames.com/). To use this lib, provide a frame driver (such as `window.requestAnimationFrame`) and implement the following callbacks:
- `integrate`: Apply physics or motion rules.
- `interpolate`: Account for leftover accumulator time.
- `reconcile`: Check for collisions, process inputs, and adjucate world state.
- `render`: Draw to the screen.## Install
Install the package:
- `npm install @eirba/gameloop -S`
- `yarn add @eirba/gameloop -S`
- `pnpm add @eirba/gameloop`
- or some other package manager...## Sample Code
Here is an example using `window.requestAnimationFrame()`:
```javascript
import { Game, NewGameLoop } from '@eirba/gameloop';const game: Game = {
reconcile: () => scene.reconcile(),
integrate: (t, dt) => scene.integrate(t, dt),
interpolate: (alpha) => scene.interpolate(alpha),
render: () => renderManager.render(scene.renderables()),
};const gameLoop = NewGameLoop(game, 1 / 60); // 60FPS
const onAnimationFrame = (time: number) => {
gameLoop.nextFrame(time);
window.requestAnimationFrame(onAnimationFrame);
};window.requestAnimationFrame(onAnimationFrame);
```