formatting, minor refactor, ui engine start

materials
Valerie 2021-07-25 18:52:19 -04:00
parent a75c18d197
commit c062ce445f
6 changed files with 177 additions and 44 deletions

17
src/Memories.ts 100644
View File

@ -0,0 +1,17 @@
import { Game } from "@game";
import { Pawn } from "./Pawn.js";
export function injectTravelMemory(target: Pawn) {
return {
type: "travel",
time: {
age: target.age,
locale: Game.current.clock.toString()
},
location: Game.current.name
}
}
function injectMemory(pawn: Pawn, memory: any) {
pawn.memories.push(memory);
}

View File

@ -20,6 +20,10 @@ export type BirthMemory = ProtoMemory & {
location: string,
}
export type AnyMemory =
TravelMemory
| BirthMemory;
export function stringify(memory: Memory): string {
switch(memory.type) {
case "birth": {

View File

@ -11,6 +11,7 @@ import { inspect } from 'util'
import { Pawn } from '../Pawn.js';
import { Game } from '../Game.js';
import { Player } from './Player.js';
import { injectTravelMemory } from '../Memories.js';
const mdns = bonjour();
const ID = uuid.v4();
@ -43,15 +44,8 @@ export async function ready(name: string) {
const pawns = [];
for(const pawnJson of pawnJsons) {
const pawn: Pawn = Pawn.fromJson(pawnJson);
pawn.memories.push({
type: "travel",
time: {
age: pawn.age,
locale: Game.current.clock.toString()
},
location: Game.current.name
})
pawns.push(pawn);
injectTravelMemory(pawn);
}
Popup.show(`${(() => {
if(pawns.length === 0) return `A care package has arrived from ${from}.`;

View File

@ -20,6 +20,7 @@
},
"include": [
"src/**/*.ts",
"content/**/*.ts"
"content/**/*.ts",
"ui-engine/**/*.ts"
]
}

55
ui-engine/Node.ts 100644
View File

@ -0,0 +1,55 @@
export abstract class Node {
x: number;
y: number;
w: number;
h: number;
children: Node[]
constructor() {
this.children = [];
}
layout(w: number, h: number) {
this.w = w;
this.h = h;
this.onResize(w, h);
this.children.forEach(node => node.layout(w, h));
}
append(child: Node) {
this.children.push(child);
this.layout(this.w, this.h);
}
abstract render(x: number, y: number): [number, string];
abstract onResize(w: number, h: number): void
}
export class TextNode extends Node {
_content: string;
constructor(content: string) {
super();
this.content = content;
}
set content(val: string) {
this._content = val
this.layout(this.w, this.h);
}
onResize(w: number, h: number): void {
return;
}
render(x: number, y: number): [number, string] {
if(y !== this.y) return null;
if(x < this.x) return null;
if(x > this.x + this.content.length) return null;
return [0, this.content[x - this.x]];
}
}

62
ui-engine/index.ts 100644
View File

@ -0,0 +1,62 @@
import ansi from 'sisteransi';
import { Node, TextNode } from './Node.js';
const TERMINAL_HIGH_BUFFER = '\x1b[?1049h';
const TERMINAL_LOW_BUFFER = '\x1b[?1049l'
class Screen extends Node {
buffer: ArrayBuffer;
bufferView: Uint8Array;
paletteBuffer: ArrayBuffer;
paletteBufferView: Uint8Array;
constructor() {
super();
process.stdout.write(TERMINAL_HIGH_BUFFER);
process.stdout.write(ansi.cursor.hide);
this.layout(process.stdout.columns, process.stdout.rows);
}
onResize(w: number, h: number) {
this.buffer = new ArrayBuffer(w * h);
this.bufferView = new Uint8Array(this.buffer);
this.paletteBuffer = new ArrayBuffer(w * h);
this.paletteBufferView = new Uint8Array(this.paletteBuffer);
}
// paletteAt(x: number, y: number) {
// }
offset(x: number, y: number) {
return y * this.h + x
}
updateTerminal() {
process.stdout.write(ansi.cursor.to(0, 0))
for(let y = 0; y < this.h; y ++) {
for(let x = 0; x < this.w; x ++) {
}
process.stdout.write('\r\n')
}
}
render(x: number, y: number): [number, string] {
throw new Error('Method not implemented.');
}
destroy() {
process.stdout.write(TERMINAL_LOW_BUFFER);
process.stdout.write(ansi.cursor.show);
}
}
// class Palette {
// }
const screen = new Screen();
screen.append(new TextNode('Test'))