Last active
August 29, 2015 14:14
-
-
Save AngryAnt/dbf341d87bba035adaee to your computer and use it in GitHub Desktop.
Tree.ReflectClassForwards (GameObject) and Tree.SetForwards (int id, IActionClass actionClass). This is now built-in per Behave 2.6.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using Behave.Runtime; | |
using Library = BLYourLibraryName; | |
using Tree = Behave.Runtime.Tree; | |
public interface IActionClass | |
{ | |
bool OnForward (Tree sender); | |
BehaveResult Init (Tree sender); | |
BehaveResult Tick (Tree sender); | |
void Reset (Tree sender); | |
} | |
public static class BehaveExtensions | |
{ | |
static readonly string s_IActionTypeName = typeof (IActionClass).Name; | |
const string kActionClassPostfix = "Action"; | |
public static void ReflectClassForwards (this Tree tree, GameObject gameObject) | |
{ | |
foreach (MonoBehaviour behaviour in gameObject.GetComponents<MonoBehaviour> ()) | |
{ | |
System.Type type = behaviour.GetType (); | |
if (type.GetInterface (s_IActionTypeName) == null) | |
{ | |
continue; | |
} | |
string name = type.Name; | |
int actionIndex = name.IndexOf (kActionClassPostfix); | |
if (actionIndex < 1) | |
{ | |
Debug.LogError (string.Format ( | |
"Bad {0} type name (format is \"[Name]{1}\"): {2}", | |
s_IActionTypeName, | |
kActionClassPostfix, | |
name | |
)); | |
continue; | |
} | |
name = name.Substring (0, actionIndex); | |
int id = 0; | |
try | |
{ | |
id = (int)System.Enum.Parse (typeof (Library.ActionType), name); | |
} | |
catch | |
{ | |
Debug.LogError ("Unknown action type: " + name); | |
continue; | |
} | |
tree.SetForwards (id, (IActionClass)behaviour); | |
} | |
} | |
public static bool SetForwards (this Tree tree, int id, IActionClass actionClass) | |
{ | |
// TODO: When Tree.ActionIDs has been implemented, we should first check if tree.ActionIDs contains id - returning false if not | |
if (!actionClass.OnForward (tree)) | |
{ | |
return false; | |
} | |
tree.SetInitForward (id, actionClass.Init); | |
tree.SetTickForward (id, actionClass.Tick); | |
tree.SetResetForward (id, actionClass.Reset); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment