feat: layer & layerView

This commit is contained in:
cha0s 2019-03-25 23:39:44 -05:00
parent af143670cc
commit 750730eda6
3 changed files with 53 additions and 0 deletions

View File

@ -1,3 +1,5 @@
export {Layer} from './layer';
export {LayerView} from './layer-view';
export {TilesRenderer} from './tiles-renderer';
export {Tiles} from './tiles';
export {Tileset} from './tileset';

View File

@ -0,0 +1,30 @@
import {Container, Renderable, Sprite} from '@avocado/graphics';
import {Layer, Tileset, TilesRenderer} from '@avocado/topdown';
export class LayerView extends Renderable {
constructor(layer, renderer) {
super();
this.container = new Container();
this.layer = layer;
this._renderer = renderer;
this._tileset = undefined;
Tileset.load(layer.tilesetUri).then((tileset) => {
this.tileset = tileset;
});
}
get internal() {
return this.container.internal;
}
set tileset(tileset) {
this._tileset = tileset;
const tilesRenderer = new TilesRenderer(this.layer.tiles, tileset);
const chunk = tilesRenderer.renderChunk(this._renderer);
const tilesSprite = new Sprite(chunk);
this.container.removeAllChildren();
this.container.addChild(tilesSprite);
}
}

21
packages/topdown/layer.js Normal file
View File

@ -0,0 +1,21 @@
import {Tiles} from '@avocado/topdown';
export class Layer {
constructor() {
this.tiles = undefined;
this.tilesetUri = undefined;
this.entityList = undefined;
}
fromJSON(json) {
if (json.tiles) {
this.tiles = (new Tiles()).fromJSON(json.tiles)
}
if (json.tilesetUri) {
this.tilesetUri = json.tilesetUri;
}
return this;
}
}