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";
|
|
|
|
|
2020-05-10 22:47:03 +00:00
|
|
|
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 } from "../net/LockstepClient";
|
|
|
|
import { LoopbackServer } from "../net/LoopbackServer";
|
2020-05-02 01:45:31 +00:00
|
|
|
import { Data, Engine, PlayerControl } from "./GameComponents";
|
2020-02-16 05:01:30 +00:00
|
|
|
import { Buttons } from "./Input";
|
|
|
|
|
2020-05-02 02:51:21 +00:00
|
|
|
export class Main extends LockstepClient<KeyName[], KeyName[][], Data> {
|
2020-02-16 05:01:30 +00:00
|
|
|
|
|
|
|
buttons = new Buttons();
|
|
|
|
|
2020-05-03 01:20:54 +00:00
|
|
|
constructor(canvas: HTMLCanvasElement, cx: CanvasRenderingContext2D, keys: KeyControl, server: LoopbackServer<KeyName[], Data>) {
|
2020-02-16 05:01:30 +00:00
|
|
|
super(new Engine());
|
|
|
|
keys.setHandler(this.buttons);
|
|
|
|
|
2020-05-03 01:14:15 +00:00
|
|
|
this.connect(server.socket);
|
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>) {
|
2020-04-04 22:52:25 +00:00
|
|
|
const newState = new Data(patch);
|
2020-02-16 05:01:30 +00:00
|
|
|
|
|
|
|
Create(newState, {
|
2020-05-02 01:45:31 +00:00
|
|
|
playerControl: new PlayerControl({
|
|
|
|
playerNumber: 0
|
|
|
|
}),
|
2020-02-16 05:01:30 +00:00
|
|
|
location: new Location({
|
2020-05-02 01:45:31 +00:00
|
|
|
X: 100,
|
2020-02-16 05:01:30 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-05-02 01:45:31 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|