code cleanup and using addValue instead of value += everywhere

This commit is contained in:
Rudis Muiznieks 2021-08-22 11:51:16 -05:00
parent 03ffdf0380
commit a4ffd985ef
11 changed files with 100 additions and 103 deletions

View File

@ -50,13 +50,14 @@ class GameState {
: 0; : 0;
if (inc > 0 && (max === null if (inc > 0 && (max === null
|| this._resources[rkey].value < max)) { || this._resources[rkey].value < max)) {
this._resources[rkey].value += inc * time / 1000; this._resources[rkey].addValue(inc * time / 1000, this);
} }
if (max !== null && this._resources[rkey].value > max) { const val: number = this._resources[rkey].value;
this._resources[rkey].value = max; if (max !== null && val > max) {
this._resources[rkey].addValue((val - max) * -1, this);
} }
if (this._resources[rkey].value < 0) { if (val < 0) {
this._resources[rkey].value = 0; this._resources[rkey].addValue(val * -1, this);
} }
} }
} }
@ -82,7 +83,7 @@ class GameState {
if (cost === null || Object.keys(cost) === null) return true; if (cost === null || Object.keys(cost) === null) return true;
if (!this.isPurchasable(cost)) return false; if (!this.isPurchasable(cost)) return false;
for (const rkey of Object.keys(cost)) { for (const rkey of Object.keys(cost)) {
this._resources[rkey].value -= cost[rkey]; this._resources[rkey].addValue(cost[rkey] * -1, this);
} }
return true; return true;
} }
@ -151,7 +152,9 @@ class GameState {
if (saveObj[rkey] !== undefined if (saveObj[rkey] !== undefined
&& saveObj[rkey].value !== undefined && saveObj[rkey].value !== undefined
&& saveObj[rkey].cost !== undefined) { && saveObj[rkey].cost !== undefined) {
// @ts-ignore
this._resources[rkey].value = saveObj[rkey].value; this._resources[rkey].value = saveObj[rkey].value;
// @ts-ignore
this._resources[rkey].cost = saveObj[rkey].cost; this._resources[rkey].cost = saveObj[rkey].cost;
} }
} }

View File

