Class CurrencyStatic<UpgradeInitArray, UpgradeIds, ItemInitArray, ItemIds>

Represents the backend for a currency in the game. All the functions are here instead of the currency class.

const currency = new CurrencyStatic();
currency.gain();
console.log(currency.value); // Decimal.dOne

Type Parameters

Hierarchy (view full)

Constructors

Properties

boost: Boost

A boost object that affects the currency gain.

defaultBoost: Decimal

The default boost of the currency.

defaultVal: Decimal

The default value of the currency.

items: Record<ItemIds, Item>

An array that represents items and their effects.

pointerFn: (() => Currency)

A function that returns the pointer of the data

upgrades: Record<UpgradeIds, UpgradeStatic>

An array that represents upgrades

Accessors

Methods

  • Adds an item.

    Parameters

    • items: ItemInit | ItemInit[]

      The items to add.

    • runEffectInstantly: boolean = true

      Whether to run the effect immediately. Defaults to true.

    Returns void

  • Creates upgrades. To update an upgrade, use updateUpgrade instead.

    Parameters

    • upgrades: UpgradeInit | UpgradeInit[]

      An array of upgrade objects.

    • runEffectInstantly: boolean = true

      Whether to run the effect immediately. Defaults to true.

    Returns UpgradeStatic[]

    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.

    Parameters

    • id: ItemIds

      The ID or position of the item to buy or upgrade.

    • Optionaltier: DecimalSource

      The tier of the item that to calculate.

    • Optionaltarget: DecimalSource

      The target level or quantity to reach for the item. See the argument in calculateItem.

    Returns boolean

    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.

    Parameters

    Returns boolean

    Returns true if the purchase or upgrade is successful, or false if there is not enough currency or the upgrade does not exist.

    // Attempt to buy up to 10 healthBoost upgrades at once
    currency.buyUpgrade("healthBoost", 10);
  • Calculates the cost and how many items you can buy. See calculateItem for more information.

    Parameters

    • id: ItemIds

      The ID or position of the item to calculate.

    • Optionaltier: DecimalSource

      The tier of the item that to calculate.

    • Optionaltarget: DecimalSource

      The target level or quantity to reach for the item. If omitted, it calculates the maximum affordable quantity.

    Returns [amount: Decimal, cost: Decimal]

    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.

    Parameters

    • id: UpgradeIds

      The ID or position of the upgrade to calculate.

    • target: DecimalSource = Infinity

      The target level or quantity to reach for the upgrade. If omitted, it calculates the maximum affordable quantity.

    • Optionalmode: MeanMode

      See the argument in calculateUpgrade.

    • Optionaliterations: number

      See the argument in calculateUpgrade.

    Returns [amount: Decimal, cost: Decimal]

    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].

    // Calculate how many healthBoost upgrades you can buy and the cost of the upgrades
    const [amount, cost] = currency.calculateUpgrade("healthBoost", 10);
  • The new currency value after applying the boost.

    Parameters

    • dt: DecimalSource = 1000

      Delta time / multiplier in milliseconds, assuming you gain once every second. Ex. 500 = 0.5 seconds = half gain.

    Returns Decimal

    What was gained, NOT the new value.

    // Gain a random number between 1 and 10, and return the amount gained.
    currency.gain(Math.random() * 10000);
  • Calculates how much is needed for the next upgrade.

    Parameters

    Returns Decimal

    The cost of the next upgrade.

    Use getNextCostMax instead as it is more versatile.

    // Calculate the cost of the next healthBoost upgrade
    const nextCost = currency.getNextCost("healthBoost");
  • Calculates the cost of the next upgrade after the maximum affordable quantity.

    Parameters

    Returns Decimal

    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)
  • Updates / applies effects to the currency on load.

    Returns void

  • Queries upgrades based on the provided id. Returns an array of upgrades that match the id.

    Parameters

    Returns UpgradeStatic[]

    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.

    Parameters

    • OptionalresetCurrency: boolean

      Whether to reset the currency value. Default is true.

    • OptionalresetUpgradeLevels: boolean

      Whether to reset the upgrade levels. Default is true.

    • OptionalrunUpgradeEffect: boolean

      Whether to run the upgrade effect. Default is true.

    Returns void

    currency.reset();
    console.log(currency.value); // Decimal.dZero, or the default value
  • Parameters

    • Optionalreset: Partial<CurrencyStaticResetOptions>

    Returns void

  • Runs the effect of an upgrade or item.

    Parameters

    • item: Item

      The item to run the effect for.

    • tier: DecimalSource = Decimal.dOne

      The tier of the item that was bought.

    Returns void

  • Runs the effect of an upgrade or item.

    Parameters

    Returns void

  • Updates an upgrade. To create an upgrade, use addUpgrade instead.

    Parameters

    Returns void

    currency.updateUpgrade("healthBoost", {
    name: "New Health Boost".
    cost: (level) => level.mul(20),
    maxLevel: 20,
    effect: (level, context) => {
    console.log("Health Boost effect");
    }
    });