summaryrefslogtreecommitdiffstats
path: root/src/renderer/view/map.ts
blob: 18def05dc47b109833446b83f6ece1cb3659647a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { EntityView } from './entity';
import { Renderer } from './renderer/renderer';
import { SpriteCoords, SpriteView, SpriteViewBuilder } from './sprite';
import { loadImage, mkTexture } from './util/image';

import { MapData } from '../model/data/map';

import { nextPowerOf2 } from '../util';

interface StaticMapTile {
	type: 'static';
	image: HTMLImageElement;
}

interface EntityTile {
	type: 'entity';
	entity: EntityView;
}

type MapTile = StaticMapTile | EntityTile;

interface StaticTilesetTile {
	type: 'static';
	coords: SpriteCoords;
}

type TilesetTile = StaticTilesetTile | EntityTile;

interface Tileset {
	texture: WebGLTexture;
	tiles: TilesetTile[];
}

async function loadTile(r: Renderer, tile: string): Promise<MapTile> {
	const name = tile.substr(1);
	switch (tile[0]) {
	case '-':
		return {
			type: 'static',
			image: await loadImage(`resources/sprite/tile/${name}.png`),
		};

	case '@':
		return {
			type: 'entity',
			entity: await EntityView.load(r, name),
		};

	default:
		throw new Error('invalid tile specifier');
	}
}

function loadTiles(r: Renderer, tiles: string[]): Promise<MapTile[]> {
	return Promise.all(tiles.map((tile) => loadTile(r, tile)));
}

function mkTileset(
	r: Renderer,
	mapTiles: MapTile[],
): Tileset {
	const tileSize = 32;

	const canvasDim = nextPowerOf2(Math.sqrt(mapTiles.length));
	const canvasSize = canvasDim * tileSize;

	const canvas = document.createElement('canvas');
	canvas.width = canvas.height = canvasSize;

	let x = 0, y = 0;
	const tiles: TilesetTile[] = [];
	const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;

	for (const tile of mapTiles) {
		switch (tile.type) {
		case 'static':
			ctx.drawImage(tile.image, x * tileSize, y * tileSize);
			tiles.push({
				type: 'static',
				coords: [x / canvasDim, y / canvasDim, (x + 1) / canvasDim, (y + 1) / canvasDim],
			});

			x++;
			if (x === canvasDim) {
				x = 0;
				y++;
			}
			break;

		case 'entity':
			tiles.push(tile);
			break;
		}
	}

	const [texture] = mkTexture(r, canvas);

	return {
		texture,
		tiles,
	};
}

function addSprite(
	builder: SpriteViewBuilder,
	entityTiles: Array<[[number, number], EntityView]>,
	tileset: Tileset,
	x: number,
	y: number,
	tile: number) {
	if (!tile)
		return;

	const tilesetTile = tileset.tiles[tile - 1];

	switch (tilesetTile.type) {
		case 'static':
			builder.addSprite([x, y, x + 1, y + 1], tilesetTile.coords);
			break;

		case 'entity':
			entityTiles.push([[x + 0.5, y + 0.5], tilesetTile.entity]);
			break;
	}
}

function buildMapLayer(r: Renderer, tileset: Tileset, layer: number[][]): MapLayerView {
	const builder = new SpriteViewBuilder(r, tileset.texture);
	const entityTiles: Array<[[number, number], EntityView]> = [];

	for (let x = 0; x < layer[0].length; x++)
		for (let y = 0; y < layer.length; y++)
			addSprite(builder, entityTiles, tileset, x, y, layer[y][x]);

	return new MapLayerView(r, builder.build(), entityTiles);
}

class MapLayerView {
	public constructor(
		private r: Renderer,
		private staticTiles: SpriteView,
		private entityTiles: Array<[[number, number], EntityView]>,
	) {
	}

	public render(time: number): void {
		this.r.setTranslation([0, 0]);
		this.staticTiles.render();

		for (const [coords, entity] of this.entityTiles) {
			this.r.setTranslation(coords);
			entity.renderByTime(time);
		}
	}
}

export class MapView {
	public static async load(r: Renderer, map: MapData): Promise<MapView> {
		const tiles = await loadTiles(r, map.tiles);
		const tileset = mkTileset(r, tiles);

		const layers = map.layers.map((layer) => buildMapLayer(r, tileset, layer.tiles));
		return new MapView(layers);
	}

	private constructor(private layers: MapLayerView[]) {
	}

	public render(time: number): void {
		for (const layer of this.layers)
			layer.render(time);
	}
}