├── .gitignore ├── Assets ├── BehaviorLibrary.meta ├── BehaviorLibrary │ ├── BehaviorNode.cs │ ├── BehaviorNode.cs.meta │ ├── BehaviorTree.cs │ ├── BehaviorTree.cs.meta │ ├── Components.meta │ ├── Components │ │ ├── Actions.meta │ │ ├── Actions │ │ │ ├── BAction.cs │ │ │ └── BAction.cs.meta │ │ ├── BehaviorComponent.cs │ │ ├── BehaviorComponent.cs.meta │ │ ├── Composites.meta │ │ ├── Composites │ │ │ ├── Selector.cs │ │ │ ├── Selector.cs.meta │ │ │ ├── SelectorBranch.cs │ │ │ ├── SelectorBranch.cs.meta │ │ │ ├── SelectorContinued.cs │ │ │ ├── SelectorContinued.cs.meta │ │ │ ├── SelectorRandom.cs │ │ │ ├── SelectorRandom.cs.meta │ │ │ ├── Sequence.cs │ │ │ ├── Sequence.cs.meta │ │ │ ├── SequenceContinued.cs │ │ │ ├── SequenceContinued.cs.meta │ │ │ ├── StatefulSelector.cs │ │ │ ├── StatefulSelector.cs.meta │ │ │ ├── StatefulSequence.cs │ │ │ └── StatefulSequence.cs.meta │ │ ├── Conditionals.meta │ │ ├── Conditionals │ │ │ ├── ConditionalGameObjectDistance.cs │ │ │ ├── ConditionalGameObjectDistance.cs.meta │ │ │ ├── ConditionalGeneric.cs │ │ │ ├── ConditionalGeneric.cs.meta │ │ │ ├── ConditionalLambda.cs │ │ │ ├── ConditionalLambda.cs.meta │ │ │ ├── ConditionalVector3Distance.cs │ │ │ └── ConditionalVector3Distance.cs.meta │ │ ├── Decorators.meta │ │ └── Decorators │ │ │ ├── Alterable.cs │ │ │ ├── Alterable.cs.meta │ │ │ ├── ChanceFailure.cs │ │ │ ├── ChanceFailure.cs.meta │ │ │ ├── ChanceRunning.cs │ │ │ ├── ChanceRunning.cs.meta │ │ │ ├── Counter.cs │ │ │ ├── Counter.cs.meta │ │ │ ├── Inverter.cs │ │ │ ├── Inverter.cs.meta │ │ │ ├── RepeatUntilFailureConstant.cs │ │ │ ├── RepeatUntilFailureConstant.cs.meta │ │ │ ├── RepeatUntilFailureContinued.cs │ │ │ ├── RepeatUntilFailureContinued.cs.meta │ │ │ ├── RepeatUntilSuccessConstant.cs │ │ │ ├── RepeatUntilSuccessConstant.cs.meta │ │ │ ├── RepeatUntilSuccessContinued.cs │ │ │ ├── RepeatUntilSuccessContinued.cs.meta │ │ │ ├── Timer.cs │ │ │ ├── Timer.cs.meta │ │ │ ├── UntilFailure.cs │ │ │ ├── UntilFailure.cs.meta │ │ │ ├── UntilSuccess.cs │ │ │ └── UntilSuccess.cs.meta │ ├── ConditionalType.cs │ ├── ConditionalType.cs.meta │ ├── Editor.meta │ ├── Editor │ │ ├── PreviewEditorWindow.cs │ │ ├── PreviewEditorWindow.cs.meta │ │ ├── PreviewPropertyDrawer.cs │ │ └── PreviewPropertyDrawer.cs.meta │ ├── Shaders.meta │ └── Shaders │ │ ├── ColorBlended.shader │ │ ├── ColorBlended.shader.meta │ │ ├── ColoredBlended.shader │ │ └── ColoredBlended.shader.meta ├── Example.meta ├── Example │ ├── BehaviorTest.cs │ ├── BehaviorTest.cs.meta │ ├── BehaviorTest.prefab │ ├── BehaviorTest.prefab.meta │ ├── BehaviorTest.unity │ └── BehaviorTest.unity.meta ├── Plugins.meta └── Plugins │ ├── Editor.meta │ └── Editor │ ├── JetBrains.meta │ └── JetBrains │ ├── JetBrains.Rider.Unity.Editor.Plugin.Repacked.dll │ └── JetBrains.Rider.Unity.Editor.Plugin.Repacked.dll.meta ├── LICENSE ├── Misc ├── BehaviorTree1.PNG └── BehaviorTree2.PNG ├── Packages └── manifest.json ├── ProjectSettings ├── AudioManager.asset ├── ClusterInputManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── PresetManager.asset ├── ProjectSettings.asset ├── ProjectVersion.txt ├── QualitySettings.asset ├── TagManager.asset ├── TimeManager.asset ├── UnityConnectSettings.asset └── VFXManager.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | /[Bb]uilds/ 6 | /.idea/ 7 | /[Ll]ogs/ 8 | /Assets/AssetStoreTools* 9 | 10 | # Autogenerated VS/MD solution and project files 11 | ExportedObj/ 12 | *.csproj 13 | *.unityproj 14 | *.sln 15 | *.suo 16 | *.tmp 17 | *.user 18 | *.userprefs 19 | *.pidb 20 | *.booproj 21 | *.svd 22 | 23 | 24 | # Unity3D generated meta files 25 | *.pidb.meta 26 | 27 | # Unity3D Generated File On Crash Reports 28 | sysinfo.txt 29 | 30 | # Builds 31 | *.apk 32 | *.unitypackage 33 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cbb6fd055930f0d4bbd54bbcf9af700b 3 | folderAsset: yes 4 | timeCreated: 1468198403 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/BehaviorNode.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | 4 | namespace BehaviorLibrary 5 | { 6 | // TODO: Move this into BehaviorLibrary.Editor 7 | public class BehaviorNode 8 | { 9 | public BehaviorComponent BehaviorComponent; 10 | 11 | const float kNodeHeight = 20.0f; 12 | private const float kNodeWidth = 150.0f; 13 | 14 | static BehaviorNode selection = null; 15 | static bool connecting = false; 16 | 17 | Vector2 position; 18 | Rect nodeRect; 19 | 20 | private Material material; 21 | 22 | public BehaviorNode(BehaviorComponent behaviorComponent, Vector2 position) 23 | { 24 | BehaviorComponent = behaviorComponent; 25 | Position = position; 26 | BehaviorComponent.Node = this; 27 | } 28 | 29 | public static BehaviorNode Selection 30 | { 31 | get 32 | { 33 | return selection; 34 | } 35 | set 36 | { 37 | selection = value; 38 | if (selection == null) 39 | { 40 | connecting = false; 41 | } 42 | } 43 | } 44 | 45 | public Vector2 Position 46 | { 47 | get 48 | { 49 | return position; 50 | } 51 | set 52 | { 53 | position = value; 54 | 55 | nodeRect = new Rect( 56 | position.x - kNodeWidth * 0.5f, 57 | position.y - kNodeHeight * 0.5f, 58 | kNodeWidth, 59 | kNodeHeight 60 | ); 61 | } 62 | } 63 | 64 | public BehaviorNode TopNode; 65 | 66 | public void OnGUI() 67 | { 68 | switch (Event.current.type) 69 | { 70 | case EventType.MouseDown: 71 | break; 72 | case EventType.MouseUp: 73 | if (nodeRect.Contains(Event.current.mousePosition)) 74 | // Select this node if we clicked it 75 | { 76 | if ( selection != this ) 77 | { 78 | selection = this; 79 | } 80 | else 81 | { 82 | selection = null; 83 | } 84 | 85 | Event.current.Use(); 86 | } 87 | break; 88 | case EventType.MouseDrag: 89 | //if (selection == this) 90 | //// If doing a mouse drag with this component selected... 91 | //{ 92 | // Position += Event.current.delta; 93 | // Event.current.Use(); 94 | //} 95 | break; 96 | case EventType.Repaint: 97 | DrawBox( nodeRect, new Color(0.65f, 0.65f, 0.65f, .65f )); 98 | GUIContent content = new GUIContent( BehaviorComponent.Name ); 99 | GUIStyle style = new GUIStyle(); 100 | style.fontStyle = FontStyle.BoldAndItalic; 101 | style.alignment = TextAnchor.MiddleCenter; 102 | style.Draw( nodeRect, content, 0 ); 103 | GUI.skin.box.Draw( nodeRect, new GUIContent(), false, false, false, false ); 104 | break; 105 | } 106 | 107 | GUI.color = new Color(.5f, .5f, .5f, .65f); 108 | if (TopNode != null) 109 | { 110 | Color color = Color.gray; 111 | DrawConnection(TopNode.Position, Position, color); 112 | } 113 | } 114 | 115 | public void OnHistoryGUI() 116 | { 117 | Color color = new Color(0.05f, 0.05f, 0.05f, .65f); 118 | 119 | if (BehaviorComponent.Status == Status.Running) 120 | { 121 | color = new Color(0, 0, 1, .65f); 122 | } 123 | if (BehaviorComponent.Status == Status.Success) 124 | { 125 | color = new Color(0, 1, 0, .65f); 126 | } 127 | if (BehaviorComponent.Status == Status.Failure) 128 | { 129 | color = new Color(1, 0, 0, .65f); 130 | } 131 | 132 | switch (Event.current.type) 133 | { 134 | case EventType.MouseDown: 135 | break; 136 | case EventType.MouseUp: 137 | // Select this node if we clicked it 138 | if ( nodeRect.Contains(Event.current.mousePosition)) 139 | { 140 | if (selection != this) 141 | { 142 | selection = this; 143 | } 144 | else 145 | { 146 | selection = null; 147 | } 148 | 149 | Event.current.Use(); 150 | } 151 | break; 152 | case EventType.MouseDrag: 153 | //if (selection == this) 154 | //// If doing a mouse drag with this component selected... 155 | //{ 156 | // if (connecting) 157 | // // ... and in connect mode, just use the event as we'll be painting the new connection 158 | // { 159 | // Event.current.Use(); 160 | // } 161 | // else 162 | // // ... and not in connect mode, drag the component 163 | // { 164 | // Position += Event.current.delta; 165 | // Event.current.Use(); 166 | // } 167 | //} 168 | break; 169 | case EventType.Repaint: 170 | DrawBox(nodeRect, color); 171 | GUIContent content = new GUIContent(BehaviorComponent.Name); 172 | GUIStyle style = new GUIStyle(); 173 | style.fontStyle = FontStyle.BoldAndItalic; 174 | style.alignment = TextAnchor.MiddleCenter; 175 | style.Draw(nodeRect, content, 0); 176 | GUI.skin.box.Draw(nodeRect, new GUIContent(), false, false, false, false); 177 | break; 178 | } 179 | 180 | GUI.color = color; 181 | if (TopNode != null) 182 | { 183 | color = Color.gray; 184 | if (TopNode.BehaviorComponent.Status == Status.Running) 185 | { 186 | color = Color.blue; 187 | } 188 | if (TopNode.BehaviorComponent.Status == Status.Success) 189 | { 190 | color = Color.green; 191 | } 192 | if (TopNode.BehaviorComponent.Status == Status.Failure) 193 | { 194 | color = Color.red; 195 | } 196 | DrawConnection(TopNode.Position, Position, color); 197 | } 198 | } 199 | 200 | private void DrawBox(Rect rect, Color color) 201 | { 202 | if (rect.y < 0) 203 | { 204 | rect.height += rect.y; 205 | if (rect.height < 0) 206 | { 207 | rect.height = 0; 208 | } 209 | rect.y = 0; 210 | } 211 | 212 | CreateMaterial(); 213 | material.SetPass(0); 214 | 215 | GL.Color(color); 216 | GL.Begin(GL.QUADS); 217 | GL.Vertex3(rect.x, rect.y, 0); 218 | GL.Vertex3(rect.x + rect.width, rect.y, 0); 219 | GL.Vertex3(rect.x + rect.width, rect.y + rect.height, 0); 220 | GL.Vertex3(rect.x, rect.y + rect.height, 0); 221 | GL.End(); 222 | } 223 | 224 | private void CreateMaterial() 225 | { 226 | if (material != null) 227 | return; 228 | 229 | material = new Material(Shader.Find("Lines/ColoredBlended")); 230 | } 231 | 232 | public static void DrawConnection(Vector2 from, Vector2 to, Color color) 233 | { 234 | bool left = from.x > to.x; 235 | Handles.DrawBezier( 236 | new Vector3(from.x + (left ? -kNodeWidth : kNodeWidth) * 0.5f, from.y, 0.0f), 237 | new Vector3(to.x + (left ? kNodeWidth : -kNodeWidth) * 0.5f, to.y, 0.0f), 238 | new Vector3(from.x, from.y, 0.0f) + Vector3.right * 100.0f * (left ? -1.0f : 1.0f), 239 | new Vector3(to.x, to.y, 0.0f) + Vector3.right * 100.0f * (left ? 1.0f : -1.0f), 240 | color, 241 | null, 242 | 4.0f 243 | ); 244 | } 245 | } 246 | } -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/BehaviorNode.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 00215d7d460c213499a2c30ee6913378 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/BehaviorTree.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using Debug = UnityEngine.Debug; 5 | 6 | namespace BehaviorLibrary 7 | { 8 | public enum Status 9 | { 10 | Failure, 11 | Success, 12 | Running, 13 | Dormant // TODO: fully implement usage 14 | } 15 | 16 | public delegate Status BehaviorReturn(); 17 | 18 | [Serializable] 19 | public class BehaviorTree : ScriptableObject 20 | { 21 | public Action BehaviorTreeUpdated; 22 | 23 | public Func CheckIfUpdateNeeded; 24 | 25 | public List History; 26 | public Dictionary AlterableComponents; 27 | 28 | public BehaviorComponent Root; 29 | 30 | public Status CurrentStatus { get; set; } 31 | 32 | public BehaviorTree(BehaviorComponent root) 33 | { 34 | Init(root); 35 | } 36 | 37 | public void Init(BehaviorComponent root) 38 | { 39 | Root = root; 40 | History = new List(); 41 | AlterableComponents = new Dictionary(); 42 | SetupHistoryCallBackAndAlterable(Root); 43 | } 44 | 45 | public Status Behave() 46 | { 47 | History.Clear(); 48 | if (CheckIfUpdateNeeded != null && CheckIfUpdateNeeded()) 49 | { 50 | RefreshBehaviorTree(); 51 | } 52 | return CurrentStatus = Root.Execute(); 53 | } 54 | 55 | // TODO: consider moving these into a Behavior.Editor history object 56 | private void AddToHistory(BehaviorComponent behaviorComponent) 57 | { 58 | History.Add(behaviorComponent); 59 | } 60 | 61 | public void SetupHistoryCallBackAndAlterable(BehaviorComponent behaviorComponent) 62 | { 63 | behaviorComponent.AddToHistory = AddToHistory; 64 | if (behaviorComponent is Alterable) 65 | { 66 | Alterable alterable = (Alterable) behaviorComponent; 67 | alterable.BehaviorTree = this; 68 | AlterableComponents.Add(alterable.ID, alterable); 69 | } 70 | if (behaviorComponent.Behaviors != null) 71 | { 72 | for (int i = 0; i < behaviorComponent.Behaviors.Length; i++) 73 | { 74 | BehaviorComponent behavior = behaviorComponent.Behaviors[i]; 75 | behavior.AddToHistory = AddToHistory; 76 | if (behavior.Behaviors != null) 77 | { 78 | SetupHistoryCallBackAndAlterable(behavior); 79 | } 80 | } 81 | } 82 | } 83 | 84 | public void RefreshBehaviorTree() 85 | { 86 | AlterableComponents.Clear(); 87 | SetupHistoryCallBackAndAlterable(Root); 88 | #if UNITY_EDITOR 89 | if (BehaviorTreeUpdated != null) 90 | { 91 | Debug.Log("Refreshing Tree"); 92 | BehaviorTreeUpdated(this); 93 | } 94 | #endif 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/BehaviorTree.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 31385de42188553478f1b9051cc671cc 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4cb2d3663666dfe42a7da8bb272cc250 3 | folderAsset: yes 4 | timeCreated: 1468198403 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Actions.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 847df03dac0956c4e850cb58a26b4c3d 3 | folderAsset: yes 4 | timeCreated: 1468198403 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Actions/BAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BehaviorLibrary 4 | { 5 | public class BAction : BehaviorComponent 6 | { 7 | public override BehaviorComponent[] Behaviors 8 | { 9 | get { return null; } 10 | } 11 | 12 | public Func Action; 13 | 14 | public BAction(string name = "BehaviorAction") 15 | { 16 | Name = name; 17 | } 18 | 19 | public BAction(Func action) 20 | { 21 | Action = action; 22 | Name = "BehaviorAction"; 23 | } 24 | 25 | public override Status Execute() 26 | { 27 | AddToHistory(this); 28 | switch (Action.Invoke()) 29 | { 30 | case Status.Success: 31 | Status = Status.Success; 32 | return Status; 33 | case Status.Failure: 34 | Status = Status.Failure; 35 | return Status; 36 | case Status.Running: 37 | Status = Status.Running; 38 | return Status; 39 | default: 40 | Status = Status.Failure; 41 | return Status; 42 | } 43 | } 44 | 45 | public BAction SetName(string name) 46 | { 47 | Name = name; 48 | return this; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Actions/BAction.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 38298dddab3b6b341ac6a04c367b80c3 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/BehaviorComponent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace BehaviorLibrary 5 | { 6 | public abstract class BehaviorComponent: ICloneable 7 | { 8 | #if UNITY_EDITOR 9 | public BehaviorNode Node; 10 | public bool NotifyOnExecute; 11 | #endif 12 | 13 | public Action AddToHistory; 14 | 15 | public virtual BehaviorComponent[] Behaviors 16 | { 17 | get { return behaviors; } 18 | } 19 | protected BehaviorComponent[] behaviors; 20 | 21 | protected BehaviorComponent AssignBehaviors(BehaviorComponent[] behaviorComponents) 22 | { 23 | // This is done to prevent previewer errors 24 | #if UNITY_EDITOR 25 | behaviors = new BehaviorComponent[behaviorComponents.Length]; 26 | for (int i = 0; i < behaviorComponents.Length; i++) 27 | { 28 | if (behaviorComponents[i] == null) 29 | { 30 | Debug.Log("Error during iteration: " + i); 31 | } 32 | behaviors[i] = (BehaviorComponent)behaviorComponents[i].Clone(); 33 | } 34 | #else 35 | behaviors = behaviors; 36 | #endif 37 | return this; 38 | } 39 | 40 | public virtual string Name 41 | { 42 | get { return name; } 43 | set { name = value; } 44 | } 45 | protected string name; 46 | 47 | public Status Status 48 | { 49 | get { return status; } 50 | set 51 | { 52 | status = value; 53 | #if UNITY_EDITOR 54 | if (NotifyOnExecute) 55 | { 56 | Debug.Log(Name + "Was Executed at " + Time.time + "Status was " + Status); 57 | } 58 | #endif 59 | } 60 | } 61 | private Status status; 62 | 63 | public BehaviorComponent(){} 64 | 65 | public abstract Status Execute(); 66 | 67 | public object Clone() 68 | { 69 | return MemberwiseClone(); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/BehaviorComponent.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e09b8057e23f87847986629e94d4fd3c 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c154a76cbe402a6459ed3117041a11bb 3 | folderAsset: yes 4 | timeCreated: 1468198403 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/Selector.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | public class Selector : BehaviorComponent 4 | { 5 | public Selector(params BehaviorComponent[] behaviors) 6 | { 7 | AssignBehaviors(behaviors); 8 | Name = "Selector"; 9 | } 10 | 11 | public override Status Execute() 12 | { 13 | AddToHistory(this); 14 | for (int i = 0; i < behaviors.Length; i++) 15 | { 16 | switch (behaviors[i].Execute()) 17 | { 18 | case Status.Failure: 19 | Status = Status.Failure;; 20 | continue; 21 | case Status.Success: 22 | Status = Status.Success; 23 | return Status; 24 | case Status.Running: 25 | Status = Status.Running; 26 | return Status; 27 | default: 28 | continue; 29 | } 30 | } 31 | 32 | Status = Status.Failure; 33 | return Status; 34 | } 35 | 36 | public Selector SetName(string name) 37 | { 38 | Name = name; 39 | return this; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/Selector.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 617d6374afb7ca845a2821c6ba31700e 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/SelectorBranch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BehaviorLibrary 4 | { 5 | public class SelectorBranch : BehaviorComponent 6 | { 7 | private Func Index; 8 | 9 | public SelectorBranch(Func index, params BehaviorComponent[] behaviors) 10 | { 11 | Index = index; 12 | AssignBehaviors(behaviors); 13 | Name = "Branch Selector"; 14 | } 15 | 16 | public override Status Execute() 17 | { 18 | AddToHistory(this); 19 | switch (Behaviors[Index.Invoke()].Execute()) 20 | { 21 | case Status.Failure: 22 | Status = Status.Failure; 23 | return Status; 24 | case Status.Success: 25 | Status = Status.Success; 26 | return Status; 27 | case Status.Running: 28 | Status = Status.Running; 29 | return Status; 30 | default: 31 | Status = Status.Running; 32 | return Status; 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/SelectorBranch.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b47598507f82ec348a9581afbfe7d5c7 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/SelectorContinued.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | public class SelectorContinued : BehaviorComponent 4 | { 5 | private short selections = 0; 6 | 7 | private short selLength = 0; 8 | 9 | public SelectorContinued(params BehaviorComponent[] behaviors) 10 | { 11 | AssignBehaviors(behaviors); 12 | selLength = (short)Behaviors.Length; 13 | Name = "ContinuedSelector"; 14 | } 15 | 16 | public SelectorContinued SetName(string name) 17 | { 18 | Name = name; 19 | return this; 20 | } 21 | 22 | public override Status Execute() 23 | { 24 | AddToHistory(this); 25 | while (selections < selLength) 26 | { 27 | switch (Behaviors[selections].Execute()) 28 | { 29 | case Status.Failure: 30 | selections++; 31 | Status = Status.Running; 32 | return Status; 33 | case Status.Success: 34 | selections = 0; 35 | Status = Status.Success; 36 | return Status; 37 | case Status.Running: 38 | Status = Status.Running; 39 | return Status; 40 | default: 41 | selections++; 42 | Status = Status.Failure; 43 | return Status; 44 | } 45 | } 46 | 47 | selections = 0; 48 | Status = Status.Failure; 49 | return Status; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/SelectorContinued.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7c57148ad9349de4c976354d265f8963 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/SelectorRandom.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BehaviorLibrary 4 | { 5 | public class SelectorRandom : BehaviorComponent 6 | { 7 | private System.Random random = new System.Random(DateTime.Now.Millisecond); 8 | 9 | public SelectorRandom(params BehaviorComponent[] behaviors) 10 | { 11 | AssignBehaviors(behaviors); 12 | Name = "RandomSelector"; 13 | } 14 | 15 | public override Status Execute() 16 | { 17 | AddToHistory(this); 18 | random = new System.Random(DateTime.Now.Millisecond); 19 | 20 | switch (behaviors[random.Next(0, behaviors.Length - 1)].Execute()) 21 | { 22 | case Status.Failure: 23 | Status = Status.Failure; 24 | return Status; 25 | case Status.Success: 26 | Status = Status.Success; 27 | return Status; 28 | case Status.Running: 29 | Status = Status.Running; 30 | return Status; 31 | default: 32 | Status = Status.Failure; 33 | return Status; 34 | } 35 | } 36 | 37 | public SelectorRandom SetName(string name) 38 | { 39 | Name = name; 40 | return this; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/SelectorRandom.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f1b76a373288f684cb51bf0e54a3103f 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/Sequence.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | public class Sequence : BehaviorComponent 4 | { 5 | public Sequence(params BehaviorComponent[] behaviors) 6 | { 7 | AssignBehaviors(behaviors); 8 | Name = "Sequence"; 9 | } 10 | 11 | public override Status Execute() 12 | { 13 | AddToHistory(this); 14 | //add watch for any running behaviors 15 | bool anyRunning = false; 16 | 17 | for(int i = 0; i < behaviors.Length;i++) 18 | { 19 | switch (behaviors[i].Execute()) 20 | { 21 | case Status.Failure: 22 | Status = Status.Failure; 23 | return Status; 24 | case Status.Success: 25 | Status = Status.Success; 26 | continue; 27 | case Status.Running: 28 | anyRunning = true; 29 | Status = Status.Running; 30 | continue; 31 | default: 32 | Status = Status.Success; 33 | return Status; 34 | } 35 | } 36 | 37 | //TODO: Review this 38 | //if none running, return success, otherwise return running 39 | Status = !anyRunning ? Status.Success : Status.Running; 40 | return Status; 41 | } 42 | 43 | public Sequence SetName(string name) 44 | { 45 | Name = name; 46 | return this; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/Sequence.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c41b96d2b4a115843930807c191e4fc3 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/SequenceContinued.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | public class SequenceContinued : BehaviorComponent 4 | { 5 | private short sequence = 0; 6 | 7 | private short seqLength = 0; 8 | 9 | public SequenceContinued(params BehaviorComponent[] behaviors) 10 | { 11 | AssignBehaviors(behaviors); 12 | seqLength = (short) Behaviors.Length; 13 | Name = "PartialSequence"; 14 | } 15 | 16 | public override Status Execute() 17 | { 18 | AddToHistory(this); 19 | 20 | while (sequence < seqLength) 21 | { 22 | switch (Behaviors[sequence].Execute()) 23 | { 24 | case Status.Failure: 25 | sequence = 0; 26 | Status = Status.Failure; 27 | return Status; 28 | case Status.Success: 29 | sequence++; 30 | Status = Status.Running; 31 | return Status; 32 | case Status.Running: 33 | Status = Status.Running; 34 | return Status; 35 | } 36 | 37 | } 38 | 39 | sequence = 0; 40 | Status = Status.Success; 41 | return Status; 42 | } 43 | 44 | public SequenceContinued SetName(string name) 45 | { 46 | Name = name; 47 | return this; 48 | } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/SequenceContinued.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 61f44fa37345e774cad2f55dbc0cbc85 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/StatefulSelector.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | public class StatefulSelector : BehaviorComponent 4 | { 5 | private int lastBehavior = 0; 6 | 7 | public StatefulSelector(params BehaviorComponent[] behaviors) 8 | { 9 | AssignBehaviors(behaviors); 10 | Name = "StatefulSelector"; 11 | } 12 | 13 | public override Status Execute() 14 | { 15 | AddToHistory(this); 16 | 17 | for (; lastBehavior < behaviors.Length; lastBehavior++) 18 | { 19 | switch (behaviors[lastBehavior].Execute()) 20 | { 21 | case Status.Failure: 22 | Status = Status.Failure; 23 | continue; 24 | case Status.Success: 25 | lastBehavior = 0; 26 | Status = Status.Success; 27 | return Status; 28 | case Status.Running: 29 | Status = Status.Running; 30 | return Status; 31 | default: 32 | continue; 33 | } 34 | } 35 | 36 | lastBehavior = 0; 37 | Status = Status.Failure; 38 | return Status; 39 | } 40 | 41 | public StatefulSelector SetName(string name) 42 | { 43 | Name = name; 44 | return this; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/StatefulSelector.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 251c32c2f68cd624cb438abf8379c01f 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/StatefulSequence.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | public class StatefulSequence : BehaviorComponent 4 | { 5 | private int lastBehavior = 0; 6 | 7 | public StatefulSequence(params BehaviorComponent[] behaviors) 8 | { 9 | AssignBehaviors(behaviors); 10 | Name = "StatefulSequence"; 11 | } 12 | 13 | public override Status Execute() 14 | { 15 | AddToHistory(this); 16 | //start from last remembered position 17 | for (; lastBehavior < behaviors.Length; lastBehavior++) 18 | { 19 | switch (behaviors[lastBehavior].Execute()) 20 | { 21 | case Status.Failure: 22 | lastBehavior = 0; 23 | Status = Status.Failure; 24 | return Status; 25 | case Status.Success: 26 | Status = Status.Success; 27 | continue; 28 | case Status.Running: 29 | Status = Status.Running; 30 | return Status; 31 | default: 32 | lastBehavior = 0; 33 | Status = Status.Success; 34 | return Status; 35 | } 36 | } 37 | 38 | lastBehavior = 0; 39 | Status = Status.Success; 40 | return Status; 41 | } 42 | 43 | public StatefulSequence SetName(string name) 44 | { 45 | Name = name; 46 | return this; 47 | } 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Composites/StatefulSequence.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 03ab81b06e90cc34a91877ebf5f77fff 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Conditionals.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 53a302ec0268ac1429cdb508f62a56c1 3 | folderAsset: yes 4 | timeCreated: 1468198403 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Conditionals/ConditionalGameObjectDistance.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace BehaviorLibrary 5 | { 6 | public class ConditionalGameObjectDistance : BehaviorComponent 7 | { 8 | 9 | public GameObject ValueA; 10 | public GameObject ValueB; 11 | public float Distance; 12 | public ConditionType Condition; 13 | 14 | public override BehaviorComponent[] Behaviors 15 | { 16 | get { return null; } 17 | } 18 | 19 | public ConditionalGameObjectDistance(GameObject a, GameObject b, float d, ConditionType condition) 20 | { 21 | ValueA = a; 22 | ValueB = b; 23 | Distance = d; 24 | Condition = condition; 25 | Name = "Conditional GameObject Distance"; 26 | } 27 | 28 | public ConditionalGameObjectDistance(Transform a, Transform b, float d, ConditionType condition) 29 | { 30 | ValueA = a.gameObject; 31 | ValueB = b.gameObject; 32 | Distance = d; 33 | Condition = condition; 34 | Name = "Conditional GameObject Distance"; 35 | } 36 | 37 | public ConditionalGameObjectDistance(GameObject a, Transform b, float d, ConditionType condition) 38 | { 39 | ValueA = a; 40 | ValueB = b.gameObject; 41 | Distance = d; 42 | Condition = condition; 43 | Name = "Conditional GameObject Distance"; 44 | } 45 | 46 | public ConditionalGameObjectDistance(Transform a, GameObject b, float d, ConditionType condition) 47 | { 48 | ValueA = a.gameObject; 49 | ValueB = b; 50 | Distance = d; 51 | Condition = condition; 52 | Name = "Conditional GameObject Distance"; 53 | } 54 | 55 | public override Status Execute() 56 | { 57 | AddToHistory(this); 58 | 59 | switch (Test()) 60 | { 61 | case true: 62 | Status = Status.Success; 63 | return Status; 64 | case false: 65 | Status = Status.Failure; 66 | return Status; 67 | default: 68 | Status = Status.Failure; 69 | return Status; 70 | } 71 | } 72 | 73 | public bool Test() 74 | { 75 | if (ValueA == null) 76 | { 77 | throw new Exception("ValueA is null"); 78 | } 79 | if (ValueB == null) 80 | { 81 | throw new Exception("ValueB is null"); 82 | } 83 | switch (Condition) 84 | { 85 | case ConditionType.Greater: 86 | return Vector3.Distance(ValueA.transform.position, ValueB.transform.position) > Distance; 87 | case ConditionType.GreaterOrEqual: 88 | return Vector3.Distance(ValueA.transform.position, ValueB.transform.position) >= Distance; 89 | case ConditionType.Lesser: 90 | return Vector3.Distance(ValueA.transform.position, ValueB.transform.position) < Distance; 91 | case ConditionType.LesserOrEqual: 92 | return Vector3.Distance(ValueA.transform.position, ValueB.transform.position) <= Distance; 93 | case ConditionType.Equals: 94 | return Vector3.Distance(ValueA.transform.position, ValueB.transform.position) == Distance; 95 | case ConditionType.NotEqual: 96 | return Vector3.Distance(ValueA.transform.position, ValueB.transform.position) != Distance; 97 | } 98 | return false; 99 | } 100 | 101 | public ConditionalGameObjectDistance SetName(string name) 102 | { 103 | Name = name; 104 | return this; 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Conditionals/ConditionalGameObjectDistance.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fa8f97ffc7439d945aa87e0b7aa01219 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Conditionals/ConditionalGeneric.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BehaviorLibrary 4 | { 5 | public class ConditionalGeneric : BehaviorComponent where T : IComparable 6 | { 7 | public T ValueA; 8 | public T ValueB; 9 | public ConditionType Condition; 10 | 11 | public override BehaviorComponent[] Behaviors 12 | { 13 | get { return null; } 14 | } 15 | 16 | public ConditionalGeneric(T a, T b, ConditionType condition) 17 | { 18 | ValueA = a; 19 | ValueB = b; 20 | Condition = condition; 21 | Name = "Conditional Generic"; 22 | } 23 | 24 | public override Status Execute() 25 | { 26 | AddToHistory(this); 27 | switch (Test()) 28 | { 29 | case true: 30 | Status = Status.Success; 31 | return Status; 32 | case false: 33 | Status = Status.Failure; 34 | return Status; 35 | default: 36 | Status = Status.Failure; 37 | return Status; 38 | } 39 | } 40 | 41 | public bool Test() 42 | { 43 | switch (Condition) 44 | { 45 | case ConditionType.Greater: 46 | return ValueA.CompareTo(ValueB) > 0; 47 | case ConditionType.GreaterOrEqual: 48 | return ValueA.CompareTo(ValueB) >= 0; 49 | case ConditionType.Lesser: 50 | return ValueA.CompareTo(ValueB) < 0; 51 | case ConditionType.LesserOrEqual: 52 | return ValueA.CompareTo(ValueB) <= 0; 53 | case ConditionType.Equals: 54 | return ValueA.CompareTo(ValueB) == 0; 55 | case ConditionType.NotEqual: 56 | return ValueA.CompareTo(ValueB) != 0; 57 | } 58 | return false; 59 | } 60 | 61 | public ConditionalGeneric SetName(string name) 62 | { 63 | Name = name; 64 | return this; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Conditionals/ConditionalGeneric.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0fedd5342d853bb47acd2bb0402cb5c3 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Conditionals/ConditionalLambda.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq.Expressions; 4 | using Object = System.Object; 5 | 6 | namespace BehaviorLibrary 7 | { 8 | public class ConditionalLambda : BehaviorComponent 9 | { 10 | #if UNITY_EDITOR 11 | public Expression> Expression; 12 | public Action TestAction; 13 | public List ExpressionObjects = new List(); 14 | public string ConditionalDescription; 15 | #endif 16 | 17 | public Func TestFunc; 18 | public bool Result; 19 | 20 | 21 | public override BehaviorComponent[] Behaviors 22 | { 23 | get { return null; } 24 | } 25 | 26 | #if !UNITY_EDITOR 27 | public ConditionalLambda(Func test) 28 | { 29 | TestFunc = test; 30 | Name = "ConditionalLambda"; 31 | } 32 | #endif 33 | 34 | #if UNITY_EDITOR 35 | public ConditionalLambda(Action test) 36 | { 37 | TestAction = test; 38 | Name = "ConditionalLambda"; 39 | } 40 | 41 | public ConditionalLambda(Expression> test) 42 | { 43 | Expression = test; 44 | Name = "ConditionalLambda"; 45 | } 46 | #endif 47 | 48 | public override Status Execute() 49 | { 50 | AddToHistory(this); 51 | 52 | #if !UNITY_EDITOR 53 | if (TestFunc != null) 54 | { 55 | Result = TestFunc.Invoke(); 56 | } 57 | #endif 58 | 59 | #if UNITY_EDITOR 60 | if (Expression != null) 61 | { 62 | Expression.Compile().Invoke(); 63 | } 64 | if (TestAction != null) 65 | { 66 | ExpressionObjects.Clear(); 67 | TestAction.Invoke(this); 68 | } 69 | #endif 70 | 71 | switch (Result) 72 | { 73 | case true: 74 | Status = Status.Success; 75 | return Status; 76 | case false: 77 | Status = Status.Failure; 78 | return Status; 79 | default: 80 | Status = Status.Failure; 81 | return Status; 82 | } 83 | } 84 | 85 | public ConditionalLambda SetName(string name) 86 | { 87 | Name = name; 88 | return this; 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Conditionals/ConditionalLambda.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 984d9a6fcef47f74097ca2919c561b5b 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Conditionals/ConditionalVector3Distance.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace BehaviorLibrary 5 | { 6 | public class ConditionalVector3Distance : BehaviorComponent 7 | { 8 | 9 | public Vector3 ValueA; 10 | public Vector3 ValueB; 11 | public float Distance; 12 | public ConditionType Condition; 13 | 14 | public override BehaviorComponent[] Behaviors 15 | { 16 | get { return null; } 17 | } 18 | 19 | public ConditionalVector3Distance(Vector3 a, Vector3 b, float d, ConditionType condition) 20 | { 21 | ValueA = a; 22 | ValueB = b; 23 | Distance = d; 24 | Condition = condition; 25 | Name = "Conditional Vector3 Distance"; 26 | } 27 | 28 | public override Status Execute() 29 | { 30 | AddToHistory(this); 31 | switch (Test()) 32 | { 33 | case true: 34 | Status = Status.Success; 35 | return Status; 36 | case false: 37 | Status = Status.Failure; 38 | return Status; 39 | default: 40 | Status = Status.Failure; 41 | return Status; 42 | } 43 | } 44 | 45 | public bool Test() 46 | { 47 | if (ValueA == null) 48 | { 49 | throw new Exception("ValueA is null"); 50 | } 51 | if (ValueB == null) 52 | { 53 | throw new Exception("ValueB is null"); 54 | } 55 | switch (Condition) 56 | { 57 | case ConditionType.Greater: 58 | return Vector3.Distance(ValueA, ValueB) > Distance; 59 | case ConditionType.GreaterOrEqual: 60 | return Vector3.Distance(ValueA, ValueB) >= Distance; 61 | case ConditionType.Lesser: 62 | return Vector3.Distance(ValueA, ValueB) < Distance; 63 | case ConditionType.LesserOrEqual: 64 | return Vector3.Distance(ValueA, ValueB) <= Distance; 65 | case ConditionType.Equals: 66 | return Vector3.Distance(ValueA, ValueB) == Distance; 67 | case ConditionType.NotEqual: 68 | return Vector3.Distance(ValueA, ValueB) != Distance; 69 | } 70 | return false; 71 | } 72 | 73 | public ConditionalVector3Distance SetName(string name) 74 | { 75 | Name = name; 76 | return this; 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Conditionals/ConditionalVector3Distance.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 94c8a554a56629244ac7be275cfd082a 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f5bf83d5178bfd74b8c20bc762648ed8 3 | folderAsset: yes 4 | timeCreated: 1468198403 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/Alterable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace BehaviorLibrary 4 | { 5 | public class Alterable : BehaviorComponent 6 | { 7 | 8 | public string ID; 9 | public BehaviorTree BehaviorTree; 10 | 11 | public Alterable(BehaviorComponent behavior, string id) 12 | { 13 | Name = "Atlerable"; 14 | ID = id; 15 | if (behavior is Alterable) 16 | { 17 | Debug.LogError("Cannot have Alterable BehaviorComponent child of Alterable"); 18 | return; 19 | } 20 | AssignBehaviors(new[] { behavior }); 21 | } 22 | 23 | ~Alterable() 24 | { 25 | BehaviorTree.AlterableComponents.Remove(ID); 26 | } 27 | 28 | public override Status Execute() 29 | { 30 | AddToHistory(this); 31 | return Status = behaviors[0].Execute(); 32 | } 33 | 34 | public Alterable SetName(string name) 35 | { 36 | Name = name; 37 | return this; 38 | } 39 | 40 | public override string Name 41 | { 42 | get { return name + ":" + ID; } 43 | set { name = value; } 44 | } 45 | 46 | public BehaviorComponent UpdateBehaviors(BehaviorComponent behavior) 47 | { 48 | if (behavior is Alterable) 49 | { 50 | Debug.LogError("Cannot have Alterable Decorator as Child of Alterable"); 51 | return this; 52 | } 53 | #if UNITY_EDITOR 54 | behaviors = new[] { (BehaviorComponent) behavior.Clone() }; 55 | #else 56 | behaviors = new[] { behavior }; 57 | #endif 58 | //BehaviorTree.RefreshBehaviorTree(); 59 | return this; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/Alterable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7fc036a55548d2a4ea6481bb9fe2af54 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/ChanceFailure.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BehaviorLibrary 4 | { 5 | public class ChanceFailure : BehaviorComponent 6 | { 7 | 8 | private float probability; 9 | 10 | private Func randomFunction; 11 | 12 | public ChanceFailure(float probability, Func randomFunction, BehaviorComponent behavior) 13 | { 14 | this.probability = probability; 15 | this.randomFunction = randomFunction; 16 | AssignBehaviors(new[] {behavior}); 17 | Name = "ChanceFailure"; 18 | } 19 | 20 | 21 | public override Status Execute() 22 | { 23 | AddToHistory(this); 24 | 25 | if (randomFunction.Invoke() <= probability) 26 | { 27 | Status = behaviors[0].Execute(); 28 | return Status; 29 | } 30 | else 31 | { 32 | Status = Status.Failure; 33 | return Status; 34 | } 35 | } 36 | 37 | public ChanceFailure SetName(string name) 38 | { 39 | Name = name; 40 | return this; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/ChanceFailure.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bf03531c887d32741a972ff6ab9c6f74 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/ChanceRunning.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BehaviorLibrary 4 | { 5 | public class ChanceRunning : BehaviorComponent 6 | { 7 | 8 | private float probability; 9 | 10 | private Func randomFunction; 11 | 12 | public ChanceRunning(float probability, Func randomFunction, BehaviorComponent behavior) 13 | { 14 | this.probability = probability; 15 | this.randomFunction = randomFunction; 16 | AssignBehaviors(new[] { behavior }); 17 | Name = "ChanceRunning"; 18 | } 19 | 20 | 21 | public override Status Execute() 22 | { 23 | AddToHistory(this); 24 | 25 | if (randomFunction.Invoke() <= probability) 26 | { 27 | Status = behaviors[0].Execute(); 28 | return Status; 29 | } 30 | else 31 | { 32 | Status = Status.Running; 33 | return Status.Running; 34 | } 35 | } 36 | 37 | public ChanceRunning SetName(string name) 38 | { 39 | Name = name; 40 | return this; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/ChanceRunning.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 94afac987a4678b4ca72a9f83261e10e 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/Counter.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | /// 4 | /// Will not Execute behavior till max iterations are met 5 | /// 6 | public class Counter : BehaviorComponent 7 | { 8 | private int maxIterations; 9 | private int iteration = 0; 10 | 11 | public Counter(int maxIterations, BehaviorComponent behavior) 12 | { 13 | this.maxIterations = maxIterations; 14 | AssignBehaviors(new[] { behavior }); 15 | Name = "Counter"; 16 | } 17 | 18 | public override Status Execute() 19 | { 20 | AddToHistory(this); 21 | 22 | if (iteration < maxIterations) 23 | { 24 | iteration++; 25 | Status = Status.Running; 26 | return Status.Running; 27 | } 28 | else 29 | { 30 | iteration = 0; 31 | Status = behaviors[0].Execute(); 32 | return Status; 33 | } 34 | } 35 | 36 | public Counter SetName(string name) 37 | { 38 | Name = name; 39 | return this; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/Counter.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 22606b6b3f00a4042b6ab2568f5bb7b5 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/Inverter.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | public class Inverter : BehaviorComponent 4 | { 5 | 6 | public Inverter(BehaviorComponent behavior) 7 | { 8 | AssignBehaviors(new[] { behavior }); 9 | Name = "Inverter"; 10 | } 11 | 12 | public override Status Execute() 13 | { 14 | AddToHistory(this); 15 | 16 | switch (behaviors[0].Execute()) 17 | { 18 | case Status.Failure: 19 | Status = Status.Success; 20 | return Status; 21 | case Status.Success: 22 | Status = Status.Failure; 23 | return Status; 24 | case Status.Running: 25 | Status = Status.Running; 26 | return Status; 27 | } 28 | 29 | Status = Status.Success; 30 | return Status; 31 | } 32 | 33 | public Inverter SetName(string name) 34 | { 35 | Name = name; 36 | return this; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/Inverter.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d58229137ad7eb444aa1a2e0983ad30f 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/RepeatUntilFailureConstant.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | /// 4 | /// Repeats Behavior in single execution till failure or max iteration is met 5 | /// 6 | public class RepeatUntilFailureConstant : BehaviorComponent 7 | { 8 | private int maxIterations; 9 | private int iteration = 0; 10 | 11 | public RepeatUntilFailureConstant(int maxIterations, BehaviorComponent behavior) 12 | { 13 | this.maxIterations = maxIterations; 14 | AssignBehaviors(new[] { behavior }); 15 | Name = "Counter"; 16 | } 17 | 18 | public override Status Execute() 19 | { 20 | AddToHistory(this); 21 | for (int i = 0; i < maxIterations; i++) 22 | { 23 | if (behaviors[0].Execute() == Status.Failure) 24 | { 25 | Status = Status.Success; 26 | return Status.Success; 27 | } 28 | } 29 | Status = Status.Failure; 30 | return Status.Failure; 31 | } 32 | 33 | public RepeatUntilFailureConstant SetName(string name) 34 | { 35 | Name = name; 36 | return this; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/RepeatUntilFailureConstant.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bd2942d349b5b8e49b88c085f8739999 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/RepeatUntilFailureContinued.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | /// 4 | /// Repeats behavior till failure or max iteration is met 5 | /// 6 | public class RepeaterUntilFailureContinued : BehaviorComponent 7 | { 8 | private int maxIterations; 9 | private int iteration = 0; 10 | 11 | public RepeaterUntilFailureContinued(int maxIterations, BehaviorComponent behavior) 12 | { 13 | this.maxIterations = maxIterations; 14 | AssignBehaviors(new[] { behavior }); 15 | Name = "RepeaterContinued"; 16 | } 17 | 18 | public override Status Execute() 19 | { 20 | AddToHistory(this); 21 | if (iteration <= maxIterations) 22 | { 23 | iteration++; 24 | if (behaviors[0].Execute() == Status.Failure) 25 | { 26 | Status = Status.Success; 27 | return Status.Success; 28 | } 29 | Status = Status.Running; 30 | return Status.Running; 31 | } 32 | iteration = 0; 33 | Status = Status.Failure; 34 | return Status.Failure; 35 | } 36 | 37 | public RepeaterUntilFailureContinued SetName(string name) 38 | { 39 | Name = name; 40 | return this; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/RepeatUntilFailureContinued.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3d5af5a6974764c47849c996e5326bbe 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/RepeatUntilSuccessConstant.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | /// 4 | /// Repeats Behavior in single execution till Success or max iteration is met 5 | /// 6 | public class RepeatUntilSuccessConstant : BehaviorComponent 7 | { 8 | private int maxIterations; 9 | private int iteration = 0; 10 | 11 | public RepeatUntilSuccessConstant(int maxIterations, BehaviorComponent behavior) 12 | { 13 | this.maxIterations = maxIterations; 14 | AssignBehaviors(new[] { behavior }); 15 | Name = "Counter"; 16 | } 17 | 18 | public override Status Execute() 19 | { 20 | AddToHistory(this); 21 | for (int i = 0; i < maxIterations; i++) 22 | { 23 | if (behaviors[0].Execute() == Status.Success) 24 | { 25 | Status = Status.Success; 26 | return Status; 27 | } 28 | } 29 | Status = Status.Failure; 30 | return Status; 31 | } 32 | 33 | public RepeatUntilSuccessConstant SetName(string name) 34 | { 35 | Name = name; 36 | return this; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/RepeatUntilSuccessConstant.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7f2349ab0aa91ba4aac3bbeb61825c3e 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/RepeatUntilSuccessContinued.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | /// 4 | /// Repeats behavior till Success or max iteration is met 5 | /// 6 | public class RepeaterUntilSuccessContinued : BehaviorComponent 7 | { 8 | private int maxIterations; 9 | private int iteration = 0; 10 | 11 | public RepeaterUntilSuccessContinued(int maxIterations, BehaviorComponent behavior) 12 | { 13 | this.maxIterations = maxIterations; 14 | AssignBehaviors(new[] { behavior }); 15 | Name = "RepeaterContinued"; 16 | } 17 | 18 | public override Status Execute() 19 | { 20 | AddToHistory(this); 21 | if (iteration <= maxIterations) 22 | { 23 | iteration++; 24 | if (behaviors[0].Execute() == Status.Success) 25 | { 26 | Status = Status.Success; 27 | return Status; 28 | } 29 | Status = Status.Running; 30 | return Status; 31 | } 32 | iteration = 0; 33 | Status = Status.Failure; 34 | return Status; 35 | } 36 | 37 | public RepeaterUntilSuccessContinued SetName(string name) 38 | { 39 | Name = name; 40 | return this; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/RepeatUntilSuccessContinued.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: abe003fa5cca38344ba88fff86ef37c9 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/Timer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace BehaviorLibrary 4 | { 5 | public class Timer : BehaviorComponent 6 | { 7 | public override string Name 8 | { 9 | get { return name + " " + (waitTime - timeElapsed); } 10 | set { name = value; } 11 | } 12 | 13 | private float lastTime = 0; 14 | private float timeElapsed = 0; 15 | 16 | private float waitTime; 17 | 18 | public Timer(float timeToWait, BehaviorComponent behavior) 19 | { 20 | AssignBehaviors(new[] { behavior }); 21 | waitTime = timeToWait; 22 | Name = "Timer"; 23 | } 24 | 25 | public override Status Execute() 26 | { 27 | AddToHistory(this); 28 | timeElapsed = Time.time - lastTime; 29 | if (timeElapsed >= waitTime) 30 | { 31 | timeElapsed = 0; 32 | lastTime = Time.time; 33 | Status = behaviors[0].Execute(); 34 | return Status; 35 | } 36 | else 37 | { 38 | Status = Status.Running; 39 | return Status; 40 | } 41 | } 42 | 43 | public Timer SetName(string name) 44 | { 45 | Name = name; 46 | return this; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/Timer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ba6b7da731875e7459747d2b36282c2e 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/UntilFailure.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | /// 4 | /// Repeats behavior till return status is Failure 5 | /// 6 | public class UntilFailure : BehaviorComponent 7 | { 8 | public UntilFailure(BehaviorComponent behavior) 9 | { 10 | AssignBehaviors(new[] { behavior }); 11 | Name = "UntilFail"; 12 | } 13 | 14 | public override Status Execute() 15 | { 16 | AddToHistory(this); 17 | switch (behaviors[0].Execute()) 18 | { 19 | case Status.Success: 20 | case Status.Running: 21 | Status = Status.Running; 22 | return Status; 23 | case Status.Failure: 24 | Status = Status.Failure; 25 | return Status; 26 | } 27 | return Status.Running; 28 | } 29 | 30 | public UntilFailure SetName(string name) 31 | { 32 | Name = name; 33 | return this; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/UntilFailure.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: dc64babf9a2205c42876b0e4297e8b17 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/UntilSuccess.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | /// 4 | /// Repeats behavior till return status is Success 5 | /// 6 | public class UntilSuccess : BehaviorComponent 7 | { 8 | public UntilSuccess(BehaviorComponent behavior) 9 | { 10 | AssignBehaviors(new[] { behavior }); 11 | Name = "RepeaterContinued"; 12 | } 13 | 14 | public override Status Execute() 15 | { 16 | AddToHistory(this); 17 | switch (behaviors[0].Execute()) 18 | { 19 | case Status.Failure: 20 | case Status.Running: 21 | Status = Status.Running; 22 | return Status; 23 | case Status.Success: 24 | Status = Status.Success; 25 | return Status; 26 | } 27 | return Status.Running; 28 | } 29 | 30 | public UntilSuccess SetName(string name) 31 | { 32 | Name = name; 33 | return this; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Components/Decorators/UntilSuccess.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 10fe3817fd3421548a425dd259a73741 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/ConditionalType.cs: -------------------------------------------------------------------------------- 1 | namespace BehaviorLibrary 2 | { 3 | public enum ConditionType 4 | { 5 | Greater, 6 | GreaterOrEqual, 7 | Lesser, 8 | LesserOrEqual, 9 | Equals, 10 | NotEqual 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/ConditionalType.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 93847185f733c1f409eeb08c6c4785f9 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3041518081b684543a955687d4d135c1 3 | folderAsset: yes 4 | timeCreated: 1468198403 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Editor/PreviewEditorWindow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using BehaviorLibrary; 4 | using UnityEditor; 5 | using UnityEngine; 6 | using System.Collections.Generic; 7 | 8 | namespace BehaviorLibary.Editor 9 | { 10 | public class PreviewEditorWindow : EditorWindow 11 | { 12 | public static BehaviorTree Behavior; 13 | private static List nodes = new List(); 14 | 15 | private static float x; 16 | private static float y; 17 | 18 | public const float CanvasSize = 50000; 19 | public static Color GridMinorColor = new Color(0,0,0, 0.18f); 20 | public static Color GridMajorColor = new Color(0, 0, 0, 0.28f); 21 | public static Rect GraphRect; 22 | public static Rect ViewRect = new Rect(0,0,50000,50000); 23 | 24 | private static Rect History; 25 | private static Rect SelectedNode; 26 | 27 | // Selected Node Properties 28 | private Type type; 29 | private FieldInfo[] fields; 30 | 31 | public static void Launch(BehaviorTree behaviour) 32 | { 33 | GraphRect = new Rect(0, 0, CanvasSize, CanvasSize); 34 | nodes.Clear(); 35 | x = 100; 36 | y = 60; 37 | Behavior = behaviour; 38 | Behavior.BehaviorTreeUpdated = Launch; 39 | GetWindow().titleContent.text = "Behavior Tree Previewer"; 40 | GetNodes(); 41 | } 42 | 43 | private static void GetNodes() 44 | { 45 | Debug.Log("Getting Nodes"); 46 | nodes.Clear(); 47 | BehaviorNode node = new BehaviorNode(Behavior.Root, new Vector2( x, y )); 48 | nodes.Add(node); 49 | GetBehaviorChildNodes(Behavior.Root, node); 50 | } 51 | 52 | private static void GetBehaviorChildNodes(BehaviorComponent behaviorComponent, BehaviorNode topNode) 53 | { 54 | y = nodes[0].Position.y; 55 | for (int i = 0; i < nodes.Count; i++) 56 | { 57 | if (y < nodes[i].Position.y) 58 | { 59 | y = nodes[i].Position.y; 60 | } 61 | } 62 | if (y != topNode.Position.y) 63 | { 64 | y = y + 35; 65 | } 66 | 67 | topNode.Position = new Vector2(topNode.Position.x, y); 68 | x += 200; 69 | if (behaviorComponent.Behaviors != null) 70 | { 71 | float _y = y; 72 | for (int i = 0; i < nodes.Count; i++) 73 | { 74 | if (nodes[i].Position == new Vector2(x, y)) 75 | { 76 | _y = _y + 35; 77 | topNode.Position = new Vector2(topNode.Position.x, _y); 78 | } 79 | } 80 | BehaviorNode previousNode = null; 81 | for (int i = 0; i < behaviorComponent.Behaviors.Length; i++) 82 | { 83 | BehaviorComponent behavior = behaviorComponent.Behaviors[i]; 84 | BehaviorNode node = new BehaviorNode(behavior, new Vector2(x, (_y + (35 * i)))); 85 | if (previousNode != null) 86 | { 87 | node.Position = new Vector2(x, previousNode.Position.y + 35); 88 | } 89 | node.BehaviorComponent = behavior; 90 | node.TopNode = topNode; 91 | nodes.Add(node); 92 | 93 | if (behavior.Behaviors != null) 94 | { 95 | GetBehaviorChildNodes(behavior, node); 96 | } 97 | previousNode = node; 98 | } 99 | } 100 | x -= 200; 101 | } 102 | 103 | void OnGUI() 104 | { 105 | DrawGraphGUI(); 106 | 107 | BeginWindows(); 108 | 109 | if (!Behavior) 110 | return; 111 | 112 | for (int i = 0; i < nodes.Count; i++) 113 | { 114 | if (!Behavior.History.Contains(nodes[i].BehaviorComponent)) 115 | { 116 | nodes[i].OnGUI(); 117 | } 118 | } 119 | 120 | if (BehaviorHasUpdated()) 121 | { 122 | History = GUILayout.Window(1, new Rect(position.width - (10 + History.width), 10, 150, 10), DrawHistoryWindow, "Behavior Tree History"); 123 | 124 | if (BehaviorNode.Selection != null) 125 | { 126 | //TODO: make this public static so selected BehaviorNode can call it. 127 | GetSelectedNodeFields(); 128 | SelectedNode = GUILayout.Window(2, new Rect(10, 10, 150, 10), DrawNodeSelected, BehaviorNode.Selection.BehaviorComponent.Name); 129 | } 130 | 131 | for (int i = 0; i < Behavior.History.Count; i++) 132 | { 133 | Behavior.History[i].Node.OnHistoryGUI(); 134 | } 135 | } 136 | GUI.changed = false; 137 | 138 | wantsMouseMove = BehaviorNode.Selection != null; 139 | 140 | // If we have a selection, we're doing an operation which requires an update each mouse move 141 | switch (Event.current.type) 142 | { 143 | case EventType.MouseUp: 144 | // If we had a mouse up event which was not handled by the nodes, clear our selection 145 | BehaviorNode.Selection = null; 146 | Event.current.Use(); 147 | break; 148 | case EventType.MouseDown: 149 | //if (Event.current.clickCount == 2) 150 | //// If we double-click and no node handles the event, create a new node there 151 | //{ 152 | // Node.Selection = new Node("Node " + nodes.Count, Event.current.mousePosition); 153 | // nodes.Add(Node.Selection); 154 | // Event.current.Use(); 155 | //} 156 | break; 157 | case EventType.MouseDrag: 158 | DragNodes(); 159 | break; 160 | } 161 | 162 | EndWindows(); 163 | 164 | if (GUI.changed) 165 | { 166 | Repaint(); 167 | } 168 | } 169 | 170 | void Update() 171 | { 172 | if (EditorApplication.isPlaying) 173 | { 174 | Repaint(); 175 | } 176 | } 177 | 178 | private bool BehaviorHasUpdated() 179 | { 180 | switch (Behavior.CurrentStatus) 181 | { 182 | case Status.Failure: 183 | case Status.Success: 184 | case Status.Running: 185 | return true; 186 | case Status.Dormant: 187 | default: 188 | return false; 189 | } 190 | } 191 | 192 | private void DrawHistoryWindow(int id) 193 | { 194 | GUILayout.BeginVertical(); 195 | for (int i = 0; i < Behavior.History.Count; i++) 196 | { 197 | string prfx = ""; 198 | switch (Behavior.History[i].Status) 199 | { 200 | case Status.Running: 201 | prfx = "R "; 202 | break; 203 | case Status.Success: 204 | prfx = "S "; 205 | break; 206 | case Status.Failure: 207 | prfx = "F "; 208 | break; 209 | } 210 | GUIStyle style = new GUIStyle(); 211 | style.fontStyle = FontStyle.BoldAndItalic; 212 | GUILayout.Label(prfx + Behavior.History[i].Name, style); 213 | } 214 | GUILayout.EndVertical(); 215 | } 216 | 217 | private void GetSelectedNodeFields() 218 | { 219 | type = BehaviorNode.Selection.BehaviorComponent.GetType(); 220 | fields = type.GetFields(); 221 | } 222 | 223 | private void DrawNodeSelected(int id) 224 | { 225 | BehaviorComponent component = BehaviorNode.Selection.BehaviorComponent; 226 | 227 | GUILayout.BeginVertical(); 228 | GUIStyle style = new GUIStyle(); 229 | style.fontStyle = FontStyle.Bold; 230 | GUILayout.Label("Behavior Type: " + component.GetType().Name, style); 231 | 232 | if (component.GetType() == typeof(ConditionalLambda)) 233 | { 234 | ConditionalLambda conditional = ((ConditionalLambda)component); 235 | 236 | if (conditional.TestAction != null) 237 | { 238 | conditional.ExpressionObjects.Clear(); 239 | conditional.TestAction.Invoke(conditional); 240 | 241 | GUILayout.Label("Conditional Description: " + conditional.ConditionalDescription); 242 | if (conditional.ExpressionObjects != null) 243 | { 244 | for (int i = 0; i < conditional.ExpressionObjects.Count; i++) 245 | { 246 | GUILayout.Label("Conditional Object Type: " + conditional.ExpressionObjects[i].GetType().Name); 247 | GUILayout.Label("Conditional Object Value: " + conditional.ExpressionObjects[i]); 248 | } 249 | } 250 | 251 | GUILayout.Label("Conditional Result: " + conditional.Result); 252 | } 253 | else 254 | { 255 | GUILayout.Label("Conditional Result: " + conditional.Result); 256 | GUILayout.Label("Conditional Body: " + conditional.Expression); 257 | GUILayout.Label("Conditional Not Using Action Constructor for Debugging"); 258 | } 259 | } 260 | else 261 | { 262 | foreach (FieldInfo field in fields) 263 | { 264 | string name = field.Name; 265 | object value = field.GetValue(component); 266 | 267 | if (value != null && field.Name != "Node" && field.Name != "NotifyOnExecute" && field.Name != "AddToHistory" && field.Name != "Action") 268 | { 269 | GUILayout.Label( "Field: " + name ); 270 | GUILayout.Label( "Value: " + value ); 271 | } 272 | } 273 | } 274 | component.NotifyOnExecute = GUILayout.Toggle( component.NotifyOnExecute, "Notify On Execute" ); 275 | GUILayout.EndVertical(); 276 | } 277 | 278 | private void DragNodes() 279 | { 280 | for (int i = 0; i < nodes.Count; i++) 281 | { 282 | Vector2 pos = new Vector2(nodes[ i ].Position.x, nodes[ i ].Position.y); 283 | pos += Event.current.delta; 284 | nodes[i].Position = pos; 285 | } 286 | Event.current.Use(); 287 | } 288 | 289 | private void DrawGraphGUI() 290 | { 291 | if (Event.current.type == EventType.Repaint) 292 | { 293 | UnityEditor.Graphs.Styles.graphBackground.Draw(GraphRect, false, false, false, false); 294 | DrawGrid(); 295 | } 296 | } 297 | 298 | private void DrawGrid() 299 | { 300 | if (Event.current.type != EventType.Repaint) 301 | { 302 | return; 303 | } 304 | 305 | GL.PushMatrix(); 306 | GL.Begin(1); 307 | DrawGridLines(10.0f, GridMinorColor); 308 | DrawGridLines(100.0f, GridMajorColor); 309 | GL.End(); 310 | GL.PopMatrix(); 311 | } 312 | 313 | private void DrawGridLines(float gridSize, Color gridColor) 314 | { 315 | GL.Color(gridColor); 316 | for (float i = GraphRect.x + gridSize; i < GraphRect.x + GraphRect.width; i = i + gridSize) 317 | { 318 | DrawLine(new Vector2(i, GraphRect.y), new Vector2(i, GraphRect.y + GraphRect.height)); 319 | } 320 | for (float j = GraphRect.y + gridSize; j < GraphRect.y + GraphRect.height; j = j + gridSize) 321 | { 322 | DrawLine(new Vector2(GraphRect.x, j), new Vector2(GraphRect.x + GraphRect.width, j)); 323 | } 324 | } 325 | 326 | private void DrawLine(Vector2 p1, Vector2 p2) 327 | { 328 | GL.Vertex(p1); 329 | GL.Vertex(p2); 330 | } 331 | 332 | public void OnDestroy() 333 | { 334 | if (Behavior.BehaviorTreeUpdated != null) 335 | { 336 | Behavior.BehaviorTreeUpdated -= Launch; 337 | } 338 | } 339 | } 340 | } 341 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Editor/PreviewEditorWindow.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fadd7dca6fa1ba640bb5db7abf80080d 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Editor/PreviewPropertyDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using UnityEngine; 3 | using BehaviorLibrary; 4 | 5 | namespace BehaviorLibary.Editor 6 | { 7 | [CustomPropertyDrawer( typeof( BehaviorTree ) )] 8 | public class PreviewPropertyDrawer : PropertyDrawer 9 | { 10 | public override void OnGUI( Rect position, SerializedProperty property, GUIContent label ) 11 | { 12 | if ( !UnityEditor.EditorApplication.isPlaying ) 13 | { 14 | GUI.enabled = false; 15 | } 16 | 17 | if ( GUI.Button( position, "Preview Behavior Tree" ) ) 18 | { 19 | BehaviorTree behavior = property.objectReferenceValue as BehaviorTree; 20 | if ( behavior != null ) 21 | { 22 | PreviewEditorWindow.Launch( behavior ); 23 | } 24 | else 25 | { 26 | Debug.LogWarning( "AIPreviewer SerializedProperty ObjectReferenceValue cast to BehaviorTree result was null!" ); 27 | } 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Editor/PreviewPropertyDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 53a8b7be6d394174fa682bb4f8f9654b 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c9a0ed6cdb5299d4d9a1c4609e0b957f 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Shaders/ColorBlended.shader: -------------------------------------------------------------------------------- 1 | Shader "Lines/ColorBlended" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | Blend SrcAlpha OneMinusSrcAlpha 8 | Zwrite Off Cull Off Fog { Mode Off } 9 | BindChannels { Bind "vertex", vertex Bind "color", color } 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Shaders/ColorBlended.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d65893048ee03ee4ab3c427e10605727 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Shaders/ColoredBlended.shader: -------------------------------------------------------------------------------- 1 | Shader "Lines/ColoredBlended" 2 | { 3 | SubShader 4 | { 5 | Pass 6 | { 7 | Blend SrcAlpha OneMinusSrcAlpha 8 | Zwrite Off Cull Off 9 | CGPROGRAM 10 | #pragma vertex vert 11 | #pragma fragment frag 12 | #include "UnityCG.cginc" 13 | 14 | struct appdata 15 | { 16 | float4 vertex : POSITION; 17 | float4 color : COLOR; 18 | }; 19 | 20 | struct v2f 21 | { 22 | float4 vertex : SV_POSITION; 23 | float4 color : COLOR; 24 | }; 25 | 26 | v2f vert (appdata v) 27 | { 28 | v2f o; 29 | o.vertex = UnityObjectToClipPos(v.vertex); 30 | o.color = v.color; 31 | return o; 32 | } 33 | 34 | fixed4 frag (v2f i) : SV_Target 35 | { 36 | fixed4 col = i.color; 37 | return col; 38 | } 39 | ENDCG 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Assets/BehaviorLibrary/Shaders/ColoredBlended.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 39118d1722ad8bf468cc30d79027f25c 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Example.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1dcf2bdb34a95c9408afa7153006d8b8 3 | folderAsset: yes 4 | timeCreated: 1468287776 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Example/BehaviorTest.cs: -------------------------------------------------------------------------------- 1 | using BehaviorLibrary; 2 | using UnityEngine; 3 | 4 | public class BehaviorTest : MonoBehaviour 5 | { 6 | public GameObject Target; 7 | public float SightDistance; 8 | public float FollowDistance; 9 | public Transform PatrolPoint; 10 | public float PatrolRadius; 11 | public bool CanBehave; 12 | 13 | public BehaviorTree behavior; 14 | 15 | private Vector3 patrolPoint; 16 | private float updateTick; 17 | 18 | //Note: this could be used to actively check states and switch behaviors or listen for tasks and switch 19 | public int switchBehaviors() 20 | { 21 | return 0; 22 | } 23 | 24 | #region Conditions 25 | private bool CanSeeTarget() 26 | { 27 | //if (Vector3.Distance(transform.position, Target.transform.position) <= SightDistance) 28 | //{ 29 | // Debug.Log("Can see target"); 30 | //} 31 | return Vector3.Distance(transform.position, Target.transform.position) <= SightDistance; 32 | } 33 | 34 | private bool TooCloseToTarget() 35 | { 36 | //if (Vector3.Distance(transform.position, Target.transform.position) <= FollowDistance) 37 | //{ 38 | // Debug.Log("too close to target"); 39 | //} 40 | return Vector3.Distance(transform.position, Target.transform.position) <= FollowDistance; 41 | } 42 | #endregion 43 | 44 | #region Actions 45 | private Status FollowTarget() 46 | { 47 | //Debug.Log("Following Target"); 48 | transform.position = Vector3.Lerp(transform.position, Target.transform.position, .1f); 49 | return Status.Success; 50 | } 51 | 52 | private Status CircleTarget() 53 | { 54 | //Debug.Log("Circling Target"); 55 | Vector3 targetPos = Target.transform.position; 56 | Vector3 pos = targetPos + (new Vector3(Mathf.Cos(Time.time), 0, Mathf.Sin(Time.time))*FollowDistance); 57 | transform.position = Vector3.Lerp(transform.position, pos, .1f); 58 | return Status.Success; 59 | } 60 | 61 | private Status GetRandomPatrolPoint() 62 | { 63 | if (Time.time > updateTick) 64 | { 65 | updateTick = Time.time + 2.5f; 66 | patrolPoint = (UnityEngine.Random.insideUnitSphere*PatrolRadius) + PatrolPoint.position; 67 | patrolPoint.y = 0; 68 | } 69 | return Status.Success; 70 | } 71 | 72 | private Status GoToPatrolPoint() 73 | { 74 | //Debug.Log("Going to Patrol Point"); 75 | transform.position = Vector3.Lerp(transform.position, patrolPoint, .1f); 76 | return Status.Success; 77 | } 78 | #endregion 79 | 80 | //Run this on awake, enable, or start 81 | public void DefineBehavior() 82 | { 83 | //Conditions 84 | ConditionalLambda canSeeTarget = new ConditionalLambda(() => Vector3.Distance(transform.position, Target.transform.position) <= SightDistance); 85 | ConditionalLambda tooCloseToTarget = new ConditionalLambda(() => Vector3.Distance(transform.position, Target.transform.position) <= FollowDistance); 86 | 87 | //Actions 88 | BAction followTarget = new BAction(FollowTarget); 89 | BAction getPatrolPoint = new BAction(GetRandomPatrolPoint); 90 | BAction goToPatrolPoint = new BAction(GoToPatrolPoint); 91 | BAction circleTarget = new BAction(CircleTarget); 92 | 93 | SequenceContinued checkIfShouldCircle = new SequenceContinued(tooCloseToTarget, circleTarget); 94 | //If too circle target, if not follow Target 95 | SelectorContinued following = new SelectorContinued(checkIfShouldCircle, followTarget); 96 | SequenceContinued checkIfCanFollow = new SequenceContinued(canSeeTarget, following); 97 | SequenceContinued patrol = new SequenceContinued(getPatrolPoint, goToPatrolPoint); 98 | 99 | SelectorContinued lookForTarget = new SelectorContinued(checkIfCanFollow, patrol); 100 | 101 | //NOTE: RootSelector allows for StateMachine type usage in behavior tree 102 | //setup root node, choose initialization phase or pathing/movement phase 103 | SelectorBranch root = new SelectorBranch(switchBehaviors, lookForTarget); 104 | 105 | //set a reference to the root 106 | behavior = ScriptableObject.CreateInstance(); 107 | behavior.Init( root ); 108 | CanBehave = true; 109 | } 110 | 111 | public void Start() 112 | { 113 | DefineBehavior(); 114 | } 115 | 116 | public void Update() 117 | { 118 | if (CanBehave && behavior != null) 119 | { 120 | behavior.Behave(); 121 | } 122 | } 123 | } -------------------------------------------------------------------------------- /Assets/Example/BehaviorTest.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6d78310c9ae8d494e900efdd3a838e25 3 | timeCreated: 1468198404 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Example/BehaviorTest.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/Assets/Example/BehaviorTest.prefab -------------------------------------------------------------------------------- /Assets/Example/BehaviorTest.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 31fd72d989b397847ab435b46d72bee1 3 | timeCreated: 1468290320 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Example/BehaviorTest.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/Assets/Example/BehaviorTest.unity -------------------------------------------------------------------------------- /Assets/Example/BehaviorTest.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c69443ec14f10a94bbba8f9eefb1148a 3 | timeCreated: 1468290270 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Plugins.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6263b42f4cdd90d49a1b33accba4a026 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Plugins/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 78ad80d8906a47a4e963807077d9846f 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Plugins/Editor/JetBrains.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 71de4663be1f7f8418e1bed8f97db40b 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Plugins/Editor/JetBrains/JetBrains.Rider.Unity.Editor.Plugin.Repacked.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/Assets/Plugins/Editor/JetBrains/JetBrains.Rider.Unity.Editor.Plugin.Repacked.dll -------------------------------------------------------------------------------- /Assets/Plugins/Editor/JetBrains/JetBrains.Rider.Unity.Editor.Plugin.Repacked.dll.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3e0389fec5208a3418ceeb619c0a181f 3 | PluginImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | iconMap: {} 7 | executionOrder: {} 8 | defineConstraints: [] 9 | isPreloaded: 0 10 | isOverridable: 0 11 | isExplicitlyReferenced: 0 12 | platformData: 13 | - first: 14 | Any: 15 | second: 16 | enabled: 0 17 | settings: {} 18 | - first: 19 | Editor: Editor 20 | second: 21 | enabled: 1 22 | settings: 23 | DefaultValueInitialized: true 24 | - first: 25 | Windows Store Apps: WindowsStoreApps 26 | second: 27 | enabled: 0 28 | settings: 29 | CPU: AnyCPU 30 | userData: 31 | assetBundleName: 32 | assetBundleVariant: 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /Misc/BehaviorTree1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/Misc/BehaviorTree1.PNG -------------------------------------------------------------------------------- /Misc/BehaviorTree2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/Misc/BehaviorTree2.PNG -------------------------------------------------------------------------------- /Packages/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "com.unity.ads": "2.3.1", 4 | "com.unity.analytics": "3.2.2", 5 | "com.unity.collab-proxy": "1.2.15", 6 | "com.unity.package-manager-ui": "2.0.3", 7 | "com.unity.purchasing": "2.0.3", 8 | "com.unity.textmeshpro": "1.3.0", 9 | "com.unity.modules.ai": "1.0.0", 10 | "com.unity.modules.animation": "1.0.0", 11 | "com.unity.modules.assetbundle": "1.0.0", 12 | "com.unity.modules.audio": "1.0.0", 13 | "com.unity.modules.cloth": "1.0.0", 14 | "com.unity.modules.director": "1.0.0", 15 | "com.unity.modules.imageconversion": "1.0.0", 16 | "com.unity.modules.imgui": "1.0.0", 17 | "com.unity.modules.jsonserialize": "1.0.0", 18 | "com.unity.modules.particlesystem": "1.0.0", 19 | "com.unity.modules.physics": "1.0.0", 20 | "com.unity.modules.physics2d": "1.0.0", 21 | "com.unity.modules.screencapture": "1.0.0", 22 | "com.unity.modules.terrain": "1.0.0", 23 | "com.unity.modules.terrainphysics": "1.0.0", 24 | "com.unity.modules.tilemap": "1.0.0", 25 | "com.unity.modules.ui": "1.0.0", 26 | "com.unity.modules.uielements": "1.0.0", 27 | "com.unity.modules.umbra": "1.0.0", 28 | "com.unity.modules.unityanalytics": "1.0.0", 29 | "com.unity.modules.unitywebrequest": "1.0.0", 30 | "com.unity.modules.unitywebrequestassetbundle": "1.0.0", 31 | "com.unity.modules.unitywebrequestaudio": "1.0.0", 32 | "com.unity.modules.unitywebrequesttexture": "1.0.0", 33 | "com.unity.modules.unitywebrequestwww": "1.0.0", 34 | "com.unity.modules.vehicles": "1.0.0", 35 | "com.unity.modules.video": "1.0.0", 36 | "com.unity.modules.vr": "1.0.0", 37 | "com.unity.modules.wind": "1.0.0", 38 | "com.unity.modules.xr": "1.0.0" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ClusterInputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/ClusterInputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/PresetManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/PresetManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 2018.3.0f2 2 | -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityConnectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/UnityConnectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/VFXManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lcremer/Unity_AI/50f7de9560956d4ac76036e5bb88928300958f5e/ProjectSettings/VFXManager.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #AI Behavior Tree Framework 2 | 3 | NOTE: this is still very much a Prototype|Proof of Concept 4 | 5 | Create Behavior Trees in C#, without having to work with an editor. Preview behavior tree in realtime. 6 | I wanted something that would let me quickly define and create behavior tree nodes how I wanted, something fairly lightweight. 7 | This framework was an attempt to tackle that. 8 | 9 | Included is a demo scene BehaviorTest. In the scene is a BehaviorTest prefab. 10 | The prefab has a BehaviorTest (Forgive my naming!) component that gives and example of how to use the framework. 11 | Hitting play will allow you to hit the "Open Behavior Tree Preview" button on the component without errors. 12 | 13 | Note: the behavior tree is executed so quickly its best to pause and tick forward to see how the nodes are iterated through. 14 | 15 | Pending updates: 16 | Preview without having to hit play. 17 | Tree Execution Speed Adjustment (Tree nodes are acted on so quickly it may be hard to debug) 18 | Collapsable Branches 19 | Node Logging 20 | Utility Curve Selectors 21 | 22 | Examples of the Tree Previewer in Unity 5: 23 | https://github.com/lcremer/Unity_AI/blob/master/Misc/BehaviorTree1.PNG 24 | https://github.com/lcremer/Unity_AI/blob/master/Misc/BehaviorTree2.PNG 25 | --------------------------------------------------------------------------------