mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-27 06:03:01 +02:00
EntityAIGroupBuilder
This commit is contained in:
parent
3bba4ea704
commit
1115b829a1
@ -44,6 +44,15 @@ public interface EntityAI {
|
|||||||
addAIGroup(group);
|
addAIGroup(group);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new {@link EntityAIGroup} builder.
|
||||||
|
*
|
||||||
|
* @return a builder to create and add an {@link EntityAIGroup}.
|
||||||
|
*/
|
||||||
|
default EntityAIGroupBuilder newAIGroupBuilder() {
|
||||||
|
return new EntityAIGroupBuilder(this);
|
||||||
|
}
|
||||||
|
|
||||||
default void aiTick(long time) {
|
default void aiTick(long time) {
|
||||||
getAIGroups().forEach(group -> group.tick(time));
|
getAIGroups().forEach(group -> group.tick(time));
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package net.minestom.server.entity.ai;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EntityAIGroupBuilder {
|
||||||
|
|
||||||
|
private final EntityAI ai;
|
||||||
|
private final List<GoalSelector> goalSelectors = new ArrayList<>();
|
||||||
|
private final List<TargetSelector> targetSelectors = new ArrayList<>();
|
||||||
|
|
||||||
|
EntityAIGroupBuilder(EntityAI ai) {
|
||||||
|
this.ai = ai;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds {@link GoalSelector} to the list of goal selectors of the building {@link EntityAIGroup}.
|
||||||
|
* Addition order is also a priority: priority the higher the earlier selector was added.
|
||||||
|
*
|
||||||
|
* @param goalSelector goal selector to be added.
|
||||||
|
* @return this builder.
|
||||||
|
*/
|
||||||
|
public EntityAIGroupBuilder addGoalSelector(GoalSelector goalSelector) {
|
||||||
|
this.goalSelectors.add(goalSelector);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds {@link TargetSelector} to the list of target selectors of the building {@link EntityAIGroup}.
|
||||||
|
* Addition order is also a priority: priority the higher the earlier selector was added.
|
||||||
|
*
|
||||||
|
* @param targetSelector target selector to be added.
|
||||||
|
* @return this builder.
|
||||||
|
*/
|
||||||
|
public EntityAIGroupBuilder addTargetSelector(TargetSelector targetSelector) {
|
||||||
|
this.targetSelectors.add(targetSelector);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new {@link EntityAIGroup} and adds it to the owning {@link EntityAI} of this builder.
|
||||||
|
*/
|
||||||
|
public void build() {
|
||||||
|
this.ai.addAIGroup(this.goalSelectors, this.targetSelectors);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,12 +3,17 @@ package demo.entity;
|
|||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import net.minestom.server.attribute.Attributes;
|
import net.minestom.server.attribute.Attributes;
|
||||||
import net.minestom.server.entity.LivingEntity;
|
import net.minestom.server.entity.LivingEntity;
|
||||||
|
import net.minestom.server.entity.ai.goal.DoNothingGoal;
|
||||||
|
import net.minestom.server.entity.ai.goal.MeleeAttackGoal;
|
||||||
import net.minestom.server.entity.ai.goal.RandomStrollGoal;
|
import net.minestom.server.entity.ai.goal.RandomStrollGoal;
|
||||||
|
import net.minestom.server.entity.ai.target.ClosestEntityTarget;
|
||||||
|
import net.minestom.server.entity.ai.target.LastEntityDamagerTarget;
|
||||||
import net.minestom.server.entity.damage.DamageType;
|
import net.minestom.server.entity.damage.DamageType;
|
||||||
import net.minestom.server.entity.type.animal.EntityChicken;
|
import net.minestom.server.entity.type.animal.EntityChicken;
|
||||||
import net.minestom.server.event.entity.EntityAttackEvent;
|
import net.minestom.server.event.entity.EntityAttackEvent;
|
||||||
import net.minestom.server.utils.Position;
|
import net.minestom.server.utils.Position;
|
||||||
import net.minestom.server.utils.Vector;
|
import net.minestom.server.utils.Vector;
|
||||||
|
import net.minestom.server.utils.time.TimeUnit;
|
||||||
|
|
||||||
public class ChickenCreature extends EntityChicken {
|
public class ChickenCreature extends EntityChicken {
|
||||||
|
|
||||||
@ -27,6 +32,15 @@ public class ChickenCreature extends EntityChicken {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Another way to register previously added EntityAIGroup, using specialized builder:
|
||||||
|
// newAIGroupBuilder()
|
||||||
|
// .addGoalSelector(new DoNothingGoal(this, 500, .1F))
|
||||||
|
// .addGoalSelector(new MeleeAttackGoal(this, 500, 2, TimeUnit.MILLISECOND))
|
||||||
|
// .addGoalSelector(new RandomStrollGoal(this, 2))
|
||||||
|
// .addTargetSelector(new LastEntityDamagerTarget(this, 15))
|
||||||
|
// .addTargetSelector(new ClosestEntityTarget(this, 15, LivingEntity.class))
|
||||||
|
// .build();
|
||||||
|
|
||||||
getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.1f);
|
getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.1f);
|
||||||
|
|
||||||
addEventCallback(EntityAttackEvent.class, event -> {
|
addEventCallback(EntityAttackEvent.class, event -> {
|
||||||
|
@ -11,11 +11,8 @@ public class ZombieCreature extends EntityZombie {
|
|||||||
|
|
||||||
public ZombieCreature(Position spawnPosition) {
|
public ZombieCreature(Position spawnPosition) {
|
||||||
super(spawnPosition);
|
super(spawnPosition);
|
||||||
addAIGroup(
|
newAIGroupBuilder()
|
||||||
ImmutableList.of(
|
.addGoalSelector(new RandomLookAroundGoal(this, 20))
|
||||||
new RandomLookAroundGoal(this, 20)
|
.build();
|
||||||
),
|
|
||||||
Collections.emptyList()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user