Made resetTime more useful and various setup functions use BaseLayer

This commit is contained in:
thepaperpilot 2022-05-23 23:34:59 -05:00
parent 5b62080786
commit d123ed3feb
21 changed files with 37 additions and 42 deletions

View file

@ -22,7 +22,7 @@ import {
TreeNodeOptions TreeNodeOptions
} from "features/trees/tree"; } from "features/trees/tree";
import { Modifier } from "game/modifiers"; import { Modifier } from "game/modifiers";
import { Persistent, persistent } from "game/persistence"; import { DefaultValue, Persistent, persistent } from "game/persistence";
import player from "game/player"; import player from "game/player";
import Decimal, { DecimalSource, format } from "util/bignum"; import Decimal, { DecimalSource, format } from "util/bignum";
import { WithRequired } from "util/common"; import { WithRequired } from "util/common";
@ -47,6 +47,7 @@ export interface ResetButtonOptions extends ClickableOptions {
display?: Computable<CoercableComponent>; display?: Computable<CoercableComponent>;
canClick?: Computable<boolean>; canClick?: Computable<boolean>;
minimumGain?: Computable<DecimalSource>; minimumGain?: Computable<DecimalSource>;
resetTime?: Persistent<DecimalSource>;
} }
export type ResetButton<T extends ResetButtonOptions> = Replace< export type ResetButton<T extends ResetButtonOptions> = Replace<
@ -136,6 +137,9 @@ export function createResetButton<T extends ClickableOptions & ResetButtonOption
} }
resetButton.conversion.convert(); resetButton.conversion.convert();
resetButton.tree.reset(resetButton.treeNode); resetButton.tree.reset(resetButton.treeNode);
if (resetButton.resetTime) {
resetButton.resetTime.value = resetButton.resetTime[DefaultValue];
}
onClick?.(); onClick?.();
}; };

View file

