import {compose} from '@avocado/core'; import {StateProperty, Trait} from '@avocado/entity'; const decorate = compose( StateProperty('isCheckingCollisions'), ); export class Collider extends decorate(Trait) { static defaultParams() { return { collidesWithGroups: [ 'default', ], collisionGroup: 'default', isSensor: false, } } static defaultState() { return { isCheckingCollisions: true, } } initialize() { this._collisionGroup = this.params.get('collisionGroup'); this._collidesWithGroups = this.params.get('collidesWithGroups').toJS(); this._isCollidingWith = []; this._isSensor = this.params.get('isSensor'); } get collisionGroup() { return this._collisionGroup; } get collidesWithGroups() { return this._collidesWithGroups; } get isCollidingWith() { return this._isCollidingWith; } get isSensor() { return this._isSensor; } listeners() { return { collisionEnd: (other) => { const index = this._isCollidingWith.indexOf(other); this._isCollidingWith.splice(index, 1); }, collisionStart: (other) => { this._isCollidingWith.push(other); }, }; } methods() { return { collidesWith: (entity) => { if (!entity.is('collider')) { return false; } const collisionGroup = entity.collisionGroup(); return -1 !== this._collidesWithGroups.indexOf(collisionGroup); }, }; } }