You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.6 KiB
128 lines
3.6 KiB
using SampleBehavirTree;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
public class SampleBehaviourTree : MonoBehaviour
|
|
{
|
|
private BehaviourNode root;
|
|
private List<BehaviourNode> behaviourNodes = new List<BehaviourNode>();
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
BehaviourCtrl.Init();
|
|
behaviourNodes = CreatBehaviorNodes(AISys.Instance._workerNum);
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
ExcuteBehaviourNodes(behaviourNodes);
|
|
}
|
|
|
|
[ContextMenu("执行行为树")]
|
|
void ExcuteBehaviour()
|
|
{
|
|
StartCoroutine(c_ExcuteBehaviour());
|
|
}
|
|
|
|
void ExcuteBehaviourNodes(List<BehaviourNode> nodes)
|
|
{
|
|
StartCoroutine(c_ExcuteBehaviour(nodes));
|
|
}
|
|
|
|
IEnumerator c_ExcuteBehaviour()
|
|
{
|
|
yield return StartCoroutine(root.Execute());
|
|
}
|
|
|
|
|
|
IEnumerator c_ExcuteBehaviour(List<BehaviourNode> nodes)
|
|
{
|
|
for (int i = 0; i < nodes.Count; i++)
|
|
{
|
|
yield return StartCoroutine(nodes[i].Execute());
|
|
}
|
|
}
|
|
|
|
|
|
public BehaviourNode CreatBehaviour()
|
|
{
|
|
var selector = new Selector();
|
|
|
|
var sequence_1 = new Sequence();
|
|
|
|
var condition1 = new condition_Behavir();
|
|
|
|
var action1 = new DebugTest();
|
|
action1.info = "测试行为树";
|
|
//添加条件
|
|
sequence_1.nodes.Add(condition1);
|
|
//添加行为
|
|
sequence_1.nodes.Add(action1);
|
|
//
|
|
selector.nodes.Add(sequence_1);
|
|
//不同类别Action 的参数配置
|
|
return selector;
|
|
}
|
|
|
|
public List<BehaviourNode> CreatBehaviorNodes(int num)
|
|
{
|
|
List<BehaviourNode> nodeList = new List<BehaviourNode>();
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
var selector = new Selector();
|
|
selector.ID = i;
|
|
|
|
var sequence_1 = new Sequence();
|
|
var sequence_2 = new Sequence();
|
|
var sequence_3 = new Sequence();
|
|
var sequence_4 = new Sequence();
|
|
|
|
var condition1 = new AI_WorkerWorkState();
|
|
var condition2 = new AI_WorkerMoveState();
|
|
var condition3 = new AI_WorkerDieState();
|
|
var condition4 = new AI_WorkerCheckState();
|
|
condition1._Worker = AISys.Instance._workerGroup[i];
|
|
condition2._Worker = AISys.Instance._workerGroup[i];
|
|
condition3._Worker = AISys.Instance._workerGroup[i];
|
|
condition4._Worker = AISys.Instance._workerGroup[i];
|
|
|
|
var action1 = new WorkAction();
|
|
var action2 = new MoveAction();
|
|
var action3 = new DieAction();
|
|
var action4 = new CheckAction();
|
|
action1._Worker = condition1._Worker;
|
|
action2._Worker = condition2._Worker;
|
|
action3._Worker = condition3._Worker;
|
|
action4._Worker = condition4._Worker;
|
|
//1号分支
|
|
sequence_1.nodes.Add(condition1);
|
|
sequence_1.nodes.Add(action1);
|
|
//2号分支
|
|
sequence_2.nodes.Add(condition2);
|
|
sequence_2.nodes.Add(action2);
|
|
|
|
//3号分支
|
|
sequence_3.nodes.Add(condition3);
|
|
sequence_3.nodes.Add(action3);
|
|
//4号分支
|
|
sequence_4.nodes.Add(condition4);
|
|
sequence_4.nodes.Add(action4);
|
|
|
|
selector.nodes.Add(sequence_1);
|
|
selector.nodes.Add(sequence_2);
|
|
selector.nodes.Add(sequence_3);
|
|
selector.nodes.Add(sequence_4);
|
|
|
|
nodeList.Add(selector);
|
|
}
|
|
return nodeList;
|
|
}
|
|
|
|
|
|
}
|