mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-03 23:17:48 +01:00
Create RandomLookAroundGoal
This commit is contained in:
parent
4e38b3dfb3
commit
e44d5283b4
13
src/main/java/fr/themode/demo/entity/ZombieCreature.java
Normal file
13
src/main/java/fr/themode/demo/entity/ZombieCreature.java
Normal file
@ -0,0 +1,13 @@
|
||||
package fr.themode.demo.entity;
|
||||
|
||||
import net.minestom.server.entity.ai.goal.RandomLookAroundGoal;
|
||||
import net.minestom.server.entity.type.EntityZombie;
|
||||
import net.minestom.server.utils.Position;
|
||||
|
||||
public class ZombieCreature extends EntityZombie {
|
||||
|
||||
public ZombieCreature(Position spawnPosition) {
|
||||
super(spawnPosition);
|
||||
goalSelectors.add(new RandomLookAroundGoal(this, 20));
|
||||
}
|
||||
}
|
@ -1077,7 +1077,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||
/**
|
||||
* Change the entity eye height
|
||||
*
|
||||
* @param eyeHeight the entity eye eight
|
||||
* @param eyeHeight the entity eye height
|
||||
*/
|
||||
public void setEyeHeight(float eyeHeight) {
|
||||
this.eyeHeight = eyeHeight;
|
||||
|
@ -13,14 +13,14 @@ public class EatBlockGoal extends GoalSelector {
|
||||
private static final Random RANDOM = new Random();
|
||||
private final Map<Short, Short> eatBelowMap;
|
||||
private final Map<Short, Short> eatInMap;
|
||||
private int chancePerTick;
|
||||
private final int chancePerTick;
|
||||
private int eatAnimationTick;
|
||||
|
||||
/**
|
||||
* @param entityCreature Creature that should eat a block.
|
||||
* @param eatInMap Map containing the block IDs that the entity can eat (when inside the block) and the block ID of the replacement block.
|
||||
* @param eatInMap Map containing the block IDs that the entity can eat (when inside the block) and the block ID of the replacement block.
|
||||
* @param eatBelowMap Map containing block IDs that the entity can eat (when above the block) and the block ID of the replacement block.
|
||||
* @param chancePerTick The chance that the entity eats. Settings this to N would mean there is a 1 in N chance.
|
||||
* @param chancePerTick The chance (per tick) that the entity eats. Settings this to N would mean there is a 1 in N chance.
|
||||
*/
|
||||
public EatBlockGoal(
|
||||
@NotNull EntityCreature entityCreature,
|
||||
@ -40,8 +40,8 @@ public class EatBlockGoal extends GoalSelector {
|
||||
return false;
|
||||
}
|
||||
Instance instance = entityCreature.getInstance();
|
||||
final short blockIdIn = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0, 1,0));
|
||||
final short blockIdBelow = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0,2,0));
|
||||
final short blockIdIn = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0, 1, 0));
|
||||
final short blockIdBelow = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0, 2, 0));
|
||||
|
||||
return eatInMap.containsKey(blockIdIn) || eatBelowMap.containsKey(blockIdBelow);
|
||||
}
|
||||
|
@ -0,0 +1,84 @@
|
||||
package net.minestom.server.entity.ai.goal;
|
||||
|
||||
import net.minestom.server.entity.EntityCreature;
|
||||
import net.minestom.server.entity.ai.GoalSelector;
|
||||
import net.minestom.server.utils.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class RandomLookAroundGoal extends GoalSelector {
|
||||
private static final Random RANDOM = new Random();
|
||||
private final int chancePerTick;
|
||||
private final Supplier<Integer> minimalLookTimeSupplier;
|
||||
private final Function<EntityCreature, Vector> randomDirectionFunction;
|
||||
private Vector lookDirection;
|
||||
private int lookTime = 0;
|
||||
|
||||
public RandomLookAroundGoal(EntityCreature entityCreature, int chancePerTick) {
|
||||
this(entityCreature, chancePerTick,
|
||||
// These two functions act similarily enough to how MC randomly looks around.
|
||||
|
||||
// Look in one direction for at most 40 ticks and at minimum 20 ticks.
|
||||
() -> 20 + RANDOM.nextInt(20),
|
||||
// Look at a random block
|
||||
(creature) -> {
|
||||
final double n = Math.PI * 2 * RANDOM.nextDouble();
|
||||
return new Vector(
|
||||
(float) Math.cos(n),
|
||||
0,
|
||||
(float) Math.sin(n)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param entityCreature Creature that should randomly look around.
|
||||
* @param chancePerTick The chance (per tick) that the entity looks around. Setting this to N would mean there is a 1 in N chance.
|
||||
* @param minimalLookTimeSupplier A supplier that returns the minimal amount of time an entity looks in a direction.
|
||||
* @param randomDirectionFunction A function that returns a random vector that the entity will look in/at.
|
||||
*/
|
||||
public RandomLookAroundGoal(
|
||||
EntityCreature entityCreature,
|
||||
int chancePerTick,
|
||||
@NotNull Supplier<Integer> minimalLookTimeSupplier,
|
||||
@NotNull Function<EntityCreature, Vector> randomDirectionFunction
|
||||
) {
|
||||
super(entityCreature);
|
||||
this.chancePerTick = chancePerTick;
|
||||
this.minimalLookTimeSupplier = minimalLookTimeSupplier;
|
||||
this.randomDirectionFunction = randomDirectionFunction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldStart() {
|
||||
if (RANDOM.nextInt(chancePerTick) != 0) {
|
||||
return false;
|
||||
}
|
||||
return entityCreature.getPathPosition() == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
lookTime = minimalLookTimeSupplier.get();
|
||||
lookDirection = randomDirectionFunction.apply(entityCreature);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
--lookTime;
|
||||
entityCreature.setView(entityCreature.getPosition().clone().setDirection(lookDirection));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldEnd() {
|
||||
return this.lookTime < 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void end() {
|
||||
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ public class EntityZombie extends EntityCreature {
|
||||
public EntityZombie(Position spawnPosition) {
|
||||
super(EntityType.ZOMBIE, spawnPosition);
|
||||
setBoundingBox(0.6f, 1.95f, 0.6f);
|
||||
setEyeHeight(1.74f);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user