avocado-old/packages/entity/list.js

131 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-03-17 23:45:48 -05:00
import * as I from 'immutable';
import mapValues from 'lodash.mapvalues';
import {compose} from '@avocado/core';
2019-03-20 18:35:19 -05:00
import {QuadTree} from '@avocado/math';
import {EventEmitter} from '@avocado/mixins';
2019-03-17 23:45:48 -05:00
import {create} from './index';
const decorate = compose(
EventEmitter,
);
class EntityListBase {
2019-03-17 23:45:48 -05:00
constructor() {
this.entities_PRIVATE = {};
2019-03-20 18:35:19 -05:00
this.quadTree_PRIVATE = new QuadTree();
2019-03-23 18:26:35 -05:00
this.world_PRIVATE = undefined;
2019-03-17 23:45:48 -05:00
this.state_PRIVATE = I.Map();
this.uuidMap_PRIVATE = {};
}
*[Symbol.iterator]() {
for (const uuid in this.entities_PRIVATE) {
const entity = this.entities_PRIVATE[uuid];
yield entity;
}
}
acceptStateChange(change) {
for (const uuid in change) {
const localUuid = this.uuidMap_PRIVATE[uuid];
const entity = this.entities_PRIVATE[localUuid];
if (entity) {
if (false === change[uuid]) {
// Entity removed.
this.removeEntity(entity);
}
else {
entity.acceptStateChange(change[uuid]);
2019-03-27 01:02:16 -05:00
this.state_PRIVATE = this.state_PRIVATE.set(localUuid, entity.state);
2019-03-17 23:45:48 -05:00
}
}
else {
2019-03-18 21:22:54 -05:00
// New entity. Create with change as traits.
const newEntity = create().fromJSON({
traits: change[uuid],
});
2019-03-17 23:45:48 -05:00
this.uuidMap_PRIVATE[uuid] = newEntity.instanceUuid;
2019-03-21 18:30:44 -05:00
this.addEntity(newEntity);
2019-03-17 23:45:48 -05:00
}
}
}
addEntity(entity) {
const uuid = entity.instanceUuid;
this.entities_PRIVATE[uuid] = entity;
2019-03-27 01:02:16 -05:00
this.state_PRIVATE = this.state_PRIVATE.set(uuid, entity.state);
2019-03-21 00:36:06 -05:00
entity.addTrait('listed');
entity.list = this;
2019-03-21 00:09:17 -05:00
entity.once('destroyed', () => {
this.removeEntity(entity);
2019-03-20 18:35:19 -05:00
});
this.emit('entityAdded', entity);
2019-03-17 23:45:48 -05:00
}
2019-03-21 01:32:49 -05:00
destroy() {
for (const entity of this) {
entity.destroy();
}
}
2019-03-17 23:45:48 -05:00
entity(uuid) {
return this.entities_PRIVATE[uuid];
}
2019-03-20 21:07:57 -05:00
mappedUuid(uuid) {
return this.uuidMap_PRIVATE[uuid];
}
2019-03-23 18:26:35 -05:00
get world() {
return this.world_PRIVATE;
}
set world(world) {
this.world_PRIVATE = world;
}
quadTree() {
return this.quadTree_PRIVATE;
2019-03-20 18:35:19 -05:00
}
2019-03-17 23:45:48 -05:00
recomputeState() {
for (const uuid in this.entities_PRIVATE) {
const entity = this.entities_PRIVATE[uuid];
2019-03-27 01:02:16 -05:00
this.state_PRIVATE = this.state_PRIVATE.set(uuid, entity.state);
2019-03-17 23:45:48 -05:00
}
}
removeEntity(entity) {
const uuid = entity.instanceUuid;
delete this.entities_PRIVATE[uuid];
this.state_PRIVATE = this.state_PRIVATE.delete(uuid);
this.emit('entityRemoved', entity);
2019-03-23 18:49:19 -05:00
if (entity.is('listed')) {
2019-03-20 23:23:34 -05:00
entity.removeTrait('listed');
}
2019-03-17 23:45:48 -05:00
}
2019-03-26 15:41:49 -05:00
get state() {
2019-03-17 23:45:48 -05:00
return this.state_PRIVATE;
}
tick(elapsed) {
for (const uuid in this.entities_PRIVATE) {
const entity = this.entities_PRIVATE[uuid];
if ('tick' in entity) {
entity.tick(elapsed);
}
}
2019-03-23 18:26:35 -05:00
if (this.world_PRIVATE) {
this.world_PRIVATE.tick(elapsed);
}
2019-03-17 23:45:48 -05:00
this.recomputeState();
}
}
export class EntityList extends decorate(EntityListBase) {}