Implemented modifiers

This commit is contained in:
thepaperpilot 2022-04-11 22:50:02 -05:00
parent aa5d43ad17
commit 4e2095fd51
2 changed files with 78 additions and 38 deletions

View file

@ -1,8 +1,8 @@
import { GenericLayer } from "game/layers";
import { Modifier } from "game/modifiers";
import Decimal, { DecimalSource } from "util/bignum";
import {
Computable,
convertComputable,
GetComputableTypeWithDefault,
processComputable,
ProcessedComputable
@ -23,7 +23,7 @@ export interface ConversionOptions {
buyMax?: Computable<boolean>;
roundUpCost?: Computable<boolean>;
convert?: VoidFunction;
gainModifier?: GainModifier;
gainModifier?: Modifier;
}
export interface BaseConversion {
@ -54,11 +54,6 @@ export type GenericConversion = Replace<
}
>;
export interface GainModifier {
apply: (gain: DecimalSource) => DecimalSource;
revert: (gain: DecimalSource) => DecimalSource;
}
export function createConversion<T extends ConversionOptions>(
optionsFunc: OptionsFunc<T, Conversion<T>, BaseConversion>
): Conversion<T> {
@ -307,34 +302,3 @@ export function addHardcap(
currentGain: conversion => Decimal.min(scaling.currentGain(conversion), unref(cap))
};
}
export function createAdditiveModifier(addend: Computable<DecimalSource>): GainModifier {
const processedAddend = convertComputable(addend);
return {
apply: gain => Decimal.add(gain, unref(processedAddend)),
revert: gain => Decimal.sub(gain, unref(processedAddend))
};
}
export function createMultiplicativeModifier(multiplier: Computable<DecimalSource>): GainModifier {
const processedMultiplier = convertComputable(multiplier);
return {
apply: gain => Decimal.times(gain, unref(processedMultiplier)),
revert: gain => Decimal.div(gain, unref(processedMultiplier))
};
}
export function createExponentialModifier(exponent: Computable<DecimalSource>): GainModifier {
const processedExponent = convertComputable(exponent);
return {
apply: gain => Decimal.pow(gain, unref(processedExponent)),
revert: gain => Decimal.root(gain, unref(processedExponent))
};
}
export function createSequentialModifier(...modifiers: GainModifier[]): GainModifier {
return {
apply: gain => modifiers.reduce((gain, modifier) => modifier.apply(gain), gain),
revert: gain => modifiers.reduceRight((gain, modifier) => modifier.revert(gain), gain)
};
}

76
src/game/modifiers.ts Normal file
View file

@ -0,0 +1,76 @@
import Decimal, { DecimalSource, format } from "util/bignum";
import { Computable, convertComputable, ProcessedComputable } from "util/computed";
import { computed, unref } from "vue";
export interface Modifier {
apply: (gain: DecimalSource) => DecimalSource;
revert: (gain: DecimalSource) => DecimalSource;
enabled: ProcessedComputable<boolean>;
description?: ProcessedComputable<string>;
}
export function createAdditiveModifier(
addend: Computable<DecimalSource>,
description?: string,
enabled?: Computable<boolean>
): Modifier {
const processedAddend = convertComputable(addend);
const processedEnabled = convertComputable(enabled == null ? true : enabled);
return {
apply: gain => Decimal.add(gain, unref(processedAddend)),
revert: gain => Decimal.sub(gain, unref(processedAddend)),
enabled: processedEnabled,
description: computed(() => `+${format(unref(processedAddend))} ${description}`)
};
}
export function createMultiplicativeModifier(
multiplier: Computable<DecimalSource>,
description?: string,
enabled?: Computable<boolean>
): Modifier {
const processedMultiplier = convertComputable(multiplier);
const processedEnabled = convertComputable(enabled == null ? true : enabled);
return {
apply: gain => Decimal.times(gain, unref(processedMultiplier)),
revert: gain => Decimal.div(gain, unref(processedMultiplier)),
enabled: processedEnabled,
description: computed(() => `x${format(unref(processedMultiplier))} ${description}`)
};
}
export function createExponentialModifier(
exponent: Computable<DecimalSource>,
description?: string,
enabled?: Computable<boolean>
): Modifier {
const processedExponent = convertComputable(exponent);
const processedEnabled = convertComputable(enabled == null ? true : enabled);
return {
apply: gain => Decimal.pow(gain, unref(processedExponent)),
revert: gain => Decimal.root(gain, unref(processedExponent)),
enabled: processedEnabled,
description: computed(() => `^${format(unref(processedExponent))} ${description}`)
};
}
export function createSequentialModifier(...modifiers: Modifier[]): Modifier {
return {
apply: gain =>
modifiers
.filter(m => unref(m.enabled))
.reduce((gain, modifier) => modifier.apply(gain), gain),
revert: gain =>
modifiers
.filter(m => unref(m.enabled))
.reduceRight((gain, modifier) => modifier.revert(gain), gain),
enabled: computed(() => modifiers.filter(m => unref(m.enabled)).length > 0),
description: computed(() => {
return modifiers
.filter(m => unref(m.enabled))
.map(m => unref(m.description))
.filter(d => d)
.join("\n");
})
};
}