This repository has been archived on 2022-01-16. You can view files and clone it, but cannot push or open issues or pull requests.
irreligious/src/main.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-08-18 10:09:19 -05:00
/// <reference path="./model/GameConfig.ts" />
/// <reference path="./render/DebugRenderer.ts" />
2021-08-18 10:09:19 -05:00
2021-09-04 22:21:14 -05:00
let globalStartTime = 0;
2021-09-05 14:45:37 -05:00
let globalTimeout: number | null = null;
2021-09-04 22:21:14 -05:00
const cycleLength = 250;
2021-08-18 10:09:19 -05:00
function gameLoop (state: GameState, renderer: IRenderer): void {
2021-08-18 10:09:19 -05:00
// figure out how much actual time has passed
const elapsedTime: number = globalStartTime > 0
2021-09-04 23:11:13 -05:00
? new Date().getTime() - globalStartTime : 0;
2021-08-18 10:09:19 -05:00
2021-08-18 15:36:02 -05:00
state.advance(elapsedTime);
2021-08-21 19:02:57 -05:00
renderer.render(state);
2021-08-18 15:36:02 -05:00
2021-08-18 10:09:19 -05:00
// run again in 1sec
2021-09-04 23:11:13 -05:00
globalStartTime = new Date().getTime();
2021-09-05 14:45:37 -05:00
globalTimeout = setTimeout((): void => {
gameLoop(state, renderer);
}, cycleLength);
2021-08-18 10:09:19 -05:00
}
2021-08-22 09:02:12 -05:00
function startGame (state: GameState, renderer: IRenderer): void {
state.load(); // load saved game if one exists
gameLoop(state, renderer); // start the main loop
}
2021-08-18 10:09:19 -05:00
// run with default config at startup
((): void => {
const config: GameConfig = new GameConfig();
// debug values to make the game play faster while testing
2021-08-22 11:07:49 -05:00
config.cfgTitheAmount = 1000;
config.cfgTimeBetweenTithes = 5000;
config.cfgCryptoReturnAmount = 100;
config.cfgCredibilityRestoreRate = 5;
config.cfgPastorRecruitRate = 0.5;
const renderer: IRenderer = new DebugRenderer();
const state: GameState = config.generateState();
2021-08-18 15:36:02 -05:00
// re-run main loop immediately on user clicks
state.onResourceClick.push((): void => {
if (globalTimeout !== null) {
clearTimeout(globalTimeout);
gameLoop(state, renderer);
}
});
2021-08-22 09:02:12 -05:00
if (document.readyState !== 'loading') startGame(state, renderer);
2021-09-05 14:45:37 -05:00
else document.addEventListener('DOMContentLoaded', (): void => {
startGame(state, renderer);
});
2021-08-18 10:09:19 -05:00
})();