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";
|
2020-04-04 22:52:25 +00:00
|
|
|
import { Location, Polygon, PolygonComponent, RenderBounds } from "../Ecs/Components";
|
2020-02-16 05:01:30 +00:00
|
|
|
import { Create } from "../Ecs/Data";
|
|
|
|
import { RunRenderBounds } from "../Ecs/Renderers";
|
|
|
|
import { LockstepClient } from "../Net/LockstepClient";
|
2020-02-16 05:42:58 +00:00
|
|
|
import { Loopback } from "../Net/LoopbackServer";
|
2020-02-16 05:01:30 +00:00
|
|
|
import { Data, Engine } from "./GameComponents";
|
|
|
|
import { Buttons } from "./Input";
|
|
|
|
|
|
|
|
export class Main extends LockstepClient<KeyName[], Data> {
|
|
|
|
|
|
|
|
buttons = new Buttons();
|
|
|
|
|
|
|
|
constructor(canvas: HTMLCanvasElement, cx: CanvasRenderingContext2D, keys: KeyControl) {
|
|
|
|
super(new Engine());
|
|
|
|
keys.setHandler(this.buttons);
|
|
|
|
|
2020-02-16 05:42:58 +00:00
|
|
|
this.connect(Loopback);
|
|
|
|
|
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>) {
|
2020-04-04 22:52:25 +00:00
|
|
|
const newState = new Data(patch);
|
2020-02-16 05:01:30 +00:00
|
|
|
|
|
|
|
Create(newState, {
|
|
|
|
location: new Location({
|
|
|
|
X: 200,
|
|
|
|
Y: 200,
|
|
|
|
}),
|
2020-04-04 22:52:25 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|