2022-01-14 04:25:47 +00:00
|
|
|
import {
|
|
|
|
DefaultValue,
|
|
|
|
getUniqueID,
|
|
|
|
Persistent,
|
|
|
|
persistent,
|
|
|
|
PersistentRef,
|
|
|
|
Replace,
|
|
|
|
SetupPersistence
|
|
|
|
} from "@/features/feature";
|
|
|
|
import { globalBus } from "@/game/events";
|
|
|
|
import { GenericLayer } from "@/game/layers";
|
|
|
|
import Decimal from "@/lib/break_eternity";
|
|
|
|
import { Computable, GetComputableType, processComputable } from "@/util/computed";
|
|
|
|
import { createProxy } from "@/util/proxies";
|
|
|
|
import { Unsubscribe } from "nanoevents";
|
|
|
|
import { computed, isRef, unref } from "vue";
|
|
|
|
|
|
|
|
export const ResetType = Symbol("Reset");
|
|
|
|
|
|
|
|
export interface ResetOptions {
|
|
|
|
thingsToReset: Computable<Record<string, unknown>[]>;
|
|
|
|
onReset?: VoidFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BaseReset {
|
|
|
|
id: string;
|
|
|
|
reset: VoidFunction;
|
|
|
|
type: typeof ResetType;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Reset<T extends ResetOptions> = Replace<
|
|
|
|
T & BaseReset,
|
|
|
|
{
|
|
|
|
thingsToReset: GetComputableType<T["thingsToReset"]>;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type GenericReset = Reset<ResetOptions>;
|
|
|
|
|
|
|
|
export function createReset<T extends ResetOptions>(options: T & ThisType<Reset<T>>): Reset<T> {
|
|
|
|
const reset: T & Partial<BaseReset> = options;
|
|
|
|
reset.id = getUniqueID("reset-");
|
|
|
|
reset.type = ResetType;
|
|
|
|
|
2022-01-25 04:23:30 +00:00
|
|
|
reset.reset = function () {
|
2022-01-14 04:25:47 +00:00
|
|
|
const handleObject = (obj: Record<string, unknown>) => {
|
|
|
|
Object.keys(obj).forEach(key => {
|
|
|
|
const value = obj[key];
|
|
|
|
if (value && typeof value === "object") {
|
|
|
|
if (SetupPersistence in value && isRef(value)) {
|
|
|
|
if (DefaultValue in value) {
|
|
|
|
(value as PersistentRef).value = (value as PersistentRef)[DefaultValue];
|
|
|
|
} else if (DefaultValue in obj) {
|
2022-01-25 04:23:30 +00:00
|
|
|
(value as PersistentRef).value = (obj as unknown as Persistent)[
|
2022-01-14 04:25:47 +00:00
|
|
|
DefaultValue
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
handleObject(value as Record<string, unknown>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
unref(proxy.thingsToReset).forEach(handleObject);
|
|
|
|
globalBus.emit("reset", proxy);
|
|
|
|
proxy.onReset?.();
|
|
|
|
};
|
|
|
|
|
|
|
|
processComputable(reset as T, "thingsToReset");
|
|
|
|
|
2022-01-25 04:23:30 +00:00
|
|
|
const proxy = createProxy(reset as unknown as Reset<T>);
|
2022-01-14 04:25:47 +00:00
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupAutoReset(
|
|
|
|
layer: GenericLayer,
|
|
|
|
reset: GenericReset,
|
|
|
|
autoActive: Computable<boolean> = true
|
|
|
|
): void {
|
|
|
|
const isActive = typeof autoActive === "function" ? computed(autoActive) : autoActive;
|
|
|
|
layer.on("update", () => {
|
|
|
|
if (unref(isActive)) {
|
|
|
|
reset.reset();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-25 04:23:30 +00:00
|
|
|
const listeners: Record<string, Unsubscribe | undefined> = {};
|
2022-01-14 04:25:47 +00:00
|
|
|
export function trackResetTime(layer: GenericLayer, reset: GenericReset): PersistentRef<Decimal> {
|
|
|
|
const resetTime = persistent<Decimal>(new Decimal(0));
|
|
|
|
listeners[layer.id] = layer.on("preUpdate", (diff: Decimal) => {
|
|
|
|
resetTime.value = Decimal.add(resetTime.value, diff);
|
|
|
|
});
|
|
|
|
globalBus.on("reset", currentReset => {
|
|
|
|
if (currentReset === reset) {
|
|
|
|
resetTime.value = new Decimal(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return resetTime;
|
|
|
|
}
|
|
|
|
globalBus.on("removeLayer", layer => {
|
|
|
|
// unsubscribe from preUpdate
|
|
|
|
listeners[layer.id]?.();
|
2022-01-25 04:23:30 +00:00
|
|
|
listeners[layer.id] = undefined;
|
2022-01-14 04:25:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
declare module "@/game/events" {
|
|
|
|
interface GlobalEvents {
|
|
|
|
reset: (reset: GenericReset) => void;
|
|
|
|
}
|
|
|
|
}
|