> game development toolkit
npm install gamedevjssh
$ npm install --save gamedevjs
`
Usage
Node
`js
var gamedev = require("gamedevjs").gamedev;
`
ES6
`js
// ES6
import {gamedev} from "gamedevjs";
`
Browser
CDN: https://cdn.jsdelivr.net/npm/gamedevjs
`html
`
Scheduler
$3
#### Schedule tasks at specific times
`js
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();
`
#### Schedule sequence tasks
`js
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);
`
#### Repeat
`js
// 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);
`
$3
#### Push task at specific time
`js
...
// 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);
`
#### Stop scheduler will terminate all running tasks
`js
scheduler.stop();
`
Finite State Machine
Inspired by javascript-state-machine (author: jakesgordon)
$3
NOTICE: event name must NOT be "any"
`js
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"
`
$3
`js
...
fsm.pushEvents({
die: {from: ["stand", "run", "walk"], to: "dead"}
});
fsm.events.die();
`
$3
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
`js
...
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();
`
$3
`js
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"
`
$3
`js
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);
`
Object Pool
$3
#### Item List
Predefine a list of items for object pool.
`js
var gamedev = require("gamedevjs").gamedev;
var pool = gamedev.pool.config({
items: [
{point: 0}
]
});
`
#### Sample Item
`js
...
var pool = gamedev.pool.config({
sampleItem: {point: 2}
sampleCount: 2
});
`
$3
Predefined items are added to the pool first, then sample items will be added later.
`js
var pool = gamedev.pool.config({
items: [
{point: 0}
],
sampleItem: {point: 2}
sampleCount: 2
});
`
$3
#### Get
`js
...
// 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
`js
...
// return item to the pool and trigger onreturn event
pool.returnItem(item);
`
#### Push
`js
...
// push item to the pool and trigger onpush event
pool.pushItem(item);
`
#### Clear
Clear all items
`js
...
pool.clear();
`
$3
- 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)
`js
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}
});
`
Event Manager
$3
`js
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();
`
$3
#### Common usage
`js
...
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); // 700
`
#### Register Once
`js
let 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); // 700
`
#### Register Replace
`js
let 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); // 200
`
#### Multiple Register
`js
let 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); // 500
`
#### Unregister list
`js
gamedev.event.unregisterList(["hit", "death"]);
`
#### Unregister All
`js
gamedev.event.unregisterAll();
``