Merge pull request #13 from murapix/main

Add feature decorator system
This commit is contained in:
Anthony Lawn 2023-04-19 20:28:11 -05:00 committed by GitHub
commit c9ca85978d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 299 additions and 27 deletions

View file

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Requirements can use them so repeatables and challenges can be "buy max" without any extra effort - Requirements can use them so repeatables and challenges can be "buy max" without any extra effort
- Conversions now use formulas instead of the old scaling functions system, allowing for arbitrary functions that are much easier to follow - Conversions now use formulas instead of the old scaling functions system, allowing for arbitrary functions that are much easier to follow
- Modifiers have a new getFormula property - Modifiers have a new getFormula property
- Feature decorators, which simplify the process of adding extra values to features
- Action feature, which is a clickable with a cooldown - Action feature, which is a clickable with a cooldown
- ETA util (calculates time until a specific amount of a resource, based on its current gain rate) - ETA util (calculates time until a specific amount of a resource, based on its current gain rate)
- createCollapsibleAchievements util - createCollapsibleAchievements util

View file

@ -2,6 +2,7 @@ import { computed } from "@vue/reactivity";
import { isArray } from "@vue/shared"; import { isArray } from "@vue/shared";
import Select from "components/fields/Select.vue"; import Select from "components/fields/Select.vue";
import AchievementComponent from "features/achievements/Achievement.vue"; import AchievementComponent from "features/achievements/Achievement.vue";
import { Decorator, GenericDecorator } from "features/decorators/common";
import { import {
CoercableComponent, CoercableComponent,
Component, Component,
@ -137,9 +138,11 @@ export type GenericAchievement = Replace<
* @param optionsFunc Achievement options. * @param optionsFunc Achievement options.
*/ */
export function createAchievement<T extends AchievementOptions>( export function createAchievement<T extends AchievementOptions>(
optionsFunc?: OptionsFunc<T, BaseAchievement, GenericAchievement> optionsFunc?: OptionsFunc<T, BaseAchievement, GenericAchievement>,
...decorators: GenericDecorator[]
): Achievement<T> { ): Achievement<T> {
const earned = persistent<boolean>(false, false); const earned = persistent<boolean>(false, false);
const decoratedData = decorators.reduce((current, next) => Object.assign(current, next.getPersistentData?.()), {});
return createLazyProxy(feature => { return createLazyProxy(feature => {
const achievement = const achievement =
optionsFunc?.call(feature, feature) ?? optionsFunc?.call(feature, feature) ??
@ -148,6 +151,10 @@ export function createAchievement<T extends AchievementOptions>(
achievement.type = AchievementType; achievement.type = AchievementType;
achievement[Component] = AchievementComponent as GenericComponent; achievement[Component] = AchievementComponent as GenericComponent;
for (const decorator of decorators) {
decorator.preConstruct?.(achievement);
}
achievement.earned = earned; achievement.earned = earned;
achievement.complete = function () { achievement.complete = function () {
earned.value = true; earned.value = true;
@ -179,6 +186,8 @@ export function createAchievement<T extends AchievementOptions>(
} }
}; };
Object.assign(achievement, decoratedData);
processComputable(achievement as T, "visibility"); processComputable(achievement as T, "visibility");
setDefault(achievement, "visibility", Visibility.Visible); setDefault(achievement, "visibility", Visibility.Visible);
const visibility = achievement.visibility as ProcessedComputable<Visibility | boolean>; const visibility = achievement.visibility as ProcessedComputable<Visibility | boolean>;
@ -219,6 +228,11 @@ export function createAchievement<T extends AchievementOptions>(
processComputable(achievement as T, "showPopups"); processComputable(achievement as T, "showPopups");
setDefault(achievement, "showPopups", true); setDefault(achievement, "showPopups", true);
for (const decorator of decorators) {
decorator.postConstruct?.(achievement);
}
const decoratedProps = decorators.reduce((current, next) => Object.assign(current, next.getGatheredProps?.(achievement)), {});
achievement[GatherProps] = function (this: GenericAchievement) { achievement[GatherProps] = function (this: GenericAchievement) {
const { const {
visibility, visibility,
@ -242,7 +256,8 @@ export function createAchievement<T extends AchievementOptions>(
classes, classes,
mark, mark,
small, small,
id id,
...decoratedProps
}; };
}; };

View file

@ -31,6 +31,7 @@ import { coerceComponent, isCoercableComponent, render } from "util/vue";
import { computed, Ref, ref, unref } from "vue"; import { computed, Ref, ref, unref } from "vue";
import { BarOptions, createBar, GenericBar } from "./bars/bar"; import { BarOptions, createBar, GenericBar } from "./bars/bar";
import { ClickableOptions } from "./clickables/clickable"; import { ClickableOptions } from "./clickables/clickable";
import { Decorator, GenericDecorator } from "./decorators/common";
/** A symbol used to identify {@link Action} features. */ /** A symbol used to identify {@link Action} features. */
export const ActionType = Symbol("Action"); export const ActionType = Symbol("Action");
@ -102,9 +103,11 @@ export type GenericAction = Replace<
* @param optionsFunc Action options. * @param optionsFunc Action options.
*/ */
export function createAction<T extends ActionOptions>( export function createAction<T extends ActionOptions>(
optionsFunc?: OptionsFunc<T, BaseAction, GenericAction> optionsFunc?: OptionsFunc<T, BaseAction, GenericAction>,
...decorators: GenericDecorator[]
): Action<T> { ): Action<T> {
const progress = persistent<DecimalSource>(0); const progress = persistent<DecimalSource>(0);
const decoratedData = decorators.reduce((current, next) => Object.assign(current, next.getPersistentData?.()), {});
return createLazyProxy(feature => { return createLazyProxy(feature => {
const action = const action =
optionsFunc?.call(feature, feature) ?? optionsFunc?.call(feature, feature) ??
@ -116,8 +119,13 @@ export function createAction<T extends ActionOptions>(
// Required because of display changing types // Required because of display changing types
const genericAction = action as unknown as GenericAction; const genericAction = action as unknown as GenericAction;
for (const decorator of decorators) {
decorator.preConstruct?.(action);
}
action.isHolding = ref(false); action.isHolding = ref(false);
action.progress = progress; action.progress = progress;
Object.assign(action, decoratedData);
processComputable(action as T, "visibility"); processComputable(action as T, "visibility");
setDefault(action, "visibility", Visibility.Visible); setDefault(action, "visibility", Visibility.Visible);
@ -229,6 +237,11 @@ export function createAction<T extends ActionOptions>(
} }
}; };
for (const decorator of decorators) {
decorator.postConstruct?.(action);
}
const decoratedProps = decorators.reduce((current, next) => Object.assign(current, next.getGatheredProps?.(action)));
action[GatherProps] = function (this: GenericAction) { action[GatherProps] = function (this: GenericAction) {
const { const {
display, display,
@ -252,7 +265,8 @@ export function createAction<T extends ActionOptions>(
canClick, canClick,
small, small,
mark, mark,
id id,
...decoratedProps
}; };
}; };

View file

@ -1,4 +1,5 @@
import BarComponent from "features/bars/Bar.vue"; import BarComponent from "features/bars/Bar.vue";
import { Decorator, GenericDecorator } from "features/decorators/common";
import type { import type {
CoercableComponent, CoercableComponent,
GenericComponent, GenericComponent,
@ -101,14 +102,22 @@ export type GenericBar = Replace<
* @param optionsFunc Bar options. * @param optionsFunc Bar options.
*/ */
export function createBar<T extends BarOptions>( export function createBar<T extends BarOptions>(
optionsFunc: OptionsFunc<T, BaseBar, GenericBar> optionsFunc: OptionsFunc<T, BaseBar, GenericBar>,
...decorators: GenericDecorator[]
): Bar<T> { ): Bar<T> {
const decoratedData = decorators.reduce((current, next) => Object.assign(current, next.getPersistentData?.()), {});
return createLazyProxy(feature => { return createLazyProxy(feature => {
const bar = optionsFunc.call(feature, feature); const bar = optionsFunc.call(feature, feature);
bar.id = getUniqueID("bar-"); bar.id = getUniqueID("bar-");
bar.type = BarType; bar.type = BarType;
bar[Component] = BarComponent as GenericComponent; bar[Component] = BarComponent as GenericComponent;
for (const decorator of decorators) {
decorator.preConstruct?.(bar);
}
Object.assign(bar, decoratedData);
processComputable(bar as T, "visibility"); processComputable(bar as T, "visibility");
setDefault(bar, "visibility", Visibility.Visible); setDefault(bar, "visibility", Visibility.Visible);
processComputable(bar as T, "width"); processComputable(bar as T, "width");
@ -124,6 +133,11 @@ export function createBar<T extends BarOptions>(
processComputable(bar as T, "display"); processComputable(bar as T, "display");
processComputable(bar as T, "mark"); processComputable(bar as T, "mark");
for (const decorator of decorators) {
decorator.postConstruct?.(bar);
}
const decoratedProps = decorators.reduce((current, next) => Object.assign(current, next.getGatheredProps?.(bar)), {});
bar[GatherProps] = function (this: GenericBar) { bar[GatherProps] = function (this: GenericBar) {
const { const {
progress, progress,
@ -155,7 +169,8 @@ export function createBar<T extends BarOptions>(
baseStyle, baseStyle,
fillStyle, fillStyle,
mark, mark,
id id,
...decoratedProps
}; };
}; };

View file

@ -1,6 +1,7 @@
import { isArray } from "@vue/shared"; import { isArray } from "@vue/shared";
import Toggle from "components/fields/Toggle.vue"; import Toggle from "components/fields/Toggle.vue";
import ChallengeComponent from "features/challenges/Challenge.vue"; import ChallengeComponent from "features/challenges/Challenge.vue";
import { Decorator, GenericDecorator } from "features/decorators/common";
import type { import type {
CoercableComponent, CoercableComponent,
GenericComponent, GenericComponent,
@ -148,10 +149,12 @@ export type GenericChallenge = Replace<
* @param optionsFunc Challenge options. * @param optionsFunc Challenge options.
*/ */
export function createChallenge<T extends ChallengeOptions>( export function createChallenge<T extends ChallengeOptions>(
optionsFunc: OptionsFunc<T, BaseChallenge, GenericChallenge> optionsFunc: OptionsFunc<T, BaseChallenge, GenericChallenge>,
...decorators: GenericDecorator[]
): Challenge<T> { ): Challenge<T> {
const completions = persistent(0); const completions = persistent(0);
const active = persistent(false, false); const active = persistent(false, false);
const decoratedData = decorators.reduce((current, next) => Object.assign(current, next.getPersistentData?.()), {});
return createLazyProxy(feature => { return createLazyProxy(feature => {
const challenge = optionsFunc.call(feature, feature); const challenge = optionsFunc.call(feature, feature);
@ -159,8 +162,14 @@ export function createChallenge<T extends ChallengeOptions>(
challenge.type = ChallengeType; challenge.type = ChallengeType;
challenge[Component] = ChallengeComponent as GenericComponent; challenge[Component] = ChallengeComponent as GenericComponent;
for (const decorator of decorators) {
decorator.preConstruct?.(challenge);
}
challenge.completions = completions; challenge.completions = completions;
challenge.active = active; challenge.active = active;
Object.assign(challenge, decoratedData);
challenge.completed = computed(() => challenge.completed = computed(() =>
Decimal.gt((challenge as GenericChallenge).completions.value, 0) Decimal.gt((challenge as GenericChallenge).completions.value, 0)
); );
@ -258,6 +267,11 @@ export function createChallenge<T extends ChallengeOptions>(
}); });
} }
for (const decorator of decorators) {
decorator.postConstruct?.(challenge);
}
const decoratedProps = decorators.reduce((current, next) => Object.assign(current, next.getGatheredProps?.(challenge)), {});
challenge[GatherProps] = function (this: GenericChallenge) { challenge[GatherProps] = function (this: GenericChallenge) {
const { const {
active, active,
@ -287,7 +301,8 @@ export function createChallenge<T extends ChallengeOptions>(
mark, mark,
id, id,
toggle, toggle,
requirements requirements,
...decoratedProps
}; };
}; };

View file

@ -1,4 +1,5 @@
import ClickableComponent from "features/clickables/Clickable.vue"; import ClickableComponent from "features/clickables/Clickable.vue";
import { Decorator, GenericDecorator } from "features/decorators/common";
import type { import type {
CoercableComponent, CoercableComponent,
GenericComponent, GenericComponent,
@ -95,8 +96,10 @@ export type GenericClickable = Replace<
* @param optionsFunc Clickable options. * @param optionsFunc Clickable options.
*/ */
export function createClickable<T extends ClickableOptions>( export function createClickable<T extends ClickableOptions>(
optionsFunc?: OptionsFunc<T, BaseClickable, GenericClickable> optionsFunc?: OptionsFunc<T, BaseClickable, GenericClickable>,
...decorators: GenericDecorator[]
): Clickable<T> { ): Clickable<T> {
const decoratedData = decorators.reduce((current, next) => Object.assign(current, next.getPersistentData?.()), {});
return createLazyProxy(feature => { return createLazyProxy(feature => {
const clickable = const clickable =
optionsFunc?.call(feature, feature) ?? optionsFunc?.call(feature, feature) ??
@ -105,6 +108,12 @@ export function createClickable<T extends ClickableOptions>(
clickable.type = ClickableType; clickable.type = ClickableType;
clickable[Component] = ClickableComponent as GenericComponent; clickable[Component] = ClickableComponent as GenericComponent;
for (const decorator of decorators) {
decorator.preConstruct?.(clickable);
}
Object.assign(clickable, decoratedData);
processComputable(clickable as T, "visibility"); processComputable(clickable as T, "visibility");
setDefault(clickable, "visibility", Visibility.Visible); setDefault(clickable, "visibility", Visibility.Visible);
processComputable(clickable as T, "canClick"); processComputable(clickable as T, "canClick");
@ -131,6 +140,11 @@ export function createClickable<T extends ClickableOptions>(
}; };
} }
for (const decorator of decorators) {
decorator.postConstruct?.(clickable);
}
const decoratedProps = decorators.reduce((current, next) => Object.assign(current, next.getGatheredProps?.(clickable)), {});
clickable[GatherProps] = function (this: GenericClickable) { clickable[GatherProps] = function (this: GenericClickable) {
const { const {
display, display,
@ -154,7 +168,8 @@ export function createClickable<T extends ClickableOptions>(
canClick, canClick,
small, small,
mark, mark,
id id,
...decoratedProps
}; };
}; };

View file

@ -15,6 +15,7 @@ import { convertComputable, processComputable } from "util/computed";
import { createLazyProxy } from "util/proxies"; import { createLazyProxy } from "util/proxies";
import type { Ref } from "vue"; import type { Ref } from "vue";
import { computed, unref } from "vue"; import { computed, unref } from "vue";
import { GenericDecorator } from "./decorators/common";
/** An object that configures a {@link Conversion}. */ /** An object that configures a {@link Conversion}. */
export interface ConversionOptions { export interface ConversionOptions {
@ -123,11 +124,16 @@ 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, BaseConversion, GenericConversion> optionsFunc: OptionsFunc<T, BaseConversion, GenericConversion>,
...decorators: GenericDecorator[]
): Conversion<T> { ): Conversion<T> {
return createLazyProxy(feature => { return createLazyProxy(feature => {
const conversion = optionsFunc.call(feature, feature); const conversion = optionsFunc.call(feature, feature);
for (const decorator of decorators) {
decorator.preConstruct?.(conversion);
}
(conversion as GenericConversion).formula = conversion.formula( (conversion as GenericConversion).formula = conversion.formula(
Formula.variable(conversion.baseResource) Formula.variable(conversion.baseResource)
); );
@ -187,6 +193,10 @@ export function createConversion<T extends ConversionOptions>(
processComputable(conversion as T, "buyMax"); processComputable(conversion as T, "buyMax");
setDefault(conversion, "buyMax", true); setDefault(conversion, "buyMax", true);
for (const decorator of decorators) {
decorator.postConstruct?.(conversion);
}
return conversion as unknown as Conversion<T>; return conversion as unknown as Conversion<T>;
}); });
} }

View file

@ -0,0 +1,97 @@
import { Replace } from "features/feature";
import Decimal, { DecimalSource } from "util/bignum";
import { Computable, GetComputableType, ProcessedComputable, processComputable } from "util/computed";
import { Ref, computed, unref } from "vue";
import { Decorator } from "./common";
export interface BonusAmountFeatureOptions {
bonusAmount: Computable<DecimalSource>;
totalAmount?: Computable<DecimalSource>;
}
export interface BonusCompletionsFeatureOptions {
bonusCompletions: Computable<DecimalSource>;
totalCompletions?: Computable<DecimalSource>;
}
export interface BaseBonusAmountFeature {
amount: Ref<DecimalSource>;
bonusAmount: ProcessedComputable<DecimalSource>;
totalAmount?: Ref<DecimalSource>;
}
export interface BaseBonusCompletionsFeature {
completions: Ref<DecimalSource>;
bonusCompletions: ProcessedComputable<DecimalSource>;
totalCompletions?: Ref<DecimalSource>;
}
export type BonusAmountFeature<T extends BonusAmountFeatureOptions> = Replace<
T, { bonusAmount: GetComputableType<T["bonusAmount"]>; }
>;
export type BonusCompletionsFeature<T extends BonusCompletionsFeatureOptions> = Replace<
T, { bonusAmount: GetComputableType<T["bonusCompletions"]>; }
>;
export type GenericBonusAmountFeature = Replace<
BonusAmountFeature<BonusAmountFeatureOptions>,
{
bonusAmount: ProcessedComputable<DecimalSource>;
totalAmount: ProcessedComputable<DecimalSource>;
}
>;
export type GenericBonusCompletionsFeature = Replace<
BonusCompletionsFeature<BonusCompletionsFeatureOptions>,
{
bonusCompletions: ProcessedComputable<DecimalSource>;
totalCompletions: ProcessedComputable<DecimalSource>;
}
>;
/**
* Allows the addition of "bonus levels" to the decorated feature, with an accompanying "total amount".
* To function properly, the `createFeature()` function must have its generic type extended by {@linkcode BonusAmountFeatureOptions}.
* Additionally, the base feature must have an `amount` property.
* To allow access to the decorated values outside the `createFeature()` function, the output type must be extended by {@linkcode GenericBonusAmountFeature}.
* @example ```ts
* createRepeatable<RepeatableOptions & BonusAmountFeatureOptions>(() => ({
* bonusAmount: noPersist(otherRepeatable.amount),
* ...
* }), bonusAmountDecorator) as GenericRepeatable & GenericBonusAmountFeature
*/
export const bonusAmountDecorator: Decorator<BonusAmountFeatureOptions, BaseBonusAmountFeature, GenericBonusAmountFeature> = {
preConstruct(feature) {
if (feature.amount === undefined) {
console.error(`Decorated feature ${feature.id} does not contain the required 'amount' property"`);
}
},
postConstruct(feature) {
processComputable(feature, "bonusAmount");
if (feature.totalAmount === undefined) {
feature.totalAmount = computed(() => Decimal.add(
unref(feature.amount ?? 0),
unref(feature.bonusAmount as ProcessedComputable<DecimalSource>)
));
}
}
}
/**
* Allows the addition of "bonus levels" to the decorated feature, with an accompanying "total amount".
* To function properly, the `createFeature()` function must have its generic type extended by {@linkcode BonusCompletionFeatureOptions}.
* To allow access to the decorated values outside the `createFeature()` function, the output type must be extended by {@linkcode GenericBonusCompletionFeature}.
* @example ```ts
* createChallenge<ChallengeOptions & BonusCompletionFeatureOptions>(() => ({
* bonusCompletions: noPersist(otherChallenge.completions),
* ...
* }), bonusCompletionDecorator) as GenericChallenge & GenericBonusCompletionFeature
*/
export const bonusCompletionsDecorator: Decorator<BonusCompletionsFeatureOptions, BaseBonusCompletionsFeature, GenericBonusCompletionsFeature> = {
postConstruct(feature) {
processComputable(feature, "bonusCompletions");
if (feature.totalCompletions === undefined) {
feature.totalCompletions = computed(() => Decimal.add(
unref(feature.completions ?? 0),
unref(feature.bonusCompletions as ProcessedComputable<DecimalSource>)
));
}
}
}

View file

@ -0,0 +1,43 @@
import { Replace, OptionsObject } from "../feature";
import { Computable, GetComputableType, processComputable, ProcessedComputable } from "util/computed";
import { Persistent, State } from "game/persistence";
export type Decorator<FeatureOptions, BaseFeature = object, GenericFeature = BaseFeature, S extends State = State> = {
getPersistentData?(): Record<string, Persistent<S>>;
preConstruct?(feature: OptionsObject<FeatureOptions,BaseFeature & {id:string},GenericFeature>): void;
postConstruct?(feature: OptionsObject<FeatureOptions,BaseFeature & {id:string},GenericFeature>): void;
getGatheredProps?(feature: OptionsObject<FeatureOptions,BaseFeature & {id:string},GenericFeature>): Partial<OptionsObject<FeatureOptions,BaseFeature & {id:string},GenericFeature>>
}
export type GenericDecorator = Decorator<unknown>;
export interface EffectFeatureOptions {
effect: Computable<any>;
}
export type EffectFeature<T extends EffectFeatureOptions> = Replace<
T, { effect: GetComputableType<T["effect"]>; }
>;
export type GenericEffectFeature = Replace<
EffectFeature<EffectFeatureOptions>,
{ effect: ProcessedComputable<any>; }
>;
/**
* Allows the usage of an `effect` field in the decorated feature.
* To function properly, the `createFeature()` function must have its generic type extended by {@linkcode EffectFeatureOptions}.
* To allow access to the decorated values outside the `createFeature()` function, the output type must be extended by {@linkcode GenericEffectFeature}.
* @example ```ts
* createRepeatable<RepeatableOptions & EffectFeatureOptions>(() => ({
* effect() { return Decimal.pow(2, this.amount); },
* ...
* }), effectDecorator) as GenericUpgrade & GenericEffectFeature;
* ```
*/
export const effectDecorator: Decorator<EffectFeatureOptions, unknown, GenericEffectFeature> = {
postConstruct(feature) {
processComputable(feature, "effect");
}
}

View file

@ -42,7 +42,9 @@ 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, R = unknown, S = R> = (obj: R) => T & Partial<R> & ThisType<T & S>; export type OptionsFunc<T, R = unknown, S = R> = (obj: R) => OptionsObject<T,R,S>;
export type OptionsObject<T, R = unknown, S = R> = T & Partial<R> & ThisType<T & S>;
let id = 0; let id = 0;
/** /**

View file

@ -30,6 +30,7 @@ import { createLazyProxy } from "util/proxies";
import { coerceComponent, isCoercableComponent } from "util/vue"; import { coerceComponent, isCoercableComponent } from "util/vue";
import type { Ref } from "vue"; import type { Ref } from "vue";
import { computed, unref } from "vue"; import { computed, unref } from "vue";
import { Decorator, GenericDecorator } from "./decorators/common";
/** A symbol used to identify {@link Repeatable} features. */ /** A symbol used to identify {@link Repeatable} features. */
export const RepeatableType = Symbol("Repeatable"); export const RepeatableType = Symbol("Repeatable");
@ -129,19 +130,27 @@ export type GenericRepeatable = Replace<
* @param optionsFunc Repeatable options. * @param optionsFunc Repeatable options.
*/ */
export function createRepeatable<T extends RepeatableOptions>( export function createRepeatable<T extends RepeatableOptions>(
optionsFunc: OptionsFunc<T, BaseRepeatable, GenericRepeatable> optionsFunc: OptionsFunc<T, BaseRepeatable, GenericRepeatable>,
...decorators: GenericDecorator[]
): Repeatable<T> { ): Repeatable<T> {
const amount = persistent<DecimalSource>(0); const amount = persistent<DecimalSource>(0);
return createLazyProxy(feature => { const decoratedData = decorators.reduce((current, next) => Object.assign(current, next.getPersistentData?.()), {});
return createLazyProxy<Repeatable<T>, Repeatable<T>>(feature => {
const repeatable = optionsFunc.call(feature, feature); const repeatable = optionsFunc.call(feature, feature);
repeatable.id = getUniqueID("repeatable-"); repeatable.id = getUniqueID("repeatable-");
repeatable.type = RepeatableType; repeatable.type = RepeatableType;
repeatable[Component] = ClickableComponent as GenericComponent; repeatable[Component] = ClickableComponent as GenericComponent;
for (const decorator of decorators) {
decorator.preConstruct?.(repeatable);
}
repeatable.amount = amount; repeatable.amount = amount;
repeatable.amount[DefaultValue] = repeatable.initialAmount ?? 0; repeatable.amount[DefaultValue] = repeatable.initialAmount ?? 0;
Object.assign(repeatable, decoratedData);
const limitRequirement = { const limitRequirement = {
requirementMet: computed(() => requirementMet: computed(() =>
Decimal.sub( Decimal.sub(
@ -223,14 +232,12 @@ export function createRepeatable<T extends RepeatableOptions>(
{currDisplay.showAmount === false ? null : ( {currDisplay.showAmount === false ? null : (
<div> <div>
<br /> <br />
{unref(genericRepeatable.limit) === Decimal.dInf ? ( joinJSX(
<>Amount: {formatWhole(genericRepeatable.amount.value)}</> <>Amount: {formatWhole(genericRepeatable.amount.value)}</>,
) : ( {unref(genericRepeatable.limit) !== Decimal.dInf ? (
<> <> / {formatWhole(unref(genericRepeatable.limit))}</>
Amount: {formatWhole(genericRepeatable.amount.value)} /{" "} ) : undefined}
{formatWhole(unref(genericRepeatable.limit))} )
</>
)}
</div> </div>
)} )}
{currDisplay.effectDisplay == null ? null : ( {currDisplay.effectDisplay == null ? null : (
@ -263,6 +270,11 @@ export function createRepeatable<T extends RepeatableOptions>(
processComputable(repeatable as T, "small"); processComputable(repeatable as T, "small");
processComputable(repeatable as T, "maximize"); processComputable(repeatable as T, "maximize");
for (const decorator of decorators) {
decorator.postConstruct?.(repeatable);
}
const decoratedProps = decorators.reduce((current, next) => Object.assign(current, next.getGatheredProps?.(repeatable)), {});
repeatable[GatherProps] = function (this: GenericRepeatable) { repeatable[GatherProps] = function (this: GenericRepeatable) {
const { display, visibility, style, classes, onClick, canClick, small, mark, id } = const { display, visibility, style, classes, onClick, canClick, small, mark, id } =
this; this;
@ -275,7 +287,8 @@ export function createRepeatable<T extends RepeatableOptions>(
canClick, canClick,
small, small,
mark, mark,
id id,
...decoratedProps
}; };
}; };

View file

@ -1,3 +1,4 @@
import { Decorator, GenericDecorator } from "features/decorators/common";
import type { import type {
CoercableComponent, CoercableComponent,
GenericComponent, GenericComponent,
@ -101,8 +102,10 @@ export type GenericTreeNode = Replace<
* @param optionsFunc Tree Node options. * @param optionsFunc Tree Node options.
*/ */
export function createTreeNode<T extends TreeNodeOptions>( export function createTreeNode<T extends TreeNodeOptions>(
optionsFunc?: OptionsFunc<T, BaseTreeNode, GenericTreeNode> optionsFunc?: OptionsFunc<T, BaseTreeNode, GenericTreeNode>,
...decorators: GenericDecorator[]
): TreeNode<T> { ): TreeNode<T> {
const decoratedData = decorators.reduce((current, next) => Object.assign(current, next.getPersistentData?.()), {});
return createLazyProxy(feature => { return createLazyProxy(feature => {
const treeNode = const treeNode =
optionsFunc?.call(feature, feature) ?? optionsFunc?.call(feature, feature) ??
@ -111,6 +114,12 @@ export function createTreeNode<T extends TreeNodeOptions>(
treeNode.type = TreeNodeType; treeNode.type = TreeNodeType;
treeNode[Component] = TreeNodeComponent as GenericComponent; treeNode[Component] = TreeNodeComponent as GenericComponent;
for (const decorator of decorators) {
decorator.preConstruct?.(treeNode);
}
Object.assign(decoratedData);
processComputable(treeNode as T, "visibility"); processComputable(treeNode as T, "visibility");
setDefault(treeNode, "visibility", Visibility.Visible); setDefault(treeNode, "visibility", Visibility.Visible);
processComputable(treeNode as T, "canClick"); processComputable(treeNode as T, "canClick");
@ -122,6 +131,10 @@ export function createTreeNode<T extends TreeNodeOptions>(
processComputable(treeNode as T, "style"); processComputable(treeNode as T, "style");
processComputable(treeNode as T, "mark"); processComputable(treeNode as T, "mark");
for (const decorator of decorators) {
decorator.postConstruct?.(treeNode);
}
if (treeNode.onClick) { if (treeNode.onClick) {
const onClick = treeNode.onClick.bind(treeNode); const onClick = treeNode.onClick.bind(treeNode);
treeNode.onClick = function (e) { treeNode.onClick = function (e) {
@ -139,6 +152,7 @@ export function createTreeNode<T extends TreeNodeOptions>(
}; };
} }
const decoratedProps = decorators.reduce((current, next) => Object.assign(current, next.getGatheredProps?.(treeNode)), {});
treeNode[GatherProps] = function (this: GenericTreeNode) { treeNode[GatherProps] = function (this: GenericTreeNode) {
const { const {
display, display,
@ -164,7 +178,8 @@ export function createTreeNode<T extends TreeNodeOptions>(
glowColor, glowColor,
canClick, canClick,
mark, mark,
id id,
...decoratedProps
}; };
}; };

View file

@ -1,4 +1,5 @@
import { isArray } from "@vue/shared"; import { isArray } from "@vue/shared";
import { Decorator, GenericDecorator } from "features/decorators/common";
import type { import type {
CoercableComponent, CoercableComponent,
GenericComponent, GenericComponent,
@ -14,11 +15,13 @@ import {
setDefault, setDefault,
Visibility Visibility
} from "features/feature"; } from "features/feature";
import { createResource } from "features/resources/resource";
import UpgradeComponent from "features/upgrades/Upgrade.vue"; import UpgradeComponent from "features/upgrades/Upgrade.vue";
import type { GenericLayer } from "game/layers"; import type { GenericLayer } from "game/layers";
import type { Persistent } from "game/persistence"; import type { Persistent } from "game/persistence";
import { persistent } from "game/persistence"; import { persistent } from "game/persistence";
import { import {
createCostRequirement,
createVisibilityRequirement, createVisibilityRequirement,
payRequirements, payRequirements,
Requirements, Requirements,
@ -115,16 +118,24 @@ export type GenericUpgrade = Replace<
* @param optionsFunc Upgrade options. * @param optionsFunc Upgrade options.
*/ */
export function createUpgrade<T extends UpgradeOptions>( export function createUpgrade<T extends UpgradeOptions>(
optionsFunc: OptionsFunc<T, BaseUpgrade, GenericUpgrade> optionsFunc: OptionsFunc<T, BaseUpgrade, GenericUpgrade>,
...decorators: GenericDecorator[]
): Upgrade<T> { ): Upgrade<T> {
const bought = persistent<boolean>(false, false); const bought = persistent<boolean>(false, false);
const decoratedData = decorators.reduce((current, next) => Object.assign(current, next.getPersistentData?.()), {});
return createLazyProxy(feature => { return createLazyProxy(feature => {
const upgrade = optionsFunc.call(feature, feature); const upgrade = optionsFunc.call(feature, feature);
upgrade.id = getUniqueID("upgrade-"); upgrade.id = getUniqueID("upgrade-");
upgrade.type = UpgradeType; upgrade.type = UpgradeType;
upgrade[Component] = UpgradeComponent as GenericComponent; upgrade[Component] = UpgradeComponent as GenericComponent;
for (const decorator of decorators) {
decorator.preConstruct?.(upgrade);
}
upgrade.bought = bought; upgrade.bought = bought;
Object.assign(upgrade, decoratedData);
upgrade.canPurchase = computed(() => requirementsMet(upgrade.requirements)); upgrade.canPurchase = computed(() => requirementsMet(upgrade.requirements));
upgrade.purchase = function () { upgrade.purchase = function () {
const genericUpgrade = upgrade as GenericUpgrade; const genericUpgrade = upgrade as GenericUpgrade;
@ -150,6 +161,11 @@ export function createUpgrade<T extends UpgradeOptions>(
processComputable(upgrade as T, "display"); processComputable(upgrade as T, "display");
processComputable(upgrade as T, "mark"); processComputable(upgrade as T, "mark");
for (const decorator of decorators) {
decorator.preConstruct?.(upgrade);
}
const decoratedProps = decorators.reduce((current, next) => Object.assign(current, next.getGatheredProps?.(upgrade)), {});
upgrade[GatherProps] = function (this: GenericUpgrade) { upgrade[GatherProps] = function (this: GenericUpgrade) {
const { const {
display, display,
@ -173,7 +189,8 @@ export function createUpgrade<T extends UpgradeOptions>(
bought, bought,
mark, mark,
id, id,
purchase purchase,
...decoratedProps
}; };
}; };