Skip to content

Game development toolkit for js game developer, a collection of common components

License

Notifications You must be signed in to change notification settings

nshermione/gamedevjs

Repository files navigation

gamedevjs NPM version Build Status

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

Table of contents

Installation

$ npm install --save gamedevjs

Usage

Node

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>

Scheduler


Creation


Schedule tasks at specific times

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

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

// 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);

Manipulation


Push task at specific time

...
// 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

scheduler.stop();

Finite State Machine

Inspired by javascript-state-machine (author: jakesgordon)


Creation

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"

Manipulation

...
fsm.pushEvents({
    die: {from: ["stand", "run", "walk"], to: "dead"}
});

fsm.events.die();

Event Handler

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();

Cancel Event Handler

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"

Async Event Handler

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


Creation

Item List

Predefine a list of items for object pool.

var gamedev = require("gamedevjs").gamedev;

var pool = gamedev.pool.config({
    items: [
        {point: 0}
    ]
});

Sample Item

...

var pool = gamedev.pool.config({
    sampleItem: {point: 2}
    sampleCount: 2
});

Combine

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
});

Manipulation

Get

...

// 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

...
// return item to the pool and trigger onreturn event
pool.returnItem(item);

Push

...
// push item to the pool and trigger onpush event
pool.pushItem(item);

Clear

Clear all items

...
pool.clear();

Events

  • 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}
});

Event Manager

Creation

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();

Manipulation

Common usage

...
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

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

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

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

gamedev.event.unregisterList(["hit", "death"]);

Unregister All

gamedev.event.unregisterAll();

License

ISC ©

Author

Thinh Tran

Donation

Donate

About

Game development toolkit for js game developer, a collection of common components

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published