Skip to content

Commit

Permalink
Add Empty and IfElse
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 27, 2013
1 parent 9069411 commit 157f80a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/net/citizensnpcs/api/ai/tree/Decorator.java
Expand Up @@ -11,6 +11,9 @@
* A decorator is a wrapper over a {@link Behavior}, which can add functionality
* such as filtering {@link BehaviorStatus}es, conditions, timer loops and more
* without knowing the internals of the behavior it wraps.
*
* Note that there are often simpler alternatives to a full-blown decorator,
* which has to be generic for many different scenarios.
*/
public class Decorator extends BehaviorGoalAdapter {
private final Collection<Runnable> resetCallbacks;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/net/citizensnpcs/api/ai/tree/Empty.java
@@ -0,0 +1,22 @@
package net.citizensnpcs.api.ai.tree;

public class Empty extends BehaviorGoalAdapter {
private Empty() {
}

@Override
public void reset() {
}

@Override
public BehaviorStatus run() {
return null;
}

@Override
public boolean shouldExecute() {
return false;
}

public static Empty INSTANCE = new Empty();
}
45 changes: 45 additions & 0 deletions src/main/java/net/citizensnpcs/api/ai/tree/IfElse.java
@@ -0,0 +1,45 @@
package net.citizensnpcs.api.ai.tree;

public class IfElse extends BehaviorGoalAdapter {
private final Condition condition;
private Behavior executing;
private final Behavior ifBehavior, elseBehavior;

public IfElse(Condition condition, Behavior ifBehavior, Behavior elseBehavior) {
this.condition = condition;
this.ifBehavior = ifBehavior;
this.elseBehavior = elseBehavior;
}

@Override
public void reset() {
if (executing != null) {
executing.reset();
executing = null;
}
}

@Override
public BehaviorStatus run() {
return executing.run();
}

@Override
public boolean shouldExecute() {
boolean cond = condition.get();
if (cond) {
executing = ifBehavior;
} else {
executing = elseBehavior;
}
if (executing == null || !executing.shouldExecute()) {
executing = null;
return false;
}
return true;
}

public static IfElse create(Condition condition, Behavior ifBehavior, Behavior elseBehavior) {
return new IfElse(condition, ifBehavior, elseBehavior);
}
}
Expand Up @@ -54,7 +54,7 @@ public boolean shouldExecute() {
/**
* Creates a {@link Precondition} that returns either
* {@link BehaviorStatus#SUCCESS} or {@link BehaviorStatus#FAILURE}
* depending on the underlying {@link Condition}s return status.
* depending on the underlying {@link Condition}'s return status.
*
* @param condition
* The condition to check while executing
Expand Down

0 comments on commit 157f80a

Please sign in to comment.