Created
January 9, 2016 13:09
-
-
Save SReject/8ec1bd65216daa79187b to your computer and use it in GitHub Desktop.
Aspect List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
small library for handling Thaumcraft 4.2.2 aspects | |
Constructor: | |
@base - (object) - Required | |
Containing a key-value pair of aspects and their make-up components or false for primals | |
@compile - (boolean) - optional; default to true | |
If true the working aspect list will be compiled from the base | |
Functions: | |
compile() - (undefined) | |
Builds a new list of aspects working from the base list and enabled addons | |
addonAdd(name, aspectList, options) - (undefined) | |
Adds the addon for use when compiling the aspect list | |
@name - (string) - required | |
addon name | |
@aspectList - (object) - required | |
a keyvalue pairing of each aspect and its components or false for primals | |
@options - (object) - optional | |
@enable - (boolean) - optional | |
if true or not specified the addon will be enabled for calls to compile() | |
@compile - (boolean) - optional | |
if true or not specified the aspect list will be recompiled | |
addonEnable(name, compile) - (undefined) | |
@name - (string) - required | |
addon name to enable; | |
@compile - (boolean) - optional | |
If true or not specified, the aspect list will be recompiled | |
addonDisable(name, compile) - (undefined) | |
@name - (string) - required | |
addon name to disable | |
@compile - (boolean) - optional | |
if true or not specified, the aspect list will be recompiled | |
has(aspect) - (boolean) | |
Returns true if the specified aspect is in the compiled list | |
@aspect - (string) - required | |
Aspect to check | |
components(aspect) - (boolean or array) | |
Returns the components of the specified aspect or false if primal | |
@aspect - (string) - required | |
breakdown(aspect) - object | |
Returns a keyvalue pairing of each primal aspect and the count of said primal that makes up the aspect | |
@aspect - (string) - required | |
*/ | |
(function () { | |
'use strict'; | |
function AspectList(base, compile) { | |
this.baseList = base; | |
this.addons = {}; | |
if (compile !== false) { | |
this.compile(); | |
} | |
} | |
AspectList.prototype = { | |
// stores the specified addon aspect list | |
addonAdd: function (addonName, aspectList, options) { | |
options = options || {}; | |
this.addons[addonName] = { | |
enable: options.enable || false, | |
aspects: aspectList | |
}; | |
if (options.compile !== false) { | |
this.compile(); | |
} | |
}, | |
// enables the addon's aspects when compiling the aspect list | |
addonEnable: function (addonName, rebuild) { | |
// check if addon exists | |
if (!this.addons.hasOwnProperty(addonName)) { | |
throw new Error("Unknown Addon: " + addonName); | |
} | |
// if addon is disabled, enable it and rebuild the compiled aspect list if requested | |
if (!this.addons[addonName].enable) { | |
this.addons[addonName].enable = true; | |
if (rebuild) { | |
this.compile(); | |
} | |
} | |
}, | |
// disables the addon's aspects when compiling the aspect list | |
addonDisable: function (addonName, rebuild) { | |
// check if addon exists | |
if (!this.addons.hasOwnProperty(addonName)) { | |
throw new Error("Unknown Addon: " + addonName); | |
} | |
// if addon is enabled; disable it and rebuild the compiled list if requested | |
if (this.addons[addonName].enable) { | |
this.addons[addonName].enable = false; | |
if (rebuild) { | |
this.compile(); | |
} | |
} | |
}, | |
// function to compile usable aspect list | |
compile: (function () { | |
// loop over each aspect in the specified aspect list | |
function addAspects(buildList, aspectList) { | |
var aspect, aspectComponents; | |
for (aspect in aspectList) { | |
if (aspectList.hasOwnProperty(aspect)) { | |
aspectComponents = aspectList[aspect]; | |
// if the aspect is a primal; simply add it with a false boolean value | |
if (!aspectComponents) { | |
buildList[aspect] = false; | |
// otherwise make a copy of the compenent array and add it for the aspect's value | |
} else { | |
buildList[aspect] = [aspectComponents[0], aspectComponents[1]]; | |
} | |
} | |
} | |
// return the updated aspect list | |
return buildList; | |
} | |
return function () { | |
var aspectList, addonName, addon; | |
// add base aspects to the compiled list | |
aspectList = addAspects({}, this.baseList); | |
// loop over each addon | |
for (addonName in this.addons) { | |
if (this.addons.hasOwnProperty(addonName)) { | |
addon = this.addons[addonName]; | |
// check if addon is enabled, and if so add its aspects to the compiled list | |
if (addon.enable) { | |
aspectList = addAspects(aspectList, addon.aspects); | |
} | |
} | |
} | |
// update the stored compiled list | |
this.compiledList = aspectList; | |
}; | |
}()), | |
// returns true of the compiled list has the specified aspect | |
has: function (aspect) { | |
return this.compiledList.hasOwnProperty(aspect); | |
}, | |
// returns the components that make up an aspect | |
components: function (aspect) { | |
if (!this.has(aspect)) { | |
throw new Error("Unknown Aspect: " + aspect); | |
} | |
return this.compiledList[aspect]; | |
}, | |
// returns the count of each primal that makes up an aspect | |
breakdown: (function () { | |
// enclosed variables | |
var primals, self; | |
// function to recursively break down aspects into their components ending with only primals | |
function walk(aspect) { | |
// if the specified aspect has components; call the walk function on each component | |
var parts = self.components(aspect); | |
if (parts !== false) { | |
walk(parts[0]); | |
walk(parts[1]); | |
// otherwise add 1 for the primal of the object we'll be returning | |
} else { | |
primals[aspect] = primals.hasOwnProperty(aspect) ? primals[aspect] + 1 : 1; | |
} | |
} | |
// wrapper function | |
return function (aspect) { | |
// setup for recursive break down | |
self = this; | |
primals = {}; | |
// call the function that will handle the breaking down of aspects | |
walk(aspect); | |
// return the list of primals | |
return primals; | |
}; | |
}()) | |
}; | |
// add to module.exports if ran under a node-esq enviornment | |
if (typeof module !== 'undefined' && this.module !== module) { | |
module.exports = AspectList; | |
// otherwise add to 'this' | |
} else { | |
this.AspectList = AspectList; | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment