// state update function
    void UpdateAIState()
    {
        // no need to update state if next state is still the same
        if (m_nextState == m_currentState && m_nextState != EnemyAIState.Restart)
        {
            return;
        }

        // call state end
        m_currentStateExit?.Invoke();

        // switch state
        if (m_nextState != EnemyAIState.Restart)
        {
            m_currentState = m_nextState;
        }
        m_nextState = m_currentState;
        switch (m_currentState)
        {
        case EnemyAIState.Idle:
            m_currentStateInit   = null;
            m_currentStateUpdate = IdleStateUpdate;
            m_currentStateExit   = null;
            break;

        case EnemyAIState.MoveToPlayer:
            m_currentStateInit   = MoveToPlayerStateInit;
            m_currentStateUpdate = MoveToPlayerStateUpdate;
            m_currentStateExit   = null;
            break;

        case EnemyAIState.FireAtPlayer:
            m_currentStateInit   = null;
            m_currentStateUpdate = FireAtPlayerUpdate;
            m_currentStateExit   = null;
            break;

        default: throw new System.InvalidOperationException("Undefined enemy AI state!");
        }

        // call state begin
        m_currentStateInit?.Invoke();
        m_reactionProcessing = m_reactionSpeed;
    }
Beispiel #2
0
 public virtual void MotionDefend(GameObject target)
 {
     m_anim.SetBool("Walk", true);
     nav.SetDestination(target.transform.position);
     if (CheckEnemy())
     {
         if (EnemyIsAround(AttackDistance, theTarget) || TowerIsAround(30, target))
         {
             Debug.Log("Enemy IS In AttackRange");
             m_anim.SetBool("Walk", false);
             m_aiState = EnemyAIState.ATTACK;
         }
         else
         {
             Debug.Log("Wander See Enemy Start Chase");
             m_aiState = EnemyAIState.Chase;
         }
     }
 }
    // initialize function
    public void Initialize(GameManager gameManager, int depth, int cell, bool isAmbush)
    {
        // do poolable initialize
        base.Initialize();

        // get references
        m_gameManager = gameManager;
        m_playerActor = m_gameManager.m_player.GetComponent <Actor>();

        // get and initialize actor
        m_actor = GetComponent <Actor>();
        m_actor.Initialize(this, gameManager, depth, cell, isAmbush);

        // initialize state
        m_currentState     = EnemyAIState.Restart;
        m_nextState        = isAmbush ? EnemyAIState.MoveToPlayer : EnemyAIState.Idle;
        m_currentStateInit = null;
        UpdateAIState();
        m_reactionProcessing = 0f;
    }
Beispiel #4
0
 private void CheckForEnemy()
 {
     Collider2D[] foundObjects = Physics2D.OverlapCircleAll(transform.position, chaseRange);
     if (foundObjects.Length > 0)
     {
         bool foundPlayer = false;
         for (int i = 0; i < foundObjects.Length && !foundPlayer; i++)
         {
             for (Transform trans = foundObjects[i].transform; trans != null; trans = trans.parent)
             {
                 if (trans.CompareTag("Player"))
                 {
                     targetObject = trans;
                     aiState      = EnemyAIState.Chase;
                     break;
                 }
             }
         }
     }
 }
Beispiel #5
0
    private void Aggro()
    {
        if (isGrounded)
        {
            rb.MovePosition(transform.position + transform.right * moveSpeed * Time.deltaTime);
        }

        KnockbackCheck();

        //if the player leaves the chase radius choose a new state.
        if (!PlayerRangeCheck(chaseRadius))
        {
            ChooseState();
        }

        //if the player is within the attack radius change to the combat state.
        if (PlayerRangeCheck(attackRadius))
        {
            enemyState = EnemyAIState.Combat;
        }

        StateIndicator(aggroColor);
    }
    // State switch method
    public void SetState(EnemyAIState newState)
    {
        stateLastFrame      = currentEnemyAIState;
        currentEnemyAIState = newState;

        switch (currentEnemyAIState)
        {
        case EnemyAIState.Patrolling:
            OnPatrollingEnter();
            break;

        case EnemyAIState.Chasing:
            OnChasingEnter();
            break;

        case EnemyAIState.Attacking:
            OnAttackingEnter();
            break;

        case EnemyAIState.Dead:
            OnDeadEnter();
            break;
        }
    }
Beispiel #7
0
 void ChangeState(EnemyAIState enemyAIState)
 {
     CurrentState.OnStateExit(this);
     CurrentState = enemyAIState;
     enemyAIState.OnStateEnter(this);
 }
Beispiel #8
0
 public void SetState(EnemyAIState newState)
 {
     state = newState;
 }
