avocado-old/packages/topdown/room.js

101 lines
2.2 KiB
JavaScript
Raw Normal View History

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
);
class RoomBase {
2019-03-26 17:04:52 -05:00
constructor() {
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);
this.layers.on('entityAdded', this.onEntityAddedToRoom);
this.layers.on('entityRemoved', this.onEntityRemovedFromRoom);
}
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 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() {
for (const layer of this.layers) {
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
}
export class Room extends decorate(RoomBase) {
2019-03-27 01:52:24 -05:00
2019-03-27 16:11:37 -05:00
constructor() {
super();
this.onWorldChanged = this.onWorldChanged.bind(this);
this.on('worldChanged', this.onWorldChanged);
2019-03-27 01:52:24 -05:00
}
}