Make buyables support buying max

This commit is contained in:
thepaperpilot 2023-02-05 02:51:07 -06:00
parent 7eeb0318e2
commit 68702c5620
2 changed files with 20 additions and 7 deletions

View file

@ -6,6 +6,7 @@ import { DefaultValue, Persistent, persistent } from "game/persistence";
import { import {
createVisibilityRequirement, createVisibilityRequirement,
displayRequirements, displayRequirements,
maxRequirementsMet,
payRequirements, payRequirements,
Requirements, Requirements,
requirementsMet requirementsMet
@ -44,6 +45,7 @@ export interface BuyableOptions {
style?: Computable<StyleValue>; style?: Computable<StyleValue>;
mark?: Computable<boolean | string>; mark?: Computable<boolean | string>;
small?: Computable<boolean>; small?: Computable<boolean>;
buyMax?: Computable<boolean>;
display?: Computable<BuyableDisplay>; display?: Computable<BuyableDisplay>;
onPurchase?: VoidFunction; onPurchase?: VoidFunction;
} }
@ -70,6 +72,7 @@ export type Buyable<T extends BuyableOptions> = Replace<
style: GetComputableType<T["style"]>; style: GetComputableType<T["style"]>;
mark: GetComputableType<T["mark"]>; mark: GetComputableType<T["mark"]>;
small: GetComputableType<T["small"]>; small: GetComputableType<T["small"]>;
buyMax: GetComputableType<T["buyMax"]>;
display: Ref<CoercableComponent>; display: Ref<CoercableComponent>;
} }
>; >;
@ -98,9 +101,9 @@ export function createBuyable<T extends BuyableOptions>(
const limitRequirement = { const limitRequirement = {
requirementMet: computed(() => requirementMet: computed(() =>
Decimal.lt( Decimal.sub(
(buyable as GenericBuyable).amount.value, unref((buyable as GenericBuyable).purchaseLimit),
unref((buyable as GenericBuyable).purchaseLimit) (buyable as GenericBuyable).amount.value
) )
), ),
requiresPay: false, requiresPay: false,
@ -138,7 +141,12 @@ export function createBuyable<T extends BuyableOptions>(
if (!unref(genericBuyable.canClick)) { if (!unref(genericBuyable.canClick)) {
return; return;
} }
payRequirements(buyable.requirements); payRequirements(
buyable.requirements,
unref(genericBuyable.buyMax)
? maxRequirementsMet(genericBuyable.requirements)
: 1
);
genericBuyable.amount.value = Decimal.add(genericBuyable.amount.value, 1); genericBuyable.amount.value = Decimal.add(genericBuyable.amount.value, 1);
genericBuyable.onPurchase?.(); genericBuyable.onPurchase?.();
}; };
@ -187,7 +195,12 @@ export function createBuyable<T extends BuyableOptions>(
{genericBuyable.maxed.value ? null : ( {genericBuyable.maxed.value ? null : (
<div> <div>
<br /> <br />
{displayRequirements(genericBuyable.requirements)} {displayRequirements(
genericBuyable.requirements,
unref(genericBuyable.buyMax)
? maxRequirementsMet(genericBuyable.requirements)
: 1
)}
</div> </div>
)} )}
</span> </span>
@ -203,6 +216,7 @@ export function createBuyable<T extends BuyableOptions>(
processComputable(buyable as T, "style"); processComputable(buyable as T, "style");
processComputable(buyable as T, "mark"); processComputable(buyable as T, "mark");
processComputable(buyable as T, "small"); processComputable(buyable as T, "small");
processComputable(buyable as T, "buyMax");
buyable[GatherProps] = function (this: GenericBuyable) { buyable[GatherProps] = function (this: GenericBuyable) {
const { display, visibility, style, classes, onClick, canClick, small, mark, id } = const { display, visibility, style, classes, onClick, canClick, small, mark, id } =

View file

@ -214,8 +214,7 @@ export function displayRequirements(requirements: Requirements, amount: DecimalS
return requirements.display?.() ?? <></>; return requirements.display?.() ?? <></>;
} }
export function payRequirements(requirements: Requirements, buyMax = false) { export function payRequirements(requirements: Requirements, amount: DecimalSource = 1) {
const amount = buyMax ? maxRequirementsMet(requirements) : 1;
if (isArray(requirements)) { if (isArray(requirements)) {
requirements.filter(r => unref(r.requiresPay)).forEach(r => r.pay?.(amount)); requirements.filter(r => unref(r.requiresPay)).forEach(r => r.pay?.(amount));
} else if (unref(requirements.requiresPay)) { } else if (unref(requirements.requiresPay)) {