npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gamedevjs

v1.1.18

Published

> game development toolkit

Downloads

89

Readme

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