From 68702c5620e29c0dbe53d241cbd28cec9cbc5cf4 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 cdff9a0..89dcada 100644 --- a/src/features/buyable.tsx +++ b/src/features/buyable.tsx @@ -6,6 +6,7 @@ import { DefaultValue, Persistent, persistent } from "game/persistence"; import { createVisibilityRequirement, displayRequirements, + maxRequirementsMet, payRequirements, Requirements, requirementsMet @@ -44,6 +45,7 @@ export interface BuyableOptions { style?: Computable; mark?: Computable; small?: Computable; + buyMax?: Computable; display?: Computable; onPurchase?: VoidFunction; } @@ -70,6 +72,7 @@ export type Buyable = Replace< style: GetComputableType; mark: GetComputableType; small: GetComputableType; + buyMax: GetComputableType; display: Ref; } >; @@ -98,9 +101,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, @@ -138,7 +141,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?.(); }; @@ -187,7 +195,12 @@ export function createBuyable( {genericBuyable.maxed.value ? null : (

- {displayRequirements(genericBuyable.requirements)} + {displayRequirements( + genericBuyable.requirements, + unref(genericBuyable.buyMax) + ? maxRequirementsMet(genericBuyable.requirements) + : 1 + )}
)} @@ -203,6 +216,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)) {