Skip to content

Commit

Permalink
Add templates for currency item ids
Browse files Browse the repository at this point in the history
Note: Might be backwards incompatable with CurrencyStatic.defaults constructor parameter
  • Loading branch information
xShadowBlade committed Jul 10, 2024
1 parent bf112fe commit 79e1f5b
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 107 deletions.
44 changes: 24 additions & 20 deletions dist/game/eMath.game.js
Original file line number Diff line number Diff line change
Expand Up @@ -6081,10 +6081,26 @@ __decorateClass([
(0, import_class_transformer4.Type)(() => ItemData)
], Currency.prototype, "items", 2);
var CurrencyStatic = class {
/** @returns The pointer of the data. */
get pointer() {
return this.pointerFn();
}
/**
* The current value of the currency.
* Note: If you want to change the value, use {@link gain} instead.
* @returns The current value of the currency.
*/
get value() {
return this.pointer.value;
}
set value(value) {
this.pointer.value = value;
}
/**
* Constructs a new currnecy
* @param pointer - A function or reference that returns the pointer of the data / frontend.
* @param upgrades - An array of upgrade objects.
* @param items - An array of item objects.
* @param defaults - The default value and boost of the currency.
* @example
* const currency = new CurrencyStatic(undefined, [
Expand All @@ -6099,31 +6115,16 @@ var CurrencyStatic = class {
* ] as const satisfies UpgradeInit[]);
* // CurrencyStatic<["upgId1", "upgId2"]>
*/
constructor(pointer = new Currency(), upgrades, defaults = { defaultVal: Decimal.dZero, defaultBoost: Decimal.dOne }) {
/** An array that represents items and their effects. */
this.items = {};
constructor(pointer = new Currency(), upgrades, items, defaults = { defaultVal: Decimal.dZero, defaultBoost: Decimal.dOne }) {
this.defaultVal = defaults.defaultVal;
this.defaultBoost = defaults.defaultBoost;
this.pointerFn = typeof pointer === "function" ? pointer : () => pointer;
this.boost = new Boost(this.defaultBoost);
this.pointer.value = this.defaultVal;
this.upgrades = {};
if (upgrades) this.addUpgrade(upgrades);
}
/** @returns The pointer of the data. */
get pointer() {
return this.pointerFn();
}
/**
* The current value of the currency.
* Note: If you want to change the value, use {@link gain} instead.
* @returns The current value of the currency.
*/
get value() {
return this.pointer.value;
}
set value(value) {
this.pointer.value = value;
this.items = {};
if (items) this.addItem(items);
}
/**
* Updates / applies effects to the currency on load.
Expand Down Expand Up @@ -7400,20 +7401,22 @@ var Game = class _Game {
* It automatically adds the currency and currencyStatic objects to the data and static objects for saving and loading.
* @template N - The name
* @template U - The upgrade names for the currency. See {@link CurrencyStatic} for more information.
* @template I - The item names for the currency. See {@link CurrencyStatic} for more information.
* @param name - The name of the currency section. This is also the name of the data and static objects, so it must be unique.
* @param upgrades - The upgrades for the currency.
* @param items - The items for the currency.
* @returns A new instance of the gameCurrency class.
* @example
* const currency = game.addCurrency("currency");
* currency.static.gain();
* console.log(currency.value); // Decimal.dOne
*/
addCurrency(name, upgrades = []) {
addCurrency(name, upgrades = [], items = []) {
this.dataManager.setData(name, {
currency: new Currency()
});
const classInstance = new GameCurrency(
[() => this.dataManager.getData(name).currency, upgrades],
[() => this.dataManager.getData(name).currency, upgrades, items],
this,
name
);
Expand Down Expand Up @@ -7450,6 +7453,7 @@ var Game = class _Game {
*/
// public addReset (currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset): GameReset {
addReset(...args) {
console.warn("Game.addReset is deprecated. Use the GameReset class instead.");
const reset = new GameReset(...args);
return reset;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/game/eMath.game.min.js

Large diffs are not rendered by default.

44 changes: 24 additions & 20 deletions dist/game/eMath.game.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6047,10 +6047,26 @@ __decorateClass([
Type3(() => ItemData)
], Currency.prototype, "items", 2);
var CurrencyStatic = class {
/** @returns The pointer of the data. */
get pointer() {
return this.pointerFn();
}
/**
* The current value of the currency.
* Note: If you want to change the value, use {@link gain} instead.
* @returns The current value of the currency.
*/
get value() {
return this.pointer.value;
}
set value(value) {
this.pointer.value = value;
}
/**
* Constructs a new currnecy
* @param pointer - A function or reference that returns the pointer of the data / frontend.
* @param upgrades - An array of upgrade objects.
* @param items - An array of item objects.
* @param defaults - The default value and boost of the currency.
* @example
* const currency = new CurrencyStatic(undefined, [
Expand All @@ -6065,31 +6081,16 @@ var CurrencyStatic = class {
* ] as const satisfies UpgradeInit[]);
* // CurrencyStatic<["upgId1", "upgId2"]>
*/
constructor(pointer = new Currency(), upgrades, defaults = { defaultVal: Decimal.dZero, defaultBoost: Decimal.dOne }) {
/** An array that represents items and their effects. */
this.items = {};
constructor(pointer = new Currency(), upgrades, items, defaults = { defaultVal: Decimal.dZero, defaultBoost: Decimal.dOne }) {
this.defaultVal = defaults.defaultVal;
this.defaultBoost = defaults.defaultBoost;
this.pointerFn = typeof pointer === "function" ? pointer : () => pointer;
this.boost = new Boost(this.defaultBoost);
this.pointer.value = this.defaultVal;
this.upgrades = {};
if (upgrades) this.addUpgrade(upgrades);
}
/** @returns The pointer of the data. */
get pointer() {
return this.pointerFn();
}
/**
* The current value of the currency.
* Note: If you want to change the value, use {@link gain} instead.
* @returns The current value of the currency.
*/
get value() {
return this.pointer.value;
}
set value(value) {
this.pointer.value = value;
this.items = {};
if (items) this.addItem(items);
}
/**
* Updates / applies effects to the currency on load.
Expand Down Expand Up @@ -7366,20 +7367,22 @@ var Game = class _Game {
* It automatically adds the currency and currencyStatic objects to the data and static objects for saving and loading.
* @template N - The name
* @template U - The upgrade names for the currency. See {@link CurrencyStatic} for more information.
* @template I - The item names for the currency. See {@link CurrencyStatic} for more information.
* @param name - The name of the currency section. This is also the name of the data and static objects, so it must be unique.
* @param upgrades - The upgrades for the currency.
* @param items - The items for the currency.
* @returns A new instance of the gameCurrency class.
* @example
* const currency = game.addCurrency("currency");
* currency.static.gain();
* console.log(currency.value); // Decimal.dOne
*/
addCurrency(name, upgrades = []) {
addCurrency(name, upgrades = [], items = []) {
this.dataManager.setData(name, {
currency: new Currency()
});
const classInstance = new GameCurrency(
[() => this.dataManager.getData(name).currency, upgrades],
[() => this.dataManager.getData(name).currency, upgrades, items],
this,
name
);
Expand Down Expand Up @@ -7416,6 +7419,7 @@ var Game = class _Game {
*/
// public addReset (currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset): GameReset {
addReset(...args) {
console.warn("Game.addReset is deprecated. Use the GameReset class instead.");
const reset = new GameReset(...args);
return reset;
}
Expand Down
37 changes: 19 additions & 18 deletions dist/main/eMath.js
Original file line number Diff line number Diff line change
Expand Up @@ -5398,10 +5398,26 @@ __decorateClass([
(0, import_class_transformer4.Type)(() => ItemData)
], Currency.prototype, "items", 2);
var CurrencyStatic = class {
/** @returns The pointer of the data. */
get pointer() {
return this.pointerFn();
}
/**
* The current value of the currency.
* Note: If you want to change the value, use {@link gain} instead.
* @returns The current value of the currency.
*/
get value() {
return this.pointer.value;
}
set value(value) {
this.pointer.value = value;
}
/**
* Constructs a new currnecy
* @param pointer - A function or reference that returns the pointer of the data / frontend.
* @param upgrades - An array of upgrade objects.
* @param items - An array of item objects.
* @param defaults - The default value and boost of the currency.
* @example
* const currency = new CurrencyStatic(undefined, [
Expand All @@ -5416,31 +5432,16 @@ var CurrencyStatic = class {
* ] as const satisfies UpgradeInit[]);
* // CurrencyStatic<["upgId1", "upgId2"]>
*/
constructor(pointer = new Currency(), upgrades, defaults = { defaultVal: Decimal.dZero, defaultBoost: Decimal.dOne }) {
/** An array that represents items and their effects. */
this.items = {};
constructor(pointer = new Currency(), upgrades, items, defaults = { defaultVal: Decimal.dZero, defaultBoost: Decimal.dOne }) {
this.defaultVal = defaults.defaultVal;
this.defaultBoost = defaults.defaultBoost;
this.pointerFn = typeof pointer === "function" ? pointer : () => pointer;
this.boost = new Boost(this.defaultBoost);
this.pointer.value = this.defaultVal;
this.upgrades = {};
if (upgrades) this.addUpgrade(upgrades);
}
/** @returns The pointer of the data. */
get pointer() {
return this.pointerFn();
}
/**
* The current value of the currency.
* Note: If you want to change the value, use {@link gain} instead.
* @returns The current value of the currency.
*/
get value() {
return this.pointer.value;
}
set value(value) {
this.pointer.value = value;
this.items = {};
if (items) this.addItem(items);
}
/**
* Updates / applies effects to the currency on load.
Expand Down
2 changes: 1 addition & 1 deletion dist/main/eMath.min.js

Large diffs are not rendered by default.

37 changes: 19 additions & 18 deletions dist/main/eMath.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5333,10 +5333,26 @@ __decorateClass([
Type3(() => ItemData)
], Currency.prototype, "items", 2);
var CurrencyStatic = class {
/** @returns The pointer of the data. */
get pointer() {
return this.pointerFn();
}
/**
* The current value of the currency.
* Note: If you want to change the value, use {@link gain} instead.
* @returns The current value of the currency.
*/
get value() {
return this.pointer.value;
}
set value(value) {
this.pointer.value = value;
}
/**
* Constructs a new currnecy
* @param pointer - A function or reference that returns the pointer of the data / frontend.
* @param upgrades - An array of upgrade objects.
* @param items - An array of item objects.
* @param defaults - The default value and boost of the currency.
* @example
* const currency = new CurrencyStatic(undefined, [
Expand All @@ -5351,31 +5367,16 @@ var CurrencyStatic = class {
* ] as const satisfies UpgradeInit[]);
* // CurrencyStatic<["upgId1", "upgId2"]>
*/
constructor(pointer = new Currency(), upgrades, defaults = { defaultVal: Decimal.dZero, defaultBoost: Decimal.dOne }) {
/** An array that represents items and their effects. */
this.items = {};
constructor(pointer = new Currency(), upgrades, items, defaults = { defaultVal: Decimal.dZero, defaultBoost: Decimal.dOne }) {
this.defaultVal = defaults.defaultVal;
this.defaultBoost = defaults.defaultBoost;
this.pointerFn = typeof pointer === "function" ? pointer : () => pointer;
this.boost = new Boost(this.defaultBoost);
this.pointer.value = this.defaultVal;
this.upgrades = {};
if (upgrades) this.addUpgrade(upgrades);
}
/** @returns The pointer of the data. */
get pointer() {
return this.pointerFn();
}
/**
* The current value of the currency.
* Note: If you want to change the value, use {@link gain} instead.
* @returns The current value of the currency.
*/
get value() {
return this.pointer.value;
}
set value(value) {
this.pointer.value = value;
this.items = {};
if (items) this.addItem(items);
}
/**
* Updates / applies effects to the currency on load.
Expand Down
17 changes: 10 additions & 7 deletions dist/types/classes/Currency.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MeanMode } from "./numericalAnalysis";
import { UpgradeData, UpgradeStatic } from "./Upgrade";
import { ItemData, Item } from "./Item";
import type { UpgradeInitArrayType, UpgradeInit } from "./Upgrade";
import type { ItemInit } from "./Item";
import type { ItemInit, ItemInitArrayType } from "./Item";
import type { Pointer, IsPrimitiveString } from "../common/types";
interface CurrencyStaticResetOptions {
resetCurrency: boolean;
Expand Down Expand Up @@ -37,16 +37,18 @@ declare class Currency {
* All the functions are here instead of the `currency` class.
* @template UpgradeInitArray - The inital upgrades
* @template UpgradeIds - An string union that represents the names of the upgrades.
* @template ItemInitArray - The inital items
* @template ItemIds - An string union that represents the names of the items.
* @example
* const currency = new CurrencyStatic();
* currency.gain();
* console.log(currency.value); // Decimal.dOne
*/
declare class CurrencyStatic<UpgradeInitArray extends Readonly<UpgradeInit>[] = [], UpgradeIds extends string = UpgradeInitArrayType<UpgradeInitArray>> {
declare class CurrencyStatic<UpgradeInitArray extends Readonly<UpgradeInit>[] = [], UpgradeIds extends string = UpgradeInitArrayType<UpgradeInitArray>, ItemInitArray extends Readonly<ItemInit>[] = [], ItemIds extends string = ItemInitArrayType<ItemInitArray>> {
/** An array that represents upgrades */
readonly upgrades: Record<UpgradeIds, UpgradeStatic>;
/** An array that represents items and their effects. */
readonly items: Record<string, Item>;
readonly items: Record<ItemIds, Item>;
/** A function that returns the pointer of the data */
protected readonly pointerFn: (() => Currency);
/** @returns The pointer of the data. */
Expand All @@ -68,6 +70,7 @@ declare class CurrencyStatic<UpgradeInitArray extends Readonly<UpgradeInit>[] =
* Constructs a new currnecy
* @param pointer - A function or reference that returns the pointer of the data / frontend.
* @param upgrades - An array of upgrade objects.
* @param items - An array of item objects.
* @param defaults - The default value and boost of the currency.
* @example
* const currency = new CurrencyStatic(undefined, [
Expand All @@ -82,7 +85,7 @@ declare class CurrencyStatic<UpgradeInitArray extends Readonly<UpgradeInit>[] =
* ] as const satisfies UpgradeInit[]);
* // CurrencyStatic<["upgId1", "upgId2"]>
*/
constructor(pointer?: Pointer<Currency>, upgrades?: UpgradeInitArray, defaults?: {
constructor(pointer?: Pointer<Currency>, upgrades?: UpgradeInitArray, items?: ItemInitArray, defaults?: {
defaultVal: Decimal;
defaultBoost: Decimal;
});
Expand Down Expand Up @@ -281,7 +284,7 @@ declare class CurrencyStatic<UpgradeInitArray extends Readonly<UpgradeInit>[] =
* @param id - The id of the item to retrieve.
* @returns The item object if found, otherwise null.
*/
getItem(id: string): Item | null;
getItem<T extends ItemIds>(id: T): IsPrimitiveString<ItemIds> extends false ? Item : Item | null;
/**
* Calculates the cost and how many items you can buy.
* See {@link calculateItem} for more information.
Expand All @@ -290,14 +293,14 @@ declare class CurrencyStatic<UpgradeInitArray extends Readonly<UpgradeInit>[] =
* @param target - The target level or quantity to reach for the item. If omitted, it calculates the maximum affordable quantity.
* @returns 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].
*/
calculateItem(id: string, tier?: DecimalSource, target?: DecimalSource): [amount: Decimal, cost: Decimal];
calculateItem(id: ItemIds, tier?: DecimalSource, target?: DecimalSource): [amount: Decimal, cost: Decimal];
/**
* Buys an item based on its ID or array position if enough currency is available.
* @param id - The ID or position of the item to buy or upgrade.
* @param tier - The tier of the item that to calculate.
* @param target - The target level or quantity to reach for the item. See the argument in {@link calculateItem}.
* @returns Returns true if the purchase or upgrade is successful, or false if there is not enough currency or the item does not exist.
*/
buyItem(id: string, tier: DecimalSource, target?: DecimalSource): boolean;
buyItem(id: ItemIds, tier: DecimalSource, target?: DecimalSource): boolean;
}
export { Currency, CurrencyStatic };
Loading

0 comments on commit 79e1f5b

Please sign in to comment.