stratagem
Version:
A lightweight node module for simulating turn-based role-playing game battles
46 lines (44 loc) • 970 B
JavaScript
import ModifierTypes from './modifier-types.js'
export default class Attribute {
constructor(base) {
this.base = base
this.value = base
this.modifiers = {}
}
setBase(base) {
this.base = base
this._calculate()
return this
}
setModifier(id, type, value) {
this.modifiers[id] = { type, value }
this._calculate()
return this
}
deleteModifier(id) {
delete this.modifiers[id]
this._calculate()
return this
}
getModifier(id) {
return this.modifiers[id]
}
hasModifier(id) {
return this.modifiers[id] != undefined
}
_calculate() {
let multiplier = 1.0
let addend = 0
Object.values(this.modifiers).forEach(({ type, value }) => {
switch (type) {
case ModifierTypes.MULTIPLIER:
multiplier *= value
break
case ModifierTypes.ADDEND:
addend += value
break
}
})
this.value = (this.base + addend) * multiplier
}
}