hadean-old/src/Game.ts

86 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-06-14 22:03:55 -04:00
import { Frigid, Serializable } from 'frigid';
import { DEBUG } from 'frigid/out/Serializable.js';
import { Pawn } from './Pawn.js';
import { TaskList } from './TaskList.js';
import { Inventory } from './Inventory.js';
2021-06-16 15:26:42 -04:00
import { Menu } from './ui/Menu.js';
2021-06-14 22:03:55 -04:00
import Time, { Tickable } from './Time.js';
2021-07-20 16:37:38 -04:00
import { render, Renderable, setTitle, start } from '@ui';
2021-06-16 15:26:42 -04:00
import { ready } from './multiplayer/mDNS.js';
2021-06-15 21:53:00 -04:00
import faker from 'faker';
2021-07-16 20:49:29 -04:00
import { World } from '@world';
2021-06-14 22:03:55 -04:00
2021-06-26 13:07:11 -04:00
let game: Game = null;
2021-06-14 22:03:55 -04:00
export class Game extends Frigid implements Tickable, Renderable {
2021-06-22 19:25:41 -04:00
pawns: Pawn[] = [];
selected: Pawn;
inventory: Inventory;
board: TaskList;
menu: Menu;
clock: Time;
name: string;
2021-07-16 20:49:29 -04:00
world: World;
2021-06-14 22:03:55 -04:00
2021-06-22 19:25:41 -04:00
[DEBUG] = true;
2021-06-14 22:03:55 -04:00
2021-06-22 19:25:41 -04:00
static get current(): Game {
if (!game) throw new Error('Somehow called a game before it existed?');
return game;
}
2021-06-14 22:03:55 -04:00
2021-06-22 19:25:41 -04:00
async tick() {
for(const pawn of this.pawns) {
pawn.tick();
}
render();
}
2021-06-14 22:03:55 -04:00
2021-06-22 19:25:41 -04:00
get inv() { return this.inventory; }
2021-06-14 22:03:55 -04:00
2021-06-22 19:25:41 -04:00
removePawn(pawn: Pawn) {
if(pawn === this.selected) {
if(this.pawns.indexOf(this.selected) === this.pawns.length - 1) this.advanceSelection(-1);
else this.advanceSelection(1);
}
2021-06-14 22:03:55 -04:00
2021-06-22 19:25:41 -04:00
this.pawns = this.pawns.filter(testPawn => {
return pawn !== testPawn;
});
}
2021-06-14 22:03:55 -04:00
2021-06-26 13:07:11 -04:00
advanceSelection(number: number) {
2021-06-22 19:25:41 -04:00
let index = this.pawns.indexOf(this.selected);
this.selected = this.pawns[Math.min(Math.max(index + number, 0), this.pawns.length - 1)];
}
ctor () {
game = this;
start();
this.name ??= faker.address.city();
setTitle(this.name);
2021-07-16 20:49:29 -04:00
this.world ??= new World();
2021-06-22 19:25:41 -04:00
this.pawns ??= [];
this.selected ??= this.pawns[0] || null;
this.menu = new Menu();
this.board ??= new TaskList();
this.inventory ??= new Inventory();
this.inventory.validate();
this.clock ??= new Time();
this.clock.thing = this;
this.clock.start();
ready(this.name);
render(this);
}
2021-06-14 22:03:55 -04:00
2021-06-22 19:25:41 -04:00
static serializationDependencies() {
2021-07-16 20:49:29 -04:00
return [ Pawn, Inventory, TaskList, Time, World ];
2021-06-22 19:25:41 -04:00
}
2021-06-14 22:03:55 -04:00
2021-06-22 19:25:41 -04:00
render() {
this.menu.render();
this.board.render();
2021-07-19 01:53:30 -04:00
// TODO this logic dont make sense
return '';
2021-06-22 19:25:41 -04:00
}
2021-06-14 22:03:55 -04:00
}