hadean-old/src/index.ts

63 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-06-16 15:26:42 -04:00
import { render } from './ui/UI.js';
2021-06-18 02:02:50 -04:00
import { ensureDirSync } from 'fs-extra';
import { lstatSync } from 'fs';
import { parse, resolve } from 'path';
import walkSync from 'walk-sync';
import { fileURLToPath } from 'url';
2021-06-22 19:25:41 -04:00
import { Game } from '@game';
2021-06-26 03:11:18 -04:00
import { isStarted, stop } from './ui/UI.js';
import { writeFileSync } from 'fs';
2021-07-16 20:49:29 -04:00
console.clear();
2021-06-26 03:11:18 -04:00
function gracefulShutdown() {
if(isStarted()) {
stop();
}
console.log('shutting down gracefully...');
if(Game.current) {
console.log('saving world...');
Game.current.sync();
}
console.log('exitting');
process.exit(0);
}
process.on('exit', gracefulShutdown);
2021-06-14 22:03:55 -04:00
2021-06-18 02:02:50 -04:00
const saveFile = process.argv[2] || 'data/world01.json';
2021-06-15 20:05:07 -04:00
2021-06-18 02:02:50 -04:00
ensureDirSync(parse(saveFile).dir);
2021-06-22 19:25:41 -04:00
// TODO extract extension loading into separate file
console.log('df-idle: Loading extensions');
const extensionsPath = resolve(parse(fileURLToPath(import.meta.url)).dir, '../content');
const extensions = walkSync(extensionsPath)
2021-07-16 20:49:29 -04:00
.map(path => [path, resolve(extensionsPath, path)])
.filter(path => lstatSync(path[1]).isFile())
.filter(path => parse(path[1]).ext === '.js');
2021-06-22 19:25:41 -04:00
console.log('found', extensions.length, 'extensions');
for(const path of extensions) {
2021-07-16 20:49:29 -04:00
console.log('=== [', path[0], '] ===');
await import(path[1]);
2021-06-22 19:25:41 -04:00
console.log();
}
2021-07-16 20:49:29 -04:00
console.log('Setup Complete.');
for(let seconds = 2; seconds > 0; seconds --) {
process.stdout.write('Starting DF-Idle in ' + seconds + '\r');
await new Promise(res => setTimeout(res, 1000));
}
console.log();
2021-06-22 19:25:41 -04:00
// TODO move render logic into game, so that the ui doesnt exist until the game does...
// maybe, i mean, an argument could be made for not that, because the game
// isnt necessarily the entire thing, its just one instance of a save file.
// But probably the initial menu screens will be their own thing entirely.
const game = Game.create(saveFile);