Beispiel #1
0
        private void Start()
        {
            fsm          = new FiniteStateMachine("AITest FSM");
            IdleState    = fsm.AddState("IdleState");
            ScanState    = fsm.AddState("ScanState");
            PatrolState  = fsm.AddState("WanderState");
            PatrolAction = new WanderAction(PatrolState);
            IdleAction   = new IdleAction(IdleState);
            ScanAction   = new ScanningAction(ScanState);
            deathState   = fsm.AddState("DeathState");
            DeathAction  = new DeathState(deathState);
            //This adds the actions to the state and add state to it's transition map
            PatrolState.AddAction(PatrolAction);
            IdleState.AddAction(IdleAction);

            PatrolState.AddTransition("ToIdle", IdleState);
            IdleState.AddTransition("ToPatrol", PatrolState);
            ScanState.AddTransition("ToScanning", ScanState);
            deathState.AddTransition("ToDeath", deathState);
            PatrolAction.Init(target, 3.0f, gameObject.GetComponent <EnemyAstar>(), "ToIdle");
            IdleAction.Init(target, "AI on Idle", 3.0f, gameObject.GetComponent <EnemyAstar>(), "ToPatrol");
            DeathAction.Init(3.0f, gameObject.GetComponent <EnemyAstar>());
            ScanAction.Init(3.0f);
            fsm.StartMachine("IdleState");
        }
    public void SetWanderTarget(WanderAction action, AIStateController controller)
    {
        Vector2 v = Random.insideUnitCircle * action.WanderDistance;

        wanderTarget = startPosition + new Vector3(v.x, 0, v.y);
        controller.Agent.destination = wanderTarget;
        controller.Animator.SetInteger("Walk", 1);
    }
        public void AfterAddTwoActions_NextActionIs_FirstAddedAction_Test()
        {
            Action action       = new Think();
            Action idleAction   = new IdleAction();
            Action wanderAction = new WanderAction();

            action.AddAction(idleAction);
            action.AddAction(wanderAction);
            Assert.True(action.NextAction().Equals(idleAction));
        }
 public void UpdateCurrentIdleTime(WanderAction action, AIStateController controller)
 {
     currentIdleTime += Time.deltaTime;
     if (currentIdleTime > action.IdleTime)
     {
         currentIdleTime = 0;
         IsIdle          = false;
         SetWanderTarget(action, controller);
     }
 }
        public void AfterAddTwoActions_HasTwoInList_Test()
        {
            Action action       = new Think();
            Action idleAction   = new IdleAction();
            Action wanderAction = new WanderAction();

            action.AddAction(idleAction);
            action.AddAction(wanderAction);
            Assert.Equal(2, action.ActionListCount());
        }
        public void RemoveAction_OnListWithTwoActions_CurrentActionIsFirstAddedAction_Test()
        {
            Action action       = new Think();
            Action idleAction   = new IdleAction();
            Action wanderAction = new WanderAction();

            action.AddAction(idleAction);
            action.AddAction(wanderAction);
            action.RemoveAction();
            Assert.True(action.CurrentAction().Equals(idleAction));
        }
        public void AddTwoActionsByList_LastAddedIsCurrentAction_Test()
        {
            Action        action       = new Think();
            List <Action> list         = new List <Action>();
            Action        idleAction   = new IdleAction();
            Action        wanderAction = new WanderAction();

            list.Add(idleAction);
            list.Add(wanderAction);
            action.AddActions(list);
            Assert.True(action.CurrentAction().Equals(wanderAction));
        }
        public void AddTwoActionsByList_Test()
        {
            Action        action       = new Think();
            List <Action> list         = new List <Action>();
            Action        idleAction   = new IdleAction();
            Action        wanderAction = new WanderAction();

            list.Add(idleAction);
            list.Add(wanderAction);
            action.AddActions(list);
            Assert.Equal(2, action.ActionListCount());
        }
    // Start is called before the first frame update
    void Awake()
    {
        damage       = enemyPreset.damage;
        moveTime     = enemyPreset.moveTime;
        idleTimeMin  = enemyPreset.idleTimeMin;
        idleTimeMax  = enemyPreset.idleTimeMax;
        wanderRadius = enemyPreset.wanderRadius;

        home = transform.position;

        trigger = GetComponentInChildren <DetectPlayer>();

        trigger.GetComponent <SphereCollider>().radius = enemyPreset.detectionRadius;

        navMeshAgent = GetComponent <NavMeshAgent>();

        navMeshAgent.speed = enemyPreset.speed;

        fsm = new FSM("MeleeAI FSM");

        WanderState = fsm.AddState("WanderState");
        IdleState   = fsm.AddState("IdleState");
        AlertState  = fsm.AddState("AlertState");
        MeleeState  = fsm.AddState("MeleeState");

        WanderAction = new WanderAction(WanderState);
        IdleAction   = new TextAction(IdleState);
        alertAction  = new AlertAction(AlertState);
        meleeAction  = new MeleeAction(MeleeState);

        WanderState.AddAction(WanderAction);
        IdleState.AddAction(IdleAction);
        AlertState.AddAction(alertAction);
        MeleeState.AddAction(meleeAction);

        WanderState.AddTransition("ToIdle", IdleState);
        WanderState.AddTransition("PlayerDetect", AlertState);
        IdleState.AddTransition("ToWander", WanderState);
        IdleState.AddTransition("PlayerDetect", AlertState);

        AlertState.AddTransition("ToIdle", IdleState);
        AlertState.AddTransition("ToMelee", MeleeState);
        MeleeState.AddTransition("ToAlert", AlertState);

        WanderAction.Init(this.transform, home, navMeshAgent, wanderRadius, moveTime, "ToIdle");
        IdleAction.Init("Idling", Random.Range(idleTimeMin, idleTimeMax), "ToWander");

        alertAction.Init(trigger, navMeshAgent, "ToIdle");
        meleeAction.Init(this.transform, damage, trigger, FindObjectOfType <PlayerManager>(), "ToAlert");

        fsm.Start("IdleState");
    }
