// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        AI_StateMachine_Helper helper = animator.GetComponent <AI_StateMachine_Helper>();
        Movement    movement          = helper.getMovement();
        AI_Movement aiMovement        = helper.getAIMovement();
        Orient      orient            = helper.getOrient();
        Equipment   equipment         = helper.getEquipment();
        AI_Finder   aiEnemyFinder     = helper.getEnemyFinder();
        Vector2     thisPos           = movement.getPosition();

        List <Transform> memoryCache = aiEnemyFinder.getSightCache();

        if (memoryCache.Count == 0)
        {
            Debug.LogWarning("attack target not found");
            return;
        }

        // Get closest
        Vector2 tmp               = memoryCache[0].position;
        Vector2 closestPos        = tmp;
        float   closestDistSqrTmp = (tmp - thisPos).sqrMagnitude;
        float   closestDistSqr    = closestDistSqrTmp;

        for (int i = 1; i < memoryCache.Count; i++)
        {
            tmp = memoryCache[i].position;
            closestDistSqrTmp = (tmp - thisPos).sqrMagnitude;
            if (closestDistSqr > closestDistSqrTmp)
            {
                closestDistSqr = closestDistSqrTmp;
                closestPos     = tmp;
            }
        }

        // Engage
        orient.lookAtStep(closestPos);
        aiMovement.Move(closestPos);

        // Attack when close
        AI_Weapon_Helper weaponAIHelper = equipment.getWeapon().GetComponent <AI_Weapon_Helper>();

        Assert.IsNotNull(weaponAIHelper);
        float meleeRange   = weaponAIHelper.getAttackRange();
        float enemyDistSqr = (closestPos - thisPos).sqrMagnitude;

        if (enemyDistSqr < meleeRange * meleeRange)
        {
            equipment.initPrimaryAttack();
        }
    }
Exemplo n.º 2
0
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        AI_StateMachine_Helper helper = animator.GetComponent <AI_StateMachine_Helper>();
        Movement    movement          = helper.getMovement();
        AI_Movement aiMovement        = helper.getAIMovement();
        Orient      orient            = helper.getOrient();
        Equipment   equipment         = helper.getEquipment();
        AI_Finder   weaponFinder      = helper.getWeaponFinder();

        List <Transform> sightCache = weaponFinder.getSightCache();

        if (sightCache.Count == 0)
        {
            Debug.LogWarning("Weapon not found");
            return;
        }

        // Find closest weapon
        Vector2   thisPos           = movement.getPosition();
        Transform tmp               = sightCache[0];
        Transform weaponClosest     = tmp;
        float     closestDistSqrTmp = ((Vector2)tmp.position - thisPos).sqrMagnitude;
        float     closestDistSqr    = closestDistSqrTmp;

        for (int i = 1; i < sightCache.Count; i++)
        {
            tmp = sightCache[i];
            closestDistSqrTmp = ((Vector2)tmp.position - thisPos).sqrMagnitude;
            if (closestDistSqr > closestDistSqrTmp)
            {
                closestDistSqr = closestDistSqrTmp;
                weaponClosest  = tmp;
            }
        }

        // Move & Face enemy
        Vector2 enemyPos = weaponClosest.position;

        orient.lookAtStep(enemyPos);
        aiMovement.Move(enemyPos);

        // Fetch when close
        float pickupRadius = equipment.getPickUpRadius();
        float enemyDistSqr = (enemyPos - thisPos).sqrMagnitude;

        if (enemyDistSqr < pickupRadius * pickupRadius)
        {
            equipment.pickUp();
        }
    }
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        AI_StateMachine_Helper helper = animator.GetComponent <AI_StateMachine_Helper>();
        Equipment equipment           = helper.getEquipment();
        Movement  movement            = helper.getMovement();
        Orient    orient        = helper.getOrient();
        AI_Finder aiEnemyFinder = helper.getEnemyFinder();
        Vector2   thisPos       = movement.getPosition();

        List <Transform> sightCache = aiEnemyFinder.getSightCache();

        if (sightCache.Count == 0)
        {
            Debug.LogWarning("attack target not found");
            return;
        }

        // Get closest
        Vector2 tmp               = sightCache[0].position;
        Vector2 enemyPos          = tmp;
        float   closestDistSqrTmp = (tmp - thisPos).sqrMagnitude;
        float   closestDistSqr    = closestDistSqrTmp;

        for (int i = 1; i < sightCache.Count; i++)
        {
            tmp = sightCache[i].position;
            closestDistSqrTmp = (tmp - thisPos).sqrMagnitude;
            if (closestDistSqr > closestDistSqrTmp)
            {
                closestDistSqr = closestDistSqrTmp;
                enemyPos       = tmp;
            }
        }

        // Engage
        orient.lookAtStep(enemyPos);
        equipment.initPrimaryAttack();
    }