From 750730eda6680c886465d45e371e9ded2338f929 Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 25 Mar 2019 23:39:44 -0500 Subject: [PATCH] feat: layer & layerView --- packages/topdown/index.js | 2 ++ packages/topdown/layer-view.js | 30 ++++++++++++++++++++++++++++++ packages/topdown/layer.js | 21 +++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 packages/topdown/layer-view.js create mode 100644 packages/topdown/layer.js diff --git a/packages/topdown/index.js b/packages/topdown/index.js index 60ca282..f69c664 100644 --- a/packages/topdown/index.js +++ b/packages/topdown/index.js @@ -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'; diff --git a/packages/topdown/layer-view.js b/packages/topdown/layer-view.js new file mode 100644 index 0000000..d24a626 --- /dev/null +++ b/packages/topdown/layer-view.js @@ -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); + } + +} diff --git a/packages/topdown/layer.js b/packages/topdown/layer.js new file mode 100644 index 0000000..c7ba1da --- /dev/null +++ b/packages/topdown/layer.js @@ -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; + } + +} \ No newline at end of file