avocado-old/packages/topdown/room.js

168 lines
3.9 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 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-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
);
export class Room extends decorate(class {}) {
2019-03-26 17:04:52 -05:00
constructor() {
super();
2019-03-27 23:22:05 -05:00
this.bounds = [];
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);
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);
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-04-05 15:16:55 -05:00
patchState(patch) {
if (patch.width) {
this.width = patch.width;
}
if (patch.height) {
this.height = patch.height;
}
if (patch.layers) {
this.layers.patchState(patch.layers);
}
}
2019-03-27 01:52:24 -05:00
removeEntityFromLayer(entity, layerIndex = 0) {
this.layers.removeEntityFromLayer(entity, layerIndex);
}
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
get state() {
return this._state;
}
tick(elapsed) {
2019-03-27 01:52:24 -05:00
this.layers.tick(elapsed);
2019-03-27 23:22:05 -05:00
this._state = this._state.set('height', this.height);
this._state = this._state.set('width', this.width);
2019-03-27 01:52:24 -05:00
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
}