summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/index.ts7
-rw-r--r--src/model/state/MapState.ts6
-rw-r--r--src/view/MapView.ts38
-rw-r--r--src/view/map.ts (renamed from src/view/MapLoader.ts)34
4 files changed, 27 insertions, 58 deletions
diff --git a/src/index.ts b/src/index.ts
index e21b006..3e7f33d 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,7 +1,6 @@
import MapData from './model/data/MapData';
-import MapState from './model/state/MapState';
-import {loadMap} from './view/MapLoader';
+import { loadMap } from './view/map';
import Renderer from './view/renderer/Renderer';
window.onload = () => {
@@ -14,9 +13,7 @@ window.onload = () => {
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', async function() {
- const mapData = new MapData(JSON.parse(this.responseText));
- const map = new MapState(mapData);
-
+ const map = new MapData(JSON.parse(this.responseText));
const mapView = await loadMap(renderer, map);
mapView.render();
});
diff --git a/src/model/state/MapState.ts b/src/model/state/MapState.ts
deleted file mode 100644
index 2931dfb..0000000
--- a/src/model/state/MapState.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import MapData from '../data/MapData';
-
-export default class MapState {
- public constructor(public readonly data: MapData) {
- }
-}
diff --git a/src/view/MapView.ts b/src/view/MapView.ts
deleted file mode 100644
index a5af8f7..0000000
--- a/src/view/MapView.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import MapState from '../model/state/MapState';
-import { TileMap } from './MapLoader';
-import Renderer from './renderer/Renderer';
-import { TileView, TileViewBuilder } from './tile';
-
-export default class MapView {
- private static addTile(builder: TileViewBuilder, tileMap: TileMap, x: number, y: number, tile: string) {
- if (tile === ' ')
- return;
-
- const tilePos = tileMap.tiles.get(tile);
- if (!tilePos)
- throw new Error('invalid tile specifier in map data');
-
- builder.addTile([x, y, x + 1, y + 1], tilePos);
- }
-
- private readonly tileView: TileView;
-
- constructor(
- r: Renderer,
- map: MapState,
- tileMap: TileMap,
- ) {
- const builder = new TileViewBuilder(r, tileMap.texture);
-
- for (let x = 0; x < map.data.width; x++)
- for (let y = 0; y < map.data.height; y++)
- for (const layer of map.data.layers)
- MapView.addTile(builder, tileMap, x, y, layer[y][x]);
-
- this.tileView = builder.build();
- }
-
- public render(): void {
- this.tileView.render();
- }
-}
diff --git a/src/view/MapLoader.ts b/src/view/map.ts
index c3504e3..954e39e 100644
--- a/src/view/MapLoader.ts
+++ b/src/view/map.ts
@@ -1,13 +1,11 @@
-import { loadImages, mkTexture } from 'util/image';
+import MapData from '../model/data/MapData';
import { mapValues, nextPowerOf2 } from '../util';
-import { TileCoords } from './tile';
-
-import MapState from '../model/state/MapState';
-import MapView from './MapView';
import Renderer from './renderer/Renderer';
+import { TileCoords, TileView, TileViewBuilder } from './tile';
+import { loadImages, mkTexture } from './util/image';
-export interface TileMap {
+interface TileMap {
texture: WebGLTexture;
tiles: Map<string, TileCoords>;
}
@@ -49,9 +47,27 @@ function mkTileMap(
};
}
-export async function loadMap(r: Renderer, map: MapState): Promise<MapView> {
- const tiles = await loadTiles(map.data.tiles);
+function addTile(builder: TileViewBuilder, tileMap: TileMap, x: number, y: number, tile: string) {
+ if (tile === ' ')
+ return;
+
+ const tilePos = tileMap.tiles.get(tile);
+ if (!tilePos)
+ throw new Error('invalid tile specifier in map data');
+
+ builder.addTile([x, y, x + 1, y + 1], tilePos);
+}
+
+export async function loadMap(r: Renderer, map: MapData): Promise<TileView> {
+ const tiles = await loadTiles(map.tiles);
const tileMap = mkTileMap(r.getContext(), tiles);
- return new MapView(r, map, tileMap);
+ const builder = new TileViewBuilder(r, tileMap.texture);
+
+ for (const layer of map.layers)
+ for (let x = 0; x < map.width; x++)
+ for (let y = 0; y < map.height; y++)
+ addTile(builder, tileMap, x, y, layer[y][x]);
+
+ return builder.build();
}