-
Notifications
You must be signed in to change notification settings - Fork 195
/
ObservingDecorator.cs
117 lines (103 loc) · 3.85 KB
/
ObservingDecorator.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
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
using UnityEngine;
using System.Collections;
using UnityEngine.Assertions;
namespace NPBehave
{
public abstract class ObservingDecorator : Decorator
{
protected Stops stopsOnChange;
private bool isObserving;
public ObservingDecorator(string name, Stops stopsOnChange, Node decoratee) : base(name, decoratee)
{
this.stopsOnChange = stopsOnChange;
this.isObserving = false;
}
protected override void DoStart()
{
if (stopsOnChange != Stops.NONE)
{
if (!isObserving)
{
isObserving = true;
StartObserving();
}
}
if (!IsConditionMet())
{
Stopped(false);
}
else
{
Decoratee.Start();
}
}
override protected void DoStop()
{
Decoratee.Stop();
}
protected override void DoChildStopped(Node child, bool result)
{
Assert.AreNotEqual(this.CurrentState, State.INACTIVE);
if (stopsOnChange == Stops.NONE || stopsOnChange == Stops.SELF)
{
if (isObserving)
{
isObserving = false;
StopObserving();
}
}
Stopped(result);
}
override protected void DoParentCompositeStopped(Composite parentComposite)
{
if (isObserving)
{
isObserving = false;
StopObserving();
}
}
protected void Evaluate()
{
if (IsActive && !IsConditionMet())
{
if (stopsOnChange == Stops.SELF || stopsOnChange == Stops.BOTH || stopsOnChange == Stops.IMMEDIATE_RESTART)
{
// Debug.Log( this.key + " stopped self ");
this.Stop();
}
}
else if (!IsActive && IsConditionMet())
{
if (stopsOnChange == Stops.LOWER_PRIORITY || stopsOnChange == Stops.BOTH || stopsOnChange == Stops.IMMEDIATE_RESTART || stopsOnChange == Stops.LOWER_PRIORITY_IMMEDIATE_RESTART)
{
// Debug.Log( this.key + " stopped other ");
Container parentNode = this.ParentNode;
Node childNode = this;
while (parentNode != null && !(parentNode is Composite))
{
childNode = parentNode;
parentNode = parentNode.ParentNode;
}
Assert.IsNotNull(parentNode, "NTBtrStops is only valid when attached to a parent composite");
Assert.IsNotNull(childNode);
if (parentNode is Parallel)
{
Assert.IsTrue(stopsOnChange == Stops.IMMEDIATE_RESTART, "On Parallel Nodes all children have the same priority, thus Stops.LOWER_PRIORITY or Stops.BOTH are unsupported in this context!");
}
if (stopsOnChange == Stops.IMMEDIATE_RESTART || stopsOnChange == Stops.LOWER_PRIORITY_IMMEDIATE_RESTART)
{
if (isObserving)
{
isObserving = false;
StopObserving();
}
}
((Composite)parentNode).StopLowerPriorityChildrenForChild(childNode, stopsOnChange == Stops.IMMEDIATE_RESTART || stopsOnChange == Stops.LOWER_PRIORITY_IMMEDIATE_RESTART);
}
}
}
protected abstract void StartObserving();
protected abstract void StopObserving();
protected abstract bool IsConditionMet();
}
}