feat: afterDestructionTickers

This commit is contained in:
cha0s 2019-04-19 14:46:00 -05:00
parent 4118adf674
commit 1b9b4a6876

View File

@ -17,6 +17,7 @@ export class EntityList extends decorate(class {}) {
constructor() {
super();
this._afterDestructionTickers = [];
this._entities = {};
this._quadTree = new QuadTree();
this._uuidMap = {};
@ -37,6 +38,10 @@ export class EntityList extends decorate(class {}) {
entity.list = this;
entity.once('destroy', () => {
this.removeEntity(entity);
// In the process of destroying, allow entities to specify tickers that
// must live on past destruction.
const tickers = entity.invokeHookFlat('afterDestructionTickers');
this._afterDestructionTickers.push(...tickers);
});
this.emit('entityAdded', entity);
}
@ -59,15 +64,15 @@ export class EntityList extends decorate(class {}) {
patchStateStep(uuid, step) {
const localUuid = this._uuidMap[uuid];
const entity = this._entities[localUuid];
if ('remove' === step.op) {
// Maybe already destroyed on the client?
if (entity && 'destroy' in entity) {
entity.destroy();
}
}
if (entity) {
if ('remove' === step.op) {
// Entity removed.
this.removeEntity(entity);
}
else {
// Exists; patch.
entity.patchState([step]);
}
// Exists; patch.
entity.patchState([step]);
}
else {
// New entity. Create with patch as traits.
@ -94,10 +99,25 @@ export class EntityList extends decorate(class {}) {
}
tick(elapsed) {
// Run after destruction tickers.
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);
}
// Run normal tickers.
for (const uuid in this._entities) {
const entity = this._entities[uuid];
entity.tick(elapsed);
}
// Update state.
this.state = this.state.withMutations((state) => {
for (const uuid in this._entities) {
const entity = this._entities[uuid];