Index: binaries/data/mods/public/globalscripts/DamageTypes.js =================================================================== --- binaries/data/mods/public/globalscripts/DamageTypes.js +++ binaries/data/mods/public/globalscripts/DamageTypes.js @@ -1,22 +1,61 @@ +/** + * This class provides a cache to all damage type names and properties defined by the JSON files. + */ function DamageTypes() { - // TODO: load these from files + this.damageTypeData = []; + this.damageTypeDataObj = {}; + this.damageTypeCodes = []; + this.damageTypeNames = {}; - this.names = { - "Hack": markForTranslationWithContext("damage type", "Hack"), - "Pierce": markForTranslationWithContext("damage type", "Pierce"), - "Crush": markForTranslationWithContext("damage type", "Crush"), - }; + for (let filename of Engine.ListDirectoryFiles("simulation/data/damagetypes/", "*.json", false)) + { + let data = Engine.ReadJSONFile(filename); + if (!data) + continue; - deepfreeze(this.names); + if (data.code != data.code.toLowerCase()) + warn("Damage type codes should use lower case: " + data.code); + + this.damageTypeData.push(data); + this.damageTypeDataObj[data.code] = data; + this.damageTypeCodes.push(data.code); + this.damageTypeNames[data.code] = markForTranslationWithContext("damage type", data.name); + for (let subtype in data.subtypes) + this.damageTypeNames[subtype] = data.subtypes[subtype]; + } + + // Sort arrays by specified order + let typeSort = (a, b) => + a.order < b.order ? -1 : + a.order > b.order ? +1 : 0; + + this.damageTypeData.sort(typeSort); + this.damageTypeCodes.sort((a, b) => typeSort( + this.damageTypeData.find(damageType => damageType.code == a), + this.damageTypeData.find(damageType => damageType.code == b) + )); + + deepfreeze(this.damageTypeData); + deepfreeze(this.damageTypeDataObj); + deepfreeze(this.damageTypeCodes); + deepfreeze(this.damageTypeNames); } +/** + * Returns an object mapping resource codes to translatable resource names. Includes subtypes. + * @return {object} - data of the form { "hack": "Hack", "melee": "Melee", "ranged": "Ranged", "pierce": "Pierce", ... } + */ DamageTypes.prototype.GetNames = function() { - return this.names; + return this.damageTypeNames; }; +/** + * Returns an array containing all damage type codes ordered as defined in the resource files. + * @return {string[]} - data of the form [ "hack", "pierce", ... ] + */ DamageTypes.prototype.GetTypes = function() { - return Object.keys(this.names); + return this.damageTypeCodes; }; Index: binaries/data/mods/public/simulation/data/damagetypes/crush.json =================================================================== --- /dev/null +++ binaries/data/mods/public/simulation/data/damagetypes/crush.json @@ -0,0 +1,10 @@ +{ + "code": "Crush", + "name": "Crush", + "description": "...", + "order": 3, + "subtypes": { + "melee": "Melee", + "ranged": "ranged" + } +} Index: binaries/data/mods/public/simulation/data/damagetypes/hack.json =================================================================== --- /dev/null +++ binaries/data/mods/public/simulation/data/damagetypes/hack.json @@ -0,0 +1,9 @@ +{ + "code": "Hack", + "name": "Hack", + "description": "...", + "order": 1, + "subtypes": { + "melee": "Melee" + } +} Index: binaries/data/mods/public/simulation/data/damagetypes/pierce.json =================================================================== --- /dev/null +++ binaries/data/mods/public/simulation/data/damagetypes/pierce.json @@ -0,0 +1,10 @@ +{ + "code": "Pierce", + "name": "Pierce", + "description": "...", + "order": 2, + "subtypes": { + "melee": "Melee", + "ranged": "ranged" + } +}