Skip to content

Commit

Permalink
Change reset layer
Browse files Browse the repository at this point in the history
Change the constructor and reset function
  • Loading branch information
xShadowBlade committed Jun 9, 2024
1 parent b5db7ae commit a7df1b0
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 56 deletions.
46 changes: 32 additions & 14 deletions dist/game/eMath.game.js
Original file line number Diff line number Diff line change
Expand Up @@ -7056,26 +7056,44 @@ var GameReset = class {
/**
* Creates a new instance of the game reset.
* @param currenciesToReset The currencies to reset.
* @param extender The extender for the game reset. WARNING: Do not set this to the same object, as it will cause an infinite loop.
* @param extender The extender for the game reset.
* @param onReset Function to run during {@link reset}.
* @param condition A condition that must be met for the reset to occur.
*/
constructor(currenciesToReset, extender) {
constructor(currenciesToReset, extender, onReset, condition) {
this.currenciesToReset = Array.isArray(currenciesToReset) ? currenciesToReset : [currenciesToReset];
this.extender = Array.isArray(extender) ? extender : extender ? [extender] : [];
this.onReset = onReset;
this.condition = condition;
this.id = Symbol();
}
/**
* Resets a currency to its default value, and runs the extender's reset function if it exists (recursively).
* Resets the extenders (if any), then runs {@link onReset} and resets the currencies and upgrades.
* @param force Whether to force the reset. Defaults to `false`.
* @param forceExtenders Whether to force the reset of the extenders. Defaults to `true`.
* @param cached The set of cached symbols to prevent infinite loops.
*/
reset() {
this.onReset?.(this);
this.currenciesToReset.forEach((currency) => {
currency.static.reset();
});
reset(force = false, forceExtenders = true, cached = /* @__PURE__ */ new Set()) {
if (force || (typeof this.condition === "function" ? !this.condition(this) : !this.condition) && typeof this.condition !== "undefined") {
return;
}
const resetThis = () => {
this.onReset?.(this);
this.currenciesToReset.forEach((currency) => {
currency.static.reset();
});
};
if (this.extender.length === 0) {
resetThis();
return;
}
this.extender.forEach((extender) => {
if (extender.id !== this.id) {
extender.reset();
if (!cached.has(extender.id)) {
cached.add(extender.id);
extender.reset(forceExtenders || force, forceExtenders, cached);
}
});
resetThis();
}
};

Expand Down Expand Up @@ -7183,12 +7201,12 @@ var Game = class _Game {
}
/**
* Creates a new game reset object with the specified currencies to reset.
* @param currenciesToReset - The currencies to reset.
* @param extender - An optional object to extend the game reset object with.
* @param args - The arguments for the game reset. See {@link GameReset} for more information.
* @returns The newly created game reset object.
*/
addReset(currenciesToReset, extender) {
const reset = new GameReset(currenciesToReset, extender);
// public addReset (currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset): GameReset {
addReset(...args) {
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.

46 changes: 32 additions & 14 deletions dist/game/eMath.game.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7022,26 +7022,44 @@ var GameReset = class {
/**
* Creates a new instance of the game reset.
* @param currenciesToReset The currencies to reset.
* @param extender The extender for the game reset. WARNING: Do not set this to the same object, as it will cause an infinite loop.
* @param extender The extender for the game reset.
* @param onReset Function to run during {@link reset}.
* @param condition A condition that must be met for the reset to occur.
*/
constructor(currenciesToReset, extender) {
constructor(currenciesToReset, extender, onReset, condition) {
this.currenciesToReset = Array.isArray(currenciesToReset) ? currenciesToReset : [currenciesToReset];
this.extender = Array.isArray(extender) ? extender : extender ? [extender] : [];
this.onReset = onReset;
this.condition = condition;
this.id = Symbol();
}
/**
* Resets a currency to its default value, and runs the extender's reset function if it exists (recursively).
* Resets the extenders (if any), then runs {@link onReset} and resets the currencies and upgrades.
* @param force Whether to force the reset. Defaults to `false`.
* @param forceExtenders Whether to force the reset of the extenders. Defaults to `true`.
* @param cached The set of cached symbols to prevent infinite loops.
*/
reset() {
this.onReset?.(this);
this.currenciesToReset.forEach((currency) => {
currency.static.reset();
});
reset(force = false, forceExtenders = true, cached = /* @__PURE__ */ new Set()) {
if (force || (typeof this.condition === "function" ? !this.condition(this) : !this.condition) && typeof this.condition !== "undefined") {
return;
}
const resetThis = () => {
this.onReset?.(this);
this.currenciesToReset.forEach((currency) => {
currency.static.reset();
});
};
if (this.extender.length === 0) {
resetThis();
return;
}
this.extender.forEach((extender) => {
if (extender.id !== this.id) {
extender.reset();
if (!cached.has(extender.id)) {
cached.add(extender.id);
extender.reset(forceExtenders || force, forceExtenders, cached);
}
});
resetThis();
}
};

Expand Down Expand Up @@ -7149,12 +7167,12 @@ var Game = class _Game {
}
/**
* Creates a new game reset object with the specified currencies to reset.
* @param currenciesToReset - The currencies to reset.
* @param extender - An optional object to extend the game reset object with.
* @param args - The arguments for the game reset. See {@link GameReset} for more information.
* @returns The newly created game reset object.
*/
addReset(currenciesToReset, extender) {
const reset = new GameReset(currenciesToReset, extender);
// public addReset (currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset): GameReset {
addReset(...args) {
const reset = new GameReset(...args);
return reset;
}
};
Expand Down
5 changes: 2 additions & 3 deletions dist/types/game/Game.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ declare class Game {
addAttribute<B extends boolean = true>(name: string, useBoost?: B, initial?: DecimalSource): GameAttribute<B>;
/**
* Creates a new game reset object with the specified currencies to reset.
* @param currenciesToReset - The currencies to reset.
* @param extender - An optional object to extend the game reset object with.
* @param args - The arguments for the game reset. See {@link GameReset} for more information.
* @returns The newly created game reset object.
*/
addReset(currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset): GameReset;
addReset(...args: ConstructorParameters<typeof GameReset>): GameReset;
}
export type { GameConfigOptions };
export { Game, gameDefaultConfig };
19 changes: 14 additions & 5 deletions dist/types/game/ResetLayer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@ declare class GameReset {
/** The extender for the game reset. */
readonly extender: GameReset[];
/**
* Custom code to run after {@link reset} is called but BEFORE the currencies are reset
* Function to run after {@link reset} is called but BEFORE the currencies are reset
* @param resetContext - The reset context that the reset is called in.
*/
onReset?: (resetContext: GameReset) => void;
/**
* A condition that must be met for the reset to occur. Can be a function or a boolean / getter.
*/
condition?: ((resetContext: GameReset) => boolean) | boolean;
/**
* Creates a new instance of the game reset.
* @param currenciesToReset The currencies to reset.
* @param extender The extender for the game reset. WARNING: Do not set this to the same object, as it will cause an infinite loop.
* @param extender The extender for the game reset.
* @param onReset Function to run during {@link reset}.
* @param condition A condition that must be met for the reset to occur.
*/
constructor(currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset | GameReset[]);
constructor(currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset | GameReset[], onReset?: typeof GameReset.prototype.onReset, condition?: typeof GameReset.prototype.condition);
/**
* Resets a currency to its default value, and runs the extender's reset function if it exists (recursively).
* Resets the extenders (if any), then runs {@link onReset} and resets the currencies and upgrades.
* @param force Whether to force the reset. Defaults to `false`.
* @param forceExtenders Whether to force the reset of the extenders. Defaults to `true`.
* @param cached The set of cached symbols to prevent infinite loops.
*/
reset(): void;
reset(force?: boolean, forceExtenders?: boolean, cached?: Set<symbol>): void;
}
export { GameReset };
8 changes: 4 additions & 4 deletions src/game/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ class Game {

/**
* Creates a new game reset object with the specified currencies to reset.
* @param currenciesToReset - The currencies to reset.
* @param extender - An optional object to extend the game reset object with.
* @param args - The arguments for the game reset. See {@link GameReset} for more information.
* @returns The newly created game reset object.
*/
public addReset (currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset): GameReset {
const reset = new GameReset(currenciesToReset, extender);
// public addReset (currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset): GameReset {
public addReset (...args: ConstructorParameters<typeof GameReset>): GameReset {
const reset = new GameReset(...args);
return reset;
}
}
Expand Down
61 changes: 46 additions & 15 deletions src/game/ResetLayer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* @file This file contains all the reset layer related classes.
*/
// import type { game } from "./game";
// import type { gameCurrency } from "./game";
import type { GameCurrency } from "./GameCurrency";

/**
Expand All @@ -19,39 +17,72 @@ class GameReset {
public readonly extender: GameReset[];

/**
* Custom code to run after {@link reset} is called but BEFORE the currencies are reset
* Function to run after {@link reset} is called but BEFORE the currencies are reset
* @param resetContext - The reset context that the reset is called in.
*/
public onReset?: (resetContext: GameReset) => void;

/**
* A condition that must be met for the reset to occur. Can be a function or a boolean / getter.
*/
public condition?: ((resetContext: GameReset) => boolean) | boolean;

/**
* Creates a new instance of the game reset.
* @param currenciesToReset The currencies to reset.
* @param extender The extender for the game reset. WARNING: Do not set this to the same object, as it will cause an infinite loop.
* @param extender The extender for the game reset.
* @param onReset Function to run during {@link reset}.
* @param condition A condition that must be met for the reset to occur.
*/
constructor (currenciesToReset: GameCurrency | GameCurrency[], extender?: GameReset | GameReset[]) {
constructor (
currenciesToReset: GameCurrency | GameCurrency[],
extender?: GameReset | GameReset[],
onReset?: typeof GameReset.prototype.onReset,
condition?: typeof GameReset.prototype.condition,
) {
this.currenciesToReset = Array.isArray(currenciesToReset) ? currenciesToReset : [currenciesToReset];
this.extender = Array.isArray(extender) ? extender : extender ? [extender] : [];
this.onReset = onReset;
this.condition = condition;
this.id = Symbol();
}

/**
* Resets a currency to its default value, and runs the extender's reset function if it exists (recursively).
* Resets the extenders (if any), then runs {@link onReset} and resets the currencies and upgrades.
* @param force Whether to force the reset. Defaults to `false`.
* @param forceExtenders Whether to force the reset of the extenders. Defaults to `true`.
* @param cached The set of cached symbols to prevent infinite loops.
*/
public reset (): void {
this.onReset?.(this);
public reset (force = false, forceExtenders = true, cached = new Set<symbol>()): void {
// If there is a condition and it is not met, then return
if (force || ((typeof this.condition === "function" ? !this.condition(this) : !this.condition) && typeof this.condition !== "undefined")) {
return;
}

this.currenciesToReset.forEach((currency) => {
currency.static.reset();
});
const resetThis = (): void => {
this.onReset?.(this);

// this.extender?.reset();
this.currenciesToReset.forEach((currency) => {
currency.static.reset();
});
};

// If there are no extender, then reset the currencies
if (this.extender.length === 0) {
resetThis();
return;
}

// If there are extenders, reset the extenders first, then reset the currencies
this.extender.forEach((extender) => {
if (extender.id !== this.id) {
extender.reset();
if (!cached.has(extender.id)) {
cached.add(extender.id);
extender.reset(forceExtenders || force, forceExtenders, cached);
}
});

resetThis();
}
}

export { GameReset };
export { GameReset };

0 comments on commit a7df1b0

Please sign in to comment.