feat: Listed trait

This commit is contained in:
cha0s 2019-03-20 18:35:33 -05:00
parent 2872f729ff
commit 3f3903c882
2 changed files with 39 additions and 0 deletions

View File

@ -57,6 +57,11 @@ class EntityListBase {
const uuid = entity.instanceUuid; const uuid = entity.instanceUuid;
this.entities_PRIVATE[uuid] = entity; this.entities_PRIVATE[uuid] = entity;
this.state_PRIVATE = this.state_PRIVATE.set(uuid, entity.state()); this.state_PRIVATE = this.state_PRIVATE.set(uuid, entity.state());
entity.addTrait('listed', {
params: {
list: this,
},
});
entity.on('destroyed', () => { entity.on('destroyed', () => {
this.removeEntity(entity); this.removeEntity(entity);
}); });
@ -97,6 +102,8 @@ class EntityListBase {
delete this.entities_PRIVATE[uuid]; delete this.entities_PRIVATE[uuid];
this.state_PRIVATE = this.state_PRIVATE.delete(uuid); this.state_PRIVATE = this.state_PRIVATE.delete(uuid);
this.emit('entityRemoved', entity); this.emit('entityRemoved', entity);
entity.removeTrait('listed');
this.quadTree_PRIVATE.remove(this.quadTreePoint(entity));
} }
state() { state() {

View File

@ -0,0 +1,32 @@
import * as I from 'immutable';
import {Trait} from '@avocado/entity';
import {Rectangle, Vector} from '@avocado/math';
export class Listed extends Trait {
initialize() {
this._list = this.params.get('list');
this.params = this.params.delete('list');
this.lastNearby = I.Set();
}
get list() {
return this._list;
}
methods() {
return {
nearbyEntities: (size) => {
const position = Vector.add(
this.entity.position,
Vector.scale(size, -0.5)
);
return this._list.within(Rectangle.compose(position, size));
},
};
}
}