diff options
Diffstat (limited to 'src/view')
-rw-r--r-- | src/view/map.ts | 21 |
1 files changed, 12 insertions, 9 deletions
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<SpriteView> { - 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<SpriteView[]> { + const tiles = await loadTiles(map.tiles); + const tileset = mkTileset(r, tiles); + + return map.layers.map((layer) => buildMapLayer(r, tileset, layer.tiles)); +} |