Make buyables' onClick/purchase function support custom canPurchases

They can also now be overidden from the options function
This commit is contained in:
thepaperpilot 2022-05-10 21:23:05 -05:00
parent 3118db1402
commit ce2f9f8ee0

View file

@ -46,7 +46,7 @@ export interface BuyableOptions {
mark?: Computable<boolean | string>; mark?: Computable<boolean | string>;
small?: Computable<boolean>; small?: Computable<boolean>;
display?: Computable<BuyableDisplay>; display?: Computable<BuyableDisplay>;
onPurchase?: (cost: DecimalSource) => void; onPurchase?: (cost: DecimalSource | undefined) => void;
} }
export interface BaseBuyable { export interface BaseBuyable {
@ -144,20 +144,25 @@ export function createBuyable<T extends BuyableOptions>(
}); });
processComputable(buyable as T, "canPurchase"); processComputable(buyable as T, "canPurchase");
buyable.canClick = buyable.canPurchase as ProcessedComputable<boolean>; buyable.canClick = buyable.canPurchase as ProcessedComputable<boolean>;
buyable.onClick = buyable.purchase = function () { buyable.onClick = buyable.purchase =
const genericBuyable = buyable as GenericBuyable; buyable.onClick ??
if ( buyable.purchase ??
!unref(genericBuyable.canPurchase) || function (this: GenericBuyable) {
genericBuyable.cost == null || const genericBuyable = buyable as GenericBuyable;
genericBuyable.resource == null if (!unref(genericBuyable.canPurchase)) {
) { return;
return; }
} const cost = unref(genericBuyable.cost);
const cost = unref(genericBuyable.cost); if (genericBuyable.cost != null && genericBuyable.resource != null) {
genericBuyable.resource.value = Decimal.sub(genericBuyable.resource.value, cost); genericBuyable.resource.value = Decimal.sub(
genericBuyable.amount.value = Decimal.add(genericBuyable.amount.value, 1); genericBuyable.resource.value,
this.onPurchase?.(cost); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
}; cost!
);
genericBuyable.amount.value = Decimal.add(genericBuyable.amount.value, 1);
}
this.onPurchase?.(cost);
};
processComputable(buyable as T, "display"); processComputable(buyable as T, "display");
const display = buyable.display; const display = buyable.display;
buyable.display = jsx(() => { buyable.display = jsx(() => {