Class GameCurrency<CurrencyName, UpgradeInitArray, ItemInitArray>

Represents a game currency. Currency is the data class. This class extends CurrencyStatic and adds additional functionality for Game.

Type Parameters

  • CurrencyName extends string = string

    The name of the currency. This is optional, and you can use it for display purposes.

  • UpgradeInitArray extends Readonly<UpgradeInit>[] = []

    The upgrade names for the currency. See CurrencyStatic for more information.

  • ItemInitArray extends Readonly<ItemInit>[] = []

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.

game?: Game

The game pointer/reference

An array that represents items and their effects.

The name of the currency. This is optional, and you can use it for display purposes.

pointerFn: (() => Currency)

A function that returns the pointer of the data

An array that represents upgrades

Accessors

  • get static(): this
  • Returns this

    The static data for the currency.

    Use this class as a static class as it now has all the properties of CurrencyStatic. This property is only here for backwards compatibility.

  • get value(): Decimal
  • The current value of the currency. Note: If you want to change the value, use gain instead.

    Returns Decimal

    The current value of the currency.

  • set value(value): void
  • Parameters

    Returns void

Methods

  • 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

    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 upgrades you can buy. See calculateUpgrade for more information.

    Parameters

    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 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)
  • 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