Skip to content

Commit

Permalink
Update character.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean O'Donohue committed Oct 3, 2019
2 parents 7cb94aa + fe6c2ab commit 913a0df
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
147 changes: 147 additions & 0 deletions src/Character.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,153 @@ class Character extends Metadatable(EffectableEntity) {
}
}

/**
* Proxy all events on the player to effects
* @param {string} event
* @param {...*} args
*/
emit(event, ...args) {
super.emit(event, ...args);

this.effects.emit(event, ...args);
}

/**
* @param {string} attr Attribute name
* @return {boolean}
*/
hasAttribute(attr) {
return this.attributes.has(attr);
}

/**
* Get current maximum value of attribute (as modified by effects.)
* @param {string} attr
* @return {number}
*/
getMaxAttribute(attr) {
if (!this.hasAttribute(attr)) {
throw new RangeError(`Character does not have attribute [${attr}]`);
}

const attribute = this.attributes.get(attr);
const currentVal = this.effects.evaluateAttribute(attribute);

if (!attribute.formula) {
return currentVal;
}

const { formula } = attribute;

const requiredValues = formula.requires.map(
reqAttr => this.getMaxAttribute(reqAttr)
);

return formula.evaluate.apply(formula, [attribute, this, currentVal, ...requiredValues]);
}

/**
* @see {@link Attributes#add}
*/
addAttribute(attribute) {
this.attributes.add(attribute);
}

/**
* Get the current value of an attribute (base modified by delta)
* @param {string} attr
* @return {number}
*/
getAttribute(attr) {
if (!this.hasAttribute(attr)) {
throw new RangeError(`Character does not have attribute [${attr}]`);
}

return this.getMaxAttribute(attr) + this.attributes.get(attr).delta;
}

/**
* Get the base value for a given attribute
* @param {string} attr Attribute name
* @return {number}
*/
getBaseAttribute(attr) {
var attr = this.attributes.get(attr);
return attr && attr.base;
}

/**
* Fired when a Character's attribute is set, raised, or lowered
* @event Character#attributeUpdate
* @param {string} attributeName
* @param {Attribute} attribute
*/

/**
* Clears any changes to the attribute, setting it to its base value.
* @param {string} attr
* @fires Character#attributeUpdate
*/
setAttributeToMax(attr) {
if (!this.hasAttribute(attr)) {
throw new Error(`Invalid attribute ${attr}`);
}

this.attributes.get(attr).setDelta(0);
this.emit('attributeUpdate', attr, this.getAttribute(attr));
}

/**
* Raise an attribute by name
* @param {string} attr
* @param {number} amount
* @see {@link Attributes#raise}
* @fires Character#attributeUpdate
*/
raiseAttribute(attr, amount) {
if (!this.hasAttribute(attr)) {
throw new Error(`Invalid attribute ${attr}`);
}

this.attributes.get(attr).raise(amount);
this.emit('attributeUpdate', attr, this.getAttribute(attr));
}

/**
* Lower an attribute by name
* @param {string} attr
* @param {number} amount
* @see {@link Attributes#lower}
* @fires Character#attributeUpdate
*/
lowerAttribute(attr, amount) {
if (!this.hasAttribute(attr)) {
throw new Error(`Invalid attribute ${attr}`);
}

this.attributes.get(attr).lower(amount);
this.emit('attributeUpdate', attr, this.getAttribute(attr));
}

/**
* Update an attribute's base value.
*
* NOTE: You _probably_ don't want to use this the way you think you do. You should not use this
* for any temporary modifications to an attribute, instead you should use an Effect modifier.
*
* This will _permanently_ update the base value for an attribute to be used for things like a
* player purchasing a permanent upgrade or increasing a stat on level up
*
* @param {string} attr Attribute name
* @param {number} newBase New base value
* @fires Character#attributeUpdate
*/
setAttributeBase(attr, newBase) {
if (!this.hasAttribute(attr)) {
throw new Error(`Invalid attribute ${attr}`);
}
}

/**
* Start combat with a given target.
* @param {Character} target
Expand Down
2 changes: 1 addition & 1 deletion src/Metadatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class extends parentClass {
let base = this.metadata;

while (parts.length) {
let part = parts.pop();
let part = parts.shift();
if (!(part in base)) {
throw new RangeError(`Metadata path invalid: ${key}`);
}
Expand Down

0 comments on commit 913a0df

Please sign in to comment.