summaryrefslogtreecommitdiffstats
path: root/src/view/MapView.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/view/MapView.ts')
-rw-r--r--src/view/MapView.ts82
1 files changed, 39 insertions, 43 deletions
diff --git a/src/view/MapView.ts b/src/view/MapView.ts
index 21f83d5..97d5d7d 100644
--- a/src/view/MapView.ts
+++ b/src/view/MapView.ts
@@ -1,49 +1,30 @@
import {nextPowerOf2} from '../util';
-import Renderer from './Renderer';
import MapData from '../model/MapData';
+import Renderer from './Renderer';
+export default class MapView {
+ public static readonly tileSize: number = 32;
-class MapView {
- private redrawPending: boolean = false;
+ private redrawPending: boolean = false;
private vertexBuffer: WebGLBuffer;
private textureBuffer: WebGLBuffer;
+ constructor(
+ private r: Renderer,
+ private map: MapData,
+ private tileTexture: WebGLTexture,
+ private tileMap: Map<string, number>,
+ ) {
+ const vertexData: number[] = [];
+ const textureData: number[] = [];
- private addTile(vertexData: number[], textureData: number[], x: number, y: number, tile: string, tileCount: number) {
- let tileID = this.tileMap.get(tile);
- if (tileID === undefined)
- throw new Error('invalid tile specifier in map data');
-
- vertexData.push(x); vertexData.push(y);
- vertexData.push(x+1); vertexData.push(y);
- vertexData.push(x); vertexData.push(y+1);
-
- vertexData.push(x); vertexData.push(y+1);
- vertexData.push(x+1); vertexData.push(y);
- vertexData.push(x+1); vertexData.push(y+1);
-
- textureData.push((tileID) / tileCount); textureData.push(0);
- textureData.push((tileID+1) / tileCount); textureData.push(0);
- textureData.push((tileID) / tileCount); textureData.push(1);
-
- textureData.push((tileID) / tileCount); textureData.push(1);
- textureData.push((tileID+1) / tileCount); textureData.push(0);
- textureData.push((tileID+1) / tileCount); textureData.push(1);
- }
-
- constructor(private r: Renderer, private map: MapData, private tileTexture: WebGLTexture, private tileMap: Map<string, number>) {
- let vertexData: number[] = [];
- let textureData: number[] = [];
+ const tileCount = nextPowerOf2(tileMap.size);
- let tileCount = nextPowerOf2(tileMap.size);
-
- for (let x = 0; x < map.width; x++) {
- for (let y = 0; y < map.height; y++) {
- this.addTile(vertexData, textureData, x, y, map.layers[0][y][x], tileCount);
- }
- }
+ for (let x = 0; x < map.width; x++)
+ for (let y = 0; y < map.height; y++)
+ this.addTile(vertexData, textureData, x, y, map.layers[0][y][x], tileCount);
this.vertexBuffer = r.createBuffer();
r.gl.bindBuffer(r.gl.ARRAY_BUFFER, this.vertexBuffer);
@@ -52,9 +33,9 @@ class MapView {
this.textureBuffer = r.createBuffer();
r.gl.bindBuffer(r.gl.ARRAY_BUFFER, this.textureBuffer);
r.gl.bufferData(r.gl.ARRAY_BUFFER, new Float32Array(textureData), r.gl.STATIC_DRAW);
- }
+ }
- draw(): void {
+ public draw(): void {
this.r.gl.clear(this.r.gl.COLOR_BUFFER_BIT);
this.r.gl.activeTexture(this.r.gl.TEXTURE0);
@@ -68,12 +49,27 @@ class MapView {
this.r.gl.vertexAttribPointer(this.r.textureCoordLoc, 2, this.r.gl.FLOAT, false, 0, 0);
this.r.gl.drawArrays(this.r.gl.TRIANGLES, 0, 6 * this.map.width * this.map.height);
- }
-}
+ }
-module MapView {
- export const tileSize = 32;
-}
+ private addTile(vertexData: number[], textureData: number[], x: number, y: number, tile: string, tileCount: number) {
+ const tileID = this.tileMap.get(tile);
+ if (tileID === undefined)
+ throw new Error('invalid tile specifier in map data');
+ vertexData.push(x); vertexData.push(y);
+ vertexData.push(x + 1); vertexData.push(y);
+ vertexData.push(x); vertexData.push(y + 1);
-export default MapView;
+ vertexData.push(x); vertexData.push(y + 1);
+ vertexData.push(x + 1); vertexData.push(y);
+ vertexData.push(x + 1); vertexData.push(y + 1);
+
+ textureData.push((tileID) / tileCount); textureData.push(0);
+ textureData.push((tileID + 1) / tileCount); textureData.push(0);
+ textureData.push((tileID) / tileCount); textureData.push(1);
+
+ textureData.push((tileID) / tileCount); textureData.push(1);
+ textureData.push((tileID + 1) / tileCount); textureData.push(0);
+ textureData.push((tileID + 1) / tileCount); textureData.push(1);
+ }
+}