Skip to content

Commit 648b813

Browse files
committed
Finished conversion to Rx on behaviors
1 parent 5df47d4 commit 648b813

File tree

7 files changed

+150
-115
lines changed

7 files changed

+150
-115
lines changed

Assets/Scripts/AI/BehaviorTreeElement.cs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,10 @@
99
namespace Assets.Scripts.AI
1010
{
1111
[Serializable]
12-
public class BehaviorTreeElement : TreeElement
12+
public class BehaviorTreeElement : TreeElement, IDisposable
1313
{
14-
private string _ElementType;
15-
public string ElementType
16-
{
17-
get
18-
{
19-
return _ElementType;
20-
}
21-
22-
set
23-
{
24-
_ElementType = value;
25-
}
26-
}
14+
protected static readonly UniRx.Diagnostics.Logger BehaviorLogger = new UniRx.Diagnostics.Logger("Behavior Debugger");
15+
public string ElementType { get; set; }
2716

2817
[Newtonsoft.Json.JsonIgnore]
2918
[SerializeField]
@@ -72,7 +61,9 @@ public virtual void Initialize()
7261
foreach(var ch in allChildrenToRun)
7362
{
7463
//TODO: will be changed to an actual debugger instead of just unity logs. Issue #3
75-
ch.ObserveEveryValueChanged(x => x.CurrentState).Subscribe(x => Debug.Log(ElementType + " state changed: " + x));
64+
ch.ObserveEveryValueChanged(x => x.CurrentState)
65+
.Subscribe(x => BehaviorLogger.Log(ElementType + " state changed: " + x))
66+
.AddTo(Disposables);
7667
}
7768

7869
Initialized = true;
@@ -103,5 +94,37 @@ public override string ToString()
10394
return retString;
10495

10596
}
97+
98+
#region IDisposable Support
99+
100+
// CompositeDisposable is similar with List<IDisposable>, manage multiple IDisposable
101+
protected CompositeDisposable Disposables = new CompositeDisposable(); // field
102+
private bool disposedValue = false; // To detect redundant calls
103+
104+
protected virtual void Dispose(bool disposing)
105+
{
106+
if (!disposedValue)
107+
{
108+
if (disposing)
109+
{
110+
Disposables.Clear();
111+
}
112+
113+
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
114+
// TODO: set large fields to null.
115+
Children.Clear();
116+
Children = null;
117+
118+
disposedValue = true;
119+
}
120+
}
121+
122+
// This code added to correctly implement the disposable pattern.
123+
public void Dispose()
124+
{
125+
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
126+
Dispose(true);
127+
}
128+
#endregion
106129
}
107130
}

Assets/Scripts/AI/Components/BehaviorComponent.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public BehaviorComponent(string name, int depth, int id)
1717
public virtual void AddChild(BehaviorTreeElement element)
1818
{
1919
element.Parent = this;
20-
21-
element.BehaviorTreeManager = BehaviorTreeManager;
2220
Children.Add(element);
2321
}
2422
}

Assets/Scripts/AI/Components/ParallelRunner.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace Assets.Scripts.AI.Components
1010
{
1111
[Serializable]
12-
[Description("Runs all children at the same time.")]
12+
[Description("Runs all children at same time. Fails if NumFailures are >0 and children failed reach that number. Succeeds otherwise.")]
1313
public class ParallelRunner : BehaviorComponent
1414
{
15-
private static readonly UniRx.Diagnostics.Logger logger = new UniRx.Diagnostics.Logger("RunnerDebug");
15+
1616
/// <summary>
1717
/// Number of times the children return fail before the parallel runner returns in a fail state.
1818
/// 0 means ignore number of failures.
@@ -37,16 +37,16 @@ public override IEnumerator Tick(WaitForSeconds delayStart = null)
3737
//Initialize and start tick
3838
base.Tick(delayStart).ToObservable().Subscribe(xb => Debug.Log("Subscribed to ParallelRunner at start (base.tick()"));
3939
CurrentState = (BehaviorState.Running);
40-
if (Children == null || Children.Count <= 0)
40+
if (Children == null || Children.Count == 0)
4141
{
4242
Debug.LogWarning("Children Null in parallel runner");
43-
CurrentState = (BehaviorState.Null);
43+
CurrentState = (BehaviorState.Fail);
4444
yield break;
4545
}
4646

4747
foreach(var ch in Children)
4848
{
49-
((BehaviorTreeElement)ch).Tick().ToObservable().Debug().Subscribe(_ =>
49+
((BehaviorTreeElement)ch).Tick().ToObservable().Subscribe(_ =>
5050
{
5151
Debug.Log("parallel? Num Succeed: " + NumberOfSuccesses.Value);
5252
Debug.Log("parallel? Num Fail: " + NumberOfFailures.Value);
@@ -63,6 +63,7 @@ public override IEnumerator Tick(WaitForSeconds delayStart = null)
6363
}
6464
});
6565
}
66+
CurrentState = BehaviorState.Success;
6667
}
6768

6869
public override void Initialize()
@@ -77,8 +78,7 @@ public override void Initialize()
7778
//TODO: will be changed to an actual debugger instead of just unity logs. Issue #3
7879
ch.ObserveEveryValueChanged(x => x.CurrentState).Subscribe(x =>
7980
{
80-
81-
logger.Warning(ElementType + " state changed: " + x);
81+
BehaviorLogger.Warning(ElementType + " state changed: " + x);
8282
if (x == BehaviorState.Fail)
8383
{
8484
NumberOfFailures.SetValueAndForceNotify(NumberOfFailures.Value + 1);
@@ -87,7 +87,7 @@ public override void Initialize()
8787
{
8888
NumberOfSuccesses.SetValueAndForceNotify(NumberOfSuccesses.Value + 1);
8989
}
90-
});
90+
}).AddTo(Disposables);
9191
}
9292

