/
AlignmentBehavior.java
33 lines (27 loc) · 1.03 KB
/
AlignmentBehavior.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;
/**
* Implements alignment flocking with a particular weight i.e. steering a flock of NPCs in line with each other.
*
* @see <a href=
* "https://en.wikipedia.org/wiki/Flocking_(behavior)">https://en.wikipedia.org/wiki/Flocking_(behavior)</a>
*/
public class AlignmentBehavior implements FlockBehavior {
private final double weight;
public AlignmentBehavior(double weight) {
this.weight = weight;
}
@Override
public Vector getVector(NPC npc, Collection<NPC> nearby) {
Vector velocities = new Vector(0, 0, 0);
for (NPC neighbor : nearby) {
if (!neighbor.isSpawned())
continue;
velocities = velocities.add(neighbor.getEntity().getVelocity());
}
Vector desired = velocities.multiply((double) 1 / nearby.size());
return desired.subtract(npc.getEntity().getVelocity()).multiply(weight);
}
}