controller: clean up GameContext a bit
This commit is contained in:
parent
5372d1b417
commit
1ae0884a73
2 changed files with 16 additions and 16 deletions
|
@ -10,14 +10,6 @@ import { getJSON } from '../util';
|
||||||
|
|
||||||
import { vec2 } from 'gl-matrix';
|
import { vec2 } from 'gl-matrix';
|
||||||
|
|
||||||
function snapToGrid(out: vec2, a: vec2): void {
|
|
||||||
const res = 64;
|
|
||||||
|
|
||||||
vec2.scale(out, a, res);
|
|
||||||
vec2.round(out, out);
|
|
||||||
vec2.scale(out, out, 1 / res);
|
|
||||||
}
|
|
||||||
|
|
||||||
export class GameContext {
|
export class GameContext {
|
||||||
public static async load(renderer: Renderer): Promise<GameContext> {
|
public static async load(renderer: Renderer): Promise<GameContext> {
|
||||||
const [
|
const [
|
||||||
|
@ -66,10 +58,7 @@ export class GameContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateTime(time: number): number {
|
private updateTime(time: number): number {
|
||||||
let diff = 0;
|
const diff = this.time !== null ? time - this.time : 0;
|
||||||
if (this.time !== null)
|
|
||||||
diff = time - this.time;
|
|
||||||
|
|
||||||
this.time = time;
|
this.time = time;
|
||||||
|
|
||||||
return diff;
|
return diff;
|
||||||
|
@ -78,8 +67,10 @@ export class GameContext {
|
||||||
private update(time: number): void {
|
private update(time: number): void {
|
||||||
const diff = this.updateTime(time);
|
const diff = this.updateTime(time);
|
||||||
|
|
||||||
vec2.scaleAndAdd(this.entityPos, this.entityPos, this.entityMovement, diff * this.speed);
|
if (vec2.sqrLen(this.entityMovement) > 0)
|
||||||
snapToGrid(this.entityPos, this.entityPos);
|
vec2.scaleAndAdd(this.entityPos, this.entityPos, this.entityMovement, diff * this.speed);
|
||||||
|
else
|
||||||
|
this.renderer.snapToGrid(this.entityPos, this.entityPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
private render = (time: number) => {
|
private render = (time: number) => {
|
||||||
|
|
|
@ -3,6 +3,8 @@ import { mat4, vec2 } from 'gl-matrix';
|
||||||
import Shaders from './Shaders';
|
import Shaders from './Shaders';
|
||||||
|
|
||||||
export default class Renderer {
|
export default class Renderer {
|
||||||
|
private readonly viewScale = 64;
|
||||||
|
|
||||||
private readonly gl: WebGLRenderingContext;
|
private readonly gl: WebGLRenderingContext;
|
||||||
private readonly shaders: Shaders;
|
private readonly shaders: Shaders;
|
||||||
|
|
||||||
|
@ -49,11 +51,12 @@ export default class Renderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public setCenter(v: vec2|number[]) {
|
public setCenter(v: vec2|number[]) {
|
||||||
vec2.copy(this.center, v);
|
this.snapToGrid(this.center, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
public setTranslation(v: vec2|number[]) {
|
public setTranslation(v: vec2|number[]) {
|
||||||
vec2.sub(this.translation, v, this.center);
|
vec2.sub(this.translation, v, this.center);
|
||||||
|
this.snapToGrid(this.translation, this.translation);
|
||||||
this.gl.uniform2fv(this.shaders.translateLoc, this.translation);
|
this.gl.uniform2fv(this.shaders.translateLoc, this.translation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,6 +66,12 @@ export default class Renderer {
|
||||||
this.setTranslation([0, 0]);
|
this.setTranslation([0, 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public snapToGrid(out: vec2, a: vec2|number[]): void {
|
||||||
|
vec2.scale(out, a, this.viewScale);
|
||||||
|
vec2.round(out, out);
|
||||||
|
vec2.scale(out, out, 1 / this.viewScale);
|
||||||
|
}
|
||||||
|
|
||||||
private mkContext(): WebGLRenderingContext {
|
private mkContext(): WebGLRenderingContext {
|
||||||
const gl = (
|
const gl = (
|
||||||
this.canvas.getContext('webgl') || this.canvas.getContext('experimental-webgl')
|
this.canvas.getContext('webgl') || this.canvas.getContext('experimental-webgl')
|
||||||
|
@ -81,7 +90,7 @@ export default class Renderer {
|
||||||
this.clear();
|
this.clear();
|
||||||
|
|
||||||
mat4.identity(this.viewport);
|
mat4.identity(this.viewport);
|
||||||
mat4.scale(this.viewport, this.viewport, [2 * 64 / w, -2 * 64 / h, 1.0]);
|
mat4.scale(this.viewport, this.viewport, [2 * this.viewScale / w, -2 * this.viewScale / h, 1.0]);
|
||||||
this.gl.uniformMatrix4fv(this.shaders.viewportLoc, false, this.viewport);
|
this.gl.uniformMatrix4fv(this.shaders.viewportLoc, false, this.viewport);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue