Skip to content

Commit

Permalink
implements Poolable interface for stateful tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
mgsx-dev committed Jan 2, 2017
1 parent e9c65eb commit c74fa4e
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 1 deletion.
9 changes: 9 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/BehaviorTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ public void notifyChildAdded (Task<E> task, int index) {
listener.childAdded(task, index);
}
}

@Override
public void reset() {
removeListeners();
this.rootTask = null;
this.object = null;
this.listeners = null;
super.reset();
}

private static final class GuardEvaluator<E> extends Task<E> {

Expand Down
6 changes: 6 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/BranchTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,11 @@ protected Task<E> copyTo (Task<E> task) {

return task;
}

@Override
public void reset() {
children.clear();
super.reset();
}

}
6 changes: 6 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/Decorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,11 @@ protected Task<E> copyTo (Task<E> task) {

return task;
}

@Override
public void reset() {
child = null;
super.reset();
}

}
6 changes: 6 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/LoopDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@ public void childRunning (Task<E> runningTask, Task<E> reporter) {
super.childRunning(runningTask, reporter);
loop = false;
}

@Override
public void reset() {
loop = false;
super.reset();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,13 @@ protected Task<E>[] createRandomChildren () {
System.arraycopy(children.items, 0, rndChildren, 0, children.size);
return rndChildren;
}

@Override
public void reset() {
this.currentChildIndex = 0;
this.runningChild = null;
this.randomChildren = null;
super.reset();
}

}
11 changes: 10 additions & 1 deletion gdx-ai/src/com/badlogic/gdx/ai/btree/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.badlogic.gdx.ai.btree;

import com.badlogic.gdx.ai.btree.annotation.TaskConstraint;
import com.badlogic.gdx.utils.Pool.Poolable;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;

Expand All @@ -28,7 +29,7 @@
* @author implicit-invocation
* @author davebaol */
@TaskConstraint
public abstract class Task<E> {
public abstract class Task<E> implements Poolable {

/** The enumeration of the values that a task's status can have.
*
Expand Down Expand Up @@ -277,5 +278,13 @@ public Task<E> cloneTask () {
* @return the given task for chaining
* @throws TaskCloneException if the task cannot be successfully copied. */
protected abstract Task<E> copyTo (Task<E> task);

@Override
public void reset() {
control = null;
guard = null;
status = Status.FRESH;
tree = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ protected Task<E> copyTo (Task<E> task) {
return super.copyTo(task);
}

@Override
public void reset() {
runningChild = null;
super.reset();
}
}
9 changes: 9 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/branch/Parallel.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ protected Task<E> copyTo (Task<E> task) {

return super.copyTo(task);
}

@Override
public void reset() {
policy = Policy.Sequence;
noRunningTasks = true;
lastResult = null;
currentChildIndex = 0;
super.reset();
}

/** The enumeration of the policies supported by the {@link Parallel} task. */
public enum Policy {
Expand Down
7 changes: 7 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Include.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,11 @@ protected Task<E> copyTo (Task<E> task) {
private Task<E> createSubtreeRootTask () {
return BehaviorTreeLibraryManager.getInstance().createRootTask(subtree);
}

@Override
public void reset() {
lazy = false;
subtree = null;
super.reset();
}
}
6 changes: 6 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Random.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,10 @@ protected Task<E> copyTo (Task<E> task) {
return super.copyTo(task);
}

@Override
public void reset() {
this.p = 0;
this.success = ConstantFloatDistribution.ZERO_POINT_FIVE;
super.reset();
}
}
7 changes: 7 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Repeat.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,11 @@ protected Task<E> copyTo (Task<E> task) {

return super.copyTo(task);
}

@Override
public void reset() {
count = 0;
times = null;
super.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,12 @@ protected Task<E> copyTo (Task<E> task) {

return super.copyTo(task);
}

@Override
public void reset() {
name = null;
semaphore = null;
semaphoreAcquired = false;
super.reset();
}
}
8 changes: 8 additions & 0 deletions gdx-ai/src/com/badlogic/gdx/ai/btree/leaf/Wait.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@ protected Task<E> copyTo (Task<E> task) {
return task;
}

@Override
public void reset() {
seconds = ConstantFloatDistribution.ZERO;
startTime = 0;
timeout = 0;
super.reset();
}

}
7 changes: 7 additions & 0 deletions tests/src/com/badlogic/gdx/ai/tests/btree/dog/BarkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ protected Task<Dog> copyTo (Task<Dog> task) {
return task;
}

@Override
public void reset() {
times = ConstantIntegerDistribution.ONE;
t = 0;
super.reset();
}

}
6 changes: 6 additions & 0 deletions tests/src/com/badlogic/gdx/ai/tests/btree/dog/CareTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ protected Task<Dog> copyTo (Task<Dog> task) {
return task;
}

@Override
public void reset() {
urgentProb = 0.8f;
super.reset();
}

}
5 changes: 5 additions & 0 deletions tests/src/com/badlogic/gdx/ai/tests/btree/dog/MarkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ protected Task<Dog> copyTo (Task<Dog> task) {
return task;
}

@Override
public void reset() {
i = 0;
super.reset();
}
}
6 changes: 6 additions & 0 deletions tests/src/com/badlogic/gdx/ai/tests/btree/dog/WalkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ public void end () {
protected Task<Dog> copyTo (Task<Dog> task) {
return task;
}

@Override
public void reset() {
i = 0;
super.reset();
}

}

0 comments on commit c74fa4e

Please sign in to comment.