avocado-old/packages/entity/list/index.js

186 lines
4.8 KiB
JavaScript
Raw Normal View History

2019-03-17 23:45:48 -05:00
import * as I from 'immutable';
import mapValues from 'lodash.mapvalues';
2019-05-02 20:04:25 -05:00
import {compose, EventEmitter} from '@avocado/core';
import {QuadTree, Rectangle, Vector} from '@avocado/math';
2019-04-07 11:43:50 -05:00
import {Synchronized} from '@avocado/state';
2019-04-16 16:40:20 -05:00
import {Entity} from '../index';
2019-03-17 23:45:48 -05:00
const decorate = compose(
EventEmitter,
2019-04-16 17:52:56 -05:00
Synchronized,
);
2019-04-16 17:52:56 -05:00
export class EntityList extends decorate(class {}) {
2019-03-17 23:45:48 -05:00
constructor() {
super();
2019-04-19 14:46:00 -05:00
this._afterDestructionTickers = [];
2019-05-03 13:26:51 -05:00
this._dirtyEntities = [];
2019-04-07 12:00:11 -05:00
this._entities = {};
2019-05-05 17:11:46 -05:00
this._entityTickers = []
2019-05-03 13:26:51 -05:00
this._flatEntities = [];
this._quadTree = new QuadTree();
2019-03-17 23:45:48 -05:00
}
*[Symbol.iterator]() {
2019-04-07 12:00:11 -05:00
for (const uuid in this._entities) {
const entity = this._entities[uuid];
2019-03-17 23:45:48 -05:00
yield entity;
}
}
addEntity(entity) {
const uuid = entity.instanceUuid;
2019-04-07 12:00:11 -05:00
this._entities[uuid] = entity;
2019-05-03 13:26:51 -05:00
this._flatEntities.push(entity);
2019-05-05 17:11:46 -05:00
this._entityTickers.push(entity.tick);
2019-04-07 11:43:50 -05:00
this.state = this.state.set(uuid, entity.state);
2019-05-03 13:26:51 -05:00
entity.setIntoList(this);
entity.once('destroy', () => {
2019-03-21 00:09:17 -05:00
this.removeEntity(entity);
2019-04-19 14:46:00 -05:00
// In the process of destroying, allow entities to specify tickers that
// must live on past destruction.
const tickers = entity.invokeHookFlat('afterDestructionTickers');
2019-05-03 13:26:51 -05:00
for (let i = 0; i < tickers.length; i++) {
const ticker = tickers[i];
this._afterDestructionTickers.push(ticker);
}
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-27 17:36:57 -05:00
findEntity(uuid) {
2019-05-03 13:26:51 -05:00
if (uuid in this._entities) {
2019-04-07 12:00:11 -05:00
return this._entities[uuid];
2019-03-27 17:36:57 -05:00
}
2019-03-20 21:07:57 -05:00
}
2019-05-03 13:26:51 -05:00
markEntityDirty(entity) {
if (-1 === this._dirtyEntities.indexOf(entity)) {
this._dirtyEntities.push(entity);
}
}
2019-04-07 11:43:50 -05:00
patchStateStep(uuid, step) {
const entity = this._entities[uuid];
if ('/' === step.path) {
switch (step.op) {
case 'add':
// New entity. Create with patch as traits.
const newEntity = new Entity({
traits: step.value,
});
newEntity.instanceUuid = uuid;
this.addEntity(newEntity);
break;
case 'remove':
// Maybe already destroyed on the client?
if (entity && entity.is('existent')) {
entity.destroy();
}
break;
2019-04-06 23:19:32 -05:00
}
return;
2019-04-06 23:19:32 -05:00
}
if ('replace' === step.op && entity) {
2019-04-19 14:46:00 -05:00
// Exists; patch.
entity.patchState([step]);
}
2019-04-05 15:16:55 -05:00
}
get quadTree() {
return this._quadTree;
2019-03-20 18:35:19 -05:00
}
2019-03-17 23:45:48 -05:00
removeEntity(entity) {
const uuid = entity.instanceUuid;
2019-05-05 17:28:52 -05:00
if (!(uuid in this._entities)) {
2019-05-05 17:11:46 -05:00
return;
}
2019-04-07 12:00:11 -05:00
delete this._entities[uuid];
2019-05-05 17:28:52 -05:00
this._flatEntities.splice(this._flatEntities.indexOf(entity), 1);
2019-05-05 17:11:46 -05:00
this._entityTickers.splice(this._entityTickers.indexOf(entity.tick), 1);
2019-04-07 11:43:50 -05:00
this.state = this.state.delete(uuid);
this.emit('entityRemoved', entity);
2019-03-17 23:45:48 -05:00
}
tick(elapsed) {
2019-05-02 21:37:37 -05:00
// Run after destruction tickers.
2019-05-03 13:26:51 -05:00
if (this._afterDestructionTickers.length > 0) {
this.tickAfterDestructionTickers(elapsed);
}
2019-04-19 14:46:00 -05:00
// Run normal tickers.
this.tickEntities(elapsed)
2019-04-19 14:46:00 -05:00
// Update state.
2019-04-23 15:25:03 -05:00
if (AVOCADO_SERVER) {
2019-05-03 13:26:51 -05:00
this.tickMutateState();
2019-04-23 15:25:03 -05:00
}
2019-03-17 23:45:48 -05:00
}
tickAfterDestructionTickers(elapsed) {
const finishedTickers = [];
for (let i = 0; i < this._afterDestructionTickers.length; ++i) {
const ticker = this._afterDestructionTickers[i];
if (ticker(elapsed)) {
finishedTickers.push(ticker);
}
}
for (let i = 0; i < finishedTickers.length; ++i) {
const ticker = finishedTickers[i];
const index = this._afterDestructionTickers.indexOf(ticker);
this._afterDestructionTickers.splice(index, 1);
}
}
tickEntities(elapsed) {
2019-05-05 17:11:46 -05:00
for (let i = 0; i < this._entityTickers.length; i++) {
this._entityTickers[i](elapsed);
}
}
2019-05-03 13:26:51 -05:00
tickMutateState(state) {
2019-05-08 21:17:00 -05:00
if (0 === this._dirtyEntities.length) {
return;
}
2019-05-03 13:26:51 -05:00
this.state = this.state.withMutations((state) => {
for (let i = 0; i < this._dirtyEntities.length; i++) {
const entity = this._dirtyEntities[i];
state.set(entity.$$avocado_property_instanceUuid, entity.state);
}
});
this._dirtyEntities = [];
}
visibleEntities(query) {
2019-04-16 23:59:08 -05:00
const entities = [];
2019-05-09 01:32:49 -05:00
const entitiesChecked = [];
const quadTree = this._quadTree;
2019-04-16 23:59:08 -05:00
const nodes = quadTree.search(query);
2019-05-09 01:32:49 -05:00
// Check all nodes.
2019-04-16 23:59:08 -05:00
for (let i = 0; i < nodes.length; ++i) {
const node = nodes[i];
const entity = node.data[2];
2019-05-09 01:32:49 -05:00
const aabb = node.data[3];
if (-1 === entitiesChecked.indexOf(entity)) {
entitiesChecked.push(entity);
// Make sure the AABB is actually in the query due to expansion.
if (Rectangle.intersects(query, aabb)) {
entities.push(entity);
}
2019-05-02 20:04:25 -05:00
}
}
return entities;
}
2019-03-17 23:45:48 -05:00
}
2019-04-13 20:53:02 -05:00
export {EntityListView} from './view';