Skip to content

Commit

Permalink
Merge pull request #12 from damian-pastorini/develop
Browse files Browse the repository at this point in the history
- Reldens - Modifiers
  • Loading branch information
damian-pastorini authored Dec 3, 2020
2 parents 041584d + 36cfc17 commit 8acc53c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
30 changes: 21 additions & 9 deletions lib/modifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Modifier
}
this.key = props.key;
this.propertyKey = props.propertyKey ? props.propertyKey : props.property_key;
this.basePropertyKey = props.basePropertyKey ? props.basePropertyKey : this.propertyKey;
this.operation = props.operation;
this.type = sc.hasOwn(props, 'type') ? props.type : ModifierConst.TYPES.INT;
this.value = this.parseValue(props.value);
Expand Down Expand Up @@ -57,17 +58,26 @@ class Modifier
return value;
}

apply(target)
apply(target, useBasePropertyToGetValue, applyOnBaseProperty)
{
this.execute(target, false);
this.execute(target, false, useBasePropertyToGetValue, applyOnBaseProperty);
}

revert(target)
revert(target, useBasePropertyToGetValue, applyOnBaseProperty)
{
this.execute(target, true);
this.execute(target, true, useBasePropertyToGetValue, applyOnBaseProperty);
}

execute(target, revert)
/**
*
* Here you have two specific attributes for special features, useBasePropertyToGetValue and applyOnBaseProperty.
* When useBasePropertyToGetValue is true then the modifier will use the basePropertyKey to calculate the modified
* value.
* Beside that, no matter from which value your start the calculations then you can use the applyOnBaseProperty
* property to specify if you want to apply the new calculated value to the propertyKey or to the basePropertyKey.
*
*/
execute(target, revert, useBasePropertyToGetValue = false, applyOnBaseProperty = false)
{
// return error if target is not present at all:
if(!this.target && !target){
Expand All @@ -86,8 +96,9 @@ class Modifier
this.target = target;
}
// calculate new value, set on the owner and change state:
let newValue = this.getModifiedValue(revert);
this.setOwnerProperty(this.propertyKey, newValue);
let newValue = this.getModifiedValue(revert, useBasePropertyToGetValue);
let applyToProp = applyOnBaseProperty ? this.basePropertyKey : this.propertyKey;
this.setOwnerProperty(applyToProp, newValue);
if(revert){
this.state = ModifierConst.MOD_REVERTED;
} else {
Expand All @@ -108,10 +119,11 @@ class Modifier
return true;
}

getModifiedValue(revert = false)
getModifiedValue(revert = false, useBasePropertyToGetValue = false)
{
let usePropKey = useBasePropertyToGetValue ? this.basePropertyKey : this.propertyKey;
// get current property value (this could be already modified):
let currentValue = this.getPropertyValue(this.propertyKey);
let currentValue = this.getPropertyValue(usePropKey);
// extracted maths:
let propertyValue = this.calculator.calculateNewValue(currentValue, this.operation, this.value, revert);
// parse object specific operations:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@reldens/modifiers",
"scope": "@reldens",
"version": "0.4.3",
"version": "0.4.4",
"description": "Reldens - Modifiers",
"author": "Damian A. Pastorini",
"license": "MIT",
Expand Down

0 comments on commit 8acc53c

Please sign in to comment.