Add support for subtiles

This commit is contained in:
Matthias Schiffer 2016-01-08 12:23:05 +01:00
parent 4de2a6636a
commit 8ee24fdbdf
5 changed files with 39 additions and 10 deletions

View file

@ -1,14 +1,17 @@
'use strict'; 'use strict';
import TileData from './TileData';
interface Input { interface Input {
tiles: {[key: string]: {file: string}}; tiles: {[key: string]: TileData};
collision: string[]; collision: string[];
layers: string[][][]; layers: string[][][];
} }
export default class MapData { export default class MapData {
tiles: {[key: string]: {file: string}}; tiles: {[key: string]: TileData};
collision: string[]; collision: string[];
layers: string[][][]; layers: string[][][];

7
src/model/TileData.ts Normal file
View file

@ -0,0 +1,7 @@
'use strict';
export default class TileData {
file: string;
subtile: number;
}

View file

@ -5,6 +5,7 @@ import * as util from '../util';
import EntityPosition from '../model/EntityPosition'; import EntityPosition from '../model/EntityPosition';
import MapData from '../model/MapData'; import MapData from '../model/MapData';
import Position from '../model/Position'; import Position from '../model/Position';
import TileData from '../model/TileData';
const tileSize = 32; const tileSize = 32;
@ -25,8 +26,14 @@ function loadImages(imgs: {[key: string]: string}): Promise<{[key: string]: HTML
return util.mapPromises(_.mapValues(imgs, loadImage)); return util.mapPromises(_.mapValues(imgs, loadImage));
} }
function loadTiles(tiles: {[key: string]: {file: string}}): Promise<{[key: string]: HTMLImageElement}> { function loadTiles(tiles: {[key: string]: TileData}): Promise<{[key: string]: HTMLImageElement}> {
return loadImages(_.mapValues(tiles, (t) => `resources/sprite/tile/${t.file}.png`)); var imgs: {[key: string]: string} = {}
_.forOwn(tiles, t => {
imgs[t.file] = `resources/sprite/tile/${t.file}.png`
});
return loadImages(imgs);
} }
function loadEntities(entities: EntityPosition[]): Promise<{[key: string]: HTMLImageElement}> { function loadEntities(entities: EntityPosition[]): Promise<{[key: string]: HTMLImageElement}> {
@ -114,11 +121,23 @@ export default class MapView {
this.redraw() this.redraw()
} }
private drawTile(x: number, y: number, tile: HTMLImageElement): void { private drawTile(x: number, y: number, tile: TileData): void {
if (!tile) if (!tile)
return; return;
this.ctx.drawImage(tile, tiles(x)*this.scale, tiles(y)*this.scale, tileSize*this.scale, tileSize*this.scale); var img = this.tiles[tile.file];
if (!img)
return;
this.ctx.drawImage(
img,
tiles(tile.subtile || 0), 0,
tileSize, tileSize,
tiles(x)*this.scale,
tiles(y)*this.scale,
tileSize*this.scale,
tileSize*this.scale
);
} }
private drawEntity(e: EntityPosition, time: number): boolean { private drawEntity(e: EntityPosition, time: number): boolean {
@ -186,7 +205,7 @@ export default class MapView {
if (!tile) if (!tile)
continue; continue;
this.drawTile(x, y, this.tiles[tile]); this.drawTile(x, y, this.map.tiles[tile]);
} }
} }
}); });

View file

@ -1,8 +1,8 @@
{ {
"tiles": { "tiles": {
"G": {"rotate": 0, "file": "grass"}, "G": {"file": "grass"},
"<": {"rotate": 0, "file": "road_left"}, "<": {"file": "road", "subtile": 0},
">": {"rotate": 0, "file": "road_right"} ">": {"file": "road", "subtile": 1}
}, },
"collision": [ "collision": [
"00000000000110000000000000000000", "00000000000110000000000000000000",

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 B