rename some variables and change interruption to also check shouldStart

This commit is contained in:
GreatWyrm 2025-02-03 11:45:33 -08:00
parent 5203225c8e
commit 22511d200f
5 changed files with 26 additions and 24 deletions

View File

@ -67,7 +67,7 @@ public class EntityCreature extends Entity implements PathWalker, EntityAI {
this.aiGroup = aiGroup; this.aiGroup = aiGroup;
// Update entity creatures in AI goal // Update entity creatures in AI goal
// TODO: Is this needed? // TODO: Is this needed?
for (AIGoal aiGoal : aiGroup.getGoalSelectors()) { for (AIGoal aiGoal : aiGroup.getAIGoals()) {
aiGoal.setEntityCreature(this); aiGoal.setEntityCreature(this);
} }
} }

View File

@ -79,9 +79,11 @@ public abstract class AIGoal implements Comparable<AIGoal> {
/** /**
* Checks to see if this goal should interrupt the current goal, given the entity creature's current state * Checks to see if this goal should interrupt the current goal, given the entity creature's current state
* <p></p>
* Note: {@link EntityAIGroup} will only interrupt the current goal with this one if {@link #shouldStart()} is true as well.
* @return true if this goal should interrupt the current one, false to continue as normal * @return true if this goal should interrupt the current one, false to continue as normal
*/ */
public boolean shouldInterrupt() { public boolean canInterrupt(@NotNull AIGoal currentGoal) {
return false; return false;
} }

View File

@ -26,7 +26,7 @@ public interface EntityAI {
void setAIGroup(@NotNull EntityAIGroup aiGroup); void setAIGroup(@NotNull EntityAIGroup aiGroup);
default void addGoals(@NotNull List<AIGoal> aiGoals) { default void addGoals(@NotNull List<AIGoal> aiGoals) {
getAIGroup().getGoalSelectors().addAll(aiGoals); getAIGroup().getAIGoals().addAll(aiGoals);
} }
default void aiTick(long time) { default void aiTick(long time) {

View File

@ -12,8 +12,8 @@ import java.util.List;
*/ */
public class EntityAIGroup { public class EntityAIGroup {
private AIGoal currentGoalSelector; private AIGoal currentAIGoal;
private final List<AIGoal> goalSelectors = new ArrayList<>(); private final List<AIGoal> aiGoals = new ArrayList<>();
/** /**
* Gets the goal selectors of this group. * Gets the goal selectors of this group.
@ -21,12 +21,12 @@ public class EntityAIGroup {
* @return an unmodifiable list containing this group's goal selectors * @return an unmodifiable list containing this group's goal selectors
*/ */
@NotNull @NotNull
public List<AIGoal> getGoalSelectors() { public List<AIGoal> getAIGoals() {
return Collections.unmodifiableList(goalSelectors); return Collections.unmodifiableList(aiGoals);
} }
public void addAIGoals(@NotNull List<AIGoal> goalSelectors) { public void addAIGoals(@NotNull List<AIGoal> goalSelectors) {
this.goalSelectors.addAll(goalSelectors); this.aiGoals.addAll(goalSelectors);
Collections.sort(goalSelectors); Collections.sort(goalSelectors);
} }
@ -36,24 +36,24 @@ public class EntityAIGroup {
* @return the current AI goal of this group, null if not any * @return the current AI goal of this group, null if not any
*/ */
@Nullable @Nullable
public AIGoal getCurrentGoalSelector() { public AIGoal getCurrentAIGoal() {
return this.currentGoalSelector; return this.currentAIGoal;
} }
public void tick(long time) { public void tick(long time) {
AIGoal currentGoalSelector = getCurrentGoalSelector(); AIGoal currentGoal = getCurrentAIGoal();
if (currentGoalSelector != null) { if (currentGoal != null) {
if (currentGoalSelector.shouldEnd()) { if (currentGoal.shouldEnd()) {
currentGoalSelector.end(); currentGoal.end();
this.currentGoalSelector = null; this.currentAIGoal = null;
} else { } else {
currentGoalSelector.tick(time); currentGoal.tick(time);
// Check for any goal interrupts // Check for any goal interrupts
for (var goal : goalSelectors) { for (var goal : aiGoals) {
if (goal.shouldInterrupt()) { if (goal.canInterrupt(currentGoal) && goal.shouldStart()) {
currentGoalSelector.end(); currentGoal.end();
this.currentGoalSelector = goal; this.currentAIGoal = goal;
goal.start(); goal.start();
break; break;
} }
@ -61,11 +61,11 @@ public class EntityAIGroup {
} }
} }
if (currentGoalSelector == null) { if (currentGoal == null) {
// We don't have a current goal, pick one with the highest priority // We don't have a current goal, pick one with the highest priority
for (var goal : goalSelectors) { for (var goal : aiGoals) {
if (goal.shouldStart()) { if (goal.shouldStart()) {
this.currentGoalSelector = goal; this.currentAIGoal = goal;
goal.start(); goal.start();
break; break;
} }

View File

@ -13,7 +13,7 @@ public class EntityAIGroupBuilder {
* @return this builder. * @return this builder.
*/ */
public EntityAIGroupBuilder addAIGoal(@NotNull AIGoal goalSelector) { public EntityAIGroupBuilder addAIGoal(@NotNull AIGoal goalSelector) {
this.group.getGoalSelectors().add(goalSelector); this.group.getAIGoals().add(goalSelector);
return this; return this;
} }