This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttributes.js
99 lines (88 loc) · 2.24 KB
/
Attributes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const Attribute = require('./Attribute');
/**
* Container for a list of attributes for a {@link Character}
*
* Note: this currently defines the default list of attributes for Characters and should probably
* be refactored to allow for bundles to define this list instead
*
* @extends Map
*/
class Attributes extends Map
{
/**
* `baseStats` argument should adhere to the following format:
*
* {
* statName: 10, // where 10 is your preferred base value
* ...
* }
*
* When stats are loaded from disk, however, they will follow this format:
*
* {
* statName: {
* base: 10,
* delta: 0
* }
* }
*
* So technically either format is valid but the former is easier when setting initial defaults
*
* @param {Object} baseStats Override for default attribute set
*/
constructor(baseStats) {
super();
baseStats = baseStats || {
health: { base: 100 },
};
for (const attr in baseStats) {
const attrConfig = baseStats[attr];
if (typeof attrConfig === 'number') {
baseStats[attr] = { base: attrConfig };
}
if (typeof baseStats[attr] !== 'object' || !('base' in baseStats[attr])) {
throw new Error('Invalid base value given to attributes.\n' + JSON.stringify(baseStats, null, 2));
}
}
for (let [statName, values] of Object.entries(baseStats)) {
this.add(statName, values.base, values.delta || 0);
}
}
/**
* Creates and adds an attribute of a given name to the list
*
* @param {string} name
* @param {number} base
* @param {number} delta=0
*/
add(name, base, delta = 0) {
this.set(name, new Attribute(name, base, delta));
}
/**
* @return {Array} see {@link Map#entries}
*/
getAttributes() {
return this.entries();
}
/**
* Clear all deltas for all attributes in the list
*/
clearDeltas() {
for (let [_, attr] of this) {
attr.setDelta(0);
}
}
/**
* Gather data that will be persisted
* @return {Object}
*/
serialize() {
let data = {};
[...this].forEach(([name, attribute]) => {
data[name] = attribute.serialize();
});
return data;
}
}
module.exports = Attributes;