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

60 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: string]: number } = { };
2021-09-05 14:45:37 -05:00
public inc: ((state: GameState) => number) | null = null;
public max: ((_state: GameState) => number) | null = null;
2021-08-21 19:02:57 -05:00
protected _costMultiplier: { [key: string]: 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)) {
2021-08-21 21:06:29 -05:00
const amount: number = this._purchaseAmount(state);
if (amount > 0) {
this.value += amount;
state.log(this._purchaseLog(amount, state));
for (const rkey of Object.keys(this._costMultiplier)) {
this.cost[rkey] *= this._costMultiplier[rkey];
}
}
}
}
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;
}
2021-08-21 19:02:57 -05:00
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 {
return `You purchased ${amount} x ${this.name}.`;
}
}