/
CohesionBehavior.java
36 lines (29 loc) · 1.14 KB
/
CohesionBehavior.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
34
35
36
package net.citizensnpcs.api.ai.flocking;
import java.util.Collection;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import net.citizensnpcs.api.npc.NPC;
/**
* Implements cohesion flocking with a particular weight i.e. steering a flock of NPCs towards each other.
*
* @see <a href=
* "https://en.wikipedia.org/wiki/Flocking_(behavior)">https://en.wikipedia.org/wiki/Flocking_(behavior)</a>
*/
public class CohesionBehavior implements FlockBehavior {
private final double weight;
public CohesionBehavior(double weight) {
this.weight = weight;
}
@Override
public Vector getVector(NPC npc, Collection<NPC> nearby) {
Location dummy = new Location(null, 0, 0, 0);
Vector positions = new Vector(0, 0, 0);
for (NPC neighbor : nearby) {
if (!neighbor.isSpawned())
continue;
positions = positions.add(neighbor.getEntity().getLocation(dummy).toVector());
}
Vector center = positions.multiply((double) 1 / nearby.size());
return npc.getEntity().getLocation(dummy).toVector().subtract(center).normalize().multiply(weight);
}
}