Make buyables support buying max

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

View file

@ -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<StyleValue>;
mark?: Computable<boolean | string>;
small?: Computable<boolean>;
buyMax?: Computable<boolean>;
display?: Computable<BuyableDisplay>;
onPurchase?: VoidFunction;
}
@ -75,6 +77,7 @@ export type Buyable<T extends BuyableOptions> = Replace<
style: GetComputableType<T["style"]>;
mark: GetComputableType<T["mark"]>;
small: GetComputableType<T["small"]>;
buyMax: GetComputableType<T["buyMax"]>;
display: Ref<CoercableComponent>;
}
>;
@ -103,9 +106,9 @@ export function createBuyable<T extends BuyableOptions>(
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<T extends BuyableOptions>(
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<T extends BuyableOptions>(
{genericBuyable.maxed.value ? null : (
<div>
<br />
{displayRequirements(genericBuyable.requirements)}
{displayRequirements(
genericBuyable.requirements,
unref(genericBuyable.buyMax)
? maxRequirementsMet(genericBuyable.requirements)
: 1
)}
</div>
)}
</span>
@ -208,6 +221,7 @@ export function createBuyable<T extends BuyableOptions>(
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 } =

View file

@ -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)) {