mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-03 23:17:48 +01:00
Changed ClosestEntityTarget
This commit is contained in:
parent
ac809c4ea6
commit
bcca8a67d1
@ -9,7 +9,7 @@ import net.minestom.server.entity.EntityCreature;
|
||||
*/
|
||||
public abstract class TargetSelector {
|
||||
|
||||
private final EntityCreature entityCreature;
|
||||
protected final EntityCreature entityCreature;
|
||||
|
||||
public TargetSelector(EntityCreature entityCreature) {
|
||||
this.entityCreature = entityCreature;
|
||||
|
@ -0,0 +1,98 @@
|
||||
package net.minestom.server.entity.ai.target;
|
||||
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.EntityCreature;
|
||||
import net.minestom.server.entity.LivingEntity;
|
||||
import net.minestom.server.entity.ai.TargetSelector;
|
||||
import net.minestom.server.instance.Chunk;
|
||||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ClosestEntityTarget extends TargetSelector {
|
||||
|
||||
private float range;
|
||||
private Class<? extends LivingEntity>[] entitiesTarget;
|
||||
|
||||
public ClosestEntityTarget(EntityCreature entityCreature, float range,
|
||||
Class<? extends LivingEntity>... entitiesTarget) {
|
||||
super(entityCreature);
|
||||
this.range = range;
|
||||
this.entitiesTarget = entitiesTarget;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity findTarget() {
|
||||
final Instance instance = getEntityCreature().getInstance();
|
||||
final Chunk currentChunk = instance.getChunkAt(entityCreature.getPosition());
|
||||
final List<Chunk> chunks = getNeighbours(instance, currentChunk.getChunkX(), currentChunk.getChunkZ());
|
||||
|
||||
|
||||
Entity entity = null;
|
||||
float distance = Float.MAX_VALUE;
|
||||
|
||||
for (Chunk chunk : chunks) {
|
||||
final Set<Entity> entities = instance.getChunkEntities(chunk);
|
||||
|
||||
if (!(entities instanceof LivingEntity))
|
||||
continue;
|
||||
|
||||
for (Entity ent : entities) {
|
||||
|
||||
// Don't target itself
|
||||
if (ent.equals(entityCreature)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if the entity type can be targeted
|
||||
final Class<? extends Entity> clazz = ent.getClass();
|
||||
boolean correct = false;
|
||||
for (Class<? extends LivingEntity> targetClass : entitiesTarget) {
|
||||
if (targetClass.isAssignableFrom(clazz)) {
|
||||
correct = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!correct) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check distance
|
||||
final float d = getEntityCreature().getDistance(ent);
|
||||
if ((entity == null || d < distance) && d < range) {
|
||||
entity = ent;
|
||||
distance = d;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
private List<Chunk> getNeighbours(Instance instance, int chunkX, int chunkZ) {
|
||||
List<Chunk> chunks = new ArrayList<>();
|
||||
// Constants used to loop through the neighbors
|
||||
final int[] posX = {1, 0, -1};
|
||||
final int[] posZ = {1, 0, -1};
|
||||
|
||||
for (int x : posX) {
|
||||
for (int z : posZ) {
|
||||
|
||||
final int targetX = chunkX + x;
|
||||
final int targetZ = chunkZ + z;
|
||||
final Chunk chunk = instance.getChunk(targetX, targetZ);
|
||||
if (!ChunkUtils.isChunkUnloaded(chunk)) {
|
||||
// Chunk is loaded, add it
|
||||
chunks.add(chunk);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package net.minestom.server.entity.ai.target;
|
||||
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.EntityCreature;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.entity.ai.TargetSelector;
|
||||
import net.minestom.server.instance.Instance;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class PlayerTarget extends TargetSelector {
|
||||
|
||||
private float range;
|
||||
|
||||
public PlayerTarget(EntityCreature entityCreature, float range) {
|
||||
super(entityCreature);
|
||||
this.range = range;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity findTarget() {
|
||||
final Instance instance = getEntityCreature().getInstance();
|
||||
final Set<Player> players = instance.getPlayers();
|
||||
|
||||
Player player = null;
|
||||
{
|
||||
float distance = Float.MAX_VALUE;
|
||||
for (Player p : players) {
|
||||
final float d = getEntityCreature().getDistance(p);
|
||||
if ((player == null || d < distance) && d < range) {
|
||||
player = p;
|
||||
distance = d;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return player;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user