node interface

4
/*******************************************************************************/ /*! \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)

Upload: khan-sweetman

Post on 10-Apr-2016

67 views

Category:

Documents


0 download

DESCRIPTION

All behavior tree nodes inherit from this class.

TRANSCRIPT

Page 1: Node Interface

/*******************************************************************************//*!\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;

Page 2: Node Interface

}}

[System.Serializable]public class BT_Node{ public string Name = "Node"; [System.NonSerialized] public BT_Node Root; [System.NonSerialized] public int CurrIndex = 0; [System.NonSerialized] public BT_Status CurrStatus = BT_Status.Entering; [System.NonSerialized] public GameObject Owner; [System.NonSerialized] public AI_Base AI; [System.NonSerialized] public List<BT_Node> Children = new List<BT_Node>(); [System.NonSerialized] public 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)

Page 3: Node Interface

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; }

Page 4: Node Interface

}