forked from profectus/Profectus
Make buyables' onClick/purchase function support custom canPurchases
They can also now be overidden from the options function
This commit is contained in:
parent
3118db1402
commit
ce2f9f8ee0
1 changed files with 20 additions and 15 deletions
|
@ -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,18 +144,23 @@ 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 =
|
||||||
|
buyable.onClick ??
|
||||||
|
buyable.purchase ??
|
||||||
|
function (this: GenericBuyable) {
|
||||||
const genericBuyable = buyable as GenericBuyable;
|
const genericBuyable = buyable as GenericBuyable;
|
||||||
if (
|
if (!unref(genericBuyable.canPurchase)) {
|
||||||
!unref(genericBuyable.canPurchase) ||
|
|
||||||
genericBuyable.cost == null ||
|
|
||||||
genericBuyable.resource == null
|
|
||||||
) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const cost = unref(genericBuyable.cost);
|
const cost = unref(genericBuyable.cost);
|
||||||
genericBuyable.resource.value = Decimal.sub(genericBuyable.resource.value, cost);
|
if (genericBuyable.cost != null && genericBuyable.resource != null) {
|
||||||
|
genericBuyable.resource.value = Decimal.sub(
|
||||||
|
genericBuyable.resource.value,
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
cost!
|
||||||
|
);
|
||||||
genericBuyable.amount.value = Decimal.add(genericBuyable.amount.value, 1);
|
genericBuyable.amount.value = Decimal.add(genericBuyable.amount.value, 1);
|
||||||
|
}
|
||||||
this.onPurchase?.(cost);
|
this.onPurchase?.(cost);
|
||||||
};
|
};
|
||||||
processComputable(buyable as T, "display");
|
processComputable(buyable as T, "display");
|
||||||
|
|
Loading…
Reference in a new issue