The initial upgrades
An string union that represents the names of the upgrades.
The initial items
An string union that represents the names of the items.
Constructs a new currency
A function or reference that returns the pointer of the data / frontend.
Optional
upgrades: UpgradeInitArrayAn array of upgrade objects.
Optional
items: ItemInitArrayAn array of item objects.
The default value and boost of the currency.
Readonly
boostA boost object that affects the currency gain.
Readonly
defaultThe default boost of the currency.
Readonly
defaultThe default value of the currency.
Readonly
itemsAn array that represents items and their effects.
Protected
Readonly
pointerA function that returns the pointer of the data
Readonly
upgradesAn array that represents upgrades
Creates upgrades. To update an upgrade, use updateUpgrade instead.
An array of upgrade objects.
Whether to run the effect immediately. Defaults to true
.
The added upgrades.
currency.addUpgrade({
id: "healthBoost", // The ID of the upgrade, used to retrieve it later
name: "Health Boost", // The name of the upgrade, for display purposes (optional, defaults to the ID)
description: "Increases health by 10.", // The description of the upgrade, for display purposes (optional, defaults to "")
cost: (level) => level.mul(10), // Cost of the upgrade, 10 times the level
maxLevel: 10, // Maximum level of the upgrade (optional, defaults to 1)
// Effect of the upgrade (runs when the upgrade is bought, and instantly if runEffectInstantly is true)
effect: (level, context) => {
// Set / update the boost
// health: currencyStatic
health.boost.setBoost(
"healthBoost",
"Health Boost",
"Boosts health by 2x per level.",
n => n.mul(Decimal.pow(2, level.sub(1))),
2,
);
}
});
Buys an item based on its ID or array position if enough currency is available.
The ID or position of the item to buy or upgrade.
Optional
tier: DecimalSourceThe tier of the item that to calculate.
Optional
target: DecimalSourceThe target level or quantity to reach for the item. See the argument in calculateItem.
Returns true if the purchase or upgrade is successful, or false if there is not enough currency or the item does not exist.
Buys an upgrade based on its ID or array position if enough currency is available.
The ID or position of the upgrade to buy or upgrade.
Optional
target: DecimalSourceThe target level or quantity to reach for the upgrade. See the argument in calculateUpgrade.
Optional
mode: MeanModeSee the argument in calculateUpgrade.
Optional
iterations: numberSee the argument in calculateUpgrade.
Returns true if the purchase or upgrade is successful, or false if there is not enough currency or the upgrade does not exist.
Calculates the cost and how many items you can buy. See calculateItem for more information.
The ID or position of the item to calculate.
Optional
tier: DecimalSourceThe tier of the item that to calculate.
Optional
target: DecimalSourceThe target level or quantity to reach for the item. If omitted, it calculates the maximum affordable quantity.
The amount of items you can buy and the cost of the items. If you can't afford any, it returns [Decimal.dZero, Decimal.dZero].
Calculates the cost and how many upgrades you can buy. See calculateUpgrade for more information.
The ID or position of the upgrade to calculate.
The target level or quantity to reach for the upgrade. If omitted, it calculates the maximum affordable quantity.
Optional
mode: MeanModeSee the argument in calculateUpgrade.
Optional
iterations: numberSee the argument in calculateUpgrade.
The amount of upgrades you can buy and the cost of the upgrades. If you can't afford any, it returns [Decimal.dZero, Decimal.dZero].
The new currency value after applying the boost.
Delta time / multiplier in milliseconds, assuming you gain once every second. Ex. 500 = 0.5 seconds = half gain.
What was gained, NOT the new value.
Retrieves an item object based on the provided id.
The id of the item to retrieve.
The item object if found, otherwise null.
Calculates how much is needed for the next upgrade.
Index or ID of the upgrade
How many before the next upgrade
Optional
mode: MeanModeSee the argument in calculateUpgrade.
Optional
iterations: numberSee the argument in calculateUpgrade.
The cost of the next upgrade.
Use getNextCostMax instead as it is more versatile.
Calculates the cost of the next upgrade after the maximum affordable quantity.
Index or ID of the upgrade
How many before the next upgrade
Optional
mode: MeanModeSee the argument in calculateUpgrade.
Optional
iterations: numberSee the argument in calculateUpgrade.
The cost of the next upgrade.
// Calculate the cost of the next healthBoost upgrade
currency.gain(1e6); // Gain 1 thousand currency
console.log(currency.calculateUpgrade("healthBoost")); // The maximum affordable quantity and the cost of the upgrades. Ex. [new Decimal(100), new Decimal(1000)]
console.log(currency.getNextCostMax("healthBoost")); // The cost of the next upgrade after the maximum affordable quantity. (The cost of the 101st upgrade)
Retrieves an upgrade object based on the provided id.
The id of the upgrade to retrieve.
The upgrade object if found, otherwise null.
Queries upgrades based on the provided id. Returns an array of upgrades that match the id.
The id of the upgrade to query.
An array of upgrades that match the id.
const currency = new CurrencyStatic(undefined, [
{ id: "healthBoostSmall", cost: (level) => level.mul(10) },
{ id: "healthBoostLarge", cost: (level) => level.mul(20) },
{ id: "damageBoostSmall", cost: (level) => level.mul(10) },
{ id: "damageBoostLarge", cost: (level) => level.mul(20) },
] as const satisfies UpgradeInit[]);
// Get all health upgrades
const healthUpgrades = currency.queryUpgrade(/health/); // [{ id: "healthBoostSmall", ... }, { id: "healthBoostLarge", ... }]
// Get all small upgrades
const smallUpgrades = currency.queryUpgrade(["healthBoostSmall", "damageBoostSmall"]);
// or
const smallUpgrades2 = currency.queryUpgrade(/.*Small/);
Resets the currency and upgrade levels.
Optional
resetCurrency: booleanWhether to reset the currency value. Default is true.
Optional
resetUpgradeLevels: booleanWhether to reset the upgrade levels. Default is true.
Optional
runUpgradeEffect: booleanWhether to run the upgrade effect. Default is true.
Optional
reset: Partial<CurrencyStaticResetOptions>Runs the effect of an upgrade or item.
The item to run the effect for.
The tier of the item that was bought.
Runs the effect of an upgrade or item.
The upgrade to run the effect for.
Updates an upgrade. To create an upgrade, use addUpgrade instead.
The id of the upgrade to update.
The new upgrade object.
Represents the backend for a currency in the game. All the functions are here instead of the
currency
class.Example