Get rid of MapView and MapState
This commit is contained in:
parent
60f03e3c5e
commit
68ff0cb4b1
4 changed files with 27 additions and 58 deletions
|
@ -1,7 +1,6 @@
|
||||||
import MapData from './model/data/MapData';
|
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';
|
import Renderer from './view/renderer/Renderer';
|
||||||
|
|
||||||
window.onload = () => {
|
window.onload = () => {
|
||||||
|
@ -14,9 +13,7 @@ window.onload = () => {
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
xhr.addEventListener('load', async function() {
|
xhr.addEventListener('load', async function() {
|
||||||
const mapData = new MapData(JSON.parse(this.responseText));
|
const map = new MapData(JSON.parse(this.responseText));
|
||||||
const map = new MapState(mapData);
|
|
||||||
|
|
||||||
const mapView = await loadMap(renderer, map);
|
const mapView = await loadMap(renderer, map);
|
||||||
mapView.render();
|
mapView.render();
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
import MapData from '../data/MapData';
|
|
||||||
|
|
||||||
export default class MapState {
|
|
||||||
public constructor(public readonly data: MapData) {
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +1,11 @@
|
||||||
import { loadImages, mkTexture } from 'util/image';
|
import MapData from '../model/data/MapData';
|
||||||
import { mapValues, nextPowerOf2 } from '../util';
|
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 Renderer from './renderer/Renderer';
|
||||||
|
import { TileCoords, TileView, TileViewBuilder } from './tile';
|
||||||
|
import { loadImages, mkTexture } from './util/image';
|
||||||
|
|
||||||
export interface TileMap {
|
interface TileMap {
|
||||||
texture: WebGLTexture;
|
texture: WebGLTexture;
|
||||||
tiles: Map<string, TileCoords>;
|
tiles: Map<string, TileCoords>;
|
||||||
}
|
}
|
||||||
|
@ -49,9 +47,27 @@ function mkTileMap(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadMap(r: Renderer, map: MapState): Promise<MapView> {
|
function addTile(builder: TileViewBuilder, tileMap: TileMap, x: number, y: number, tile: string) {
|
||||||
const tiles = await loadTiles(map.data.tiles);
|
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);
|
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();
|
||||||
}
|
}
|
Reference in a new issue