private bool FindNodeRecursive(BehaviourTreeNode node, BehaviourTreeNode toFind) { if (node == null) { return(false); } if (node == toFind) { return(true); } if (node.IsParentNode()) { var childNodes = node.AsParentNode().GetChildNodes(); for (int i = 0; i < childNodes.Count; i++) { if (FindNodeRecursive(childNodes[i], toFind)) { return(true); } } } return(false); }
public override void RemoveChild(BehaviourTreeNode node) { if (DecoratedNode == node) { DecoratedNode = null; } }
private void DestroyChildrenNodes(BehaviourTreeNode node) { if (!node.IsParentNode()) { return; } var asParent = node.AsParentNode(); var children = asParent.GetChildNodes(); if (children != null && children.Count > 0) { List <BehaviourTreeNode> toRemove = new List <BehaviourTreeNode>(); foreach (var child in children) { DestroyChildrenNodes(child); toRemove.Add(child); } foreach (BehaviourTreeNode treeNode in toRemove) { asParent.RemoveChild(treeNode); DestroyImmediate(treeNode, true); } } }
private void DeleteFromAsset(UnityEngine.Object asset) { Undo.RecordObject(TreeAsset, "Deleted action"); Undo.RecordObject(asset, "Deleted action"); if (asset is BehaviourTreeNode) { BehaviourTreeNode node = (BehaviourTreeNode)asset; RemoveNodeAssetRecursive(TreeAsset.RootNode, node); DestroyChildrenNodes(node); if (node == TreeAsset.RootNode) { TreeAsset.RootNode = null; } DestroyImmediate(node, true); } else { DestroyImmediate(asset, true); } EditorUtility.SetDirty(TreeAsset); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); OnLoadAsset(TreeAsset); }
private bool RemoveNodeAssetRecursive(BehaviourTreeNode node, BehaviourTreeNode toRemove) { bool removed = false; if (node.IsParentNode()) { var asParent = node.AsParentNode(); var childNodes = asParent.GetChildNodes(); if (childNodes.Contains(toRemove)) { asParent.RemoveChild(toRemove); return(true); } else { foreach (BehaviourTreeNode childNode in childNodes) { if (RemoveNodeAssetRecursive(childNode, toRemove)) { removed = true; break; } } } } return(removed); }
public bool IsNodeCurrent(BehaviourTreeNode treeNode) { if (HasRuntimeController()) { return(RuntimeController.CurrentActingNode == treeNode); } return(false); }
public bool Contains(BehaviourTreeNode node) { //if (IsPreprocessed()) // return NodeList.Contains(node); //else { return(FindNodeRecursive(RootNode, node)); } }
public BehaviourTreeGraphNode(BehaviourTreeNode node) { TreeNode = node; Size = new Vector2(120, 40); Position = TreeNode.EditorPosition; Name = TreeNode.Name; CurrentColor = GetColor(); //Color.white; TargetColor = GetColor(); //Color.white; }
public NodeResult CheckNodeStatus(BehaviourTreeNode node) { int index = NodeList.IndexOf(node); if (index >= 0 && index < NodeList.Count && NodeStatuses.ContainsKey(index)) { return(NodeStatuses[index]); } Debug.LogErrorFormat("Unable to check status of node '{0}' of index '{1}', its not present in BehaviourTree!", node, index); throw new System.InvalidOperationException("Unable to check status of node"); }
protected void SwitchToNode(BehaviourTreeNode node) { if (IsInUpdate && CurrentController) { CurrentController.RequestSwitchToNode(this, node); } else { CurrentController.RequestSwitchToNode(null, null); Debug.LogError("Unable to switch state outside of OnUpdate()!", this); } }
private void ShowNodeCreationMenu(System.Action <Type> callback) { GenericMenu menu = new GenericMenu(); foreach (Type type in Reflection.GetSubclasses <BehaviourTreeNode>()) { string title = string.Format("{0}/{1}", BehaviourTreeNode.GetNodeTypeName(type), type.Name); menu.AddItem(new GUIContent(title), false, CreateNewNodeCallback(callback, type)); } menu.ShowAsContext(); }
public override void AddOrSetChild(BehaviourTreeNode newNode) { if (DecoratedNode == null) { newNode.Parent = this; DecoratedNode = newNode; } else { throw new InvalidOperationException(string.Format("Unable to set another child for Decorator Node descendant '{0}'", GetType().Name)); } }
private void ProcessNodeRecurrent(BehaviourTreeNode node, List <BehaviourTreeNode> nodes) { nodes.Add(node); if (node.IsParentNode()) { var children = node.AsParentNode().GetChildNodes(); for (int i = 0; i < children.Count; i++) { ProcessNodeRecurrent(children[i], nodes); } } }
private bool UpdateNodeStatus(BehaviourTreeNode node, NodeResult result) { bool changed = false; int index = NodeList.IndexOf(node); if (index >= 0 && index < NodeList.Count) { changed = NodeStatuses[index] != result; NodeStatuses[index] = result; } return(changed); }
public void RequestSwitchToNode(BehaviourTreeNode parent, BehaviourTreeNode node) { DidSwitchToAnotherNode = true; if (CurrentActingNode == parent && node.Parent == parent) { ScheduleBefore(node); // CurrentActingNode = null; // ??? } else { Debug.LogErrorFormat("Unable to switch to node {0} from {1}, because its not its parent.", node, parent); } }
private void ChangeNode(BehaviourTreeNode newNode) { if (CurrentActingNode) { CurrentActingNode.OnEnd(this); } CurrentActingNode = newNode; if (CurrentActingNode) { CurrentActingNode.OnStart(this); } }
public void ScheduleBefore(BehaviourTreeNode node) { if (node == null) { return; } int index = NodeList.IndexOf(node); if (index >= 0 && index < NodeList.Count) { NodeScheduler.AddFirst(index); } }
private void ProcessNodeRecurrent(BehaviourTreeNode node, List <BehaviourTreeNode> nodes) { nodes.Add(node); if (!node.IsParentNode()) { return; } var children = node.AsParentNode().GetChildNodes(); foreach (BehaviourTreeNode child in children) { ProcessNodeRecurrent(child, nodes); } }
public virtual void RemoveChild(BehaviourTreeNode node) { throw new System.InvalidOperationException(string.Format("Unabel to remove children from node of type {0}", GetType().Name)); }
public bool CanEditNode(BehaviourTreeNode node) { return(TreeAsset.Contains(node)); }
public NodeResult CheckNodeStatus(BehaviourTreeNode node) { return(RuntimeController.CheckNodeStatus(node)); }
public override void RemoveChild(BehaviourTreeNode node) { ChildNodes.Remove(node); }
public override void AddOrSetChild(BehaviourTreeNode newNode) { newNode.Parent = this; ChildNodes.Add(newNode); }
public bool Contains(BehaviourTreeNode node) { return(FindNodeRecursive(RootNode, node)); }
protected override void OnProcessControll() { if (NodeScheduler.Count == 0) { PrewarmScheduler(); } // Start! ShouldSuspendExecution = false; // Keep running while (!ShouldSuspendExecution) { // We reached the end of scheduler if (NodeScheduler.Count == 0) { ShouldSuspendExecution = true; break; } if (CurrentActingNode == null) { // If node is null, get fresh from scheduler ChangeNode(NodeList[NodeScheduler.First.Value]); NodeScheduler.RemoveFirst(); } if (CurrentActingNode != null) { var result = CurrentActingNode.CallUpdate(this, Blackboard); var changed = UpdateNodeStatus(CurrentActingNode, result); if (result != NodeResult.Running && changed) { if (CurrentActingNode.Parent != null) { // We continue, allow parent to process result and wait till parent sets new task node ScheduleBefore(CurrentActingNode.Parent); CurrentActingNode = null; } else { // We reached the end of scheduler CurrentActingNode = null; ShouldSuspendExecution = true; NodeScheduler.Clear(); } } else { // Suspend and allow this node progress ShouldSuspendExecution = true; } } else { // Failed because of error? ShouldSuspendExecution = true; Debug.LogError("Unexpected null instead of CurrentActingNode!"); } // If we switched nodes, allow to take them from scheduler if (DidSwitchToAnotherNode) { CurrentActingNode = null; ShouldSuspendExecution = true; } } DidSwitchToAnotherNode = false; }
public virtual void AddOrSetChild(BehaviourTreeNode newNode) { throw new System.InvalidOperationException(string.Format("Unabel to set children to node of type {0}", GetType().Name)); }