Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mgan59/game-loop-dispatch
A low level game-loop for nodejs that provides the base events for building/extending using event-emitter
https://github.com/mgan59/game-loop-dispatch
Last synced: 2 days ago
JSON representation
A low level game-loop for nodejs that provides the base events for building/extending using event-emitter
- Host: GitHub
- URL: https://github.com/mgan59/game-loop-dispatch
- Owner: mgan59
- License: mit
- Created: 2013-10-01T13:11:37.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2013-10-01T14:26:19.000Z (about 11 years ago)
- Last Synced: 2024-10-29T16:59:12.734Z (20 days ago)
- Language: JavaScript
- Size: 113 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Game Loop Dispatch
==================A low level game-loop for nodejs that provides the basic events. Game Loop Dispatch is small maybe 40 lines of code and just extends node's built-in EventEmitter. Being able to modify/extend the event hooks one can customize the loop for their game.
# Install
Use npm
npm install game-loop-dispatch
# Usage
The below code samples are included in the `examples` folder and can be run with the following
node examples/basic.js
node examples/advanced.js## Basic Usage
To start with just include and instantiate the game loop
var GameLoopDispatch = require('game-loop-dispatch');
var gameLoop = new GameLoopDispatch({
// only require value is the interval in milliseconds
'interval':1000
});gameLoop.tick = function(){
console.log('tick');
};gameLoop.start();
You should see in a stdout every second
tick
tick
tick## Advanced Usage
Advanced usage showing how you can add an additional event hooks to extend the loop functionality. In this example we will add `tickStart` and `tickEnd` so that we can pause/start the gameloop while it is processing async calls. Otherwise the loop would continue to fire every 1 sec and things would get out of control.
var GameLoopDispatch = require('game-loop-dispatch');
var gameLoop = new GameLoopDispatch({
// only require value is the interval in milliseconds
'interval':1000
});gameLoop.tick = function(){
// hack to rescope this because of setTimeout
//var self = this;// GameLoopDispatch extends eventEmitter so `emit` is
// provided internally. In this example a `tickStart`
// event is emited at the start of every `tick` event
this.emit('tickStart');
// our standard tick marker
console.log('tick');// imagine this was some async call to a processingQueue
// for AI Entities or Player Movements that takes 4 seconds
// to process
setTimeout(function(){
// now that everything was process broadcast
// an event for tickEnd
// note we use gameLoop because `this` is wrong context
gameLoop.emit('tickEnd');
}, 4000);
};//
// Create Listeners for our new Event hooks
//
gameLoop.on('tickStart', function(){
console.log('top of tick phase');
// lets pause the loop while we process
gameLoop.pause();});
gameLoop.on('tickEnd', function(){
console.log('end of tick phase');
// now that tick is complete lets restart the loop
gameLoop.start();
});gameLoop.start();
## Todo / Contributions
This is an initial release as I'm prototyping a game. Some additional low-level events are still needed such as some 'loader' event system for static content and or other async behavior. Open to suggestions and ideally would like to build this like lodash where one can generate the game-loop function hooks they need.