Vector3f drive = new Vector3f();
// find player position
// TODO: shouldn't use local player, need some way to find nearest
// player
LocalPlayer localPlayer = CoreRegistry.get(LocalPlayer.class);
if (localPlayer != null) {
Vector3f dist = new Vector3f(worldPos);
dist.sub(localPlayer.getPosition());
double distanceToPlayer = dist.lengthSquared();
ai.inDanger = false;
if (ai.dieIfPlayerFar && distanceToPlayer > ai.dieDistance) {
entity.destroy();
}
//----------------danger behavior----------
// if our AI is aggressive or hunter go and hunt player else run away
// if wild
if (ai.aggressive) {
// TODO fix this to proper attacking
if (distanceToPlayer <= ai.attackDistance) {
if (tempTime - lastAttack > ai.damageFrequency) {
localPlayer.getCharacterEntity().send(
new DoDamageEvent(ai.damage, EngineDamageTypes.PHYSICAL.get(), entity));
lastAttack = CoreRegistry.get(Time.class).getGameTimeInMs();
}
}
}
//update
if (tempTime - ai.lastChangeOfDangerAt > dangerChangeTime) {
dangerChangeTime = (long) (ai.dangerUpdateTime * random.nextDouble() * ai.hectic);
if (ai.hunter) {
if (distanceToPlayer > ai.playerdistance
&& distanceToPlayer < ai.playerSense) {
// Head to player
Vector3f tempTarget = localPlayer.getPosition();
if (ai.forgiving != 0) {
ai.movementTarget.set(new Vector3f(
tempTarget.x + random.nextFloat(-ai.forgiving, ai.forgiving),
tempTarget.y + random.nextFloat(-ai.forgiving, ai.forgiving),
tempTarget.z + random.nextFloat(-ai.forgiving, ai.forgiving)
));
} else {
ai.movementTarget.set(tempTarget);
}
ai.inDanger = true;
entity.saveComponent(ai);
// System.out.print("\nhunting palyer\n");
}
}
// run opposite direction
if (ai.wild) {
if (distanceToPlayer > ai.panicDistance
&& distanceToPlayer < ai.runDistance) {
Vector3f tempTarget = localPlayer.getPosition();
if (ai.forgiving != 0) {
ai.movementTarget.set(new Vector3f(
-tempTarget.x + random.nextFloat(-ai.forgiving, ai.forgiving),
-tempTarget.y + random.nextFloat(-ai.forgiving, ai.forgiving),
-tempTarget.z + random.nextFloat(-ai.forgiving, ai.forgiving)