Improve multiplayer

This commit is contained in:
schmelczerandras 2020-10-08 13:16:05 +02:00 • committed by Schmelczer András
commit 37954e2ef1

View file

@ -1,36 +1,29 @@
import { vec2 } from 'gl-matrix';
import { CommandExecutors, GameObject, Rectangle, settings } from 'shared';
import { calculateViewArea, CommandExecutors, GameObject } from 'shared';
import { RenderCommand } from '../commands/types/render';
import { Game } from '../game';
export class Camera extends GameObject {
private static readonly inViewAreaSize = settings.inViewAreaSize;
private _viewArea = new Rectangle();
public center: vec2 = vec2.create();
private aspectRatio?: number;
protected commandExecutors: CommandExecutors = {
[RenderCommand.type]: this.draw.bind(this),
};
constructor(public center: vec2 = vec2.create()) {
constructor(private game: Game) {
super(null);
}
public get viewArea(): Rectangle {
return this._viewArea;
}
private draw(c: RenderCommand) {
const canvasAspectRatio = c.renderer.canvasSize.x / c.renderer.canvasSize.y;
if (canvasAspectRatio !== this.aspectRatio) {
this.aspectRatio = canvasAspectRatio;
this.game.aspectRatioChanged(canvasAspectRatio);
}
this._viewArea.topLeft = vec2.fromValues(
this.center.x - this._viewArea.size.x / 2,
this.center.y + this._viewArea.size.y / 2,
);
this._viewArea.size = vec2.fromValues(
Math.sqrt(Camera.inViewAreaSize * canvasAspectRatio),
Math.sqrt(Camera.inViewAreaSize / canvasAspectRatio),
);
c.renderer.setViewArea(this._viewArea.topLeft, this._viewArea.size);
const viewArea = calculateViewArea(this.center, canvasAspectRatio);
c.renderer.setViewArea(viewArea.topLeft, viewArea.size);
}
}