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 23:22:05 -05:00
|
|
|
import {Vector} from '@avocado/math';
|
2019-03-27 16:11:37 -05:00
|
|
|
import {EventEmitter, Property} from '@avocado/mixins';
|
2019-03-27 23:22:05 -05:00
|
|
|
import {RectangleShape} from '@avocado/physics';
|
2019-04-07 11:43:50 -05:00
|
|
|
import {Synchronized} from '@avocado/state';
|
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 23:22:05 -05:00
|
|
|
Vector.Mixin('size', 'width', 'height', {
|
|
|
|
default: [0, 0],
|
2019-03-28 02:04:29 -05:00
|
|
|
track: true,
|
2019-03-27 23:22:05 -05:00
|
|
|
})
|
2019-03-27 01:52:24 -05:00
|
|
|
);
|
|
|
|
|
2019-04-07 11:43:50 -05:00
|
|
|
export class Room extends decorate(Synchronized) {
|
2019-03-26 17:04:52 -05:00
|
|
|
|
|
|
|
constructor() {
|
2019-03-27 16:18:27 -05:00
|
|
|
super();
|
2019-03-27 23:22:05 -05:00
|
|
|
this.bounds = [];
|
2019-03-27 01:52:24 -05:00
|
|
|
this.layers = new Layers();
|
|
|
|
// 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);
|
2019-03-27 23:22:05 -05:00
|
|
|
this.onSizeChanged = this.onSizeChanged.bind(this);
|
|
|
|
this.on('sizeChanged', this.onSizeChanged);
|
|
|
|
this.onWorldChanged = this.onWorldChanged.bind(this);
|
2019-03-27 16:18:27 -05:00
|
|
|
this.on('worldChanged', this.onWorldChanged);
|
2019-03-27 01:52:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-27 17:36:57 -05:00
|
|
|
findEntity(uuid) {
|
|
|
|
return this.layers.findEntity(uuid);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-03-27 23:22:05 -05:00
|
|
|
if (json.size) {
|
|
|
|
this.size = json.size;
|
|
|
|
}
|
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 23:22:05 -05:00
|
|
|
onSizeChanged() {
|
|
|
|
this.updateBounds();
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:11:37 -05:00
|
|
|
onWorldChanged() {
|
2019-03-27 23:22:05 -05:00
|
|
|
const world = this.world;
|
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')) {
|
2019-03-27 23:22:05 -05:00
|
|
|
entity.world = world;
|
2019-03-27 16:11:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 23:22:05 -05:00
|
|
|
// Update bounds.
|
|
|
|
this.updateBounds();
|
2019-03-27 16:11:37 -05:00
|
|
|
}
|
|
|
|
|
2019-03-27 01:52:24 -05:00
|
|
|
removeEntityFromLayer(entity, layerIndex = 0) {
|
|
|
|
this.layers.removeEntityFromLayer(entity, layerIndex);
|
|
|
|
}
|
|
|
|
|
2019-04-07 11:43:50 -05:00
|
|
|
synchronizedChildren() {
|
|
|
|
return [
|
|
|
|
'width',
|
|
|
|
'height',
|
|
|
|
'layers',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:22:05 -05:00
|
|
|
updateBounds() {
|
|
|
|
const world = this.world;
|
|
|
|
if (!world) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const bound of this.bounds) {
|
|
|
|
world.removeBody(bound);
|
|
|
|
}
|
|
|
|
if (Vector.isZero(this.size)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.bounds = [
|
|
|
|
// Top.
|
|
|
|
world.createBody((new RectangleShape()).fromJSON({
|
|
|
|
position: [this.width / 2, -8],
|
|
|
|
size: [this.width, 16],
|
|
|
|
})),
|
|
|
|
// Right.
|
|
|
|
world.createBody((new RectangleShape()).fromJSON({
|
|
|
|
position: [this.width + 8, this.height / 2],
|
|
|
|
size: [16, this.height],
|
|
|
|
})),
|
|
|
|
// Bottom.
|
|
|
|
world.createBody((new RectangleShape()).fromJSON({
|
|
|
|
position: [this.width / 2, this.height + 8],
|
|
|
|
size: [this.width, 16],
|
|
|
|
})),
|
|
|
|
// Left.
|
|
|
|
world.createBody((new RectangleShape()).fromJSON({
|
|
|
|
position: [-8, this.height / 2],
|
|
|
|
size: [16, this.height],
|
|
|
|
})),
|
|
|
|
];
|
|
|
|
this.bounds.forEach((bound) => {
|
|
|
|
bound.static = true;
|
|
|
|
world.addBody(bound);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:04:52 -05:00
|
|
|
tick(elapsed) {
|
2019-04-07 11:43:50 -05:00
|
|
|
super.tick(elapsed);
|
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
|
|
|
}
|