Profectus / features/conversion
features/conversion ​
This feature represents an exchange of resources based on a formula (any kind, not just a constant exchange rate). This can also handle cases where exchange is continuously produced or without actually spending the input currency. The formula works by including a function that takes the input resource as a Formula variable, and performing any mathematical operations necessary to map that variable to the output amount.
The default type of conversion is a "cumulative conversion", which means the amount of the output resource gets added onto the current amount of that resource. That is, it accumulates.
const conversion = createConversion(() => ({
baseResource: noPersist(points),
gainResource: noPersist(prestigePoints),
formula: x => x.log10()
}));
You can use the setupPassiveGeneration
utility to auto-convert the resources. A third parameter can be added for the rate (where 1 is the currentGain
every second) so it can be based off an upgrade, a toggle, how much of a repeatable you've bought, etc.
setupPassiveGeneration(layer, conversion);
In addition to the default, there is an "independent conversion", which just sets the amount of the output resource irrespective of how much was already there. This is similar to how a leveling system might work, where whenever experience is gained you can "convert" that exp amount into a level amount (without spending the experience, of courese).
const conversion = createIndependentConversion(() => ({
baseResource: noPersist(exp),
gainResource: noPersist(levels),
formula: x => x.log10(),
spend: () => {} // no-op
}));
For independent conversions, you can make them "automatic" without the use of the utility by just calling convert
whenever the base resource changes, like this:
watch(exp, conversion.convert);
The advantage to doing it this way over just a computed
property mapping experience to levels, is that conversions will expose information about how much of the base resource is required for the output to go up.