refactor: TickingPromise
This commit is contained in:
parent
99f62e31e1
commit
b591e5b08f
|
@ -1,5 +1,5 @@
|
||||||
import {behaviorItemFromJSON, Context} from '@avocado/behavior';
|
import {behaviorItemFromJSON, Context} from '@avocado/behavior';
|
||||||
import {compose} from '@avocado/core';
|
import {compose, TickingPromise} from '@avocado/core';
|
||||||
import {StateProperty, Trait} from '@avocado/entity';
|
import {StateProperty, Trait} from '@avocado/entity';
|
||||||
|
|
||||||
const decorate = compose(
|
const decorate = compose(
|
||||||
|
@ -34,9 +34,19 @@ export class Collider extends decorate(Trait) {
|
||||||
|
|
||||||
constructor(entity, params, state) {
|
constructor(entity, params, state) {
|
||||||
super(entity, params, state);
|
super(entity, params, state);
|
||||||
this._collisionActions = [];
|
|
||||||
this._collisionGroup = this.params.collisionGroup;
|
|
||||||
this._collidesWithGroups = this.params.collidesWithGroups;
|
this._collidesWithGroups = this.params.collidesWithGroups;
|
||||||
|
if (this.params.collisionEndActions) {
|
||||||
|
this._collisionEndActions = behaviorItemFromJSON(
|
||||||
|
this.params.collisionEndActions,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (this.params.collisionStartActions) {
|
||||||
|
this._collisionStartActions = behaviorItemFromJSON(
|
||||||
|
this.params.collisionStartActions,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this._collisionGroup = this.params.collisionGroup;
|
||||||
|
this._collisionTickingPromises = [];
|
||||||
this._doesNotCollideWith = [];
|
this._doesNotCollideWith = [];
|
||||||
this._isCollidingWith = [];
|
this._isCollidingWith = [];
|
||||||
this._isSensor = this.params.isSensor;
|
this._isSensor = this.params.isSensor;
|
||||||
|
@ -62,21 +72,13 @@ export class Collider extends decorate(Trait) {
|
||||||
return this._isSensor;
|
return this._isSensor;
|
||||||
}
|
}
|
||||||
|
|
||||||
pushActionBundle(paramActions, other) {
|
pushCollisionTickingPromise(actions, other) {
|
||||||
const actions = behaviorItemFromJSON(paramActions);
|
|
||||||
const context = new Context({
|
const context = new Context({
|
||||||
entity: this.entity,
|
entity: this.entity,
|
||||||
other,
|
other,
|
||||||
});
|
});
|
||||||
const bundle = {
|
const tickingPromise = actions.tickingPromise(context);
|
||||||
actions,
|
this._collisionTickingPromises.push(tickingPromise);
|
||||||
context,
|
|
||||||
};
|
|
||||||
this._collisionActions.push(bundle);
|
|
||||||
actions.once('actionsFinished', () => {
|
|
||||||
const index = this._collisionActions.indexOf(bundle);
|
|
||||||
this._collisionActions.splice(index, 1);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listeners() {
|
listeners() {
|
||||||
|
@ -86,8 +88,11 @@ export class Collider extends decorate(Trait) {
|
||||||
const index = this._isCollidingWith.indexOf(other);
|
const index = this._isCollidingWith.indexOf(other);
|
||||||
if (-1 !== index) {
|
if (-1 !== index) {
|
||||||
this._isCollidingWith.splice(index, 1);
|
this._isCollidingWith.splice(index, 1);
|
||||||
if (this.params.collisionEndActions) {
|
if (this._collisionEndActions) {
|
||||||
this.pushActionBundle(this.params.collisionEndActions, other);
|
this.pushCollisionTickingPromise(
|
||||||
|
this._collisionEndActions,
|
||||||
|
other
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -96,8 +101,11 @@ export class Collider extends decorate(Trait) {
|
||||||
const index = this._isCollidingWith.indexOf(other);
|
const index = this._isCollidingWith.indexOf(other);
|
||||||
if (-1 === index) {
|
if (-1 === index) {
|
||||||
this._isCollidingWith.push(other);
|
this._isCollidingWith.push(other);
|
||||||
if (this.params.collisionStartActions) {
|
if (this._collisionStartActions) {
|
||||||
this.pushActionBundle(this.params.collisionStartActions, other);
|
this.pushCollisionTickingPromise(
|
||||||
|
this._collisionStartActions,
|
||||||
|
other
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -152,9 +160,8 @@ export class Collider extends decorate(Trait) {
|
||||||
|
|
||||||
tick(elapsed) {
|
tick(elapsed) {
|
||||||
if (AVOCADO_SERVER) {
|
if (AVOCADO_SERVER) {
|
||||||
for (let i = 0; i < this._collisionActions.length; ++i) {
|
for (let i = 0; i < this._collisionTickingPromises.length; ++i) {
|
||||||
const {actions, context} = this._collisionActions[i];
|
this._collisionTickingPromises[i].tick(elapsed);
|
||||||
actions.tick(context, elapsed);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user