updateNeighborhood method

EntityManager updateNeighborhood(
  1. GameEntity entity
)

Updates the neighborhood of a single game entity.

Implementation

EntityManager updateNeighborhood(GameEntity entity ) {
	if ( entity.updateNeighborhood == true ) {
		entity.neighbors.length = 0;

		// determine candidates

		if ( spatialIndex != null ) {
			spatialIndex?.query( entity.position, entity.neighborhoodRadius, candidates );
		}
      else {
			// worst case runtime complexity with O(n²)
			candidates.length = 0;
			candidates.addAll( entities );
		}

		// verify if candidates are within the predefined range

		final neighborhoodRadiusSq = ( entity.neighborhoodRadius * entity.neighborhoodRadius );

		for ( int i = 0, l = candidates.length; i < l; i ++ ) {
			final candidate = candidates[ i ];

			if ( entity != candidate && candidate.active == true ) {
				final distanceSq = entity.position.squaredDistanceTo( candidate.position );
				if ( distanceSq <= neighborhoodRadiusSq ) {
					entity.neighbors.add( candidate );
				}
			}
		}
	}

	return this;
}