-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParallel.cs
More file actions
64 lines (52 loc) · 1.7 KB
/
Copy pathParallel.cs
File metadata and controls
64 lines (52 loc) · 1.7 KB
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
namespace GroveGames.BehaviourTree.Nodes.Composites;
public sealed class Parallel : Composite
{
private readonly ParallelPolicy _policy;
public Parallel(ParallelPolicy policy, string? name = null) : base(name)
{
_policy = policy;
}
public override NodeState Evaluate(float deltaTime)
{
var allSuccess = true;
var anyChildRunning = false;
foreach (var child in Children)
{
var status = child.Evaluate(deltaTime);
switch (status)
{
case NodeState.Success:
if (_policy == ParallelPolicy.AnySuccess)
{
return _nodeState = NodeState.Success;
}
break;
case NodeState.Running:
allSuccess = false;
anyChildRunning = true;
break;
case NodeState.Failure:
allSuccess = false;
if (_policy == ParallelPolicy.FirstFailure)
{
return _nodeState = NodeState.Failure;
}
break;
}
}
if (allSuccess && _policy == ParallelPolicy.AllSuccess)
{
return _nodeState = NodeState.Success;
}
return anyChildRunning ? _nodeState = NodeState.Running : _nodeState = NodeState.Failure;
}
}
public static partial class ParentExtensions
{
public static IParent Parallel(this IParent parent, ParallelPolicy policy, string? name = null)
{
var parallel = new Parallel(policy, name);
parent.Attach(parallel);
return parallel;
}
}