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

175 lines
4.8 KiB
JavaScript
Raw Normal View History

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-05-13 21:07:51 -05:00
import {EntityCreatePacket} from '../packets/entity-create.packet';
import {EntityRemovePacket} from '../packets/entity-remove.packet';
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-04-07 12:00:11 -05:00
this._entities = {};
2019-05-13 21:07:51 -05:00
this._entitiesJustAdded = [];
this._entitiesJustRemoved = [];
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;
}
}
2019-05-13 21:07:51 -05:00
acceptPacket(packet) {
if (packet instanceof EntityCreatePacket) {
if (!this.findEntity(packet.data.uuid)) {
const entity = new Entity(packet.data);
entity.instanceUuid = packet.data.uuid;
this.addEntity(entity);
}
2019-05-13 21:07:51 -05:00
}
}
2019-03-17 23:45:48 -05:00
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-05-13 21:07:51 -05:00
if (AVOCADO_SERVER) {
this._entitiesJustAdded.push(entity);
}
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() {
2019-05-13 21:07:51 -05:00
for (let i = 0; i < this._flatEntities.length; i++) {
this._flatEntities[i].destroy();
2019-03-21 01:32:49 -05:00
}
}
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-13 21:07:51 -05:00
packetsForUpdate(force = false) {
const packets = [];
if (!force) {
for (let i = 0; i < this._entitiesJustAdded.length; i++) {
const entity = this._entitiesJustAdded[i];
packets.push(new EntityCreatePacket(entity.toJSON(), entity));
2019-04-06 23:19:32 -05:00
}
2019-05-13 21:07:51 -05:00
this._entitiesJustAdded = [];
2019-04-06 23:19:32 -05:00
}
2019-05-13 21:07:51 -05:00
for (let i = 0; i < this._flatEntities.length; i++) {
const entityPackets = this._flatEntities[i].packetsForUpdate(force);
for (let j = 0; j < entityPackets.length; j++) {
packets.push(entityPackets[j]);
}
2019-04-19 14:46:00 -05:00
}
2019-05-14 03:41:38 -05:00
if (!force) {
for (let i = 0; i < this._entitiesJustRemoved.length; i++) {
const entity = this._entitiesJustRemoved[i];
packets.push(new EntityRemovePacket({}, entity));
}
this._entitiesJustRemoved = [];
}
2019-05-13 21:07:51 -05:00
return packets;
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-05-13 21:07:51 -05:00
if (AVOCADO_SERVER) {
this._entitiesJustRemoved.push(entity);
}
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);
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-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);
}
}
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';