Пример #1
0
    // 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");
    }