Example #1
0
            public AIFSM(string name, EnemyController controller) : base(name, controller)
            {
                if (!controller)
                {
                    // TODO: Add log
                    LogHalt("EnemyController object null!");
                    Halt();
                }

                                #if UNITY_EDITOR
                LogThis = true;
                                #endif

                idleState       = new IdleState(this, EAIState.IDLE, "STATE_IDLE", controller);
                alertState      = new AlertState(this, EAIState.ALERT, "STATE_ALERT", controller);
                aggressiveState = new AggressiveState(this, EAIState.AGGRESSIVE, "STATE_AGGRESSIVE", controller);

                idleState.AddTransition(new Tran_IdleToAlert(alertState, controller));

                alertState.AddTransition(new Tran_AlertToAggressive(aggressiveState, controller));
                alertState.AddTransition(new Tran_AlertToIdle(idleState, controller));

                aggressiveState.AddTransition(new Tran_AggressiveToAlert(alertState, controller));

                AddState(idleState);
                AddState(alertState);
                AddState(aggressiveState);
            }
Example #2
0
    // The NPC has two states: FollowPath and ChasePlayer
    // If it's on the first state and SawPlayer transition is fired, it changes to ChasePlayer
    // If it's on ChasePlayerState and LostPlayer transition is fired, it returns to FollowPath
    private void MakeFSM()
    {
        FollowPathState follow = new FollowPathState(/*path*/);

        follow.AddTransition(Transition.SawPlayer, StateID.ChasingPlayer);

        ChasePlayerState chase = new ChasePlayerState();

        chase.AddTransition(Transition.LostPlayer, StateID.FollowingPath);

        PatrolState patrolState = new PatrolState(PatrolWayPoints);

        patrolState.AddTransition(Transition.Alert, StateID.AlertNpc);

        AlertState alertState = new AlertState();

        alertState.AddTransition(Transition.Attack, StateID.AttackingPlayer);
        alertState.AddTransition(Transition.Patrol, StateID.Patroling);

        AttackState attackState = new AttackState();

        attackState.AddTransition(Transition.Alert, StateID.AlertNpc);


        fsm = new FSMSystem();
        fsm.AddState(patrolState);
        fsm.AddState(alertState);
        fsm.AddState(attackState);
        //fsm.AddState(chase);
        //fsm.AddState(follow);

        Debug.Log("First State: " + fsm.CurrentState.ToString());
    }
Example #3
0
    void Start()
    {
        animator = GetComponent <Animator>();

        var patrol      = new WonderState <Feed>(threshold, speed, this.transform, this.GetComponent <PatrolWonder>());
        var chase       = new ChaseState <Feed>(this.transform, target, speed);
        var alert       = new AlertState <Feed>(spotLight, this.GetComponent <Patrol>());
        var meleeattack = new PatrolAttackState <Feed>(this.transform);

        patrol.AddTransition(Feed.EnemigoEntraEnLOS, chase);

        chase.AddTransition(Feed.EnemigoSaleDeLOS, alert);
        chase.AddTransition(Feed.EntraEnRangoDeAtaque, meleeattack);

        alert.AddTransition(Feed.EnemigoEntraEnLOS, chase);
        alert.AddTransition(Feed.NoHayEnemigos, patrol);

        meleeattack.AddTransition(Feed.SaleDeRangoDeAtaque, chase);
        meleeattack.AddTransition(Feed.EnemigoSaleDeLOS, alert);

        stateMachine = new FSM <Feed>(patrol);

        los = GetComponent <LOS>();
    }
Example #4
0
    private void Start()
    {
        //Agarro el model
        _model = GetComponent <Model>();

        //Creo la FSM
        _fsm = new FSM <string>();

        //Creo los estados
        IdleState <string>   idle   = new IdleState <string>(this);
        PatrolState <string> patrol = new PatrolState <string>(Waypoints, transform, this);
        ShootState <string>  shoot  = new ShootState <string>(this);
        AlertState <string>  alert  = new AlertState <string>(this, Sight, ExclamationMark);
        SearchState <string> search = new SearchState <string>(this);

        //Creo las transiciones
        idle.AddTransition(_patrolKey, patrol);
        idle.AddTransition(_shootKey, shoot);
        idle.AddTransition(_alertKey, alert);
        idle.AddTransition(_searchKey, search);
        idle.AddTransition(_idleKey, idle); //se tienen a si mismos por si llega a tener que volverse a ejecutar con el random

        patrol.AddTransition(_idleKey, idle);
        patrol.AddTransition(_shootKey, shoot);
        patrol.AddTransition(_alertKey, alert);
        patrol.AddTransition(_searchKey, search);

        alert.AddTransition(_idleKey, idle);
        alert.AddTransition(_shootKey, shoot);
        alert.AddTransition(_patrolKey, patrol);
        alert.AddTransition(_alertKey, alert);
        alert.AddTransition(_searchKey, search);

        search.AddTransition(_searchKey, search);
        search.AddTransition(_idleKey, idle);
        search.AddTransition(_shootKey, shoot);
        search.AddTransition(_patrolKey, patrol);
        search.AddTransition(_alertKey, alert);

        //diccionario de todos los estados de idle para la roulette
        _statesRoulette = new Dictionary <string, int>();
        _statesRoulette.Add(_idleKey, 30);
        _statesRoulette.Add(_patrolKey, 70);
        _statesRoulette.Add(_searchKey, 50);

        //inicializo la FSM
        _fsm.SetInitialState(idle);

        //Inicializo los nodos del Desicion Tree
        ActionNode _shootActionNode       = new ActionNode(ChangeToShootState); //Aca pasarle funciĆ³n
        ActionNode _alertActionNode       = new ActionNode(ChangeToAlertState);
        ActionNode _randomStateActionNode = new ActionNode(ChangeToRandomState);

        _isFirstTimeQuestionNode    = new QuestionNode(Sight.SawTargetOnce, _shootActionNode, _alertActionNode);
        _isSeeingPlayerQuestionNode = new QuestionNode(Sight.IsSeeingTarget, _isFirstTimeQuestionNode, _randomStateActionNode);

        Sight.SawTarget.AddListener(ExecuteTree);

        // Roulette
        _actionRoulette = new Roulette <string>();
    }