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

62 lines
1.7 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 clickText = 'Purchase';
public clickDescription = 'Purchase';
public value = 0;
public readonly cost: { [key in ResourceKey]?: number } = { };
2021-09-05 14:45:37 -05:00
public inc: ((state: GameState) => number) | null = null;
public max: ((_state: GameState) => number) | null = null;
protected _costMultiplier: { [key in ResourceKey]?: number } = { };
2021-09-04 22:21:14 -05:00
protected _isUnlocked = false;
constructor (
public readonly name: string,
public readonly description: string
) { }
public clickAction (state: GameState): void {
2021-09-05 14:45:37 -05:00
if (this.max !== null && this.value >= this.max(state)) return;
if (state.deductCost(this.cost)) {
const amount = this._purchaseAmount(state);
2021-08-21 21:06:29 -05:00
if (amount > 0) {
this.value += amount;
state.log(this._purchaseLog(amount, 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
}
}
}
}
2021-09-05 14:45:37 -05:00
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 _purchaseAmount (_state: GameState): number {
2021-08-21 12:45:58 -05:00
return 1;
}
2021-08-21 21:06:29 -05:00
protected _purchaseLog (amount: number, _state: GameState): string {
2021-08-21 21:06:29 -05:00
return `You purchased ${amount} x ${this.name}.`;
}
}