9393
Initialized = true;

Assets/Scripts/AI/Components/Selector.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Assets.Scripts.AI.Components
1212
/// Returns and is in success state if a child was successful, otherwise returns in fail state
1313
/// </summary>
1414
[Serializable]
15-
[Description("Runs children in order. Succeeds on first child that succeeds.")]
15+
[Description("Runs children in order. Succeeds on first child that succeeds. Fails if no children succeed.")]
1616
public class Selector : BehaviorComponent
1717
{
1818
public Selector(string name, int depth, int id)
@@ -36,7 +36,6 @@ public override IEnumerator Tick(WaitForSeconds delayStart = null)
3636
return;
3737
}
3838
});
39-
4039
}
4140
//if it gets here, it went through all subbehaviors and had no successes
4241
CurrentState = BehaviorState.Fail;

Assets/Scripts/AI/Components/Sequencer.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections;
2+
using System.ComponentModel;
3+
using UniRx;
24
using UnityEngine;
35

46
namespace Assets.Scripts.AI.Components
@@ -7,6 +9,7 @@ namespace Assets.Scripts.AI.Components
79
/// Runs until a child returns in a fail state
810
/// </summary>
911
[System.Serializable]
12+
[Description("Continually runs children in sequence until a child fails. Succeeds if no children fail. Fails if any child fails.")]
1013
public class Sequencer : BehaviorComponent
1114
{
1215
public Sequencer(string name, int depth, int id)
@@ -17,23 +20,21 @@ public Sequencer(string name, int depth, int id)
1720
public override IEnumerator Tick(WaitForSeconds delayStart = null)
1821
{
1922
yield return delayStart;
20-
CurrentState = BehaviorState.Running;
23+
CurrentState = (BehaviorState.Running);
2124
foreach (BehaviorTreeElement behavior in Children)
2225
{
23-
yield return BehaviorTreeManager.StartCoroutine(behavior.Tick());
26+
if (CurrentState != BehaviorState.Running) yield break;
2427

25-
if (behavior.CurrentState != BehaviorState.Success)
28+
yield return behavior.Tick().ToObservable().Subscribe(_ =>
2629
{
27-
this.CurrentState = behavior.CurrentState;
28-
29-
if (this.CurrentState == BehaviorState.Fail)
30+
if (behavior.CurrentState == BehaviorState.Fail)
3031
{
31-
//This selector has completed, break out of the operation
32-
yield break;
32+
this.CurrentState = BehaviorState.Fail;
33+
return;
3334
}
34-
}
35+
}).AddTo(Disposables);
3536
}
36-
//if it gets here, it went through all subbehaviors and had no fails
37+
//if it gets here, it went through all subbehaviors and had no failures
3738
CurrentState = BehaviorState.Success;
3839
yield break;
3940
}

Assets/Scripts/AI/Decorators/Inverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public Inverter(string name, int depth, int id)
1313

1414
public override IEnumerator Tick(WaitForSeconds delayStart = null)
1515
{
16-
base.Tick(delayStart).ToObservable().Subscribe(xb => Debug.Log("Subscribed to ParallelRunner at start (base.tick()"));
16+
base.Tick(delayStart).ToObservable().Subscribe(xb => Debug.Log("OnNext Inverter at start (base.tick()"));
1717

1818
CurrentState = BehaviorState.Null;
1919
if (Children == null) yield return null;
@@ -38,7 +38,7 @@ public override IEnumerator Tick(WaitForSeconds delayStart = null)
3838
Debug.LogError("Something went wrong in an inverter.");
3939
break;
4040
}
41-
});
41+
}).AddTo(Disposables);
4242
}
4343
}
4444
}

0 commit comments

Comments
 (0)