Beispiel #9
0
    protected override void StateAction()
    {
        if (m_objInfo.IsDead == true)
        {
            m_aiState = EnemyAIState.DEAD;
        }
        m_objInfo.State = m_aiState.ToString();

        switch (m_aiState)
        {
        case EnemyAIState.IDLE:
        {
            //1
            MotionIdle(WaitTime);
        }

        break;

        case EnemyAIState.Wander:
        {
            MotionWander(targetPos, wanderTime);
        }

        break;

        case EnemyAIState.WALK:
        {
            //1
            MotionWalk(targetPos.gameObject);
        }


        break;

        case EnemyAIState.Chase:
        {
            MotionChase(theTarget);
        }

        break;

        case EnemyAIState.FLY:
        {
            //1
            MotionFly();
        }


        break;

        case EnemyAIState.ATTACK:
        {
            //1
            MotionAttack(theTarget);
        }


        break;

        case EnemyAIState.DEFEND:
        {
            //1
            MotionDefend(targetPos.transform.gameObject);
            //MotionDefend(GameObject.FindObjectOfType<StageChallange>().gameObject);
        }

        break;

        case EnemyAIState.Avoid:
        {
            if (currentAvoidTime < AvoidTime)
            {
                MotionAvoid(theTarget);
            }
            else
            {
                currentAvoidTime = 0;
                m_aiState        = EnemyAIState.IDLE;
            }
        }

        break;

        case EnemyAIState.Escape:
        {
            MotionEscape(theTarget);
        }

        break;

        case EnemyAIState.DEAD:
        {
            //1
            MotionDead();
        }

        break;

        default:
            break;
        }

        if (BeAttack)
        {
            particileControl.Play(0, 2, Role.transform.position + Role.transform.forward + new Vector3(0, 1, 0));
        }
    }
Beispiel #10
0
    // Update is called once per frame
    void Update()
    {
        float playerDistance = (sp.lm.playerInstance.transform.position - transform.position).magnitude;

        if (playerDistance < aggroRadius)
        {
            aggro += aggroGainRate * Time.deltaTime;
        }
        else
        {
            aggro -= aggroDecayRate * Time.deltaTime;
            aggro  = Mathf.Max(aggro, 0);
        }

        if (aggro > aggroThreshold)
        {
            if (dmg.health / dmg.maxHealth < fleeThreshold)
            {
                state = EnemyAIState.Flee;
            }
            else
            {
                state = EnemyAIState.Pursue;
            }
            this.transform.rotation = Quaternion.Euler(new Vector3(0, Quaternion.LookRotation((sp.lm.playerInstance.transform.position - transform.position).normalized).eulerAngles.y, 0));
            if (bs.isFiring && Random.value < (timeInState / triggerInertia) / (1 + (triggerHappiness * aggro)))
            {
                timeInState = 0.0f;
                bs.CeaseFirePrimary();
            }
            if (!bs.isFiring && Random.value < (triggerHappiness * aggro * timeInState) / triggerInertia)
            {
                timeInState = 0.0f;
                bs.StartFirePrimary();
            }
            timeInState += Time.deltaTime;
        }
        else
        {
            bs.CeaseFirePrimary();
            state       = EnemyAIState.Wander;
            timeInState = 0.0f;
        }

        switch (state)
        {
        case EnemyAIState.Wander:
            if (wanderTime <= 0)
            {
                Vector3 r = Random.onUnitSphere;
                r.y        = 0;
                wanderDest = transform.position + r * wanderDistance;
                wanderTime = Random.value * wanderStagnation;
            }

            break;

        case EnemyAIState.Pursue:
            if (wanderTime <= 0)
            {
                Vector3 r = Random.onUnitSphere;
                r.y        = 0;
                wanderDest = transform.position + (r + ((sp.lm.playerInstance.transform.position - this.transform.position).normalized * aggro)).normalized * wanderDistance;
                wanderTime = Random.value * wanderStagnation / (aggro + 0.1f);
            }

            break;

        case EnemyAIState.Flee:
            if (wanderTime <= 0)
            {
                Vector3 r = Random.onUnitSphere;
                r.y        = 0;
                wanderDest = transform.position + (r + ((this.transform.position - sp.lm.playerInstance.transform.position).normalized * aggro)).normalized * wanderDistance;
                wanderTime = Random.value * wanderStagnation / (aggro + 0.1f);
            }
            break;

        default:
            break;
        }
        if ((wanderDest - transform.position).magnitude >= 2.6)
        {
            if (bs.transportSocket.transport != null)
            {
                bs.transportSocket.transport.nodeObject.Drive((wanderDest - transform.position).normalized);
            }
        }
        wanderTime -= Time.deltaTime;
    }
Beispiel #11
0
 void StartRoaming()
 {
     aiState = EnemyAIState.Roaming;
 }
Beispiel #12
0
 void ChaseFleePlayer()
 {
     moving    = false;
     waitTimer = 0;
     aiState   = EnemyAIState.ChaseFleeing;
 }