Example #1
0
    /////////////////////////////////////////////////////////////////////////////
    /// Function:               RunMovingState
    /////////////////////////////////////////////////////////////////////////////
    private void RunMovingState()
    {
        // Enable the moving animation if it's not on.
        if (false == m_anAnimator.GetBool(AnimatorValues.ANIMATOR_IS_MOVING))
        {
            m_anAnimator.SetBool(AnimatorValues.ANIMATOR_IS_MOVING, true);
        }

        m_fOldTime = m_fElapsedTime + 0.01f;

        if (m_fElapsedTime > m_fCheckTime)
        {
            m_fCheckTime = m_fElapsedTime + 1;
            SetTarget();
        }

        if (m_liPath != null)
        {
            if (true == m_bReachedNode)
            {
                m_bReachedNode = false;
                m_v3CurrNode   = m_liPath[m_iNodeIndex];
            }
            else if (true == m_bReachedTarget)
            {
                // We reached our destination, switch to idle and clear the target
                //  vector.
                m_bReachedTarget = false;
                //m_bReachedNode = false;
                m_eState   = ETankState.TANK_IDLE;
                m_v3Target = Vector3.zero;
                return;
            }
            else
            {
                GoTo();
            }
        }
    }
Example #2
0
    /////////////////////////////////////////////////////////////////////////////
    /// Function:               CheckForTransitions
    /////////////////////////////////////////////////////////////////////////////
    private void CheckForTransitions()
    {
        // We need to constantly check if the NPC died.
        if (m_fHealth <= 0)
        {
            m_eState = ETankState.TANK_DEAD;
            return;
        }

        // The drone will rush to the homebase while it's holding the flag.
        if (true == m_bHasFlag)
        {
            if (m_v3Target != m_goHomeBase.transform.position)
            {
                // Ensure that we're headed towards our base.
                m_v3Target = m_goHomeBase.transform.position;

                MoveOrder(m_v3Target);
                m_eState = ETankState.TANK_MOVING;
            }
            return;
        }

        // Get a handle on the closest enemy and calculate distance from it.
        m_trClosestEnemy = GetClosestEnemy();
        if (null != m_trClosestEnemy)
        {
            float fDistance = Vector3.Distance(m_trClosestEnemy.transform.position, transform.position);

            if (fDistance <= Constants.DEFAULT_ATTACK_RANGE)
            {
                m_v3Target       = Vector3.zero;
                m_eState         = ETankState.TANK_ATTACKING;
                m_bTargetInRange = true;
            }
        }
        else
        {
            m_bTargetInRange   = false;
            m_bIsBeingAttacked = false;
        }

        // According to current state, we will check if we need to transition to
        //  a different state.
        switch (m_eState)
        {
        case ETankState.TANK_IDLE:

            if (true == m_bIsBeingAttacked)
            {
                // Attempt to decide if we should attack or flee.
                if (m_fHealth < Constants.DEFAULT_HEALTH_TANK / 2)
                {
                    // There's a 4 in 6 chance that the drone will try to run to home base.
                    if (Random.Range(0, 5) < 4)
                    {
                        m_v3Target = m_goHomeBase.transform.position;
                        m_eState   = ETankState.TANK_MOVING;
                        break;
                    }
                }
            }

            if (Vector3.zero != m_v3Target)
            {
                MoveOrder(m_v3Target);
                m_eState = ETankState.TANK_MOVING;
            }

            break;

        case ETankState.TANK_MOVING:
            break;

        case ETankState.TANK_ATTACKING:

            if (null == m_trClosestEnemy)
            {
                // The enemy has been destroyed, switch back to idle.
                m_eState           = ETankState.TANK_IDLE;
                m_v3Target         = Vector3.zero;
                m_bTargetInRange   = false;
                m_bIsBeingAttacked = false;
                break;
            }

            StartCoroutine(AttackTarget(m_trClosestEnemy.transform));

            float fDistance = Vector3.Distance(m_trClosestEnemy.transform.position, transform.position);

            if (fDistance > Constants.DEFAULT_ATTACK_RANGE + 20f)
            {
                m_v3Target       = m_trClosestEnemy.transform.position;
                m_eState         = ETankState.TANK_MOVING;
                m_bTargetInRange = false;
            }

            break;
        }
    }