diff --git a/src/features/conversion.ts b/src/features/conversion.ts index 095fb48..1dbf5a6 100644 --- a/src/features/conversion.ts +++ b/src/features/conversion.ts @@ -52,6 +52,7 @@ export interface ConversionOptions { gainResource: Resource; /** * Whether or not to cap the amount of the output resource gained by converting at 1. + * Defaults to true. */ buyMax?: Computable; /** @@ -63,6 +64,11 @@ export interface ConversionOptions { * Typically this will be set for you in a conversion constructor. */ convert?: VoidFunction; + /** + * The function that spends the {@link baseResource} as part of the conversion. + * Defaults to setting the {@link baseResource} amount to 0. + */ + spend?: (amountGained: DecimalSource) => void; /** * A callback that happens after a conversion has been completed. * Receives the amount gained via conversion. @@ -106,6 +112,7 @@ export type Conversion = Replace< currentAt: GetComputableTypeWithDefault>; nextAt: GetComputableTypeWithDefault>; buyMax: GetComputableTypeWithDefault; + spend: undefined extends T["spend"] ? (amountGained: DecimalSource) => void : T["spend"]; roundUpCost: GetComputableTypeWithDefault; } >; @@ -121,6 +128,7 @@ export type GenericConversion = Replace< currentAt: ProcessedComputable; nextAt: ProcessedComputable; buyMax: ProcessedComputable; + spend: (amountGained: DecimalSource) => void; roundUpCost: ProcessedComputable; } >; @@ -178,12 +186,17 @@ export function createConversion( conversion.gainResource.value, amountGained ); - // TODO just subtract cost? - conversion.baseResource.value = 0; + (conversion as GenericConversion).spend(amountGained); conversion.onConvert?.(amountGained); }; } + if (conversion.spend == null) { + conversion.spend = function () { + conversion.baseResource.value = 0; + }; + } + processComputable(conversion as T, "currentGain"); processComputable(conversion as T, "actualGain"); processComputable(conversion as T, "currentAt"); @@ -421,15 +434,12 @@ export function createIndependentConversion( unref((conversion as GenericConversion).currentGain) ) : unref((conversion as GenericConversion).currentGain); - // TODO just subtract cost? - // Maybe by adding a cost function to scaling and nextAt just calls the cost function - // with 1 + currentGain - conversion.baseResource.value = 0; + (conversion as GenericConversion).spend(amountGained); conversion.onConvert?.(amountGained); }); return conversion; - }); + }) as Conversion; } /**