@ -68,7 +68,7 @@ export type GenericAchievement = Replace<
>; >;
export function createAchievement<T extends AchievementOptions>( export function createAchievement<T extends AchievementOptions>(
optionsFunc?: OptionsFunc<T, Achievement<T>, BaseAchievement> optionsFunc?: OptionsFunc<T, BaseAchievement>
): Achievement<T> { ): Achievement<T> {
const earned = persistent<boolean>(false); const earned = persistent<boolean>(false);
return createLazyProxy(() => { return createLazyProxy(() => {

View file

@ -73,9 +73,7 @@ export type GenericBar = Replace<
} }
>; >;
export function createBar<T extends BarOptions>( export function createBar<T extends BarOptions>(optionsFunc: OptionsFunc<T, BaseBar>): Bar<T> {
optionsFunc: OptionsFunc<T, Bar<T>, BaseBar>
): Bar<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const bar = optionsFunc(); const bar = optionsFunc();
bar.id = getUniqueID("bar-"); bar.id = getUniqueID("bar-");

View file

@ -198,7 +198,7 @@ export type GenericBoard = Replace<
>; >;
export function createBoard<T extends BoardOptions>( export function createBoard<T extends BoardOptions>(
optionsFunc: OptionsFunc<T, Board<T>, BaseBoard> optionsFunc: OptionsFunc<T, BaseBoard>
): Board<T> { ): Board<T> {
return createLazyProxy( return createLazyProxy(
persistent => { persistent => {

View file

@ -88,7 +88,7 @@ export type GenericBuyable = Replace<
>; >;
export function createBuyable<T extends BuyableOptions>( export function createBuyable<T extends BuyableOptions>(
optionsFunc: OptionsFunc<T, Buyable<T>, BaseBuyable> optionsFunc: OptionsFunc<T, BaseBuyable>
): Buyable<T> { ): Buyable<T> {
const amount = persistent<DecimalSource>(0); const amount = persistent<DecimalSource>(0);
return createLazyProxy(() => { return createLazyProxy(() => {

View file

@ -97,7 +97,7 @@ export type GenericChallenge = Replace<
>; >;
export function createChallenge<T extends ChallengeOptions>( export function createChallenge<T extends ChallengeOptions>(
optionsFunc: OptionsFunc<T, Challenge<T>, BaseChallenge> optionsFunc: OptionsFunc<T, BaseChallenge>
): Challenge<T> { ): Challenge<T> {
const completions = persistent(0); const completions = persistent(0);
const active = persistent(false); const active = persistent(false);

View file

@ -10,7 +10,7 @@ import {
StyleValue, StyleValue,
Visibility Visibility
} from "features/feature"; } from "features/feature";
import { GenericLayer } from "game/layers"; import { BaseLayer } from "game/layers";
import { Unsubscribe } from "nanoevents"; import { Unsubscribe } from "nanoevents";
import { import {
Computable, Computable,
@ -70,7 +70,7 @@ export type GenericClickable = Replace<
>; >;
export function createClickable<T extends ClickableOptions>( export function createClickable<T extends ClickableOptions>(
optionsFunc?: OptionsFunc<T, Clickable<T>, BaseClickable> optionsFunc?: OptionsFunc<T, BaseClickable>
): Clickable<T> { ): Clickable<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const clickable = optionsFunc?.() ?? ({} as ReturnType<NonNullable<typeof optionsFunc>>); const clickable = optionsFunc?.() ?? ({} as ReturnType<NonNullable<typeof optionsFunc>>);
@ -136,7 +136,7 @@ export function createClickable<T extends ClickableOptions>(
} }
export function setupAutoClick( export function setupAutoClick(
layer: GenericLayer, layer: BaseLayer,
clickable: GenericClickable, clickable: GenericClickable,
autoActive: Computable<boolean> = true autoActive: Computable<boolean> = true
): Unsubscribe { ): Unsubscribe {

View file

@ -1,4 +1,4 @@
import { GenericLayer } from "game/layers"; import { BaseLayer } from "game/layers";
import { Modifier } from "game/modifiers"; import { Modifier } from "game/modifiers";
import Decimal, { DecimalSource } from "util/bignum"; import Decimal, { DecimalSource } from "util/bignum";
import { WithRequired } from "util/common"; import { WithRequired } from "util/common";
@ -129,7 +129,7 @@ export type GenericConversion = Replace<
* @see {@link createIndependentConversion}. * @see {@link createIndependentConversion}.
*/ */
export function createConversion<T extends ConversionOptions>( export function createConversion<T extends ConversionOptions>(
optionsFunc: OptionsFunc<T, Conversion<T>, BaseConversion> optionsFunc: OptionsFunc<T, BaseConversion>
): Conversion<T> { ): Conversion<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const conversion = optionsFunc(); const conversion = optionsFunc();
@ -362,7 +362,7 @@ export function createPolynomialScaling(
* @param optionsFunc Conversion options. * @param optionsFunc Conversion options.
*/ */
export function createCumulativeConversion<S extends ConversionOptions>( export function createCumulativeConversion<S extends ConversionOptions>(
optionsFunc: OptionsFunc<S, Conversion<S>> optionsFunc: OptionsFunc<S, BaseConversion>
): Conversion<S> { ): Conversion<S> {
return createConversion(optionsFunc); return createConversion(optionsFunc);
} }
@ -373,7 +373,7 @@ export function createCumulativeConversion<S extends ConversionOptions>(
* @param optionsFunc Converison options. * @param optionsFunc Converison options.
*/ */
export function createIndependentConversion<S extends ConversionOptions>( export function createIndependentConversion<S extends ConversionOptions>(
optionsFunc: OptionsFunc<S, Conversion<S>> optionsFunc: OptionsFunc<S, BaseConversion>
): Conversion<S> { ): Conversion<S> {
return createConversion(() => { return createConversion(() => {
const conversion: S = optionsFunc(); const conversion: S = optionsFunc();
@ -428,12 +428,12 @@ export function createIndependentConversion<S extends ConversionOptions>(
* This will automatically increase the value of conversion.gainResource without lowering the value of the input resource. * This will automatically increase the value of conversion.gainResource without lowering the value of the input resource.
* It will by default perform 100% of a conversion's currentGain per second. * It will by default perform 100% of a conversion's currentGain per second.
* If you use a ref for the rate you can set it's value to 0 when passive generation should be disabled. * If you use a ref for the rate you can set it's value to 0 when passive generation should be disabled.
* @param layer The layer this passive generation will be associated with. * @param layer The layer this passive generation will be associated with. Typically `this` when calling this function from inside a layer's options function.
* @param conversion The conversion that will determine how much generation there is. * @param conversion The conversion that will determine how much generation there is.
* @param rate A multiplier to multiply against the conversion's currentGain. * @param rate A multiplier to multiply against the conversion's currentGain.
*/ */
export function setupPassiveGeneration( export function setupPassiveGeneration(
layer: GenericLayer, layer: BaseLayer,
conversion: GenericConversion, conversion: GenericConversion,
rate: Computable<DecimalSource> = 1 rate: Computable<DecimalSource> = 1
): void { ): void {

View file

@ -41,7 +41,7 @@ export type Replace<T, S> = S & Omit<T, keyof S>;
* with "this" bound to what the type will eventually be processed into. * with "this" bound to what the type will eventually be processed into.
* Intended for making lazily evaluated objects. * Intended for making lazily evaluated objects.
*/ */
export type OptionsFunc<T, S = T, R = Record<string, unknown>> = () => T & ThisType<S> & Partial<R>; export type OptionsFunc<T, R = Record<string, unknown>> = () => T & Partial<R>;
let id = 0; let id = 0;
/** /**

View file

@ -242,9 +242,7 @@ export type GenericGrid = Replace<
} }
>; >;
export function createGrid<T extends GridOptions>( export function createGrid<T extends GridOptions>(optionsFunc: OptionsFunc<T, BaseGrid>): Grid<T> {
optionsFunc: OptionsFunc<T, Grid<T>, BaseGrid>
): Grid<T> {
const cellState = persistent<Record<string | number, State>>({}); const cellState = persistent<Record<string | number, State>>({});
return createLazyProxy(() => { return createLazyProxy(() => {
const grid = optionsFunc(); const grid = optionsFunc();

View file

@ -43,7 +43,7 @@ export type GenericHotkey = Replace<
>; >;
export function createHotkey<T extends HotkeyOptions>( export function createHotkey<T extends HotkeyOptions>(
optionsFunc: OptionsFunc<T, Hotkey<T>, BaseHotkey> optionsFunc: OptionsFunc<T, BaseHotkey>
): Hotkey<T> { ): Hotkey<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const hotkey = optionsFunc(); const hotkey = optionsFunc();

View file

@ -64,7 +64,7 @@ export type GenericInfobox = Replace<
>; >;
export function createInfobox<T extends InfoboxOptions>( export function createInfobox<T extends InfoboxOptions>(
optionsFunc: OptionsFunc<T, Infobox<T>, BaseInfobox> optionsFunc: OptionsFunc<T, BaseInfobox>
): Infobox<T> { ): Infobox<T> {
const collapsed = persistent<boolean>(false); const collapsed = persistent<boolean>(false);
return createLazyProxy(() => { return createLazyProxy(() => {

View file

@ -44,7 +44,7 @@ export type GenericLinks = Replace<
>; >;
export function createLinks<T extends LinksOptions>( export function createLinks<T extends LinksOptions>(
optionsFunc: OptionsFunc<T, Links<T>, BaseLinks> optionsFunc: OptionsFunc<T, BaseLinks>
): Links<T> { ): Links<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const links = optionsFunc(); const links = optionsFunc();

View file

@ -84,7 +84,7 @@ export type GenericMilestone = Replace<
>; >;
export function createMilestone<T extends MilestoneOptions>( export function createMilestone<T extends MilestoneOptions>(
optionsFunc?: OptionsFunc<T, Milestone<T>, BaseMilestone> optionsFunc?: OptionsFunc<T, BaseMilestone>
): Milestone<T> { ): Milestone<T> {
const earned = persistent<boolean>(false); const earned = persistent<boolean>(false);
return createLazyProxy(() => { return createLazyProxy(() => {

View file

@ -42,7 +42,7 @@ export type Particles<T extends ParticlesOptions> = Replace<
export type GenericParticles = Particles<ParticlesOptions>; export type GenericParticles = Particles<ParticlesOptions>;
export function createParticles<T extends ParticlesOptions>( export function createParticles<T extends ParticlesOptions>(
optionsFunc?: OptionsFunc<T, Particles<T>, BaseParticles> optionsFunc?: OptionsFunc<T, BaseParticles>
): Particles<T> { ): Particles<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const particles = optionsFunc?.() ?? ({} as ReturnType<NonNullable<typeof optionsFunc>>); const particles = optionsFunc?.() ?? ({} as ReturnType<NonNullable<typeof optionsFunc>>);

View file

@ -1,6 +1,6 @@
import { OptionsFunc, getUniqueID, Replace } from "features/feature"; import { OptionsFunc, getUniqueID, Replace } from "features/feature";
import { globalBus } from "game/events"; import { globalBus } from "game/events";
import { GenericLayer } from "game/layers"; import { BaseLayer } from "game/layers";
import { DefaultValue, Persistent, persistent, PersistentState } from "game/persistence"; import { DefaultValue, Persistent, persistent, PersistentState } from "game/persistence";
import Decimal from "util/bignum"; import Decimal from "util/bignum";
import { Computable, GetComputableType, processComputable } from "util/computed"; import { Computable, GetComputableType, processComputable } from "util/computed";
@ -31,7 +31,7 @@ export type Reset<T extends ResetOptions> = Replace<
export type GenericReset = Reset<ResetOptions>; export type GenericReset = Reset<ResetOptions>;
export function createReset<T extends ResetOptions>( export function createReset<T extends ResetOptions>(
optionsFunc: OptionsFunc<T, Reset<T>, BaseReset> optionsFunc: OptionsFunc<T, BaseReset>
): Reset<T> { ): Reset<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const reset = optionsFunc(); const reset = optionsFunc();
@ -64,7 +64,7 @@ export function createReset<T extends ResetOptions>(
} }
const listeners: Record<string, Unsubscribe | undefined> = {}; const listeners: Record<string, Unsubscribe | undefined> = {};
export function trackResetTime(layer: GenericLayer, reset: GenericReset): Persistent<Decimal> { export function trackResetTime(layer: BaseLayer, reset: GenericReset): Persistent<Decimal> {
const resetTime = persistent<Decimal>(new Decimal(0)); const resetTime = persistent<Decimal>(new Decimal(0));
listeners[layer.id] = layer.on("preUpdate", diff => { listeners[layer.id] = layer.on("preUpdate", diff => {
resetTime.value = Decimal.add(resetTime.value, diff); resetTime.value = Decimal.add(resetTime.value, diff);

View file

@ -37,9 +37,7 @@ export type Tab<T extends TabOptions> = Replace<
export type GenericTab = Tab<TabOptions>; export type GenericTab = Tab<TabOptions>;
export function createTab<T extends TabOptions>( export function createTab<T extends TabOptions>(optionsFunc: OptionsFunc<T, BaseTab>): Tab<T> {
optionsFunc: OptionsFunc<T, Tab<T>, BaseTab>
): Tab<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const tab = optionsFunc(); const tab = optionsFunc();
tab.id = getUniqueID("tab-"); tab.id = getUniqueID("tab-");

View file

@ -92,7 +92,7 @@ export type GenericTabFamily = Replace<
export function createTabFamily<T extends TabFamilyOptions>( export function createTabFamily<T extends TabFamilyOptions>(
tabs: Record<string, () => TabButtonOptions>, tabs: Record<string, () => TabButtonOptions>,
optionsFunc?: OptionsFunc<T, TabFamily<T>, BaseTabFamily> optionsFunc?: OptionsFunc<T, BaseTabFamily>
): TabFamily<T> { ): TabFamily<T> {
if (Object.keys(tabs).length === 0) { if (Object.keys(tabs).length === 0) {
console.warn("Cannot create tab family with 0 tabs"); console.warn("Cannot create tab family with 0 tabs");

View file

@ -73,7 +73,7 @@ export type GenericTreeNode = Replace<
>; >;
export function createTreeNode<T extends TreeNodeOptions>( export function createTreeNode<T extends TreeNodeOptions>(
optionsFunc?: OptionsFunc<T, TreeNode<T>, BaseTreeNode> optionsFunc?: OptionsFunc<T, BaseTreeNode>
): TreeNode<T> { ): TreeNode<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const treeNode = optionsFunc?.() ?? ({} as ReturnType<NonNullable<typeof optionsFunc>>); const treeNode = optionsFunc?.() ?? ({} as ReturnType<NonNullable<typeof optionsFunc>>);
@ -186,9 +186,7 @@ export type GenericTree = Replace<
} }
>; >;
export function createTree<T extends TreeOptions>( export function createTree<T extends TreeOptions>(optionsFunc: OptionsFunc<T, BaseTree>): Tree<T> {
optionsFunc: OptionsFunc<T, Tree<T>, BaseTree>
): Tree<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const tree = optionsFunc(); const tree = optionsFunc();
tree.id = getUniqueID("tree-"); tree.id = getUniqueID("tree-");

View file

@ -79,7 +79,7 @@ export type GenericUpgrade = Replace<
>; >;
export function createUpgrade<T extends UpgradeOptions>( export function createUpgrade<T extends UpgradeOptions>(
optionsFunc: OptionsFunc<T, Upgrade<T>, BaseUpgrade> optionsFunc: OptionsFunc<T, BaseUpgrade>
): Upgrade<T> { ): Upgrade<T> {
const bought = persistent<boolean>(false); const bought = persistent<boolean>(false);
return createLazyProxy(() => { return createLazyProxy(() => {

View file

@ -1,13 +1,13 @@
import Modal from "components/Modal.vue"; import Modal from "components/Modal.vue";
import { import {
CoercableComponent, CoercableComponent,
OptionsFunc,
jsx, jsx,
JSXFunction, JSXFunction,
Replace, Replace,
setDefault, setDefault,
StyleValue StyleValue
} from "features/feature"; } from "features/feature";
import { createNanoEvents, Emitter } from "nanoevents";
import { import {
Computable, Computable,
GetComputableType, GetComputableType,
@ -16,7 +16,6 @@ import {
ProcessedComputable ProcessedComputable
} from "util/computed"; } from "util/computed";
import { createLazyProxy } from "util/proxies"; import { createLazyProxy } from "util/proxies";
import { createNanoEvents, Emitter } from "nanoevents";
import { InjectionKey, Ref, ref, shallowReactive, unref } from "vue"; import { InjectionKey, Ref, ref, shallowReactive, unref } from "vue";
import { globalBus } from "./events"; import { globalBus } from "./events";
import { Persistent, persistent } from "./persistence"; import { Persistent, persistent } from "./persistence";
@ -106,7 +105,7 @@ export const persistentRefs: Record<string, Set<Persistent>> = {};
export const addingLayers: string[] = []; export const addingLayers: string[] = [];
export function createLayer<T extends LayerOptions>( export function createLayer<T extends LayerOptions>(
id: string, id: string,
optionsFunc: OptionsFunc<T, BaseLayer, BaseLayer> optionsFunc: (this: BaseLayer) => T & Partial<BaseLayer>
): Layer<T> { ): Layer<T> {
return createLazyProxy(() => { return createLazyProxy(() => {
const layer = {} as T & Partial<BaseLayer>; const layer = {} as T & Partial<BaseLayer>;
@ -119,7 +118,7 @@ export function createLayer<T extends LayerOptions>(
addingLayers.push(id); addingLayers.push(id);
persistentRefs[id] = new Set(); persistentRefs[id] = new Set();
layer.minimized = persistent(false); layer.minimized = persistent(false);
Object.assign(layer, optionsFunc.call(layer)); Object.assign(layer, optionsFunc.call(layer as BaseLayer));
if ( if (
addingLayers[addingLayers.length - 1] == null || addingLayers[addingLayers.length - 1] == null ||
addingLayers[addingLayers.length - 1] !== id addingLayers[addingLayers.length - 1] !== id