forked from Seneral/Node_Editor_Framework
/
BTreeManager.cs
171 lines (147 loc) · 4.55 KB
/
BTreeManager.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using NodeEditorFramework;
using NodeEditorFramework.Utilities;
using System.Linq;
using BehaviourTrees;
using System;
using UniRx;
/// <summary>
/// NodeEditorで作成したScriptableObjectからBehaviourTreeを構築し、実行する
/// </summary>
public class BTreeManager : MonoBehaviour {
[SerializeField, FilePath]
string filePath;
BehaviourTreeInstance node;
RuntimeNodeEditor runtimeNodeEditor;
Action m_task;
uBehaviourTreeNode m_topNode;
BehaviourTreeBase m_bTreeBase;
void Awake(){
runtimeNodeEditor = GetComponent<RuntimeNodeEditor>();
if(runtimeNodeEditor!=null){
runtimeNodeEditor.canvasPath = filePath.Replace (Application.dataPath, "Assets");
}
}
// Use this for initialization
void Start () {
m_task = ChangeColorTask;
var path = filePath.Replace (Application.dataPath, "Assets");
// Load the NodeCanvas
var canvas = NodeEditorSaveManager.LoadNodeCanvas (path);
var startNode = FindStartNode(canvas);
if(startNode==null || startNode.Outputs[0].connections==null){
return;
}
m_topNode= startNode.Outputs[0].connections[0].body as uBehaviourTreeNode;
CreateNodes(m_topNode);
}
void Update(){
if(m_task!=null){
m_task();
}
}
/// <summary>
/// Creates the nodes.
/// </summary>
/// <param name="topNode">Top node.</param>
void CreateNodes(uBehaviourTreeNode topNode){
m_bTreeBase = CreateBehaviourTreeBase(topNode);
node = new BehaviourTreeInstance(m_bTreeBase);
node.finishRP.Where(p=>p!=BehaviourTreeInstance.NodeState.READY).Subscribe(p=>ResetCoroutineStart());
node.finishRP.Value = BehaviourTreeInstance.NodeState.READY;
node.Excute();
}
/// <summary>
/// Changes the color task.
/// </summary>
void ChangeColorTask(){
if(runtimeNodeEditor!=null && runtimeNodeEditor.canvas!=null){
foreach(var one in runtimeNodeEditor.canvas.nodes){
var bnode = one as uBehaviourTreeNode;
if(bnode!=null){
//Debug.Log("Excute Node = "+node.nowExcuteUuid);
if(bnode.Uuid == node.nowExcuteUuid){
bnode.setColorFlag = true;
}
else {
bnode.setColorFlag = false;
}
}
}
}
}
/// <summary>
/// 開始ノードを検索する。
/// </summary>
/// <returns>The start node.</returns>
/// <param name="canvas">Canvas.</param>
Node FindStartNode(NodeCanvas canvas){
return canvas.nodes.Where(p=>p.GetType() == typeof(uStartNode)).FirstOrDefault();
}
/// <summary>
/// BehaviourTreeを構築する。
/// </summary>
/// <returns>The behaviour tree base.</returns>
/// <param name="node">Node.</param>
BehaviourTreeBase CreateBehaviourTreeBase(uBehaviourTreeNode bnode){
var type = Type.GetType("BehaviourTrees."+ bnode.nodeType.ToString());
var ins = Activator.CreateInstance(type) as BehaviourTreeBase;
switch (bnode.nodeType) {
case uBehaviourTreeNode.NodeType.ActionNode:
{
var nodeIns = ins as ActionNode;
nodeIns.Initialize(BTreeUtil.GetAction<ReactiveProperty<bool>>(gameObject, typeof(BTreeActionBase),bnode.MethodName),bnode.Uuid);
}
break;
case uBehaviourTreeNode.NodeType.DecoratorNode:
{
var nodeIns = ins as DecoratorNode;
var actionNode = bnode.Outputs[0].connections[0].body as uBehaviourTreeNode;
var bbase = CreateBehaviourTreeBase(actionNode);
var func = BTreeUtil.GetFunc<BehaviourTreeInstance,ExecutionResult>(gameObject, typeof(BTreeDecoratorFuncBase),bnode.MethodName);
nodeIns.Initialize(func, bbase,bnode.Uuid);
}
break;
case uBehaviourTreeNode.NodeType.SelectorNode:
{
var nodeIns = ins as SelectorNode;
var list = new List<BehaviourTreeBase>();
foreach(var one in bnode.Outputs){
if(one.connections.Count==0) continue;
var childnode = CreateBehaviourTreeBase(one.connections[0].body as uBehaviourTreeNode);
list.Add(childnode);
}
nodeIns.Initialize(list.ToArray(),bnode.Uuid);
list.Clear();
}
break;
case uBehaviourTreeNode.NodeType.SequencerNode:
{
var nodeIns = ins as SequencerNode;
var list = new List<BehaviourTreeBase>();
foreach(var one in bnode.Outputs){
if(one.connections.Count==0) continue;
var childnode = CreateBehaviourTreeBase(one.connections[0].body as uBehaviourTreeNode);
list.Add(childnode);
}
nodeIns.Initialize(list.ToArray(),bnode.Uuid);
list.Clear();
}
break;
}
return ins;
}
/// <summary>
/// Resets the coroutine start.
/// </summary>
void ResetCoroutineStart(){
StartCoroutine(ResetCoroutine());
}
IEnumerator ResetCoroutine(){
yield return null;
node.Delete();
CreateNodes(m_topNode);
}
}