From a0daffbddf3b51f27015efc1456558d24c49a03f Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Sun, 12 Sep 2021 14:29:59 -0500 Subject: [PATCH] started on inc and cost refactors --- TODO.md | 7 ++++++- src/model/resource/IResource.ts | 4 ++-- src/model/resource/Purchasable.ts | 25 ++++++++++++++++++------- src/model/resource/Resource.ts | 3 --- src/model/resource/SharedTypes.ts | 1 - 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/TODO.md b/TODO.md index 6502f55..a433cd2 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/src/model/resource/IResource.ts b/src/model/resource/IResource.ts index 1ac8511..56c9127 100644 --- a/src/model/resource/IResource.ts +++ b/src/model/resource/IResource.ts @@ -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[]; diff --git a/src/model/resource/Purchasable.ts b/src/model/resource/Purchasable.ts index 419df5e..906578c 100644 --- a/src/model/resource/Purchasable.ts +++ b/src/model/resource/Purchasable.ts @@ -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 = 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 = 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)); } } diff --git a/src/model/resource/Resource.ts b/src/model/resource/Resource.ts index e74c718..418cab6 100644 --- a/src/model/resource/Resource.ts +++ b/src/model/resource/Resource.ts @@ -1,8 +1,6 @@ /// 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; }; } diff --git a/src/model/resource/SharedTypes.ts b/src/model/resource/SharedTypes.ts index 946a466..009900b 100644 --- a/src/model/resource/SharedTypes.ts +++ b/src/model/resource/SharedTypes.ts @@ -46,7 +46,6 @@ type ResourceConfigValues = { [key: string]: string | number | boolean }; type ResourceConfig = { value: number; - cost?: ResourceNumber; config?: ResourceConfigValues; };