From 9aa7a3b3a95b4292c7a659e39cf11354238fab7b Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Wed, 18 Aug 2021 15:36:02 -0500 Subject: [PATCH] main game loop and state --- src/main.ts | 13 +++++++---- src/model/GameConfig.ts | 40 +++++++++++++++++++++++++++++++++- src/model/GameState.ts | 13 +++++++++++ src/model/IResource.ts | 3 ++- src/model/resource/Religion.ts | 16 +++++--------- 5 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 src/model/GameState.ts diff --git a/src/main.ts b/src/main.ts index 5e6e61e..ddcc415 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,20 +1,25 @@ /// +/// let globalStartTime = 0; -function gameLoop (config: GameConfig): void { +function gameLoop (state: GameState): void { // figure out how much actual time has passed const elapsedTime = globalStartTime > 0 ? (new Date()).getTime() - globalStartTime : 0; + state.advance(elapsedTime); + // run again in 1sec globalStartTime = (new Date()).getTime(); - setTimeout(() => gameLoop(config), 1000); + setTimeout(() => gameLoop(state), 1000); } // run with default config at startup (() => { const config = new GameConfig(); - if (document.readyState !== 'loading') gameLoop(config); - else document.addEventListener('DOMContentLoaded', () => gameLoop(config)); + const state = config.generateState(); + + if (document.readyState !== 'loading') gameLoop(state); + else document.addEventListener('DOMContentLoaded', () => gameLoop(state)); })(); diff --git a/src/model/GameConfig.ts b/src/model/GameConfig.ts index e5d751f..d461872 100644 --- a/src/model/GameConfig.ts +++ b/src/model/GameConfig.ts @@ -1,3 +1,4 @@ +/// /// /// @@ -14,5 +15,42 @@ class GameConfig { public relOtherShare:number = 0.02; public relNoneShare: number = 0.16; - constructor () {} + public generateState(): GameState { + const state = new GameState(); + + // create world religions + state.addResource('xtian', new Religion( + 'Christianity', 'God, Jesus, Bible, churches.', + this.relChristianityShare * this.worldPopulation)); + + state.addResource('islam', new Religion( + 'Islam', 'God, Muhammad, Quran, mosques.', + this.relIslamShare * this.worldPopulation)); + + state.addResource('hindu', new Religion( + 'Hinduism', 'Dogma-free spiritualism.', + this.relHinduismShare * this.worldPopulation)); + + state.addResource('buddh', new Religion( + 'Buddhism', 'The minimization of suffering.', + this.relBuddhismShare * this.worldPopulation)); + + state.addResource('sikhi', new Religion( + 'Sikhism', 'Meditation and ten Gurus', + this.relSikhismShare * this.worldPopulation)); + + state.addResource('judah', new Religion( + 'Judaism', 'God, Abraham, Torah, synagogues.', + this.relJudaismShare * this.worldPopulation)); + + state.addResource('other', new Religion( + 'Other', 'A variety of belief systems.', + this.relOtherShare * this.worldPopulation)); + + state.addResource('agnos', new Religion( + 'Non-Religious', 'Atheists and agnostics.', + this.relNoneShare * this.worldPopulation)); + + return state; + } } diff --git a/src/model/GameState.ts b/src/model/GameState.ts new file mode 100644 index 0000000..cdc7f27 --- /dev/null +++ b/src/model/GameState.ts @@ -0,0 +1,13 @@ +/// + +class GameState { + private _resources: {[key: string]: IResource} = {}; + + public addResource (key: string, resource: IResource): void { + this._resources[key] = resource; + } + + public advance (time: number): void { + console.log(`Advancing state by ${time}ms...`); + } +} diff --git a/src/model/IResource.ts b/src/model/IResource.ts index fc3e6a4..8523fbe 100644 --- a/src/model/IResource.ts +++ b/src/model/IResource.ts @@ -7,5 +7,6 @@ interface IResource { description: string, resourceType: ResourceType, value: number, - max: number + max?: number, + unlocked: boolean } diff --git a/src/model/resource/Religion.ts b/src/model/resource/Religion.ts index 4f22198..6fb32b5 100644 --- a/src/model/resource/Religion.ts +++ b/src/model/resource/Religion.ts @@ -1,20 +1,14 @@ -/// /// class Religion implements IResource { - public readonly description: string; - public readonly resourceType: ResourceType = ResourceType.Religion; - - public value: number; - public max: number; + public readonly resourceType = ResourceType.Religion; + public readonly max?: number = null; + public readonly unlocked = true; constructor ( - config: GameConfig, public readonly name: string, - populationShare: number + public readonly description: string, + public readonly value: number, ) { - this.description = name; - this.max = config.worldPopulation; - this.value = config.worldPopulation * populationShare; } }