public void When_child_is_running_Then_returns_running() { var returnCode = new RootSelector(() => 0, TestHelper.CreateRunningAction()).Behave(); Assert.AreEqual(BehaviourReturnCode.Running, returnCode); }
private BehaviourReturnCode BehaveInternal() { try { switch (RootSelector.Behave()) { case BehaviourReturnCode.Failure: ReturnCode = BehaviourReturnCode.Failure; return(ReturnCode); case BehaviourReturnCode.Success: ReturnCode = BehaviourReturnCode.Success; return(ReturnCode); case BehaviourReturnCode.Running: ReturnCode = BehaviourReturnCode.Running; return(ReturnCode); default: ReturnCode = BehaviourReturnCode.Running; return(ReturnCode); } } catch (Exception e) { #if DEBUG Console.Error.WriteLine(e.Message, e.StackTrace); #endif ReturnCode = BehaviourReturnCode.Failure; return(ReturnCode); } }
public void When_child_succeeds_Then_returns_success() { var returnCode = new RootSelector(() => 0, TestHelper.CreateSuccessAction()).Behave(); Assert.AreEqual(BehaviourReturnCode.Success, returnCode); }
public void When_child_fails_Then_returns_fail() { var returnCode = new RootSelector(() => 0, TestHelper.CreateFailiedAction()).Behave(); Assert.AreEqual(BehaviourReturnCode.Failure, returnCode); }
/// <summary> /// Called to execute the command. /// </summary> protected override void OnExecute() { base.OnExecute(); using (RootSelector rootSelector = new RootSelector()) { new WorkerLogic(Package.IDE, rootSelector).UnNest(); } }
public static BComponent BuildComponent(XmlNode xmlDoc, BComponent parent, Behaviors behavior) { string name = xmlDoc.Name; if (ActionBool.NAME == name) { return(ActionBool.Build(xmlDoc, parent, behavior)); } else if (ActionProperty.NAME == name) { return(ActionProperty.Build(xmlDoc, parent, behavior)); } else if (ActionVoid.NAME == name) { return(ActionVoid.Build(xmlDoc, parent, behavior)); } else if (ActionEnumerator.NAME == name) { return(ActionEnumerator.Build(xmlDoc, parent, behavior)); } else if (RandomSelector.NAME == name) { return(RandomSelector.Build(xmlDoc, parent, behavior)); } else if (RootSelector.NAME == name) { return(RootSelector.Build(xmlDoc, parent, behavior)); } else if (Selector.NAME == name) { return(Selector.Build(xmlDoc, parent, behavior)); } else if (Sequence.NAME == name) { return(Sequence.Build(xmlDoc, parent, behavior)); } else if (Inverter.NAME == name) { return(Inverter.Build(xmlDoc, parent, behavior)); } else if (UntilTrue.NAME == name) { return(UntilTrue.Build(xmlDoc, parent, behavior)); } else if (UntilFalse.NAME == name) { return(UntilFalse.Build(xmlDoc, parent, behavior)); } else { Debug.LogErrorFormat("I did not find the item: {0}", name); return(null); } }
public Behaviors(TextAsset txt, GameObject obj, float waitTime, bool updateAllTree, Window.UnitArrang arrang = null) { if (txt == null || txt.text == null || txt.text.Length < 1) { Debug.LogWarningFormat("No xml tree [{0}]", obj.name); return; } XmlDocument xmlDoc = new XmlDocument(); if (arrang != null) { string path = arrang.window.GetAssetTextPath(txt); using (TextReader tr = new StreamReader(path)) { xmlDoc.Load(tr); } } else { using (StringReader sr = new StringReader(txt.text)) { xmlDoc.Load(sr); } } XmlNodeList xmlNodes = xmlDoc.SelectNodes("root"); BComponent root = null; components = FindComponents(obj); xmlAI = txt; foreach (XmlNode xmlNode in xmlNodes) { root = BuildComponent(xmlNode, null, this); } this.root = root as RootSelector; if (this.root == null) { throw new NullReferenceException(string.Format("Behaviors: xml is invalid - has no root element [{0}]", txt.name)); } this.root.updateAllTree = updateAllTree; this.waitTime = waitTime; }
public void Setup() { var isThiefNearTreasureConditional = new Conditional(IsThiefNearTreasure); var makethiefFleeAction = new BehaviourAction(MakeThiefFlee); var sequence = new Sequence(new Inverter(isThiefNearTreasureConditional), makethiefFleeAction); var chooseCastleAction = new BehaviourAction(ChooseACastleToFlyTo); var flytoCastleAction = new BehaviourAction(FlyToCastle); var fightAction = new BehaviourAction(FightGuards); var strongEnoughConditional = new Conditional(StrongEnough); var takeGold = new BehaviourAction(TakeGold); var flytoHomeAction = new BehaviourAction(FlyToHome); var storeRobingsAction = new BehaviourAction(StoreGold); var secondSequence = new Sequence(chooseCastleAction, flytoCastleAction, fightAction, strongEnoughConditional, takeGold, flytoHomeAction, storeRobingsAction); var rootSelector = new RootSelector(SwitchNodes, sequence, secondSequence); _behaviour = new Behaviour(rootSelector); }
private void Setup() { var tooClose = new Conditional(isTooClose); var targetMoved = new Conditional(hasTargetMoved); var pathFound = new Conditional(hasPathBeenFound); var reachedCell = new Conditional(hasReachedCell); var reachedTarget = new Conditional(hasReachedTarget); var isNewPath = new Conditional(hasNewPath); //setup all actions and their delegate functions BehaviourAction moveToCell = new BehaviourAction(moveTowardsCell); BehaviourAction calcPath = new BehaviourAction(calculatePath); BehaviourAction initPathfinder = new BehaviourAction(initializePathfinder); BehaviourAction getNextCell = new BehaviourAction(getNextPathCell); BehaviourAction setPath = new BehaviourAction(setNewPath); BehaviourAction getPath = new BehaviourAction(getCurrentPath); BehaviourAction updatePosition = new BehaviourAction(updateTargetPosision); BehaviourAction reset = new BehaviourAction(resetPathfinder); BehaviourAction animate = new BehaviourAction(updateAnimation); //setup an initilization branch var initialize = new Sequence(initPathfinder, calcPath); //if the target has moved, reset and calculate a new path var ifMovedCreateNewPath = new Selector(new Inverter(targetMoved), new Inverter(reset), calcPath); var ifPathFoundGetPath = new Selector(new Inverter(pathFound), getPath); var ifPathNewUseIt = new Selector(new Inverter(isNewPath), setPath); var ifReachedCellGetNext = new Selector(new Inverter(reachedCell), getNextCell); var ifNotReachedTargetMoveTowardsCell = new Selector(reachedTarget, moveToCell); var follow = new Selector(new Inverter(tooClose), updatePosition, ifMovedCreateNewPath, ifPathFoundGetPath, ifPathNewUseIt, ifReachedCellGetNext, ifNotReachedTargetMoveTowardsCell, animate); var root = new RootSelector(SwitchBehaviours, initialize, follow); //set a reference to the root _behaviour = new Behaviour(root); }
public Behaviour(RootSelector root) { _rootSelector = root; }
/// <summary> /// /// </summary> /// <param name="root"></param> public Behavior(RootSelector root) { _Root = root; }
//constructor for behavior tree taking in a root as param public BehaviorTree( RootSelector createdRoot ){ root = createdRoot; }
//Setup Triangles BehaviorTree void Start () { triangle = GetComponent<TriangleImproved>(); //Set root behavior Tree rootTree = newTask("*Behavior Tree", TaskType.BehaviorTree); rootTree.transform.parent = transform; triangleBehavior = rootTree.GetComponent<BehaviorTree>(); //////////////////////////////////Hail Mary Behavior///////////////////////////////////////////////////////// //stop action BehaviorAction stop = newTask("Stop Action", TaskType.Action).GetComponent<BehaviorAction>(); stop.setAction(triangle.Stop); //small tri's spawn action BehaviorAction triangleAppear = newTask("Triangle Appear Action", TaskType.Action).GetComponent<BehaviorAction>(); triangleAppear.setAction(triangle.SpawnTriangles); //turn around action BehaviorAction turnAround = newTask("Tris Rotating Action", TaskType.Action).GetComponent<BehaviorAction>(); turnAround.setAction(triangle.sendTrisRotating); //charge action BehaviorAction charge = newTask("Charge Action", TaskType.Action).GetComponent<BehaviorAction>(); charge.setAction(triangle.ChargeUp); //fire beam action BehaviorAction beam = newTask("Beam Action", TaskType.Action).GetComponent<BehaviorAction>(); beam.setAction(triangle.FireBeam); //stop-triAppear-Spin-charge sequence BehaviorSequence stopAppearChargeSeq = newTask("Hail Mary Charge Sequence", TaskType.BehaviorSequnce).GetComponent<BehaviorSequence>(); stopAppearChargeSeq.setBehaviorSequence(stop, triangleAppear, turnAround, charge); //stop-triAppear-Spin-charge--beam sequence BehaviorSequence stopAppearChargeBeamSeq = newTask("Hail Mary Charge Beam Sequence", TaskType.BehaviorSequnce).GetComponent<BehaviorSequence>(); stopAppearChargeBeamSeq.setBehaviorSequence(stopAppearChargeSeq, beam); //can Hm condiiton BehaviorConditional canHailMary = newTask("Can Hail Mary Condition", TaskType.Condition).GetComponent<BehaviorConditional>(); canHailMary.setBehaviorConditional(triangle.CanHailMary); BehaviorConditional healthLessThanPercentage = newTask ("Health Less Than Percentage", TaskType.Condition).GetComponent<BehaviorConditional> (); healthLessThanPercentage.setBehaviorConditional (triangle.HealthLessThanPercentage); BehaviorAction moveToPointOne = newTask ("Move To Point one", TaskType.Action).GetComponent<BehaviorAction> (); moveToPointOne.setAction (triangle.MoveToDestinationOne); //stop-triAppear-Spin-charge--beam sequence BehaviorSequence mainHailMarySequence = newTask("Condition And Action Sequence", TaskType.BehaviorSequnce).GetComponent<BehaviorSequence>(); mainHailMarySequence.setBehaviorSequence(canHailMary, healthLessThanPercentage , moveToPointOne ,stopAppearChargeBeamSeq); ////stop-charge sequnce //BehaviorSequence depthFourNodeOneHM = newTask("Stop Turn Sequence", TaskType.BehaviorSequnce).GetComponent<BehaviorSequence>(); //depthFourNodeOneHM.setBehaviorSequence(stop, charge); ////small tri's spawn-turn around sequence //BehaviorSequence depthFourNodeTwoHM = newTask("SpawnTri TrisRotat Sequence", TaskType.BehaviorSequnce).GetComponent<BehaviorSequence>(); //depthFourNodeTwoHM.setBehaviorSequence(triangleAppear, turnAround); ////stop-charge--small tri's spawn-turn around parallel sequence //BehaviorParallelSequence depthThreeHM = newTask("Charge Spawn Parrellel Sequence", TaskType.BehaviorParallelSequence).GetComponent<BehaviorParallelSequence>(); //depthThreeHM.setBehaviorParallelSequence(depthFourNodeOneHM, depthFourNodeTwoHM); ////parallel sequnce-fire beam sequence //BehaviorSequence depthTwoHM = newTask("Parallel Beam Sequence", TaskType.BehaviorSequnce).GetComponent<BehaviorSequence>(); //depthTwoHM.setBehaviorSequence(depthThreeHM, beam); //BehaviorConditional canHailMary = new BehaviorConditional (/* pass function that will be executed this unique behavior */); //BehaviorConditional healthBelow = new BehaviorConditional (/* pass function that will be executed this unique behavior */); //BehaviorAction moveToPoint = new BehaviorAction (/* pass function that will be executed this unique behavior */); //BehaviorSequence HailMary = new BehaviorSequence (canHailMary, healthBelow, moveToPoint, depthTwoHM ); ////Aggressive Behavior //BehaviorAction moveToCircleUntilInner = new BehaviorAction(/* pass function that will be executed this unique behavior */); ////Reuse fires behavior above //BehaviorAction moreThanXHealthThanCircle = new BehaviorAction (/* pass function that will be executed this unique behavior */); //BehaviorParallelSequence depthThreeAGG = new BehaviorParallelSequence( moveToCircleUntilInner , fires ); //BehaviorSequence Aggressive = new BehaviorSequence ( moreThanXHealthThanCircle , depthThreeAGG); ////Defensive Behavior //BehaviorConditional notInTransit = new BehaviorConditional (/* pass function that will be executed this unique behavior */); //BehaviorAction moveToRandomPointThree = new BehaviorAction (/* pass function that will be executed this unique behavior */); //BehaviorSequence depthThreeNodeOneDef = new BehaviorSequence ( notInTransit , moveToRandomPointThree ); //BehaviorConditional onCD = new BehaviorConditional (/* pass function that will be executed this unique behavior */); //BehaviorAction spawnTriangles = new BehaviorAction (/* pass function that will be executed this unique behavior */); //BehaviorSequence depthThreeNodeTwoDef = new BehaviorSequence (onCD, spawnTriangles); //BehaviorRandomSelector Defensive = new BehaviorRandomSelector (depthThreeNodeOneDef, depthThreeNodeTwoDef); ////Arrange Composites trees //BehaviorSelector CombineAggDefComposites = new BehaviorSelector (Aggressive, Defensive); ////Setup Root Node which holds all behaviors root = newTask("Root Selector", TaskType.RootSelector).GetComponent<RootSelector>(); root.setRootSelector(switchbehavior, mainHailMarySequence ); triangleBehavior.setBehaviorTree(root); }