Beispiel #10
0
    public Action Process()
    {
        float distance = Vector3.Distance(wolf.target.position, wolf.transform.position);

        if (distance <= 20)
        {
            FollowpathAction followpathAction = new FollowpathAction(wolf, true);
            return(followpathAction);
        }
        if (wolf.think.ActionListCount() == 0)
        {
            WanderAction wanderAction = new WanderAction(wolf);
            return(wanderAction);
        }
        return(null);
    }
Beispiel #11
0
        public virtual BaseStateAction CreateActionClass(Entity owner)
        {
            BaseStateAction action = null;

            switch (actionType)
            {
            case ActionType.PlayerMove:
                action = new PlayerMoveAction(owner, runUpdate);
                break;

            case ActionType.PlayerJump:
                action = new PlayerJumpAction(owner, runUpdate);
                break;

            case ActionType.PlayerDash:
                action = new PlayerDashAction(owner, runUpdate);
                break;

            case ActionType.MobWander:
                action = new WanderAction(owner, runUpdate);
                break;

            case ActionType.MobTurnAtLedge:
                action = new TurnAtLedgeAction(owner, runUpdate);
                break;

            case ActionType.MobTurnAtWall:
                action = new TurnAtWallAction(owner, runUpdate);
                break;

            case ActionType.MobFaceTarget:
                action = new ChaseTargetAction(owner, runUpdate);
                break;

            case ActionType.MobMoveNormal:
                action = new MoveAction(owner, runUpdate);
                break;

            default:
                action = new BaseStateAction(owner, runUpdate);
                break;
            }

            action.PopulateAbilities(abilities);

            return(action);
        }
