From 249aebea0f4e3b45b531356873299fc2515653e4 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Thu, 25 Aug 2022 15:10:36 +0200 Subject: [PATCH] Allowed some properties to return damaged action values When a prop is damaged during an action, it now tries to show its new value during the rest of that action --- .../creatureProperties/methods/damageProperty.js | 16 +++++++++++++++- .../actions/applyPropertyByType/applyDamage.js | 10 ++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/imports/api/creature/creatureProperties/methods/damageProperty.js b/app/imports/api/creature/creatureProperties/methods/damageProperty.js index d9af78f5..075aa34f 100644 --- a/app/imports/api/creature/creatureProperties/methods/damageProperty.js +++ b/app/imports/api/creature/creatureProperties/methods/damageProperty.js @@ -24,7 +24,7 @@ const damageProperty = new ValidatedMethod({ run({ _id, operation, value }) { // Get action context - const prop = CreatureProperties.findOne(_id); + let prop = CreatureProperties.findOne(_id); if (!prop) throw new Meteor.Error( 'Damage property failed', 'Property doesn\'t exist' ); @@ -42,6 +42,14 @@ const damageProperty = new ValidatedMethod({ `Property of type "${prop.type}" can't be damaged` ); } + + // Replace the prop by its actionContext counterpart if possible + if (prop.variableName) { + const actionContextProp = actionContext.scope[prop.variableName]; + if (actionContextProp?._id === prop._id) { + prop = actionContextProp; + } + } const result = damagePropertyWork({ prop, operation, value, actionContext }); @@ -94,6 +102,9 @@ export function damagePropertyWork({ prop, operation, value, actionContext }) { }, { selector: prop }); + // Also write it straight to the prop so that it is updated in the actionContext + prop.damage = damage; + prop.value = newValue; } else if (operation === 'increment'){ let currentValue = prop.value || 0; let currentDamage = prop.damage || 0; @@ -111,6 +122,9 @@ export function damagePropertyWork({ prop, operation, value, actionContext }) { }, { selector: prop }); + // Also write it straight to the prop so that it is updated in the actionContext + prop.damage += increment; + prop.value -= increment; } applyTriggers(actionContext.triggers?.damageProperty?.after, prop, actionContext); diff --git a/app/imports/api/engine/actions/applyPropertyByType/applyDamage.js b/app/imports/api/engine/actions/applyPropertyByType/applyDamage.js index 540ec5cb..6900fa56 100644 --- a/app/imports/api/engine/actions/applyPropertyByType/applyDamage.js +++ b/app/imports/api/engine/actions/applyPropertyByType/applyDamage.js @@ -219,6 +219,16 @@ function dealDamage({target, damageType, amount, actionContext}){ if (damageType === 'healing') damageLeft = -totalDamage; healthBars.forEach(healthBar => { if (damageLeft === 0) return; + // Replace the healthbar by the one in the action context if we can + // The damagePropertyWork function bashes the prop with the damage + // So we can use the new value in later action properties + if (healthBar.variableName) { + const targetHealthBar = target.variables[healthBar.variableName]; + if (targetHealthBar?._id === healthBar._id) { + healthBar = targetHealthBar; + } + } + // Do the damage let damageAdded = damagePropertyWork({ prop: healthBar, operation: 'increment',