From 7ed71b6c4ba352f39dc54ebd9456f6784317dc37 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Sun, 5 Feb 2023 02:51:07 -0600 Subject: [PATCH] Make buyables support buying max --- src/features/buyable.tsx | 24 +++++++++++++++++++----- src/game/requirements.tsx | 3 +-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/features/buyable.tsx b/src/features/buyable.tsx index c848254..76c99bf 100644 --- a/src/features/buyable.tsx +++ b/src/features/buyable.tsx @@ -12,6 +12,7 @@ import { DefaultValue, Persistent, persistent } from "game/persistence"; import { createVisibilityRequirement, displayRequirements, + maxRequirementsMet, payRequirements, Requirements, requirementsMet @@ -49,6 +50,7 @@ export interface BuyableOptions { style?: Computable; mark?: Computable; small?: Computable; + buyMax?: Computable; display?: Computable; onPurchase?: VoidFunction; } @@ -75,6 +77,7 @@ export type Buyable = Replace< style: GetComputableType; mark: GetComputableType; small: GetComputableType; + buyMax: GetComputableType; display: Ref; } >; @@ -103,9 +106,9 @@ export function createBuyable( const limitRequirement = { requirementMet: computed(() => - Decimal.lt( - (buyable as GenericBuyable).amount.value, - unref((buyable as GenericBuyable).purchaseLimit) + Decimal.sub( + unref((buyable as GenericBuyable).purchaseLimit), + (buyable as GenericBuyable).amount.value ) ), requiresPay: false, @@ -143,7 +146,12 @@ export function createBuyable( if (!unref(genericBuyable.canClick)) { return; } - payRequirements(buyable.requirements); + payRequirements( + buyable.requirements, + unref(genericBuyable.buyMax) + ? maxRequirementsMet(genericBuyable.requirements) + : 1 + ); genericBuyable.amount.value = Decimal.add(genericBuyable.amount.value, 1); genericBuyable.onPurchase?.(); }; @@ -192,7 +200,12 @@ export function createBuyable( {genericBuyable.maxed.value ? null : (

- {displayRequirements(genericBuyable.requirements)} + {displayRequirements( + genericBuyable.requirements, + unref(genericBuyable.buyMax) + ? maxRequirementsMet(genericBuyable.requirements) + : 1 + )}
)} @@ -208,6 +221,7 @@ export function createBuyable( processComputable(buyable as T, "style"); processComputable(buyable as T, "mark"); processComputable(buyable as T, "small"); + processComputable(buyable as T, "buyMax"); buyable[GatherProps] = function (this: GenericBuyable) { const { display, visibility, style, classes, onClick, canClick, small, mark, id } = diff --git a/src/game/requirements.tsx b/src/game/requirements.tsx index b4126fc..0a73372 100644 --- a/src/game/requirements.tsx +++ b/src/game/requirements.tsx @@ -214,8 +214,7 @@ export function displayRequirements(requirements: Requirements, amount: DecimalS return requirements.display?.() ?? <>; } -export function payRequirements(requirements: Requirements, buyMax = false) { - const amount = buyMax ? maxRequirementsMet(requirements) : 1; +export function payRequirements(requirements: Requirements, amount: DecimalSource = 1) { if (isArray(requirements)) { requirements.filter(r => unref(r.requiresPay)).forEach(r => r.pay?.(amount)); } else if (unref(requirements.requiresPay)) {