This repository has been archived on 2022-01-16. You can view files and clone it, but cannot push or open issues or pull requests.
irreligious/src/model/resource/Purchasable.ts

71 lines
2.1 KiB
TypeScript
Raw Normal View History

/// <reference path="./IResource.ts" />
abstract class Purchasable implements IResource {
2021-09-05 14:45:37 -05:00
public readonly resourceType: ResourceType = ResourceType.consumable;
2021-09-04 22:21:14 -05:00
public valueInWholeNumbers = true;
public value = 0;
public readonly cost: ResourceNumber = {};
2021-09-05 19:20:24 -05:00
public inc?: (state: GameState) => number = undefined;
public max?: (_state: GameState) => number = undefined;
2021-09-05 14:45:37 -05:00
public userActions: ResourceAction[] = [
{
name: this._purchaseButtonText,
description: this._purchaseDescription,
isEnabled: (state: GameState): boolean =>
(this.max === undefined || this.value < this.max(state)) &&
state.isPurchasable(this.cost),
performAction: (state: GameState): void => {
this._purchase(state);
},
},
];
protected _costMultiplier: ResourceNumber = {};
2021-09-04 22:21:14 -05:00
protected _isUnlocked = false;
constructor(
2021-09-06 08:51:41 -05:00
public readonly label: string,
public readonly singularName: string,
public readonly pluralName: string,
public readonly description: string,
private readonly _purchaseButtonText: string = 'Purchase',
private readonly _purchaseDescription: string = `Buy a ${singularName}.`
) {}
public addValue(amount: number, _state: GameState): void {
this.value += amount;
}
public isUnlocked(state: GameState): boolean {
2021-08-21 19:02:57 -05:00
if (!this._isUnlocked && state.isPurchasable(this.cost)) {
this._isUnlocked = true;
}
return this._isUnlocked;
}
2021-08-21 12:45:58 -05:00
public advanceAction(_time: number, _state: GameState): void {
return;
}
protected _purchaseLog(amount: number, _state: GameState): string {
return `You purchased ${amount} ${
amount > 1 ? this.pluralName : this.singularName
}.`;
}
private _purchase(state: GameState): void {
if (this.max !== undefined && this.value >= this.max(state)) return;
if (state.deductCost(this.cost)) {
2021-09-06 09:51:23 -05:00
this.value += 1;
state.log(this._purchaseLog(1, state));
for (const key in this._costMultiplier) {
const rkey = <ResourceKey>key;
this.cost[rkey] =
(this.cost[rkey] ?? 0) * (this._costMultiplier[rkey] ?? 1);
}
}
2021-08-21 21:06:29 -05:00
}
}