@ -6,8 +6,9 @@ class Credibility extends Passive {
constructor () { constructor () {
super( super(
'Credibility', 'Credibility',
'Affects your ability to recruit and retain followers.', 'Affects your ability to recruit and retain followers.');
100, 100, 0.25); this._baseMax = 100;
this.value = 100;
} }
public max (state: GameState): number { public max (state: GameState): number {

View File

@ -1,13 +1,12 @@
/// <reference path="./Purchasable.ts" /> /// <reference path="./Purchasable.ts" />
class CryptoCurrency extends Purchasable { class CryptoCurrency extends Purchasable {
public readonly valueInWholeNumbers: boolean = false;
constructor () { constructor () {
super('Faithcoin', super('Faithcoin',
"A crypto coin that can't be spent directly, but provides a steady stream of passive income."); "A crypto coin that can't be spent directly, but provides a steady stream of passive income.");
this.cost.money = 100; this.cost.money = 100;
this._costMultiplier.money = 1.1; this._costMultiplier.money = 1.1;
this._baseMax = 1000; this._baseMax = 1000;
this.valueInWholeNumbers = false;
} }
} }

View File

@ -7,24 +7,19 @@ enum ResourceType {
} }
interface IResource { interface IResource {
name: string | null; readonly resourceType: ResourceType;
description: string | null; readonly name: string | null;
readonly description: string | null;
resourceType: ResourceType; readonly valueInWholeNumbers: boolean;
value: number; readonly clickText: string;
valueInWholeNumbers: boolean; readonly clickDescription: string;
readonly value: number;
clickText: string; readonly cost: { [key: string]: number };
clickDescription: string;
clickAction (state: GameState): void;
cost: { [key: string]: number };
max (state: GameState): number | null; max (state: GameState): number | null;
inc (state: GameState): number | null; inc (state: GameState): number | null;
clickAction (state: GameState): void;
addValue (amount: number, state: GameState): void;
isUnlocked (state: GameState): boolean; isUnlocked (state: GameState): boolean;
advanceAction (time: number, state: GameState): void; advanceAction (time: number, state: GameState): void;
} }

View File

@ -2,13 +2,12 @@
abstract class Job implements IResource { abstract class Job implements IResource {
public readonly resourceType: ResourceType = ResourceType.Job; public readonly resourceType: ResourceType = ResourceType.Job;
public value: number = 0;
public readonly valueInWholeNumbers: boolean = true; public readonly valueInWholeNumbers: boolean = true;
public readonly clickText: string = 'Hire';
public clickText: string = 'Hire'; public readonly clickDescription: string =
public clickDescription: string = 'Promote one of your followers.'; 'Promote one of your followers.';
public value: number = 0;
public cost: { [key: string]: number } = { }; public readonly cost: { [key: string]: number } = { };
protected _costMultiplier: { [key: string]: number } = { }; protected _costMultiplier: { [key: string]: number } = { };
protected _isUnlocked: boolean = false; protected _isUnlocked: boolean = false;
@ -18,13 +17,21 @@ abstract class Job implements IResource {
public readonly description: string public readonly description: string
) { } ) { }
public max (state: GameState): number | null {
return null;
}
public inc (state: GameState): number | null {
return null;
}
public clickAction (state: GameState): void { public clickAction (state: GameState): void {
if (this._availableJobs(state) <= 0) { if (this._availableJobs(state) <= 0) {
state.log('You have no unemployed followers to promote.'); state.log('You have no unemployed followers to promote.');
return; return;
} }
if (this.value < this.max(state) && state.deductCost(this.cost)) { if (this.value < this.max(state) && state.deductCost(this.cost)) {
this.value++; this.addValue(1, state);
state.log(this._hireLog(1, state)); state.log(this._hireLog(1, state));
for (const rkey of Object.keys(this._costMultiplier)) { for (const rkey of Object.keys(this._costMultiplier)) {
this.cost[rkey] *= this._costMultiplier[rkey]; this.cost[rkey] *= this._costMultiplier[rkey];
@ -32,22 +39,18 @@ abstract class Job implements IResource {
} }
} }
public inc (state: GameState): number | null { public addValue (amount: number, state: GameState): void {
return null; this.value += amount;
}
public max (state: GameState): number | null {
return null;
}
public advanceAction (time: number, state: GameState): void {
return;
} }
public isUnlocked (state: GameState): boolean { public isUnlocked (state: GameState): boolean {
return this._isUnlocked; return this._isUnlocked;
} }
public advanceAction (time: number, state: GameState): void {
return;
}
protected _availableJobs (state: GameState): number { protected _availableJobs (state: GameState): number {
// number of followers minus the number of filled jobs // number of followers minus the number of filled jobs
const followers: number = state.getResource('plorg').value; const followers: number = state.getResource('plorg').value;

View File

@ -1,11 +1,9 @@
/// <reference path="./Purchasable.ts" /> /// <reference path="./Purchasable.ts" />
class Money extends Purchasable { class Money extends Purchasable {
private _lastCollectionTime: number = 0; public readonly resourceType: ResourceType = ResourceType.Consumable;
public resourceType: ResourceType = ResourceType.Consumable; private _lastCollectionTime: number = 0;
public cost: { [key: string]: number } = { };
public readonly valueInWholeNumbers: boolean = false;
constructor ( constructor (
public value: number public value: number
@ -14,10 +12,14 @@ class Money extends Purchasable {
this.clickText = 'Collect Tithes'; this.clickText = 'Collect Tithes';
this.clickDescription = 'Voluntary contributions from followers.'; this.clickDescription = 'Voluntary contributions from followers.';
this._baseMax = 500000; this._baseMax = 500000;
this.valueInWholeNumbers = false;
this._isUnlocked = true;
} }
public isUnlocked (state: GameState): boolean { public max (state: GameState): number | null {
return true; let max: number = this._baseMax;
max += state.getResource('cmpnd').value * 500000;
return max;
} }
public inc (state: GameState): number { public inc (state: GameState): number {
@ -39,7 +41,7 @@ class Money extends Purchasable {
const diff: number = state.now - this._lastCollectionTime; const diff: number = state.now - this._lastCollectionTime;
if (diff < state.config.cfgTimeBetweenTithes) { if (diff < state.config.cfgTimeBetweenTithes) {
const lost: number = state.config.cfgTimeBetweenTithes / diff / 3; const lost: number = state.config.cfgTimeBetweenTithes / diff / 3;
state.getResource('creds').value -= lost; state.getResource('creds').addValue(lost * -1, state);
} }
// each follower gives you $10 // each follower gives you $10
const tithings: number = plorg.value * state.config.cfgTitheAmount; const tithings: number = plorg.value * state.config.cfgTitheAmount;
@ -51,10 +53,4 @@ class Money extends Purchasable {
const followers: number = state.getResource('plorg').value; const followers: number = state.getResource('plorg').value;
return `You collected $${state.formatNumber(amount)} from ${state.formatNumber(followers)} followers.`; return `You collected $${state.formatNumber(amount)} from ${state.formatNumber(followers)} followers.`;
} }
public max (state: GameState): number | null {
let max: number = this._baseMax;
max += state.getResource('cmpnd').value * 500000;
return max;
}
} }

View File

@ -2,32 +2,32 @@
abstract class Passive implements IResource { abstract class Passive implements IResource {
public readonly resourceType: ResourceType = ResourceType.Passive; public readonly resourceType: ResourceType = ResourceType.Passive;
public readonly valueInWholeNumbers: boolean = false;
public readonly clickText: null = null; public readonly clickText: null = null;
public readonly clickDescription: null = null; public readonly clickDescription: null = null;
public value: number = 0;
public readonly cost: null = null; public readonly cost: null = null;
public readonly clickAction: null = null; public readonly clickAction: null = null;
public readonly valueInWholeNumbers: boolean = false;
protected _baseMax: number | null; protected _baseMax: number | null;
protected _baseInc: number | null; protected _baseInc: number | null;
constructor ( constructor (
public name: string, public readonly name: string,
public description: string, public readonly description: string
public value: number, ) { }
max: number | null,
inc: number | null public max (state: GameState): number | null {
) { return this._baseMax;
this._baseMax = max;
this._baseInc = inc;
} }
public inc (state: GameState): number | null { public inc (state: GameState): number | null {
return this._baseInc; return this._baseInc;
} }
public max (state: GameState): number | null { public addValue (amount: number, state: GameState): void {
return this._baseMax; this.value += amount;
} }
public isUnlocked (state: GameState): boolean { public isUnlocked (state: GameState): boolean {

View File

@ -28,7 +28,7 @@ class Pastor extends Job {
if (collected > money.max(state) - money.value) if (collected > money.max(state) - money.value)
collected = money.max(state) - money.value; collected = money.max(state) - money.value;
if (collected > 0) { if (collected > 0) {
money.value += collected; money.addValue(collected, state);
state.log(`Your pastors collected $${state.formatNumber(collected)} in tithings from ${state.formatNumber(tithed)} followers.`); state.log(`Your pastors collected $${state.formatNumber(collected)} in tithings from ${state.formatNumber(tithed)} followers.`);
} }
this._timeSinceLastTithe = 0; this._timeSinceLastTithe = 0;

View File

@ -4,22 +4,17 @@ class PlayerOrg implements IResource {
public readonly resourceType: ResourceType = ResourceType.Religion; public readonly resourceType: ResourceType = ResourceType.Religion;
public readonly name: string = 'Player'; public readonly name: string = 'Player';
public readonly description: string = 'In you they trust.'; public readonly description: string = 'In you they trust.';
public cost: { [key: string]: number } = { };
public value: number = 0;
public readonly valueInWholeNumbers: boolean = true; public readonly valueInWholeNumbers: boolean = true;
public readonly clickText: string = 'Recruit';
public readonly clickDescription: string = 'Gather new followers.';
public value: number = 0;
public readonly cost: { [key: string]: number } = { };
public clickText: string = 'Recruit'; public readonly inc: null = null;
public clickDescription: string = 'Gather new followers.';
private _lastLostTime: number = 0; private _lastLostTime: number = 0;
private _baseMax: number = 5; private _baseMax: number = 5;
public isUnlocked (state: GameState): boolean {
return true;
}
public max (state: GameState): number { public max (state: GameState): number {
let max: number = this._baseMax; let max: number = this._baseMax;
max += state.getResource('tents').value * 2; max += state.getResource('tents').value * 2;
@ -53,14 +48,12 @@ class PlayerOrg implements IResource {
} }
} }
public inc (state: GameState): number { public addValue (amount: number, state: GameState): void {
let inc: number = 0; this.value += amount;
}
// pastor auto-recruit public isUnlocked (state: GameState): boolean {
inc += state.getResource('pstor').value return true;
* state.config.cfgPastorRecruitRate;
return inc;
} }
public advanceAction (time: number, state: GameState): void { public advanceAction (time: number, state: GameState): void {
@ -71,9 +64,9 @@ class PlayerOrg implements IResource {
const ratio: number = Math.ceil(creds.value) / creds.max(state); const ratio: number = Math.ceil(creds.value) / creds.max(state);
if (Math.random() > ratio) { if (Math.random() > ratio) {
const lost: number = Math.ceil(this.value / 25 * (1 - ratio)); const lost: number = Math.ceil(this.value / 25 * (1 - ratio));
this.value -= lost; this.addValue(lost * -1, state);
const dest: [string, IResource] = this._getRandomReligion(state); const dest: [string, IResource] = this._getRandomReligion(state);
dest[1].value += lost; dest[1].addValue(lost, state);
state.log(`You lost ${lost} followers to ${dest[1].name}.`); state.log(`You lost ${lost} followers to ${dest[1].name}.`);
} }
} }

View File

@ -2,13 +2,11 @@
abstract class Purchasable implements IResource { abstract class Purchasable implements IResource {
public readonly resourceType: ResourceType = ResourceType.Consumable; public readonly resourceType: ResourceType = ResourceType.Consumable;
public value: number = 0;
public valueInWholeNumbers: boolean = true; public valueInWholeNumbers: boolean = true;
public clickText: string = 'Purchase'; public clickText: string = 'Purchase';
public clickDescription: string = 'Purchase'; public clickDescription: string = 'Purchase';
public value: number = 0;
public cost: { [key: string]: number } = { }; public readonly cost: { [key: string]: number } = { };
protected _costMultiplier: { [key: string]: number } = { }; protected _costMultiplier: { [key: string]: number } = { };
protected _baseMax: number | null = null; protected _baseMax: number | null = null;
@ -19,6 +17,14 @@ abstract class Purchasable implements IResource {
public readonly description: string public readonly description: string
) { } ) { }
public max (state: GameState): number | null {
return this._baseMax;
}
public inc (state: GameState): number | null {
return null;
}
public clickAction (state: GameState): void { public clickAction (state: GameState): void {
if (this.max(state) !== null && this.value >= this.max(state)) return; if (this.max(state) !== null && this.value >= this.max(state)) return;
if (state.deductCost(this.cost)) { if (state.deductCost(this.cost)) {
@ -33,16 +39,8 @@ abstract class Purchasable implements IResource {
} }
} }
public inc (state: GameState): number | null { public addValue (amount: number, state: GameState): void {
return null; this.value += amount;
}
public max (state: GameState): number | null {
return this._baseMax;
}
public advanceAction (time: number, state: GameState): void {
return;
} }
public isUnlocked (state: GameState): boolean { public isUnlocked (state: GameState): boolean {
@ -52,6 +50,10 @@ abstract class Purchasable implements IResource {
return this._isUnlocked; return this._isUnlocked;
} }
public advanceAction (time: number, state: GameState): void {
return;
}
protected _purchaseAmount (state: GameState): number { protected _purchaseAmount (state: GameState): number {
return 1; return 1;
} }

View File

@ -2,14 +2,15 @@
class Religion implements IResource { class Religion implements IResource {
public readonly resourceType: ResourceType = ResourceType.Religion; public readonly resourceType: ResourceType = ResourceType.Religion;
public readonly valueInWholeNumbers: boolean = true;
public readonly clickText: null = null; public readonly clickText: null = null;
public readonly clickDescription: null = null; public readonly clickDescription: null = null;
public readonly clickAction: null = null;
public readonly advanceAction: null = null;
public readonly cost: null = null; public readonly cost: null = null;
public readonly max: null = null; public readonly max: null = null;
public readonly inc: null = null; public readonly inc: null = null;
public readonly valueInWholeNumbers: boolean = true; public readonly clickAction: null = null;
public readonly advanceAction: null = null;
constructor ( constructor (
public readonly name: string, public readonly name: string,
@ -17,6 +18,10 @@ class Religion implements IResource {
public value: number, public value: number,
) { } ) { }
public addValue (amount: number, state: GameState): void {
this.value += amount;
}
public isUnlocked (state: GameState): boolean { public isUnlocked (state: GameState): boolean {
return true; return true;
} }