2019-03-26 17:04:52 -05:00
|
|
|
import * as I from 'immutable';
|
|
|
|
|
2019-03-27 01:52:24 -05:00
|
|
|
import {compose} from '@avocado/core';
|
2019-03-27 16:11:37 -05:00
|
|
|
import {EventEmitter, Property} from '@avocado/mixins';
|
2019-03-26 17:04:52 -05:00
|
|
|
|
2019-03-27 01:52:24 -05:00
|
|
|
import {Layers} from './layers';
|
|
|
|
|
|
|
|
const decorate = compose(
|
|
|
|
EventEmitter,
|
2019-03-27 16:11:37 -05:00
|
|
|
Property('world', {
|
|
|
|
track: true,
|
|
|
|
}),
|
2019-03-27 01:52:24 -05:00
|
|
|
);
|
|
|
|
|
2019-03-27 16:18:27 -05:00
|
|
|
export class Room extends decorate(class {}) {
|
2019-03-26 17:04:52 -05:00
|
|
|
|
|
|
|
constructor() {
|
2019-03-27 16:18:27 -05:00
|
|
|
super();
|
2019-03-27 01:52:24 -05:00
|
|
|
this.layers = new Layers();
|
2019-03-26 17:04:52 -05:00
|
|
|
this._state = I.Map();
|
2019-03-27 01:52:24 -05:00
|
|
|
// Listeners.
|
|
|
|
this.onEntityAddedToRoom = this.onEntityAddedToRoom.bind(this);
|
|
|
|
this.onEntityRemovedFromRoom = this.onEntityRemovedFromRoom.bind(this);
|
2019-03-27 16:18:27 -05:00
|
|
|
this.onWorldChanged = this.onWorldChanged.bind(this);
|
2019-03-27 01:52:24 -05:00
|
|
|
this.layers.on('entityAdded', this.onEntityAddedToRoom);
|
|
|
|
this.layers.on('entityRemoved', this.onEntityRemovedFromRoom);
|
2019-03-27 16:18:27 -05:00
|
|
|
this.on('worldChanged', this.onWorldChanged);
|
2019-03-27 01:52:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
acceptStateChange(change) {
|
|
|
|
if (change.layers) {
|
|
|
|
this.layers.acceptStateChange(change.layers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addEntityToLayer(entity, layerIndex = 0) {
|
|
|
|
this.layers.addEntityToLayer(entity, layerIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.layers.destroy();
|
2019-03-27 16:11:37 -05:00
|
|
|
this.layers.off('entityAdded', this.onEntityAddedToRoom);
|
|
|
|
this.layers.off('entityRemoved', this.onEntityRemovedFromRoom);
|
|
|
|
this.off('worldChanged', this.onWorldChanged);
|
2019-03-26 17:04:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fromJSON(json) {
|
|
|
|
if (json.layers) {
|
2019-03-27 01:52:24 -05:00
|
|
|
this.layers.fromJSON(json.layers);
|
2019-03-26 17:04:52 -05:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-03-27 17:15:53 -05:00
|
|
|
layer(index) {
|
|
|
|
return this.layers.layer(index);
|
|
|
|
}
|
|
|
|
|
2019-03-27 01:52:24 -05:00
|
|
|
onEntityAddedToRoom(entity) {
|
|
|
|
entity.addTrait('roomed');
|
|
|
|
entity.room = this;
|
|
|
|
this.emit('entityAdded', entity)
|
|
|
|
}
|
|
|
|
|
|
|
|
onEntityRemovedFromRoom(entity) {
|
|
|
|
if (entity.is('roomed')) {
|
|
|
|
entity.removeTrait('roomed');
|
|
|
|
}
|
|
|
|
this.emit('entityRemoved', entity);
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:11:37 -05:00
|
|
|
onWorldChanged() {
|
2019-03-27 17:15:53 -05:00
|
|
|
for (const {layer} of this.layers) {
|
2019-03-27 16:11:37 -05:00
|
|
|
for (const entity of layer.entityList) {
|
|
|
|
if (entity.is('physical')) {
|
|
|
|
entity.world = this.world;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 01:52:24 -05:00
|
|
|
removeEntityFromLayer(entity, layerIndex = 0) {
|
|
|
|
this.layers.removeEntityFromLayer(entity, layerIndex);
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:04:52 -05:00
|
|
|
get state() {
|
|
|
|
return this._state;
|
|
|
|
}
|
|
|
|
|
|
|
|
tick(elapsed) {
|
2019-03-27 01:52:24 -05:00
|
|
|
this.layers.tick(elapsed);
|
|
|
|
this._state = this._state.set('layers', this.layers.state);
|
2019-03-27 16:11:37 -05:00
|
|
|
if (this.world) {
|
|
|
|
this.world.tick(elapsed);
|
2019-03-26 17:04:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:11:37 -05:00
|
|
|
}
|