1- using System ;
2- using System . Collections . Generic ;
3- using Adnc . FluidBT . TaskParents ;
1+ using Adnc . FluidBT . TaskParents ;
42using Adnc . FluidBT . Tasks ;
3+ using UnityEngine ;
54
65namespace Adnc . FluidBT . Trees {
76 public class BehaviorTree {
8- private bool _setup ;
9-
7+ private readonly GameObject _owner ;
8+
9+ public int TickCount { get ; private set ; }
10+
1011 public TaskRoot Root { get ; } = new TaskRoot ( ) ;
1112
12- public ITask Current { get ; set ; }
13-
14- public readonly HashSet < object > nodes = new HashSet < object > ( ) ;
15-
16- public readonly List < IEventAwake > nodeAwake = new List < IEventAwake > ( ) ;
17-
18- public BehaviorTree ( ) {
19- Current = Root ;
20- nodes . Add ( Root ) ;
13+ public BehaviorTree ( GameObject owner ) {
14+ _owner = owner ;
15+ SyncNodes ( Root ) ;
2116 }
22-
23- public void AddNode ( ITaskParent parent , ITask child ) {
24- if ( parent == null ) {
25- throw new ArgumentNullException ( nameof ( parent ) ) ;
26- }
27-
28- if ( child == null ) {
29- throw new ArgumentNullException ( nameof ( child ) ) ;
30- }
31-
32- if ( ! nodes . Contains ( parent ) ) {
33- throw new ArgumentException ( "Cannot add a node to a parent that is not in the BT" ) ;
34-
17+
18+ public void Tick ( ) {
19+ if ( Root . Update ( ) != TaskStatus . Continue ) {
20+ TickCount ++ ;
3521 }
22+ }
3623
37- if ( nodes . Contains ( child ) ) {
38- throw new ArgumentException ( "Cannot set a child node that has already been added" ) ;
39- }
24+ public void Reset ( ) {
25+ TickCount ++ ;
26+ }
4027
28+ public void AddNode ( ITaskParent parent , ITask child ) {
4129 parent . AddChild ( child ) ;
42- nodes . Add ( child ) ;
43-
44- var item = child as IEventAwake ;
45- if ( item != null ) {
46- nodeAwake . Add ( item ) ;
47- }
30+ child . ParentTree = this ;
31+ child . Owner = _owner ;
4832 }
4933
50- public void Setup ( ) {
51- if ( ! _setup ) {
52- _setup = true ;
53- } else {
54- return ;
55- }
34+ public void Splice ( ITaskParent parent , BehaviorTree tree ) {
35+ parent . AddChild ( tree . Root ) ;
5636
57- nodeAwake . ForEach ( ( n ) => {
58- n . Awake ( ) ;
59- } ) ;
37+ SyncNodes ( tree . Root ) ;
6038 }
6139
62- public void Update ( ) {
63- Current . Update ( ) ;
40+ private void SyncNodes ( ITaskParent taskParent ) {
41+ taskParent . Owner = _owner ;
42+ taskParent . ParentTree = this ;
43+
44+ foreach ( var child in taskParent . Children ) {
45+ child . Owner = _owner ;
46+ child . ParentTree = this ;
47+
48+ var parent = child as ITaskParent ;
49+ if ( parent != null ) {
50+ SyncNodes ( parent ) ;
51+ }
52+ }
6453 }
6554 }
6655}
0 commit comments