avocado-old/packages/topdown/layer-view.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

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';
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() {
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);
}
}
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
}
render() {
this.layerContainer.removeAllChildren();
2019-04-13 20:48:52 -05:00
if (!this.renderer) {
return;
}
2019-04-12 18:49:07 -05:00
if (!this.layer.tileset) {
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);
tilesSprite.anchor = [0, 0];
2019-03-26 09:30:25 -05:00
this.layerContainer.addChild(tilesSprite);
2019-03-25 23:39:44 -05:00
}
}