hadean-old/src/ui/Menu.ts

156 lines
4.8 KiB
TypeScript
Raw Normal View History

2021-06-16 15:26:42 -04:00
import { Pawn } from '../Pawn.js';
import log from '../log.js';
2021-06-18 02:02:50 -04:00
import { menuPanel, Renderable } from './UI.js';
2021-06-16 15:26:42 -04:00
import { Game } from '../Game.js';
import { ChopTreeTask } from '../tasks/ChopTreeTask.js';
2021-06-18 02:02:50 -04:00
import { progressbar, stats, barCache } from '../Progressbar.js';
2021-06-15 19:15:49 -04:00
import { Popup } from './Popup.js';
2021-06-16 15:26:42 -04:00
import mdns from '../multiplayer/mDNS.js';
2021-06-16 02:20:56 -04:00
import { GiftPopup } from './GiftPopup.js';
2021-06-16 15:26:42 -04:00
import { PawnDetails } from './PawnDetails.js';
2021-06-18 02:02:50 -04:00
import { defaultTheme, getTheme } from './Theme.js';
import { inspect } from 'util';
2021-06-18 21:29:45 -04:00
import PawnsView from './view/PawnsView.js';
import InventoryView from './view/InventoryView.js';
import MultiplayerView from './view/MultiplayerView.js';
2021-06-15 15:02:47 -04:00
2021-06-18 21:29:45 -04:00
// TODO extract View
export abstract class View implements Renderable, KeypressAcceptor {
abstract render(): void;
abstract keypress(key: {full: string}): void;
2021-06-15 15:02:47 -04:00
2021-06-18 21:29:45 -04:00
static PAWNS: View = new PawnsView();
static INVENTORY: View = new InventoryView();
static MULTIPLAYER: View = new MultiplayerView();
name: string
}
// TODO move KeypressAcceptor to ui something idk
export interface KeypressAcceptor {
keypress(key: {full: string}): void
}
2021-06-14 22:03:55 -04:00
export class Menu implements Renderable {
2021-06-15 15:02:47 -04:00
trees: number = 10;
view: View = View.PAWNS;
2021-06-14 22:03:55 -04:00
constructor() {
2021-06-15 19:15:49 -04:00
menuPanel.on('keypress', (evt, key) => {
2021-06-14 22:03:55 -04:00
log.info('keypress', key);
2021-06-15 23:53:51 -04:00
if (key.full === 'left') {
2021-06-15 15:02:47 -04:00
this.view = View[Object.keys(View)[Math.min(Math.max(Object.values(View).indexOf(this.view) - 1, 0), Object.keys(View).length - 1)]]
} else if (key.full === 'right') {
this.view = View[Object.keys(View)[Math.min(Math.max(Object.values(View).indexOf(this.view) + 1, 0), Object.keys(View).length - 1)]]
2021-06-18 21:29:45 -04:00
// debugging hotkeys
2021-06-15 19:15:49 -04:00
} else if (key.full === '1') {
2021-06-18 02:02:50 -04:00
Popup.show(inspect(stats));
2021-06-18 21:29:45 -04:00
} else if (key.full === 'z') {
Game.current.pawns.push(new Pawn());
} else if (key.full === 'x') {
Game.current.board.clear();
2021-06-15 23:53:51 -04:00
}
2021-06-18 21:29:45 -04:00
// if(this.view === View.PAWNS) {
// if (key.full === 'delete') {
// Game.current.removePawn(Game.current.selected);
// } else if (key.full === 'up') {
// Game.current.advanceSelection(-1);
// } else if (key.full === 'down') {
// Game.current.advanceSelection(1);
// } else if (key.full === 'enter') {
// new PawnDetails(Game.current.selected);
// }
// }
// if(this.view === View.MULTIPLAYER) {
// if (key.full === 'enter') {
// new GiftPopup(mdns.players[this.multiplayerSelected]);
// // mdns.players[this.multiplayerSelected].sendItem(null);
// } else if (key.full === 'up') {
// this.multiplayerSelected --;
// } else if (key.full === 'down') {
// this.multiplayerSelected ++;
// }
// }
2021-06-15 15:02:47 -04:00
2021-06-14 22:03:55 -04:00
Game.current.sync();
});
}
2021-06-15 15:02:47 -04:00
renderTopBar() {
2021-06-14 22:03:55 -04:00
const idlers = Game.current.pawns.filter(pawn => pawn.idle);
2021-06-18 02:02:50 -04:00
return ` ${Game.current.clock.render()}{|}${getTheme().normal(`Idle: ${idlers.length}`)} `;
2021-06-14 22:03:55 -04:00
}
renderPawns() {
return `${
Game.current.pawns.map(pawn => `${(function() {
const selected = pawn === Game.current.selected;
let str = '';
if(selected) {
2021-06-18 02:02:50 -04:00
str += ` ${getTheme().selected(` ${pawn.toString()}`)}{|}${pawn.status} \n`;
str += ` ${getTheme().normal('Energy')}{|}${progressbar(pawn.energy / 100, (menuPanel.width - 4) / 2)} \n`;
2021-06-14 22:03:55 -04:00
} else {
2021-06-18 02:02:50 -04:00
str += ` ${getTheme().normal(pawn.toString())}{|}${pawn.status} `;
2021-06-14 22:03:55 -04:00
}
return str;
})()}`).join('\n')
}`;
}
2021-06-15 15:02:47 -04:00
renderView() {
const colSpace = ((menuPanel.width - 2) / 2);
return `${
// ' '.repeat(colSpace - 20)
'{center}'
}${(() => {
return Object.values(View).map(view => {
if(view === this.view) {
2021-06-18 02:02:50 -04:00
return getTheme().tab.selected(` ${view} `);
2021-06-15 15:02:47 -04:00
} else {
2021-06-18 02:02:50 -04:00
return getTheme().tab.normal(` ${view} `);
2021-06-15 15:02:47 -04:00
}
}).join('');
})()}{/center}\n\n${(() => {
2021-06-18 21:29:45 -04:00
this.view.view.render();
2021-06-15 15:02:47 -04:00
})()}`
}
2021-06-15 23:53:51 -04:00
multiplayerSelected = 0;
2021-06-15 20:05:07 -04:00
renderMultiplayer() {
2021-06-18 02:02:50 -04:00
if(mdns.players.length === 0) return `{center}${getTheme().normal('No friends online')}{/center}`;
2021-06-15 23:53:51 -04:00
return mdns.players.map((player, i) => {
2021-06-18 02:02:50 -04:00
if(i === this.multiplayerSelected) return ' ' + getTheme().selected(' ' + player.toString());
else return ' ' + getTheme().normal(player.toString());
2021-06-15 23:53:51 -04:00
}).join('\n');
2021-06-15 20:05:07 -04:00
}
2021-06-15 15:02:47 -04:00
renderInv() {
return Game.current.inv.render();
}
renderTreesSubMenu() {
return [
`{center}Chop Trees`,
2021-06-18 02:02:50 -04:00
`{left} ${getTheme().hotkey('-=_+')}: ${this.trees}`,
`{left} ${getTheme().hotkey('enter')}: Create Task`,
`{left} ${getTheme().hotkey('escape')}: Cancel`
2021-06-15 15:02:47 -04:00
].join('\n');
}
2021-06-14 22:03:55 -04:00
render() {
const width = menuPanel.width - 2;
2021-06-18 02:02:50 -04:00
const hr = getTheme().normal('━'.repeat(width));
2021-06-15 15:02:47 -04:00
const content = [
this.renderTopBar(),
hr,
this.renderView(),
].join('\n');
2021-06-14 22:03:55 -04:00
menuPanel.setContent(content);
}
2021-06-15 15:02:47 -04:00
}