view: rename tile module to sprite
This commit is contained in:
parent
84762894c8
commit
69be8d2ef8
2 changed files with 17 additions and 17 deletions
|
@ -2,12 +2,12 @@ import MapData from '../model/data/MapData';
|
||||||
import { mapValues, nextPowerOf2 } from '../util';
|
import { mapValues, nextPowerOf2 } from '../util';
|
||||||
|
|
||||||
import Renderer from './renderer/Renderer';
|
import Renderer from './renderer/Renderer';
|
||||||
import { TileCoords, TileView, TileViewBuilder } from './tile';
|
import { SpriteCoords, SpriteView, SpriteViewBuilder } from './sprite';
|
||||||
import { loadImages, mkTexture } from './util/image';
|
import { loadImages, mkTexture } from './util/image';
|
||||||
|
|
||||||
interface TileMap {
|
interface TileMap {
|
||||||
texture: WebGLTexture;
|
texture: WebGLTexture;
|
||||||
tiles: Map<string, TileCoords>;
|
tiles: Map<string, SpriteCoords>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadTiles(tiles: Map<string, string>): Promise<Map<string, HTMLImageElement>> {
|
function loadTiles(tiles: Map<string, string>): Promise<Map<string, HTMLImageElement>> {
|
||||||
|
@ -27,7 +27,7 @@ function mkTileMap(
|
||||||
canvas.width = canvas.height = canvasSize;
|
canvas.width = canvas.height = canvasSize;
|
||||||
|
|
||||||
let x = 0, y = 0;
|
let x = 0, y = 0;
|
||||||
const map: Map<string, TileCoords> = new Map();
|
const map: Map<string, SpriteCoords> = new Map();
|
||||||
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
|
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
|
||||||
|
|
||||||
for (const [k, tile] of tiles) {
|
for (const [k, tile] of tiles) {
|
||||||
|
@ -47,7 +47,7 @@ function mkTileMap(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function addTile(builder: TileViewBuilder, tileMap: TileMap, x: number, y: number, tile: string) {
|
function addSprite(builder: SpriteViewBuilder, tileMap: TileMap, x: number, y: number, tile: string) {
|
||||||
if (tile === ' ')
|
if (tile === ' ')
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -55,19 +55,19 @@ function addTile(builder: TileViewBuilder, tileMap: TileMap, x: number, y: numbe
|
||||||
if (!tilePos)
|
if (!tilePos)
|
||||||
throw new Error('invalid tile specifier in map data');
|
throw new Error('invalid tile specifier in map data');
|
||||||
|
|
||||||
builder.addTile([x, y, x + 1, y + 1], tilePos);
|
builder.addSprite([x, y, x + 1, y + 1], tilePos);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadMap(r: Renderer, map: MapData): Promise<TileView> {
|
export async function loadMap(r: Renderer, map: MapData): Promise<SpriteView> {
|
||||||
const tiles = await loadTiles(map.tiles);
|
const tiles = await loadTiles(map.tiles);
|
||||||
const tileMap = mkTileMap(r.getContext(), tiles);
|
const tileMap = mkTileMap(r.getContext(), tiles);
|
||||||
|
|
||||||
const builder = new TileViewBuilder(r, tileMap.texture);
|
const builder = new SpriteViewBuilder(r, tileMap.texture);
|
||||||
|
|
||||||
for (const layer of map.layers)
|
for (const layer of map.layers)
|
||||||
for (let x = 0; x < map.width; x++)
|
for (let x = 0; x < map.width; x++)
|
||||||
for (let y = 0; y < map.height; y++)
|
for (let y = 0; y < map.height; y++)
|
||||||
addTile(builder, tileMap, x, y, layer[y][x]);
|
addSprite(builder, tileMap, x, y, layer[y][x]);
|
||||||
|
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import Renderer from './renderer/Renderer';
|
import Renderer from './renderer/Renderer';
|
||||||
|
|
||||||
export type TileCoords = [number, number, number, number];
|
export type SpriteCoords = [number, number, number, number];
|
||||||
|
|
||||||
export class TileViewBuilder {
|
export class SpriteViewBuilder {
|
||||||
private static pushTile(buf: number[], coords: TileCoords): void {
|
private static pushSprite(buf: number[], coords: SpriteCoords): void {
|
||||||
const [x1, y1, x2, y2] = coords;
|
const [x1, y1, x2, y2] = coords;
|
||||||
|
|
||||||
buf.push(x1); buf.push(y1);
|
buf.push(x1); buf.push(y1);
|
||||||
|
@ -20,17 +20,17 @@ export class TileViewBuilder {
|
||||||
|
|
||||||
constructor(private readonly r: Renderer, private readonly texture: WebGLTexture) {}
|
constructor(private readonly r: Renderer, private readonly texture: WebGLTexture) {}
|
||||||
|
|
||||||
public addTile(vertexCoords: TileCoords, texCoords: TileCoords): void {
|
public addSprite(vertexCoords: SpriteCoords, texCoords: SpriteCoords): void {
|
||||||
TileViewBuilder.pushTile(this.vertexData, vertexCoords);
|
SpriteViewBuilder.pushSprite(this.vertexData, vertexCoords);
|
||||||
TileViewBuilder.pushTile(this.textureData, texCoords);
|
SpriteViewBuilder.pushSprite(this.textureData, texCoords);
|
||||||
}
|
}
|
||||||
|
|
||||||
public build(): TileView {
|
public build(): SpriteView {
|
||||||
return new TileView(this.r, this.texture, this.vertexData, this.textureData);
|
return new SpriteView(this.r, this.texture, this.vertexData, this.textureData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TileView {
|
export class SpriteView {
|
||||||
private readonly vertexCount: number;
|
private readonly vertexCount: number;
|
||||||
private readonly vertexBuffer: WebGLBuffer;
|
private readonly vertexBuffer: WebGLBuffer;
|
||||||
private readonly textureBuffer: WebGLBuffer;
|
private readonly textureBuffer: WebGLBuffer;
|
Reference in a new issue