From ec0e51231f95b4d847e4e94eb7146d3a0c159512 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 11 Nov 2018 20:31:22 +0100 Subject: Prepare support for adding extra elements to map layers --- src/view/map.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/view') diff --git a/src/view/map.ts b/src/view/map.ts index 9bdbd30..f5d6eba 100644 --- a/src/view/map.ts +++ b/src/view/map.ts @@ -2,7 +2,7 @@ import { Renderer } from './renderer/renderer'; import { SpriteCoords, SpriteView, SpriteViewBuilder } from './sprite'; import { loadImage, mkTexture } from './util/image'; -import { MapData } from '../model/data/map'; +import { MapData, MapLayer } from '../model/data/map'; import { nextPowerOf2 } from '../util'; @@ -58,16 +58,19 @@ function addSprite(builder: SpriteViewBuilder, tileset: Tileset, x: number, y: n builder.addSprite([x, y, x + 1, y + 1], tilePos); } -export async function loadMap(r: Renderer, map: MapData): Promise { - const tiles = await loadTiles(map.tiles); - const tileset = mkTileset(r, tiles); - +function buildMapLayer(r: Renderer, tileset: Tileset, layer: number[][]): SpriteView { const builder = new SpriteViewBuilder(r, tileset.texture); - for (const layer of map.layers) - for (let x = 0; x < map.width; x++) - for (let y = 0; y < map.height; y++) - addSprite(builder, tileset, x, y, layer[y][x]); + for (let x = 0; x < layer[0].length; x++) + for (let y = 0; y < layer.length; y++) + addSprite(builder, tileset, x, y, layer[y][x]); return builder.build(); } + +export async function loadMap(r: Renderer, map: MapData): Promise { + const tiles = await loadTiles(map.tiles); + const tileset = mkTileset(r, tiles); + + return map.layers.map((layer) => buildMapLayer(r, tileset, layer.tiles)); +} -- cgit v1.2.3