2019-04-13 20:48:52 -05:00
|
|
|
import {EntityListView} from '@avocado/entity';
|
2019-03-27 01:52:24 -05:00
|
|
|
import {Container, Sprite} from '@avocado/graphics';
|
2019-03-27 19:25:00 -05:00
|
|
|
import {Vector} from '@avocado/math';
|
2019-04-12 20:16:31 -05:00
|
|
|
import {BodyView} from '@avocado/physics';
|
2019-03-28 12:31:41 -05:00
|
|
|
|
|
|
|
import {Layer} from './layer';
|
|
|
|
import {TilesRenderer} from './tiles-renderer';
|
2019-03-25 23:39:44 -05:00
|
|
|
|
2019-03-27 01:52:24 -05:00
|
|
|
export class LayerView extends Container {
|
2019-03-25 23:39:44 -05:00
|
|
|
|
|
|
|
constructor(layer, renderer) {
|
|
|
|
super();
|
2019-04-13 20:48:52 -05:00
|
|
|
this.entityListView = new EntityListView(layer.entityList);
|
2019-03-25 23:39:44 -05:00
|
|
|
this.layer = layer;
|
2019-03-26 09:30:25 -05:00
|
|
|
this.layerContainer = new Container();
|
2019-04-13 20:48:52 -05:00
|
|
|
this.renderer = renderer;
|
2019-04-12 20:16:31 -05:00
|
|
|
this.tileGeometryContainer = new Container();
|
|
|
|
this.tileGeometryViews = [];
|
2019-03-27 01:52:24 -05:00
|
|
|
// Handle entity add/remove.
|
2019-04-12 19:49:35 -05:00
|
|
|
layer.on('tileDataChanged', this.onLayerTileDataChanged, this);
|
2019-09-29 13:19:57 -05:00
|
|
|
this.onLayerTileDataChanged();
|
2019-04-12 20:16:31 -05:00
|
|
|
layer.on('tileGeometryChanged', this.onLayerTileGeometryChanged, this);
|
2019-09-29 13:19:57 -05:00
|
|
|
this.onLayerTileGeometryChanged();
|
2019-04-12 19:49:35 -05:00
|
|
|
layer.on('tilesetChanged', this.onLayerTilesetChanged, this);
|
2019-09-29 13:19:57 -05:00
|
|
|
this.onLayerTilesetChanged(layer.tileset, undefined);
|
2019-03-27 01:52:24 -05:00
|
|
|
this.addChild(this.layerContainer);
|
2019-04-12 20:16:31 -05:00
|
|
|
this.addChild(this.tileGeometryContainer);
|
2019-04-13 20:48:52 -05:00
|
|
|
this.addChild(this.entityListView);
|
2019-03-27 01:52:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
super.destroy();
|
2019-04-12 19:49:35 -05:00
|
|
|
layer.off('entityAdded', this.onLayerEntityAdded);
|
|
|
|
layer.off('entityRemoved', this.onLayerEntityRemoved);
|
|
|
|
layer.off('tileDataChanged', this.onLayerTileDataChanged);
|
|
|
|
layer.off('tilesetChanged', this.onLayerTilesetChanged);
|
2019-04-12 20:16:31 -05:00
|
|
|
this.destroyTileGeometryBodies();
|
|
|
|
}
|
|
|
|
|
|
|
|
destroyTileGeometryBodies() {
|
|
|
|
for (const view of this.tileGeometryViews) {
|
|
|
|
view.destroy();
|
|
|
|
}
|
|
|
|
this.tileGeometryViews = [];
|
2019-03-27 01:52:24 -05:00
|
|
|
}
|
|
|
|
|
2019-04-12 19:49:35 -05:00
|
|
|
onLayerTileDataChanged() {
|
2019-03-27 19:25:00 -05:00
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:16:31 -05:00
|
|
|
onLayerTileGeometryChanged() {
|
|
|
|
this.destroyTileGeometryBodies();
|
|
|
|
for (const body of this.layer.tileGeometry) {
|
|
|
|
const bodyView = new BodyView(body);
|
|
|
|
this.tileGeometryViews.push(bodyView);
|
|
|
|
this.tileGeometryContainer.addChild(bodyView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 19:50:49 -05:00
|
|
|
onLayerTilesetChanged(oldTileset, newTileset) {
|
|
|
|
if (oldTileset) {
|
|
|
|
oldTileset.off('imageChanged', this.onLayerTilesetImageChanged, this);
|
|
|
|
}
|
|
|
|
if (newTileset) {
|
|
|
|
newTileset.on('imageChanged', this.onLayerTilesetImageChanged, this);
|
|
|
|
}
|
|
|
|
this.onLayerTilesetImageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
onLayerTilesetImageChanged() {
|
2019-04-12 18:49:07 -05:00
|
|
|
this.render();
|
2019-03-25 23:39:44 -05:00
|
|
|
}
|
|
|
|
|
2019-03-27 19:25:00 -05:00
|
|
|
render() {
|
|
|
|
this.layerContainer.removeAllChildren();
|
2019-04-13 20:48:52 -05:00
|
|
|
if (!this.renderer) {
|
2019-03-27 19:25:00 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-04-12 18:49:07 -05:00
|
|
|
if (!this.layer.tileset) {
|
2019-03-27 19:25:00 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Vector.isZero(this.layer.tiles.size)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-12 18:49:07 -05:00
|
|
|
const tilesRenderer = new TilesRenderer(this.layer.tiles, this.layer.tileset);
|
2019-04-13 20:48:52 -05:00
|
|
|
const chunk = tilesRenderer.renderChunk(this.renderer);
|
2019-03-25 23:39:44 -05:00
|
|
|
const tilesSprite = new Sprite(chunk);
|
2019-04-19 16:32:17 -05:00
|
|
|
tilesSprite.anchor = [0, 0];
|
2019-03-26 09:30:25 -05:00
|
|
|
this.layerContainer.addChild(tilesSprite);
|
2019-03-25 23:39:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|