started on inc and cost refactors

This commit is contained in:
Rudis Muiznieks 2021-09-12 14:29:59 -05:00
parent 14e1b646a9
commit a0daffbddf
5 changed files with 26 additions and 14 deletions

View File

@ -1,3 +1,8 @@
## In Progress
- [ ] change `inc` to a `ResourceNumber` instead of a `number`
- [ ] change `cost` into a function that computes based on `value`
## Initial Game Progression Plan
### Phase 1: 0-1K Followers
@ -82,7 +87,7 @@
- [x] add an action to sell purchasables and regain some portion of their cost
- [ ] add a `policy` resource type that can be toggled on or off
- [x] remove recruitment effects of credibility (that will be for notoriety instead)
- [ ] change `value` to a getter that does `Math.floor` if it's a whole number resource
- [x] change `value` to a getter that does `Math.floor` if it's a whole number resource
## Long-Term Ideas

View File

@ -10,10 +10,10 @@ interface IResource {
readonly description: string;
readonly valueInWholeNumbers: boolean;
readonly cost?: ResourceNumber;
cost?: (state: GameState) => ResourceNumber;
inc?: (state: GameState) => ResourceNumber;
max?: (state: GameState) => number;
inc?: (state: GameState) => number;
advanceAction?: (time: number, state: GameState) => void;
userActions?: ResourceAction[];

View File

@ -26,6 +26,8 @@ abstract class Purchasable extends Resource {
protected _sellMultiplier?: number | ResourceNumber;
protected _isUnlocked = false;
private _lastWholeNumberValue = 0;
constructor(
public readonly label: string,
public readonly singularName: string,
@ -63,7 +65,6 @@ abstract class Purchasable extends Resource {
public restoreConfig = (config: ResourceConfig): void => {
this.rawValue = config.value;
if (config.cost !== undefined) this.cost = config.cost;
if (
config.config !== undefined &&
typeof config.config.isUnlocked === 'boolean'
@ -72,6 +73,21 @@ abstract class Purchasable extends Resource {
}
};
public addValue = (amount: number, _: GameState): void => {
this.rawValue += amount;
const wholeNumberChange = this.value - this._lastWholeNumberValue;
if (wholeNumberChange > 0) {
for (const key in this._costMultiplier) {
const rkey = <ResourceKey>key;
this.cost[rkey] =
(this.cost[rkey] ?? 0) *
(this._costMultiplier[rkey] ?? 1) *
wholeNumberChange;
}
this._lastWholeNumberValue = this.value;
}
};
protected _purchaseLog(amount: number, _state: GameState): string {
let verb = 'purchased';
if (amount < 0) {
@ -88,11 +104,6 @@ abstract class Purchasable extends Resource {
if (state.deductCost(this.cost)) {
this.addValue(1, state);
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);
}
}
}
@ -116,7 +127,7 @@ abstract class Purchasable extends Resource {
costBack[rkey] = cost * -1 * multiplier;
state.deductCost(costBack);
}
this.addValue(1, state);
this.addValue(-1, state);
state.log(this._purchaseLog(-1, state));
}
}

View File

@ -1,8 +1,6 @@
/// <reference path="./IResource.ts" />
abstract class Resource implements IResource {
public cost?: ResourceNumber = undefined;
protected rawValue = 0;
public abstract readonly resourceType: ResourceType;
@ -26,6 +24,5 @@ abstract class Resource implements IResource {
public restoreConfig = (config: ResourceConfig): void => {
this.rawValue = config.value;
this.cost = config.cost;
};
}

View File

@ -46,7 +46,6 @@ type ResourceConfigValues = { [key: string]: string | number | boolean };
type ResourceConfig = {
value: number;
cost?: ResourceNumber;
config?: ResourceConfigValues;
};