import GameMap from './map';
import Editor from './editor';
import MapView from './mapview';
+import TextView from './textview';
import { ID } from './id';
import { GameEvent } from './event';
objects: Map<ID, GameObject>;
agents: Map<ID, GameAgent>;
- text: HTMLDivElement; //GameText;
+ textView: TextView;
editor: Editor;
mapView: MapView;
editMode: boolean = true;
if (localStorage['map']) {
this.map.load(JSON.parse(localStorage.map));
}
- this.text = document.getElementById('text') as HTMLDivElement;
+ this.textView = new TextView();
this.editor = new Editor();
this.mapView = new MapView();
this.mapView.updateAll();
+
+ this.showCellText();
}
edit(initialText: string, callback: (elem: HTMLTextAreaElement) => void) {
}
print(...args: string[]) {
- const line = args.join(' ');
- const div = document.createElement('div');
- div.appendChild(document.createTextNode(line));
- this.text.appendChild(div);
+ this.textView.print(...args);
+ }
+
+ movePlayerRel(dx: number, dy: number) {
+ const [ x, y ] = this.map.getPlayerPosition();
+ this.movePlayer(x + dx, y + dy);
+ }
+
+ movePlayer(x: number, y: number) {
+ this.map.movePlayer(x, y);
+ this.showCellText();
+ }
+
+ showCellText() {
+ const cell = this.map.getCurrentCell();
+ gs.textView.clear();
+ gs.textView.print("You have entered the", cell.title);
+ gs.textView.print(cell.description);
}
}
function keydown(e: KeyboardEvent) {
switch(e.code) {
case 'ArrowRight':
- gs.map.movePlayerRel(1, 0);
+ gs.movePlayerRel(1, 0);
break;
case 'ArrowLeft':
- gs.map.movePlayerRel(-1, 0);
+ gs.movePlayerRel(-1, 0);
break;
case 'ArrowUp':
- gs.map.movePlayerRel(0, -1);
+ gs.movePlayerRel(0, -1);
break;
case 'ArrowDown':
- gs.map.movePlayerRel(0, 1);
+ gs.movePlayerRel(0, 1);
break;
case 'KeyN':
gs.map.toggleNavigable();
this.cells[oldY][oldX].removeObject(this.player);
this.player.moveTo(x, y);
this.cells[y][x].addObject(this.player);
- gs.print('moved:', this.cells[y][x].description);
gs.mapView.updateAll();
gs.mapView.updateCoords(x, y);
this.movePlayer(x + dx, y + dy);
}
+ getPlayerPosition(): [number, number] {
+ const { x, y } = this.player;
+ return [x, y];
+ }
+
+ getCurrentCell(): Cell {
+ const { x, y } = this.player;
+ return this.cells[y][x];
+ }
+
toggleNavigable() {
const { x, y } = this.player;
this.cells[y][x].navigable = !this.cells[y][x].navigable;
+export default class TextView {
+ elem: HTMLDivElement;
+
+ constructor() {
+ this.elem = <HTMLDivElement>document.getElementById('text');
+ }
+
+ clear() {
+ while (this.elem.firstChild)
+ this.elem.removeChild(this.elem.firstChild);
+ }
+
+ print(...args: string[]) {
+ const line = args.join(' ');
+ const div = document.createElement('div');
+ div.appendChild(document.createTextNode(line));
+ this.elem.appendChild(div);
+ }
+}