MovingEntity类是Yuka js库提供的一个实体类,updateNeighborhood方法可以更新实体周围的邻居列表。
updateNeighborhood( entities, radius );
const entity = new MovingEntity();
const entities = [ entity1, entity2, entity3 ];
entity.updateNeighborhood( entities, 10 );
在updateNeighborhood方法内部,首先会清空当前实体的邻居列表,然后遍历待检查邻居的实体列表。对于每一个待检查的实体,通过距离方式检查它和当前实体之间的距离。如果距离在检查半径范围内,就认为它是当前实体的邻居,加入到邻居列表中。
MovingEntity.prototype.updateNeighborhood = function ( entities, radius ) {
this.neighborhood.length = 0;
const length = entities.length;
for ( let i = 0; i < length; i ++ ) {
const entity = entities[ i ];
const distance = this.position.distanceTo( entity.position );
if ( entity !== this && distance <= radius ) {
this.neighborhood.push( entity );
}
}
};