source: ps/trunk/binaries/data/mods/public/globalscripts/DamageTypes.js@ 23992

Last change on this file since 23992 was 23992, checked in by bb, 4 years ago

Fix more JsDocs numbers/bools/strings

File size: 1.6 KB
Line 
1/**
2 * This class provides a cache for accessing damage types metadata stored in JSON files.
3 * Note that damage types need not be defined in JSON files to be handled in-game.
4 * (this is intended to simplify modding)
5 * This class must be initialised before using, as initialising it directly in globalscripts would
6 * introduce disk I/O every time e.g. a GUI page is loaded.
7 */
8class DamageTypesMetadata
9{
10 constructor()
11 {
12 this.damageTypeData = {};
13
14 let files = Engine.ListDirectoryFiles("simulation/data/template_helpers/damage_types", "*.json", false);
15 for (let filename of files)
16 {
17 let data = Engine.ReadJSONFile(filename);
18 if (!data)
19 continue;
20
21 if (data.code in this.damageTypeData)
22 {
23 error("Encountered two damage types with the code " + data.name);
24 continue;
25 }
26
27 this.damageTypeData[data.code] = data;
28 }
29
30 let hasMetadata = (a) => this.damageTypeData[a] ? -1 : 1;
31 this._sort = (a, b) => {
32 if (this.damageTypeData[a] && this.damageTypeData[b])
33 return this.damageTypeData[a].order - this.damageTypeData[b].order;
34 return hasMetadata(a) - hasMetadata(b);
35 };
36 }
37
38 /**
39 * @param {string[]} damageTypes - The damageTypes to sort.
40 * @returns {string[]} - The damageTypes in sorted order; first the ones
41 * where metadata is provided, then the rest.
42 */
43 sort(damageTypes)
44 {
45 let sorted = damageTypes.slice();
46 sorted.sort(this._sort);
47 return sorted;
48 }
49
50 /**
51 * @returns the name of the @param code damage type, or @code if no metadata exists in JSON files.
52 */
53 getName(code)
54 {
55 return this.damageTypeData[code] ? this.damageTypeData[code].name : code;
56 }
57}
Note: See TracBrowser for help on using the repository browser.