/
SeparationBehavior.java
33 lines (27 loc) · 1.07 KB
/
SeparationBehavior.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package net.citizensnpcs.api.ai.flocking;
import java.util.Collection;
import org.bukkit.util.Vector;
import net.citizensnpcs.api.npc.NPC;
public class SeparationBehavior implements FlockBehavior {
private final double weight;
public SeparationBehavior(double weight) {
this.weight = weight;
}
@Override
public Vector getVector(NPC npc, Collection<NPC> nearby) {
Vector steering = new Vector(0, 0, 0);
Vector pos = npc.getEntity().getLocation().toVector();
int c = 0;
for (NPC neighbor : nearby) {
if (!neighbor.isSpawned())
continue;
double dist = neighbor.getEntity().getLocation().toVector().distance(pos);
Vector repulse = pos.subtract(neighbor.getEntity().getLocation().toVector()).normalize()
.divide(new Vector(dist, dist, dist));
steering = repulse.add(steering);
c++;
}
steering = steering.divide(new Vector(c, c, c));
return steering.subtract(npc.getEntity().getVelocity()).multiply(weight);
}
}