-
Notifications
You must be signed in to change notification settings - Fork 7
/
SequenceTreeNode.cs
67 lines (55 loc) · 2.12 KB
/
SequenceTreeNode.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GladBehaviour.Tree
{
/// <summary>
/// Implementation of the Behavior Tree sequence node which is a <see cref="CompositeTreeNode{TContextType}"/> that
/// runs the composed nodes in a sequential order.
/// </summary>
/// <typeparam name="TContextType">The type of the context.</typeparam>
public class SequenceTreeNode<TContextType> : CompositeTreeNode<TContextType>, IRunningEvaluatable<TContextType>
{
/// <inheritdoc />
public bool isRunningNode => EvaluationEnumerator.isRunningNode;
/// <inheritdoc />
public IContextEvaluable<TContextType> RunningNode => EvaluationEnumerator.RunningNode;
/// <summary>
/// The enumeration mediator for the composite evaluables
/// </summary>
private EvaluationEnumeratorMediator<TContextType> EvaluationEnumerator { get; }
public SequenceTreeNode(IEnumerable<TreeNode<TContextType>> nodes)
: this(nodes, new SingleContinueStateCompositeContinuationStrategy(GladBehaviorTreeNodeState.Success))
{
}
public SequenceTreeNode(params TreeNode<TContextType>[] nodes)
: this((IEnumerable<TreeNode<TContextType>>)nodes)
{
}
/// <summary>
/// Internal ctor that allows for overriding the default continue strategy.
/// </summary>
/// <param name="nodes"></param>
/// <param name="continueStrategy"></param>
internal SequenceTreeNode(IEnumerable<TreeNode<TContextType>> nodes, ICompositeContinuationStrategy continueStrategy)
: base(nodes)
{
if(continueStrategy == null) throw new ArgumentNullException(nameof(continueStrategy));
EvaluationEnumerator = new EvaluationEnumeratorMediator<TContextType>(CompositionNodes.GetEnumerator(), continueStrategy);
}
/// <inheritdoc />
protected override GladBehaviorTreeNodeState OnEvaluate(TContextType context)
{
if(context == null) throw new ArgumentNullException(nameof(context));
return EvaluationEnumerator.Evaluate(context);
}
/// <inheritdoc />
public override void Reset()
{
//Propagate the reset to the enumerator
EvaluationEnumerator.OnReset?.Invoke();
}
}
}