Exemple #1
0
        protected override int onUpdate(BehaviourTreeData wData)
        {
            TBTActionPrioritizedSelectorContext thisContext = getContext <TBTActionPrioritizedSelectorContext>(wData);
            int runningState = BehaviourTreeRunningStatus.FINISHED;

            if (thisContext.currentSelectedIndex != thisContext.lastSelectedIndex)
            {
                if (IsIndexValid(thisContext.lastSelectedIndex))
                {
                    BehaviourAction node = GetChild <BehaviourAction>(thisContext.lastSelectedIndex);
                    node.Transition(wData);
                }
                thisContext.lastSelectedIndex = thisContext.currentSelectedIndex;
            }
            if (IsIndexValid(thisContext.lastSelectedIndex))
            {
                BehaviourAction node = GetChild <BehaviourAction>(thisContext.lastSelectedIndex);
                runningState = node.Update(wData);
                if (BehaviourTreeRunningStatus.IsFinished(runningState))
                {
                    thisContext.lastSelectedIndex = -1;
                }
            }
            return(runningState);
        }
        public void When_interrupt_throws_then_doesnt_interrupt()
        {
            const BehaviourReturnCode actual = BehaviourReturnCode.Success;
            var action = new BehaviourAction(() => actual);

            var condition  = new Conditional(() => { throw new Exception(); });
            var returnCode = new Interruptible(action, condition, BehaviourReturnCode.Success).Behave();

            Assert.AreEqual(actual, returnCode);
        }
Exemple #3
0
        protected override void onTransition(BehaviourTreeData wData)
        {
            TBTActionPrioritizedSelectorContext thisContext = getContext <TBTActionPrioritizedSelectorContext>(wData);
            BehaviourAction node = GetChild <BehaviourAction>(thisContext.lastSelectedIndex);

            if (node != null)
            {
                node.Transition(wData);
            }
            thisContext.lastSelectedIndex = -1;
        }
        public void When_interrupt_is_true_returns_assigned_interrupt_return_value()
        {
            var action = new BehaviourAction(() => BehaviourReturnCode.Success);

            var condition = new Conditional(() => true);

            var successInterruptable = new Interruptible(action, condition, BehaviourReturnCode.Success);
            var failureInterruptable = new Interruptible(action, condition, BehaviourReturnCode.Failure);
            var runningInterruptable = new Interruptible(action, condition, BehaviourReturnCode.Running);

            Assert.AreEqual(successInterruptable.Behave(), BehaviourReturnCode.Success);
            Assert.AreEqual(failureInterruptable.Behave(), BehaviourReturnCode.Failure);
            Assert.AreEqual(runningInterruptable.Behave(), BehaviourReturnCode.Running);
        }
        public BehaviourAction SetStateAction(T state, System.Func <bool, BehaviourAction.ActionResult> updateFunc)
        {
            var stateaction = new BehaviourAction(updateFunc, Enum.GetName(typeof(T), state));

            if (_stateActions.ContainsKey(state))
            {
                _stateActions[state] = stateaction;
            }
            else
            {
                _stateActions.Add(state, stateaction);
                AddChild(stateaction);
            }

            return(stateaction);
        }
        public void When_interrupt_is_false_child_behaves_as_normal()
        {
            var successAction = new BehaviourAction(() => BehaviourReturnCode.Success);
            var failureAction = new BehaviourAction(() => BehaviourReturnCode.Failure);
            var runningAction = new BehaviourAction(() => BehaviourReturnCode.Running);

            var condition = new Conditional(() => false);

            var successInterruptable = new Interruptible(successAction, condition, BehaviourReturnCode.Failure);
            var failureInterruptable = new Interruptible(failureAction, condition, BehaviourReturnCode.Failure);
            var runningInterruptable = new Interruptible(runningAction, condition, BehaviourReturnCode.Failure);

            Assert.AreEqual(successInterruptable.Behave(), BehaviourReturnCode.Success);
            Assert.AreEqual(failureInterruptable.Behave(), BehaviourReturnCode.Failure);
            Assert.AreEqual(runningInterruptable.Behave(), BehaviourReturnCode.Running);
        }
Exemple #7
0
        protected override bool onEvaluate(/*in*/ BehaviourTreeData wData)
        {
            TBTActionPrioritizedSelectorContext thisContext = getContext <TBTActionPrioritizedSelectorContext>(wData);

            thisContext.currentSelectedIndex = -1;
            int childCount = GetChildCount();

            for (int i = 0; i < childCount; ++i)
            {
                BehaviourAction node = GetChild <BehaviourAction>(i);
                if (node.Evaluate(wData))
                {
                    thisContext.currentSelectedIndex = i;
                    return(true);
                }
            }
            return(false);
        }
        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);
        }
Exemple #10
0
    public void DetermineAutoAction(int newTileX, int newTileY, out ulong duration)
    {
        var identityCreature = owner.GetComponent <Creature>();

        nextAction = new BehaviourAction();
        duration   = 1;

        var tileActingOn = owner.map.tileObjects[newTileY][newTileX];

        if (!tileActingOn.IsCollidable())
        {
            duration = identityCreature.ticksPerMove;
            nextAction.finishAction = () =>
            {
                owner.map.TryMoveObject(owner, newTileX, newTileY);
                if (owner.tile.objectList.Any(x => x.canBePickedUp))
                {
                    owner.PickUpAll();
                }
            };
        }
        else
        {
            foreach (var dOb in tileActingOn.objectList)
            {
                if (dOb.isCollidable)
                {
                    duration = identityCreature.ticksPerAttack;
                    if (dOb.GetComponent <Door>())
                    {
                        nextAction.startAction = () =>
                        {
                            owner.map.TryMoveObject(owner, newTileX, newTileY);
                            identityCreature.StartAttack(dOb);
                            return(false);
                        };
                        nextAction.continueAction = () =>
                        {
                            return(identityCreature.ContinueAttack(dOb));
                        };
                        nextAction.finishAction = () =>
                        {
                            identityCreature.FinishAttack(dOb);
                        };
                    }
                    else
                    {
                        nextAction.startAction = () =>
                        {
                            identityCreature.StartAttack(dOb);
                            return(false);
                        };
                        nextAction.continueAction = () =>
                        {
                            return(identityCreature.ContinueAttack(dOb));
                        };
                        nextAction.finishAction = () =>
                        {
                            owner.map.TryMoveObject(owner, newTileX, newTileY);
                            identityCreature.FinishAttack(dOb);
                        };
                    }
                    break;
                }
            }
        }
    }