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