Sie sind auf Seite 1von 3

/*******************************************************************************/

/*!
\file LEF_Node.cs
\author Khan Sweetman
\par All content 2015 DigiPen (USA) Corporation, all rights reserved.
\par Golden Bullet Games
\brief
All behavior tree nodes inherit from here. Child nodes should following the
naming convention:
- DEC_Decorator
- LEF_Leaf
- SEL_Selector
*/
/*******************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum BT_Status
{
Entering,
Running,
Success,
Fail
}
public enum BT_NodeType
{
Leaf,
Decorator,
Selector
}
public class LEF_CopyPasteThisNode : BT_Node
{
//public BT_Node[] Children;
//public string Name = "Node";
//public int CurrIndex = 0;
//public BT_Status CurrStatus = BT_Status.Entering;
//public GameObject Owner;
//public BT_Node Root;
public LEF_CopyPasteThisNode() : base() { }
// First parameter should be owner
// Second parameter should be root
public override void Initialize(object[] objs)
{
base.Initialize(objs);
}
/////////////////////////////////// Per frame Functions ///////////////////////////////////
public override BT_Status Update()
{
return CurrStatus;
}

}
[System.Serializable]
public class BT_Node
{
public string Name = "Node";
[System.NonSerialized] public
[System.NonSerialized] public
[System.NonSerialized] public
[System.NonSerialized] public
[System.NonSerialized] public
[System.NonSerialized] public
[System.NonSerialized] public

BT_Node Root;
int CurrIndex = 0;
BT_Status CurrStatus = BT_Status.Entering;
GameObject Owner;
AI_Base AI;
List<BT_Node> Children = new List<BT_Node>();
BT_NodeType NodeType = BT_NodeType.Leaf;

protected int DebugLevel = 0;


public BT_Node()
{
Name = GetType().ToString();
}
public BT_Node(string name)
{
Name = name;
}
// First parameter should be owner
// Second parameter should be root
public virtual void Initialize(object[] objs)
{
if (DebugLevel >= 2)
Debug.Log("INIT: " + Name);
Owner = (GameObject)objs[0];
Root = (BT_Node)objs[1];
DebugLevel = (int)objs[2];
AI = (AI_Base)objs[3];
// Recursively initialize children
foreach (BT_Node child in Children)
child.Initialize(objs);
}
/////////////////////////////////// Public Interface ///////////////////////////////////
public virtual void EnterBehavior()
{
// Base behavior
foreach (var child in Children)
child.EnterBehavior();
// Do whatever you need to do when you enter this node
// ...
}
public virtual void ExitBehavior()
{
// Base behavior
foreach(var child in Children)
if (child.CurrStatus == BT_Status.Running)
child.ExitBehavior();

// Do whatever you need to do if you're interrupted and need to exit


// ...
}
public virtual BT_Status SetStatus(BT_Status status)
{
if (DebugLevel >= 1)
Debug.Log(Name + " STATUS CHANGE: " + status);
// Entrance behavior
CurrStatus = status;
if (CurrStatus == BT_Status.Entering)
EnterBehavior();
return status;
}
/////////////////////////////////// Per frame Functions ///////////////////////////////////
public virtual BT_Status Update()
{
if(DebugLevel >= 2)
Debug.Log(Name + " UPDATE");
return CurrStatus;
}
///////////////////////////////////// Helper Functions /////////////////////////////////////
public string PrintTree(string tree = "", int depth = 0)
{
// Format self
string self = "";
if (depth != 0)
self += "\n";
for (int i = 0; i < depth; ++i)
self += " ";
self += Name;
// If leaf, return self
if (Children == null)
return self;
tree += self;
// Add children to tree
foreach (BT_Node child in Children)
tree += child.PrintTree(tree, depth + 1);
// If not root, recurse
if(depth != 0)
return PrintTree(tree, depth + 1);
// If root, print
Debug.Log(tree);
return tree;
}
}

Das könnte Ihnen auch gefallen