main game loop and state

This commit is contained in:
Rudis Muiznieks 2021-08-18 15:36:02 -05:00
parent b92661e934
commit 9aa7a3b3a9
5 changed files with 68 additions and 17 deletions

View File

@ -1,20 +1,25 @@
/// <reference path="./model/GameConfig.ts" />
/// <reference path="./model/GameState.ts" />
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));
})();

View File

@ -1,3 +1,4 @@
/// <reference path="./GameState.ts" />
/// <reference path="./IResource.ts" />
/// <reference path="./resource/Religion.ts" />
@ -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;
}
}

13
src/model/GameState.ts Normal file
View File

@ -0,0 +1,13 @@
/// <reference path="./IResource.ts" />
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...`);
}
}

View File

@ -7,5 +7,6 @@ interface IResource {
description: string,
resourceType: ResourceType,
value: number,
max: number
max?: number,
unlocked: boolean
}

View File

@ -1,20 +1,14 @@
/// <reference path="../GameConfig.ts" />
/// <reference path="../IResource.ts" />
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;
}
}