├── Composite ├── Selector.cs └── Sequence.cs ├── Context.cs ├── Decorator ├── Inverter.cs ├── Repeater.cs └── Succeeder.cs ├── LICENSE ├── Leaf ├── AmIHurt.cs ├── AttackEnemy.cs ├── CanAttackEnemy.cs ├── FindClosestHeal.cs ├── FindClosestPowerup.cs ├── HasEnemy.cs ├── Move.cs ├── SetMoveTargetToEnemy.cs ├── SetRandomDestination.cs ├── StopMoving.cs ├── TargetClosestEnemy.cs ├── TargetNemesis.cs └── TooCloseToEnemy.cs ├── Primitives.cs └── README.md /Composite/Selector.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class Selector : Composite 6 | { 7 | int currentChild = 0; 8 | 9 | public Selector(string compositeName, params Node[] nodes) : base(compositeName, nodes) 10 | { 11 | 12 | } 13 | 14 | public override NodeStatus OnBehave(BehaviourState state) 15 | { 16 | if(currentChild >= children.Count) 17 | { 18 | return NodeStatus.FAILURE; 19 | } 20 | 21 | NodeStatus ret = children[currentChild].Behave(state); 22 | 23 | switch(ret) 24 | { 25 | case NodeStatus.SUCCESS: 26 | return NodeStatus.SUCCESS; 27 | 28 | case NodeStatus.FAILURE: 29 | currentChild++; 30 | 31 | // If we failed, immediately process the next child 32 | return OnBehave(state); 33 | } 34 | return NodeStatus.RUNNING; 35 | } 36 | 37 | public override void OnReset() 38 | { 39 | currentChild = 0; 40 | foreach (Node child in children) 41 | { 42 | child.Reset(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Composite/Sequence.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class Sequence : Composite 6 | { 7 | int currentChild = 0; 8 | 9 | public Sequence(string compositeName, params Node[] nodes) : base(compositeName, nodes) 10 | { 11 | 12 | } 13 | 14 | public override NodeStatus OnBehave(BehaviourState state) 15 | { 16 | NodeStatus ret = children[currentChild].Behave(state); 17 | 18 | switch(ret) 19 | { 20 | case NodeStatus.SUCCESS: 21 | currentChild++; 22 | break; 23 | 24 | case NodeStatus.FAILURE: 25 | return NodeStatus.FAILURE; 26 | } 27 | 28 | if (currentChild >= children.Count) 29 | { 30 | return NodeStatus.SUCCESS; 31 | } else if(ret == NodeStatus.SUCCESS) 32 | { 33 | // if we succeeded, don't wait for the next tick to process the next child 34 | return OnBehave(state); 35 | } 36 | 37 | return NodeStatus.RUNNING; 38 | } 39 | 40 | public override void OnReset() 41 | { 42 | currentChild = 0; 43 | foreach(Node child in children) 44 | { 45 | child.Reset(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Context.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Context : BehaviourState 5 | { 6 | public Bot me; 7 | public Seeker seeker; 8 | public Living enemy = null; 9 | public Vector3? moveTarget = null; 10 | } 11 | -------------------------------------------------------------------------------- /Decorator/Inverter.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class Inverter : Decorator 6 | { 7 | public Inverter(Node child) : base(child) 8 | { 9 | 10 | } 11 | public override NodeStatus OnBehave(BehaviourState state) 12 | { 13 | switch(child.Behave(state)) 14 | { 15 | case NodeStatus.RUNNING: 16 | return NodeStatus.RUNNING; 17 | 18 | case NodeStatus.SUCCESS: 19 | return NodeStatus.FAILURE; 20 | 21 | case NodeStatus.FAILURE: 22 | return NodeStatus.SUCCESS; 23 | } 24 | 25 | Debug.Log("SHOULD NOT GET HERE"); 26 | return NodeStatus.FAILURE; 27 | } 28 | 29 | public override void OnReset() 30 | { 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Decorator/Repeater.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class Repeater : Decorator 6 | { 7 | public Repeater(Node child) : base(child) 8 | { 9 | 10 | } 11 | public override NodeStatus OnBehave(BehaviourState state) 12 | { 13 | NodeStatus ret = child.Behave(state); 14 | if (ret != NodeStatus.RUNNING) 15 | { 16 | Reset(); 17 | child.Reset(); 18 | } 19 | return NodeStatus.SUCCESS; 20 | } 21 | 22 | public override void OnReset() 23 | { 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Decorator/Succeeder.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class Succeeder : Decorator 6 | { 7 | public Succeeder(Node child) : base(child) 8 | { 9 | 10 | } 11 | public override NodeStatus OnBehave(BehaviourState state) 12 | { 13 | NodeStatus ret = child.Behave(state); 14 | 15 | if (ret == NodeStatus.RUNNING) 16 | return NodeStatus.RUNNING; 17 | 18 | return NodeStatus.SUCCESS; 19 | } 20 | 21 | public override void OnReset() 22 | { 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Leaf/AmIHurt.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class AmIHurt : Leaf 6 | { 7 | public int healthThreshold = 100; 8 | 9 | public AmIHurt(int threshold) 10 | { 11 | healthThreshold = threshold; 12 | } 13 | public override NodeStatus OnBehave(BehaviourState state) 14 | { 15 | Context context = (Context)state; 16 | 17 | if (context.me.livingInfo.GetHealth() < healthThreshold) 18 | return NodeStatus.SUCCESS; 19 | else 20 | return NodeStatus.FAILURE; 21 | } 22 | 23 | public override void OnReset() 24 | { 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Leaf/AttackEnemy.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class AttackEnemy : Leaf 6 | { 7 | public override NodeStatus OnBehave(BehaviourState state) 8 | { 9 | Context context = (Context)state; 10 | 11 | if (context.enemy == null) 12 | return NodeStatus.FAILURE; 13 | 14 | context.me.LookAt(context.enemy.transform.position); 15 | context.me.attackComponent.Swing(); 16 | 17 | // TODO - perhaps should test success of the actual attack and return failure if we missed 18 | 19 | return NodeStatus.SUCCESS; 20 | } 21 | 22 | public override void OnReset() 23 | { 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Leaf/CanAttackEnemy.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class CanAttackEnemy : Leaf 6 | { 7 | public override NodeStatus OnBehave(BehaviourState state) 8 | { 9 | Context context = (Context)state; 10 | 11 | if (context.enemy == null) 12 | return NodeStatus.FAILURE; 13 | 14 | if (!context.me.CanSee(context.enemy.gameObject)) 15 | return NodeStatus.FAILURE; 16 | 17 | if (context.me.GetComponent().CanAttack()) 18 | return NodeStatus.SUCCESS; 19 | 20 | return NodeStatus.FAILURE; 21 | } 22 | 23 | public override void OnReset() 24 | { 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Leaf/FindClosestHeal.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class FindClosestHeal : Leaf 6 | { 7 | float distanceThreshold; 8 | public FindClosestHeal(float threshold) 9 | { 10 | distanceThreshold = threshold; 11 | } 12 | 13 | public override NodeStatus OnBehave(BehaviourState state) 14 | { 15 | Context context = (Context)state; 16 | 17 | Heal[] heals = context.me.dynamicEntities.GetComponentsInChildren(); 18 | 19 | if(heals.Length == 0) 20 | { 21 | context.moveTarget = null; 22 | return NodeStatus.FAILURE; 23 | } 24 | 25 | Array.Sort(heals, delegate (Heal h1, Heal h2) 26 | { 27 | float d1 = context.me.DistanceTo(h1.transform.position); 28 | float d2 = context.me.DistanceTo(h2.transform.position); 29 | 30 | return d1.CompareTo(d2); 31 | }); 32 | 33 | if (context.me.DistanceTo(heals[0].transform.position) > distanceThreshold) 34 | return NodeStatus.FAILURE; 35 | 36 | context.moveTarget = heals[0].transform.position; 37 | return NodeStatus.SUCCESS; 38 | } 39 | 40 | public override void OnReset() 41 | { 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Leaf/FindClosestPowerup.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class FindClosestPowerup : Leaf 6 | { 7 | float distanceThreshold; 8 | public FindClosestPowerup(float threshold) 9 | { 10 | distanceThreshold = threshold; 11 | } 12 | 13 | public override NodeStatus OnBehave(BehaviourState state) 14 | { 15 | Context context = (Context)state; 16 | 17 | Item[] powerups = context.me.dynamicEntities.GetComponentsInChildren(); 18 | 19 | if (powerups.Length == 0) 20 | { 21 | context.moveTarget = null; 22 | return NodeStatus.FAILURE; 23 | } 24 | 25 | Array.Sort(powerups, delegate (Item h1, Item h2) 26 | { 27 | float d1 = context.me.DistanceTo(h1.transform.position); 28 | float d2 = context.me.DistanceTo(h2.transform.position); 29 | 30 | return d1.CompareTo(d2); 31 | }); 32 | 33 | if (context.me.DistanceTo(powerups[0].transform.position) > distanceThreshold) 34 | return NodeStatus.FAILURE; 35 | 36 | context.moveTarget = powerups[0].transform.position; 37 | return NodeStatus.SUCCESS; 38 | } 39 | 40 | public override void OnReset() 41 | { 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Leaf/HasEnemy.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class HasEnemy : Leaf 6 | { 7 | public override NodeStatus OnBehave(BehaviourState state) 8 | { 9 | Context context = (Context)state; 10 | 11 | if(context.enemy == null) 12 | { 13 | return NodeStatus.FAILURE; 14 | } 15 | 16 | return NodeStatus.SUCCESS; 17 | } 18 | 19 | public override void OnReset() 20 | { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Leaf/Move.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | using Pathfinding; 5 | 6 | public class Move : Leaf 7 | { 8 | Path currentPath = null; 9 | int waypoint = 0; 10 | 11 | public override NodeStatus OnBehave(BehaviourState state) 12 | { 13 | Context context = (Context)state; 14 | 15 | if (!context.moveTarget.HasValue) 16 | return NodeStatus.FAILURE; 17 | 18 | if(starting) 19 | { 20 | if (AtDestination(context)) 21 | return NodeStatus.SUCCESS; 22 | 23 | context.seeker.StartPath(context.me.transform.position, context.moveTarget.Value, (Path p) => 24 | { 25 | currentPath = p; 26 | waypoint = 2; 27 | }); 28 | } 29 | 30 | if(currentPath != null) 31 | { 32 | if (AtDestination(context)) 33 | return NodeStatus.SUCCESS; 34 | 35 | // Only move for a maximum of 30 ticks (.5 seconds) 36 | if (ticks > 30) 37 | { 38 | context.me.LookAt(context.moveTarget.Value); 39 | return NodeStatus.SUCCESS; 40 | } 41 | 42 | if (waypoint >= currentPath.vectorPath.Count) 43 | { 44 | context.me.LookAt(context.moveTarget.Value); 45 | return NodeStatus.SUCCESS; 46 | } 47 | 48 | Vector3 pathPoint = currentPath.vectorPath[waypoint]; 49 | if (context.me.DistanceTo(pathPoint) < 0.5f) 50 | { 51 | waypoint++; 52 | } 53 | else 54 | { 55 | context.me.LookAt(pathPoint); 56 | context.me.MoveForward(true); 57 | } 58 | } 59 | 60 | return NodeStatus.RUNNING; 61 | } 62 | 63 | bool AtDestination(Context context) 64 | { 65 | // If we're already really close, don't bother pathing 66 | if (context.me.DistanceTo(context.moveTarget.Value) < 2.0f) 67 | { 68 | context.me.LookAt(context.moveTarget.Value); 69 | return true; 70 | } 71 | return false; 72 | } 73 | 74 | public override void OnReset() 75 | { 76 | currentPath = null; 77 | waypoint = 0; 78 | } 79 | } -------------------------------------------------------------------------------- /Leaf/SetMoveTargetToEnemy.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class SetMoveTargetToEnemy : Leaf 6 | { 7 | public override NodeStatus OnBehave(BehaviourState state) 8 | { 9 | Context context = (Context)state; 10 | 11 | if (context.enemy == null) 12 | return NodeStatus.FAILURE; 13 | 14 | context.moveTarget = context.enemy.transform.position; 15 | return NodeStatus.SUCCESS; 16 | } 17 | 18 | public override void OnReset() 19 | { 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Leaf/SetRandomDestination.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class SetRandomDestination : Leaf { 6 | 7 | public override NodeStatus OnBehave(BehaviourState state) 8 | { 9 | Context context = (Context)state; 10 | context.moveTarget = new Vector3(UnityEngine.Random.Range(-1024, 1024), 0, UnityEngine.Random.Range(-1024, 1024)); 11 | return NodeStatus.SUCCESS; 12 | } 13 | 14 | public override void OnReset() 15 | { 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Leaf/StopMoving.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class StopMoving : Leaf 6 | { 7 | public override NodeStatus OnBehave(BehaviourState state) 8 | { 9 | Context context = (Context)state; 10 | context.me.StopMoving(); 11 | return NodeStatus.SUCCESS; 12 | } 13 | 14 | public override void OnReset() 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Leaf/TargetClosestEnemy.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class TargetClosestEnemy : Leaf 6 | { 7 | float distanceThreshold; 8 | public TargetClosestEnemy(float threshold) 9 | { 10 | distanceThreshold = threshold; 11 | } 12 | 13 | public override NodeStatus OnBehave(BehaviourState state) 14 | { 15 | Context context = (Context)state; 16 | 17 | Living[] livingObjects = context.me.players.GetComponentsInChildren(); 18 | Array.Sort(livingObjects, delegate (Living l1, Living l2) 19 | { 20 | float d1 = context.me.DistanceTo(l1.transform.position); 21 | float d2 = context.me.DistanceTo(l2.transform.position); 22 | 23 | return d1.CompareTo(d2); 24 | }); 25 | 26 | foreach(Living l in livingObjects) 27 | { 28 | if(l.gameObject != context.me.gameObject) 29 | { 30 | if(context.me.DistanceTo(l.gameObject.transform.position) > distanceThreshold) 31 | { 32 | context.enemy = null; 33 | return NodeStatus.FAILURE; 34 | } 35 | 36 | context.enemy = l; 37 | return NodeStatus.SUCCESS; 38 | } 39 | } 40 | context.enemy = null; 41 | return NodeStatus.FAILURE; 42 | } 43 | 44 | public override void OnReset() 45 | { 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Leaf/TargetNemesis.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | using System.Collections.Generic; 5 | using UnityEngine.Networking; 6 | using System.Linq; 7 | 8 | public class TargetNemesis : Leaf 9 | { 10 | public override NodeStatus OnBehave(BehaviourState state) 11 | { 12 | Context context = (Context)state; 13 | 14 | List> myList = context.me.GetComponent().damageHistory.ToList(); 15 | myList.Sort((first, second) => 16 | { 17 | return first.Value.CompareTo(second.Value); 18 | } 19 | ); 20 | 21 | foreach(var potentialNemesis in myList) 22 | { 23 | if (potentialNemesis.Value >= 0 || potentialNemesis.Key == context.me.netId) 24 | continue; 25 | 26 | Living potential = Utils.LivingForNetObj(potentialNemesis.Key); 27 | 28 | if (!potential) 29 | continue; 30 | 31 | context.enemy = potential; 32 | return NodeStatus.SUCCESS; 33 | } 34 | 35 | return NodeStatus.FAILURE; 36 | } 37 | 38 | public override void OnReset() 39 | { 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Leaf/TooCloseToEnemy.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class TooCloseToEnemy : Leaf 6 | { 7 | public float distanceThreshold; 8 | 9 | public TooCloseToEnemy(float threshold) 10 | { 11 | distanceThreshold = threshold; 12 | } 13 | public override NodeStatus OnBehave(BehaviourState state) 14 | { 15 | Context context = (Context)state; 16 | 17 | if(context.enemy != null && context.me.DistanceTo(context.enemy.transform.position) < distanceThreshold) 18 | { 19 | return NodeStatus.SUCCESS; 20 | } 21 | return NodeStatus.FAILURE; 22 | } 23 | 24 | public override void OnReset() 25 | { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Primitives.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System; 5 | 6 | public enum NodeStatus 7 | { 8 | FAILURE, 9 | SUCCESS, 10 | RUNNING 11 | } 12 | 13 | public abstract class BehaviourState 14 | { 15 | } 16 | 17 | public abstract class Node { 18 | public bool starting = true; 19 | protected bool debug = false; 20 | public int ticks = 0; 21 | public static List debugTypeBlacklist = new List() { "Selector", "Sequence", "Repeater", "Inverter", "Succeeder" }; 22 | public virtual NodeStatus Behave(BehaviourState state) 23 | { 24 | NodeStatus ret = OnBehave(state); 25 | 26 | if (debug && !debugTypeBlacklist.Contains(GetType().Name)) 27 | { 28 | string result = "Unknown"; 29 | switch(ret) 30 | { 31 | case NodeStatus.SUCCESS: 32 | result = "success"; 33 | break; 34 | case NodeStatus.FAILURE: 35 | result = "failure"; 36 | break; 37 | 38 | case NodeStatus.RUNNING: 39 | result = "running"; 40 | break; 41 | } 42 | Debug.Log("Behaving: " + GetType().Name + " - " + result); 43 | } 44 | 45 | ticks++; 46 | starting = false; 47 | 48 | if (ret != NodeStatus.RUNNING) 49 | Reset(); 50 | 51 | return ret; 52 | } 53 | 54 | public abstract NodeStatus OnBehave(BehaviourState state); 55 | public void Reset() 56 | { 57 | starting = true; 58 | ticks = 0; 59 | OnReset(); 60 | } 61 | 62 | public abstract void OnReset(); 63 | } 64 | 65 | public abstract class Composite : Node 66 | { 67 | protected List children = new List(); 68 | public string compositeName; 69 | 70 | public Composite(string name, params Node[] nodes) 71 | { 72 | compositeName = name; 73 | children.AddRange(nodes); 74 | } 75 | 76 | public override NodeStatus Behave(BehaviourState state) 77 | { 78 | bool shouldLog = debug && ticks == 0 ? true : false; 79 | if(shouldLog) 80 | Debug.Log("Running behaviour list: " + compositeName); 81 | 82 | NodeStatus ret = base.Behave(state); 83 | 84 | if(debug && ret != NodeStatus.RUNNING) 85 | Debug.Log("Behaviour list " + compositeName + " returned: " + ret.ToString()); 86 | 87 | return ret; 88 | } 89 | } 90 | 91 | public abstract class Leaf : Node 92 | { 93 | } 94 | 95 | public abstract class Decorator : Node 96 | { 97 | protected Node child; 98 | 99 | public Decorator(Node node) { 100 | child = node; 101 | } 102 | } 103 | 104 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UnityBehaviourTree 2 | A simple behaviour tree implementation for Unity. 3 | 4 | Example usage: 5 | 6 | Node behaviourTree; 7 | Context behaviourState = new Context(); 8 | 9 | void Start() { 10 | behaviourTree = CreateBehaviourTree(); 11 | behaviourState = new Context(); // optionally add things you might need access to in your leaf nodes 12 | } 13 | 14 | void FixedUpdate() { 15 | behaviourTree.Behave(myBehaviourContext); 16 | } 17 | 18 | Node CreateBehaviourTree() 19 | { 20 | Sequence separate = new Sequence("separate", 21 | new TooCloseToEnemy(0.2f), 22 | new SetRandomDestination(), 23 | new Move()); 24 | 25 | Sequence moveTowardsEnemy = new Sequence("moveTowardsEnemy", 26 | new HasEnemy(), 27 | new SetMoveTargetToEnemy(), 28 | new Inverter(new CanAttackEnemy()), 29 | new Inverter(new Succeeder(new Move()))); 30 | 31 | Sequence attackEnemy = new Sequence("attackEnemy", 32 | new HasEnemy(), 33 | new CanAttackEnemy(), 34 | new StopMoving(), 35 | new AttackEnemy()); 36 | 37 | Sequence needHeal = new Sequence("needHeal", 38 | new Inverter(new AmIHurt(15)), 39 | new AmIHurt(35), 40 | new FindClosestHeal(30), 41 | new Move()); 42 | 43 | Selector chooseEnemy = new Selector("chooseEnemy", 44 | new TargetNemesis(), 45 | new TargetClosestEnemy(30)); 46 | 47 | Sequence collectPowerup = new Sequence("collectPowerup", 48 | new FindClosestPowerup(50), 49 | new Move()); 50 | 51 | Selector fightOrFlight = new Selector("fightOrFlight", 52 | new Inverter(new Succeeder(chooseEnemy)), 53 | separate, 54 | needHeal, 55 | moveTowardsEnemy, 56 | attackEnemy); 57 | 58 | Repeater repeater = new Repeater(fightOrFlight); 59 | 60 | return repeater; 61 | } 62 | 63 | 64 | NOTE: The leaf nodes provided are tied to my specific implementation and you'll need to customize them 65 | to suit your own needs - but this should be rather trivial. --------------------------------------------------------------------------------