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,52 @@ +/** + * This class provides a cache to all damage type names and properties defined by the JSON files. + */ function DamageTypes() { - // TODO: load these from files + let damageTypeData = []; + 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); + damageTypeData.push(data); + this.damageTypeCodes.push(data.code); + this.damageTypeNames[data.code] = data.name; + } + + // Sort arrays by specified order + let typeSort = (a, b) => + a.order < b.order ? -1 : + a.order > b.order ? +1 : 0; + + damageTypeData.sort(typeSort); + this.damageTypeCodes.sort((a, b) => typeSort( + damageTypeData.find(damageType => damageType.code == a), + damageTypeData.find(damageType => damageType.code == b) + )); + + deepfreeze(this.damageTypeCodes); + deepfreeze(this.damageTypeNames); } +/** + * Returns an object mapping damage types codes to translatable damage types names. + * @return {object} - data of the form { "hack": "Hack", "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 damage types 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/components/tests/test_Attack.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_Attack.js +++ binaries/data/mods/public/simulation/components/tests/test_Attack.js @@ -1,5 +1,18 @@ Engine.LoadHelperScript("DamageBonus.js"); -Engine.LoadHelperScript("DamageTypes.js"); +DamageTypes = { + "BuildSchema": type => { + let schema = ""; + for (let damageType of ["Hack", "Pierce", "Crush"]) + schema += + "" + + "" + + "" + + "" + + ""; + return "" + schema + ""; + }, + "GetTypes": () => ["Hack", "Pierce", "Crush"] +}; Engine.LoadHelperScript("Player.js"); Engine.LoadHelperScript("ValueModification.js"); Engine.LoadComponentScript("interfaces/Attack.js"); Index: binaries/data/mods/public/simulation/components/tests/test_Damage.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_Damage.js +++ binaries/data/mods/public/simulation/components/tests/test_Damage.js @@ -1,5 +1,18 @@ Engine.LoadHelperScript("DamageBonus.js"); -Engine.LoadHelperScript("DamageTypes.js"); +DamageTypes = { + "BuildSchema": type => { + let schema = ""; + for (let damageType of ["Hack", "Pierce", "Crush"]) + schema += + "" + + "" + + "" + + "" + + ""; + return "" + schema + ""; + }, + "GetTypes": () => ["Hack", "Pierce", "Crush"] +}; Engine.LoadHelperScript("Player.js"); Engine.LoadHelperScript("Sound.js"); Engine.LoadHelperScript("ValueModification.js"); Index: binaries/data/mods/public/simulation/components/tests/test_DeathDamage.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_DeathDamage.js +++ binaries/data/mods/public/simulation/components/tests/test_DeathDamage.js @@ -1,5 +1,18 @@ Engine.LoadHelperScript("DamageBonus.js"); -Engine.LoadHelperScript("DamageTypes.js"); +DamageTypes = { + "BuildSchema": type => { + let schema = ""; + for (let damageType of ["Hack", "Pierce", "Crush"]) + schema += + "" + + "" + + "" + + "" + + ""; + return "" + schema + ""; + }, + "GetTypes": () => ["Hack", "Pierce", "Crush"] +}; Engine.LoadHelperScript("ValueModification.js"); Engine.LoadComponentScript("interfaces/AuraManager.js"); Engine.LoadComponentScript("interfaces/Damage.js"); Index: binaries/data/mods/public/simulation/data/damagetypes/crush.json =================================================================== --- /dev/null +++ binaries/data/mods/public/simulation/data/damagetypes/crush.json @@ -0,0 +1,6 @@ +{ + "code": "crush", + "name": "Crush", + "description": "Damage caused by sheer force, like with a club or by trampling.", + "order": 3 +} Index: binaries/data/mods/public/simulation/data/damagetypes/hack.json =================================================================== --- /dev/null +++ binaries/data/mods/public/simulation/data/damagetypes/hack.json @@ -0,0 +1,6 @@ +{ + "code": "hack", + "name": "Hack", + "description": "Damage caused by sharp objects cutting or chopping, like with a sword or an axe.", + "order": 1 +} Index: binaries/data/mods/public/simulation/data/damagetypes/pierce.json =================================================================== --- /dev/null +++ binaries/data/mods/public/simulation/data/damagetypes/pierce.json @@ -0,0 +1,6 @@ +{ + "code": "pierce", + "name": "Pierce", + "description": "Damage caused by sharp pointy objects, like arrows and spears.", + "order": 2 +}