Skip to content

Commit 82650b5

Browse files
committed
Working observer pattern test
1 parent 20a983c commit 82650b5

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

Assets/Scripts/AI/BehaviorManager.cs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class BehaviorManager : MonoBehaviour
2626
/// </summary>
2727
[SerializeField]
2828
[Description("Seconds between every tick. At 0 this will tick every frame")]
29-
public float SecondsBetweenTicks = 0.1f;
29+
public double SecondsBetweenTicks = 0.1f;
3030

3131
/// <summary>
3232
/// Number of times to tick the full trees. Set to a negative number to make an infinitely running behavior tree.
@@ -78,33 +78,10 @@ public void Reinitialize()
7878
/// <returns></returns>
7979
IEnumerator Start()
8080
{
81-
WaitForSeconds wfs = new WaitForSeconds(SecondsBetweenTicks);
82-
83-
Debug.Log("Starting ticks on Runner: \n\t" + Runner.ToString());
84-
var behaviors = Observable.EveryUpdate().DoOnSubscribe(() => Debug.Log("Subscribed!!!")).Subscribe().AddTo(this);
85-
86-
87-
Runner.ObserveEveryValueChanged(x => x.CurrentState).Subscribe(x => Debug.Log(x));
88-
89-
90-
while(TimesToTick > 0)
91-
{
92-
Observable.FromCoroutine(() => Runner.Tick()).Subscribe(xr => Debug.Log("Subscribed to " + xr), xd => Debug.Log("Destroyed " + xd)).AddTo(this);
93-
--TimesToTick;
94-
yield return wfs;
95-
}
96-
97-
98-
99-
yield return null;
100-
101-
//yield return StartCoroutine(Runner.Tick(wfs));
102-
//if (TimesToTick > 0) --TimesToTick;
103-
104-
105-
106-
//Debug.Log("All Coroutines Should be DONE now! Ending all to make sure....");
107-
StopAllCoroutines();
81+
while(TimesToTick-- > 0)
82+
yield return Runner.Tick()
83+
.ToObservable()
84+
.Subscribe(xr => Debug.Log("OnNxt called " + xr), xd => Debug.Log("Destroyed " + xd)).AddTo(this);
10885
}
10986

11087
/// <summary>

Assets/Scripts/AI/BehaviorTreeElement.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using UniRx;
67
using UnityEngine;
78

@@ -53,14 +54,30 @@ public BehaviorTreeElement(string name, int depth, int id)
5354
[Newtonsoft.Json.JsonIgnore]
5455
public BehaviorState CurrentState;
5556

57+
public bool Initialized = false;
5658
public virtual IEnumerator Tick(WaitForSeconds delayStart = null)
5759
{
60+
if (!Initialized) Initialize();
5861
if (delayStart != null)
5962
{
6063
yield return delayStart;
6164
}
6265
}
6366

67+
public virtual void Initialize()
68+
{
69+
var allChildrenToRun = from x in Children
70+
select x as BehaviorTreeElement;
71+
72+
foreach(var ch in allChildrenToRun)
73+
{
74+
ch.ObserveEveryValueChanged(x => x.CurrentState).Subscribe(x => Debug.Log(ElementType + " state changed: " + x));
75+
}
76+
77+
Initialized = true;
78+
}
79+
80+
6481
public override string ToString()
6582
{
6683
var depthPad = "";

Assets/Scripts/AI/BehaviorTreeManagerAsset.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Assets.Scripts.AI
1212
public class BehaviorTreeManagerAsset : ScriptableObject
1313
{
1414
public int TimesToTick;
15-
public float SecondsBetweenTicks = 0.1f;
15+
public double SecondsBetweenTicks = 10;
1616

1717
public string RunnerElementsJSON;
1818
}

Assets/Scripts/AI/Components/ParallelRunner.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.ComponentModel;
4+
using UniRx;
45
using UnityEngine;
56

67
namespace Assets.Scripts.AI.Components
@@ -30,7 +31,7 @@ public ParallelRunner(string name, int depth, int id)
3031

3132
public override IEnumerator Tick(WaitForSeconds delayStart = null)
3233
{
33-
yield return delayStart;
34+
base.Tick().ToObservable().Subscribe(xb => Debug.Log("Subscribed to ParallelRunner at start (base.tick()"));
3435
CurrentState = (BehaviorState.Running);
3536
if (Children == null || Children.Count <=0)
3637
{

Assets/Scripts/AI/Components/Selector.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Collections;
33
using System.ComponentModel;
4+
using System.Linq;
5+
using UniRx;
6+
using UnityEngine;
47

58
namespace Assets.Scripts.AI.Components
69
{
@@ -18,9 +21,9 @@ public Selector(string name, int depth, int id)
1821

1922
public override IEnumerator Tick(UnityEngine.WaitForSeconds delayStart = null)
2023
{
21-
base.Tick();
22-
UnityEngine.Debug.LogError("Selector START");
23-
24+
base.Tick().ToObservable().Subscribe(xb => Debug.Log("Subscribed to Selector at start (base.tick()"));
25+
26+
2427
CurrentState = (BehaviorState.Running);
2528
foreach (BehaviorTreeElement behavior in Children)
2629
{
@@ -37,12 +40,11 @@ public override IEnumerator Tick(UnityEngine.WaitForSeconds delayStart = null)
3740
yield break;
3841
}
3942
}
40-
UnityEngine.Debug.LogError("Selector is fail");
43+
Debug.LogError("Selector is fail");
4144
}
4245
//if it gets here, it went through all subbehaviors and had no successes
4346
CurrentState = BehaviorState.Fail;
4447
yield break;
4548
}
46-
4749
}
4850
}

0 commit comments

Comments
 (0)