base2020/src/game/Main.ts

78 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-04-03 22:35:23 +00:00
import pipe from "callbag-pipe";
2020-02-16 05:01:30 +00:00
import subscribe from "callbag-subscribe";
import { KeyControl, KeyName } from "../applet/Keyboard";
import { DrawSet } from "../applet/Render";
import { Location, PolygonComponent, RenderBounds } from "../ecs/Components";
import { Create } from "../ecs/Data";
import { RunRenderBounds } from "../ecs/Renderers";
import { LockstepClient, Server } from "../net/LockstepClient";
import { Data, Engine, PlayerControl } from "./GameComponents";
2020-02-16 05:01:30 +00:00
import { Buttons } from "./Input";
export class Main extends LockstepClient<KeyName[], KeyName[][], Data> {
2020-02-16 05:01:30 +00:00
buttons = new Buttons();
constructor(canvas: HTMLCanvasElement, cx: CanvasRenderingContext2D, keys: KeyControl, server: Server<KeyName[], KeyName[][], Data>) {
2020-02-16 05:01:30 +00:00
super(new Engine());
keys.setHandler(this.buttons);
this.connect(server);
2020-02-16 05:42:58 +00:00
2020-04-03 22:35:23 +00:00
pipe(
this.renderFrames,
subscribe((frame: Data) => {
const drawSet = new DrawSet();
cx.fillStyle = "#000";
cx.fillRect(0, 0, canvas.width, canvas.height);
2020-02-16 05:01:30 +00:00
2020-04-03 22:35:23 +00:00
RunRenderBounds(frame, drawSet);
2020-02-16 05:01:30 +00:00
2020-04-03 22:35:23 +00:00
drawSet.draw(cx, 0);
})
);
2020-02-16 05:01:30 +00:00
}
gatherInput() {
return this.buttons.getPressed();
}
initState(patch: Partial<Data>) {
const newState = new Data(patch);
2020-02-16 05:01:30 +00:00
Create(newState, {
playerControl: new PlayerControl({
playerNumber: 0
}),
2020-02-16 05:01:30 +00:00
location: new Location({
X: 100,
2020-02-16 05:01:30 +00:00
Y: 200,
}),
bounds: new PolygonComponent({points: [-30, 0, 30, 0, 0, 40]}),
renderBounds: new RenderBounds({
color: "#a0f",
layer: 0
}),
2020-02-16 05:01:30 +00:00
});
Create(newState, {
playerControl: new PlayerControl({
playerNumber: 1
}),
location: new Location({
X: 400,
Y: 200,
}),
bounds: new PolygonComponent({points: [-30, 0, 30, 0, 0, 40]}),
renderBounds: new RenderBounds({
color: "#f0a",
layer: 0
}),
});
2020-02-16 05:01:30 +00:00
return newState;
}
}