From 39f92040aa8bad49f5148d175d0fa4ca589c92fb Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Mon, 23 May 2022 23:40:26 -0500 Subject: [PATCH] Added onConvert callback to conversions --- src/features/conversion.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/features/conversion.ts b/src/features/conversion.ts index e8bfed9..9736288 100644 --- a/src/features/conversion.ts +++ b/src/features/conversion.ts @@ -65,6 +65,12 @@ export interface ConversionOptions { * Typically this will be set for you in a conversion constructor. */ convert?: VoidFunction; + /** + * A callback that happens after a conversion has been completed. + * Receives the amount gained via conversion. + * This will not be called whenever using currentGain without calling convert (e.g. passive generation) + */ + onConvert?: (amountGained: DecimalSource) => void; /** * An additional modifier that will be applied to the gain amounts. * Must be reversible in order to correctly calculate {@link nextAt}. @@ -169,12 +175,14 @@ export function createConversion( if (conversion.convert == null) { conversion.convert = function () { + const amountGained = unref((conversion as GenericConversion).currentGain); conversion.gainResource.value = Decimal.add( conversion.gainResource.value, - unref((conversion as GenericConversion).currentGain) + amountGained ); // TODO just subtract cost? conversion.baseResource.value = 0; + conversion.onConvert?.(amountGained); }; } @@ -409,6 +417,7 @@ export function createIndependentConversion( }); } setDefault(conversion, "convert", function () { + const amountGained = unref((conversion as GenericConversion).actualGain); conversion.gainResource.value = conversion.gainModifier ? conversion.gainModifier.apply( unref((conversion as GenericConversion).currentGain) @@ -418,6 +427,7 @@ export function createIndependentConversion( // Maybe by adding a cost function to scaling and nextAt just calls the cost function // with 1 + currentGain conversion.baseResource.value = 0; + conversion.onConvert?.(amountGained); }); return conversion;