entitydef

Simple JSON Definition Loader using findit

defloader

This is a small node utility built using findit to recursively walk a directory and find any JSON files in any of the child directories and pass the parsed JSON into the constructor for the item.

Data should be specified in the following format:

{
    "phrases": [
        "Hello there",
        "We don't need to see your identification",
        "You are not the player we're looking for",
        "Move along, move along..."
    ],
    
    "state": {
        "health": 50
    }
}

Any items in the core definition will be mapped to the prototype definition which will reduce the memory footprint of your application when you have thousands of entities running around. Any data that is considered instance specific should be included in the state member.

function NPC(opts) {
    opts = opts || {};
    
    this.phraseIndex = 0;
}
 
NPC.prototype = {
    sayfunction() {
        if (this.phraseIndex >= this.phrases.length) {
            this.phraseIndex = 0;
        }
        
        return this.phrases[this.phraseIndex++];
    }
};
 
loader('data/npc', NPC)
    .on('item', function(itemPathClassinitialState) {
        // create the new npc 
        var npc = new Class(initialState);
        
        // this a new NPC and will have the say method 
        npc.say();
    })
    .on('end', function() {
        expect(items).to.have.length(1);
        done();
    });