game development toolkit
A collection of common components which are widely used in game development:
- Scheduler
- Finite State Machine
- Object Pool
- Event Manager
(other components will be published and documented soon)
This project is evolving and need your feedback, please create Issue
$ npm install --save gamedevjsNode
var gamedev = require("gamedevjs").gamedev;ES6
// ES6 import {gamedev} from "gamedevjs";Browser CDN: https://cdn.jsdelivr.net/npm/gamedevjs
<script src="https://cdn.jsdelivr.net/npm/gamedevjs"></script> <script> var gamedev = window.gamedevjs.gamedev; </script>var scheduler = gamedev.scheduler.config([ {at: 200, run: function() {console.log("Hello 200ms")} }, {at: 1300, run: function() {console.log("Hello 1300ms")} }, {at: 5000, run: function() {console.log("Hello 5000ms")} }, ]); scheduler.start();var scheduler = gamedev.scheduler.sequence([ {duration: 1100, run: function() {console.log("Hello 200ms")} }, {duration: 3700, run: function() {console.log("Hello 1300ms")} }, {duration: 100, run: function() {console.log("Hello 5000ms")} }, ]); // start scheduler with delay time scheduler.start(200);// start scheduler with no delay time, repeat 3 times scheduler.start(0, 3); // start scheduler with no delay time, repeat forever scheduler.start(0, -1); // start scheduler with 200ms delay time, repeat 2 times // and delay 200ms between each scheduled times scheduler.start(200, 2);... // push and schedule addional task scheduler.pushStep({ at: 4000, run: function() { console.log("Hello 4000ms"); } }) // or we can push task when scheduler is running // and make sure scheduler has not reached new schedule time setTimeout(function() { scheduler.pushStep({ at: 4000, run: function() { console.log("Hello 4000ms"); } }) }, 3000);scheduler.stop();Inspired by javascript-state-machine (author: jakesgordon)
NOTICE: event name must NOT be "any"
var fsm = gamedev.fsm.config({ initial: "stand", events: { move: {from: ["stand", "run"], to: "walk"}, movefast: {from: ["stand", "walk"], to: "run"}, stop: {from: ["walk", "run"], to: "stand"}, } }); console.log(fsm.current); // "stand" fsm.events.move(); console.log(fsm.current); // "walk"... fsm.pushEvents({ die: {from: ["stand", "run", "walk"], to: "dead"} }); fsm.events.die();Event execution orders:
- beforeany - fired before any event
- beforeEVENT - fired before the event
- leaveSTATE - fired when leaving the old state (fsm.current is still old state)
- leaveany - fired when leaving any old state (fsm.current is still old state)
- enterany - fired when entering any new state (fsm.current is new state)
- enterSTATE - fired when entering the new state (fsm.current is new state)
- afterEVENT - fired after the event
- afterany - fired after any event
... fsm.events.beforemove = function() { console.log("before move"); } fsm.events.leavestand = function() { console.log("leave stand"); } fsm.events.enterwalk = function() { console.log("enter walk"); console.log(fsm.previous); // previous state console.log(fsm.current); // current state } fsm.events.aftermove = function() { console.log("after move"); } // or use registerEvent to pass this object fsm.registerEvent("beforemove", function() { console.log("before move with registerEvent"); }, thisObject); fsm.events.move();var EventStatus = require("gamedevjs").EventStatus; ... fsm.events.beforemove = function() { console.log("before move"); return EventStatus.CANCEL; } fsm.events.move(); console.log(fsm.current); // still be "stand"var EventStatus = require("gamedevjs").EventStatus; ... fsm.events.beforemove = function(next) { console.log("before move"); setTimeout(function() { next(); }, 2000); return EventStatus.ASYNC; } fsm.events.move(); console.log(fsm.current); // still be "stand" setTimeout(function() { console.log(fsm.current); // "walk" now }, 3000);Predefine a list of items for object pool.
var gamedev = require("gamedevjs").gamedev; var pool = gamedev.pool.config({ items: [ {point: 0} ] });... var pool = gamedev.pool.config({ sampleItem: {point: 2} sampleCount: 2 });Predefined items are added to the pool first, then sample items will be added later.
var pool = gamedev.pool.config({ items: [ {point: 0} ], sampleItem: {point: 2} sampleCount: 2 });... // get available item item = pool.getItem(); // if pool is empty, it will clone sampleItem // if sampleItem is undefined, item2 is undefined as well item2 = pool.getItem();... // return item to the pool and trigger onreturn event pool.returnItem(item);... // push item to the pool and trigger onpush event pool.pushItem(item);Clear all items
... pool.clear();- onpush: trigger when item is pushed to pool (init pool or push new items to pool)
- onget: trigger when item is retrieved from pool (get available item)
- onreturn: trigger when item is returned to pool (return item to pool)
pool = gamedev.pool.config({ onreturn: function(item) { console.log("return item"); }, onget: function(item) { console.log("get item"); }, onpush: function(item) { console.log("push item"); }, sampleItem: {point: 0} });var gamedev = require("gamedevjs").gamedev; var EventManager = require("gamedevjs").EventManager; // global event manager var event = gamedev.event; // or create new instance var event = new EventManager();... let isHit = false; let heath = 1200; gamedev.event.register("hit", (data) => { isHit = true; heath -= data.damage; }); gamedev.event.emit("hit", {damage: 500}); console.log(heath); // 700 gamedev.event.unregister("hit"); gamedev.event.emit("hit", {damage: 500}); console.log(heath); // 700let isHit = false; let heath = 1200; gamedev.event.registerOnce("hit", (data) => { isHit = true; heath -= data.damage; }); gamedev.event.emit("hit", {damage: 500}); console.log(heath); // 700 gamedev.event.emit("hit", {damage: 500}); console.log(heath); // 700let isHit = false; let heath = 1200; gamedev.event.register("hit", (data) => { isHit = true; heath -= data.damage; }); gamedev.event.register("hit", (data) => { isHit = true; heath -= data.damage * 2; }, true); // set third parameter to true gamedev.event.emit("hit", {damage: 500}); console.log(heath); // 200let isHit = false; let heath = 1200; gamedev.event.register("hit", (data) => { isHit = true; heath -= data.damage; }); gamedev.event.register("hit", (data) => { isHit = true; heath -= 200; }); gamedev.event.emit("hit", {damage: 500}); console.log(heath); // 500gamedev.event.unregisterList(["hit", "death"]);gamedev.event.unregisterAll();ISC ©
