feat: Listed trait
This commit is contained in:
parent
2872f729ff
commit
3f3903c882
|
@ -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() {
|
||||||
|
|
32
packages/entity/traits/listed.js
Normal file
32
packages/entity/traits/listed.js
Normal 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));
|
||||||
|
},
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user