avocado-old/packages/topdown/layer.js

189 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-03-26 17:04:44 -05:00
import * as I from 'immutable';
2019-04-28 23:45:03 -05:00
import {compose, EventEmitter, Property} from '@avocado/core';
2019-04-16 16:40:20 -05:00
import {Entity, EntityList} from '@avocado/entity';
2019-04-12 20:16:31 -05:00
import {Vector} from '@avocado/math';
2019-04-13 13:38:18 -05:00
import {ShapeList} from '@avocado/physics';
2019-04-07 11:43:50 -05:00
import {Synchronized} from '@avocado/state';
2019-03-28 12:31:41 -05:00
import {Tiles} from './tiles';
2019-04-12 18:49:07 -05:00
import {Tileset} from './tileset';
2019-03-25 23:39:44 -05:00
2019-03-27 01:52:24 -05:00
const decorate = compose(
EventEmitter,
Property('tilesetUri', {
track: true,
}),
2019-04-12 18:49:07 -05:00
Property('tileset', {
track: true,
}),
2019-04-12 20:16:31 -05:00
Property('world', {
track: true,
}),
2019-04-16 17:52:56 -05:00
Synchronized,
2019-03-27 01:52:24 -05:00
);
2019-04-16 17:52:56 -05:00
export class Layer extends decorate(class {}) {
2019-03-25 23:39:44 -05:00
constructor() {
super();
2019-03-26 17:04:44 -05:00
this.entityList = new EntityList();
2019-04-12 20:16:31 -05:00
this.tileGeometry = [];
2019-03-27 01:52:24 -05:00
this.tiles = new Tiles();
// Listeners.
2019-04-12 18:58:38 -05:00
this.entityList.on('entityAdded', this.onEntityAddedToLayer, this);
this.entityList.on('entityRemoved', this.onEntityRemovedFromLayer, this);
this.tiles.on('dataChanged', this.onTileDataChanged, this);
2019-04-12 20:16:31 -05:00
this.on('tilesetChanged', this.onTilesetChanged, this);
2019-04-12 18:58:38 -05:00
this.on('tilesetUriChanged', this.onTilesetUriChanged, this);
2019-04-12 18:49:07 -05:00
this.onTilesetUriChanged();
2019-04-12 20:16:31 -05:00
this.on('worldChanged', this.onWorldChanged, this);
this.initializeSynchronizedChildren();
2019-03-27 01:52:24 -05:00
}
addEntity(entity) {
this.entityList.addEntity(entity);
}
2019-04-12 20:16:31 -05:00
addTileGeometry() {
const tileset = this.tileset;
if (!tileset) {
return false;
}
const world = this.world;
if (!world) {
return false;
}
2019-04-13 03:40:13 -05:00
const halfTileSize = Vector.scale(tileset.tileSize, 0.5);
2019-04-12 20:16:31 -05:00
this.tiles.forEachTile((tile, x, y, i) => {
const shape = this.tileset.geometry(tile);
if (!shape) {
return;
}
2019-04-13 13:38:18 -05:00
shape.position = Vector.add(
2019-04-12 20:16:31 -05:00
halfTileSize,
Vector.mul([x, y], tileset.tileSize),
);
const body = world.createBody(shape);
body.static = true;
world.addBody(body);
this.tileGeometry.push(body);
2019-04-12 20:16:31 -05:00
});
return true;
}
2019-04-28 22:33:20 -05:00
allEntities() {
return Array.from(this.entityList);
}
2019-03-27 01:52:24 -05:00
destroy() {
this.entityList.destroy();
this.entityList.off('entityAdded', this.onEntityAddedToLayer);
this.entityList.off('entityRemoved', this.onEntityRemovedFromLayer);
2019-04-12 18:49:07 -05:00
this.tiles.off('dataChanged', this.onTileDataChanged);
this.off('tilesetUriChanged', this.onTilesetUriChanged);
if (this.tileset) {
this.tileset.destroy();
}
2019-03-25 23:39:44 -05:00
}
2019-03-27 17:36:57 -05:00
findEntity(uuid) {
return this.entityList.findEntity(uuid);
}
2019-03-25 23:39:44 -05:00
fromJSON(json) {
2019-03-27 01:52:24 -05:00
if (json.entities) {
json.entities.forEach((entityJSON) => {
2019-05-04 11:39:23 -05:00
const entity = new Entity(entityJSON);
this.entityList.addEntity(entity);
2019-03-27 01:52:24 -05:00
});
}
2019-03-25 23:39:44 -05:00
if (json.tiles) {
2019-03-27 01:52:24 -05:00
this.tiles.fromJSON(json.tiles)
2019-03-25 23:39:44 -05:00
}
if (json.tilesetUri) {
this.tilesetUri = json.tilesetUri;
}
return this;
}
2019-03-27 01:52:24 -05:00
onEntityAddedToLayer(entity) {
entity.addTrait('layered');
entity.layer = this;
this.emit('entityAdded', entity)
}
onEntityRemovedFromLayer(entity) {
if (entity.is('layered')) {
entity.removeTrait('layered');
}
this.emit('entityRemoved', entity);
}
onTileDataChanged() {
this.emit('tileDataChanged');
}
2019-04-12 20:16:31 -05:00
onTilesetChanged(oldTileset) {
let didChange = false;
if (oldTileset) {
this.removeTileGeometry();
didChange = true;
}
if (this.addTileGeometry()) {
didChange = true;
}
if (didChange) {
this.emit('tileGeometryChanged');
}
}
2019-04-12 18:49:07 -05:00
onTilesetUriChanged() {
if (!this.tilesetUri) {
return;
}
Tileset.load(this.tilesetUri).then((tileset) => {
this.tileset = tileset;
});
}
2019-04-12 20:16:31 -05:00
onWorldChanged(oldWorld) {
let didChange = false;
if (oldWorld) {
this.removeTileGeometry();
didChange = true;
}
if (this.addTileGeometry()) {
didChange = true;
}
if (didChange) {
this.emit('tileGeometryChanged');
}
}
2019-03-27 01:52:24 -05:00
removeEntity(entity) {
this.entityList.removeEntity(entity);
}
2019-04-12 20:16:31 -05:00
removeTileGeometry() {
// ... tag geometry in world for removal?
}
setTileAt(x, y, tile) {
this.tiles.setTileAt(x, y, tile);
}
2019-04-07 11:43:50 -05:00
synchronizedChildren() {
return [
'entityList',
'tiles',
'tilesetUri',
];
2019-03-26 17:04:44 -05:00
}
2019-03-27 17:39:34 -05:00
visibleEntities(query) {
return this.entityList.visibleEntities(query);
}
2019-03-27 01:52:24 -05:00
}