blob: 25505d63ed1871f03e2830e37923858247a61c10 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
namespace BehaviorDesigner.Runtime.Tasks
{
[TaskDescription("The selector evaluator is a selector task which reevaluates its children every tick. It will run the lowest priority child which returns a task status of running. " +
"This is done each tick. If a higher priority child is running and the next frame a lower priority child wants to run it will interrupt the higher priority child. " +
"The selector evaluator will return success as soon as the first child returns success otherwise it will keep trying higher priority children. This task mimics " +
"the conditional abort functionality except the child tasks don't always have to be conditional tasks.")]
[HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=109")]
[TaskIcon("{SkinColor}SelectorEvaluatorIcon.png")]
public class SelectorEvaluator : Composite
{
// The index of the child that is currently running or is about to run.
private int currentChildIndex = 0;
// The task status of the last child ran.
private TaskStatus executionStatus = TaskStatus.Inactive;
// The index of the child that was running before the tree started to be reevaluated.
private int storedCurrentChildIndex = -1;
// The task status of the last child ran before the tree started to be reevaluated.
private TaskStatus storedExecutionStatus = TaskStatus.Inactive;
public override int CurrentChildIndex()
{
return currentChildIndex;
}
public override void OnChildStarted(int childIndex)
{
// The children run sequentially so increment the index and set the status to running.
currentChildIndex++;
executionStatus = TaskStatus.Running;
}
public override bool CanExecute()
{
// We can continue to execuate as long as we have children that haven't been executed and no child has returned success.
if (executionStatus == TaskStatus.Success || executionStatus == TaskStatus.Running) {
return false;
}
// Used the storedCurrentChildIndex if reevaluating, otherwise the currentChildIndex
if (storedCurrentChildIndex != -1) {
return currentChildIndex < storedCurrentChildIndex - 1;
}
return currentChildIndex < children.Count;
}
public override void OnChildExecuted(int childIndex, TaskStatus childStatus)
{
// The child status will be inactive immediately following an abort from OnReevaluationEnded. The status will be running if the
// child task is interrupted. Ignore the status for both of these.
if (childStatus != TaskStatus.Inactive && childStatus != TaskStatus.Running) {
executionStatus = childStatus;
}
}
public override void OnConditionalAbort(int childIndex)
{
// Set the current child index to the index that caused the abort
currentChildIndex = childIndex;
executionStatus = TaskStatus.Inactive;
}
public override void OnEnd()
{
// All of the children have run. Reset the variables back to their starting values.
executionStatus = TaskStatus.Inactive;
currentChildIndex = 0;
}
public override TaskStatus OverrideStatus(TaskStatus status)
{
return executionStatus;
}
// The selector evaluator task is a parallel task to allow the previous child to continue to run while the higher priority task is active. If the
// lower priority child can run then OnReevaluationEnded will interrupt the higher priority task.
public override bool CanRunParallelChildren()
{
return true;
}
// Can reevaluate to allow the lower priority children the chance to rerun.
public override bool CanReevaluate()
{
return true;
}
// The behavior tree wants to start reevaluating the tree.
public override bool OnReevaluationStarted()
{
// Cannot reevaluate if the task hasn't even started yet
if (executionStatus == TaskStatus.Inactive) {
return false;
}
// Store the current index and execution status because it may need to be resumed.
storedCurrentChildIndex = currentChildIndex;
storedExecutionStatus = executionStatus;
currentChildIndex = 0;
executionStatus = TaskStatus.Inactive;
return true;
}
// Reevaluation has ended. Determine if a task should be interrupted or resumed from the last index.
public override void OnReevaluationEnded(TaskStatus status)
{
// Interrupt the currently running index if a lower priority child returns a status of running or success.
if (executionStatus != TaskStatus.Failure && executionStatus != TaskStatus.Inactive) {
BehaviorManager.instance.Interrupt(Owner, children[storedCurrentChildIndex - 1], this);
} else {
// The lower priority children returned the same status so resume with the current child
currentChildIndex = storedCurrentChildIndex;
executionStatus = storedExecutionStatus;
}
storedCurrentChildIndex = -1;
storedExecutionStatus = TaskStatus.Inactive;
}
}
}
|