Beispiel #12
0
        private void AttatchNewDeerFSM(Deer deer, Game game)
        {
            //Declaring Actions for states
            ScaredAction scaredAction = new ScaredAction();
            WanderAction wanderAction = new WanderAction();
            GrazeAction  grazeAction  = new GrazeAction();
            //FleeFromLionAction fleeFromLionAction = new FleeFromLionAction();
            //FleeFromHunterAction fleeFromHunterAction = new FleeFromHunterAction();
            DeerFleeAction deerFleeAction = new DeerFleeAction();
            resetWander    resetWander    = new resetWander();
            emptyAction    emptyAction    = new emptyAction();
            FlockAction    flockAction    = new FlockAction();

            //Declaring States for FSM
            State scaredState = new State("scared", scaredAction);
            State wanderState = new State("wander", emptyAction, wanderAction, resetWander);
            State grazeState  = new State("graze", grazeAction);
            State fleeState   = new State("flee", deerFleeAction);
            //fleeState.AddActions();
            State flockState = new State("flock", flockAction);


            //Declaring conditions used for transitions
            FearGreaterThan  fearGreaterThan80                 = new FearGreaterThan(80);
            FearGreaterThan  fearGreaterThan60                 = new FearGreaterThan(60);
            FearLessThan     fearLessThan40                    = new FearLessThan(40);
            FearLessThan     fearLessThan20                    = new FearLessThan(20);
            ThreatLevel      hunterHighThreatLevel             = new ThreatLevel(75f);
            DistanceToHunter distanceToHunter                  = new DistanceToHunter(200f);
            AndCondition     andThreatDistanceHunter           = new AndCondition(hunterHighThreatLevel, distanceToHunter);
            OrCondition      orFearThreat                      = new OrCondition(fearGreaterThan60, andThreatDistanceHunter);
            NeighborCountGreaterCondition neighborCountGreater = new NeighborCountGreaterCondition(5);
            NeighborCountLessCondition    neighborCountLess    = new NeighborCountLessCondition(2);
            RandomTimerCondition          fleetoScaredTimer    = new RandomTimerCondition(initialTime, 600);
            RandomTimerCondition          wandertoGrazeTimer   = new RandomTimerCondition(initialTime, shortTimer);//800 to 1200
            RandomTimerCondition          flocktoGrazeTimer    = new RandomTimerCondition(initialTime, 600);
            RandomTimerCondition          grazetoFlockTimer    = new RandomTimerCondition(initialTime, 600);
            RandomCondition randomCondition                    = new RandomCondition(2, 2);
            //AndCondition andRandomLowFear = new AndCondition(randomCondition, fearLessThan40);
            AndCondition andTimerLowFear = new AndCondition(fleetoScaredTimer, fearLessThan20);
            AndCondition andRandomNeighborCountGreater = new AndCondition(randomCondition, neighborCountGreater);
            AndCondition andRandomNeighborCountLess    = new AndCondition(grazetoFlockTimer, neighborCountLess);
            //AndCondition andRNCRandom = new AndCondition(andRandomNeighborCount, grazetoFlockTimer);
            WanderCondition wanderTrue = new WanderCondition();


            //Declaring Transitions
            Transition gotoWanderFromScared = new Transition(andTimerLowFear, wanderState, 0);
            //Transition gotoWanderFromScared = new Transition(andRandomLowFear, wanderState, 0);
            Transition gotoWanderFromGraze  = new Transition(wanderTrue, wanderState, 0);
            Transition gotoWanderFromGraze2 = new Transition(andRandomNeighborCountLess, wanderState, 0);

            Transition gotoGrazeFromScared = new Transition(andTimerLowFear, grazeState, 0);
            //Transition gotoGrazeFromScared = new Transition(andRandomLowFear, grazeState, 0);
            Transition gotoGrazeFromWander = new Transition(wandertoGrazeTimer, grazeState, 0);

            Transition gotoFlockfromGraze = new Transition(andRandomNeighborCountGreater, flockState, 0);
            Transition gotoGrazefromFlock = new Transition(flocktoGrazeTimer, grazeState, 0);

            Transition gotoScaredFromWander = new Transition(orFearThreat, scaredState, 0); //old cond was feargreaterthan 60
            Transition gotoScaredFromGraze  = new Transition(orFearThreat, scaredState, 0);
            Transition gotoScaredFromFlock  = new Transition(orFearThreat, scaredState, 0);

            Transition gotoFlee = new Transition(fearGreaterThan80, fleeState, 0);
            //Transition gotoScaredFromFlee = new Transition(andTimerLowFear, scaredState, 0);
            Transition gotoScaredFromFlee = new Transition(fearLessThan40, scaredState, 0);

            //Declaring actions for transitions
            gotoWanderFromScared.addActions(wanderAction);
            gotoWanderFromGraze.addActions(wanderAction);
            gotoWanderFromGraze2.addActions(wanderAction);
            gotoGrazeFromScared.addActions(grazeAction);
            gotoGrazeFromWander.addActions(grazeAction);
            gotoGrazefromFlock.addActions(grazeAction);
            gotoScaredFromWander.addActions(scaredAction);
            gotoScaredFromGraze.addActions(scaredAction);
            gotoFlee.addActions(deerFleeAction);
            gotoFlockfromGraze.addActions(flockAction);
            gotoScaredFromFlock.addActions(scaredAction);
            gotoScaredFromFlee.addActions(scaredAction);


            //Hooking up transitions to states
            scaredState.addTransition(gotoWanderFromScared);
            scaredState.addTransition(gotoGrazeFromScared);
            scaredState.addTransition(gotoFlee);

            wanderState.addTransition(gotoScaredFromWander);
            wanderState.addTransition(gotoGrazeFromWander);

            grazeState.addTransition(gotoScaredFromGraze);
            grazeState.addTransition(gotoWanderFromGraze);
            grazeState.addTransition(gotoFlockfromGraze);
            grazeState.addTransition(gotoWanderFromGraze2);

            fleeState.addTransition(gotoScaredFromFlee);

            flockState.addTransition(gotoGrazefromFlock);
            flockState.addTransition(gotoScaredFromFlock);


            FiniteStateMachine newFSM = new FiniteStateMachine(wanderState);

            deer.fsm = newFSM;
        }