Example #1
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)
    //{
    //    Vector2 enemyPos = aiEnemyFinder.getClosestEnemy().position;
    //    Vector2 thisPos = movement.getPosition();
    //    movement.SetDirectionVector(thisPos - enemyPos);
    //}

    // 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();
        AI_Memory   aiMemory          = helper.getMemory();
        Vector2     thisPos           = movement.getPosition();

        List <Memory> memoryCache = aiMemory.getMemoryCache();

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

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

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

        // Get hiding spot
        Transform hidingSpot = Navigation_manual.nearest_cover(thisPos, enemyPos);

        // Flee to hiding spot
        if (hidingSpot != null)
        {
            orient.lookAtStep(hidingSpot.position);
            aiMovement.Move(hidingSpot.position);

            // Combat point reach
            bool positionReached = movement.positionReached(hidingSpot.position);
            if (positionReached)
            {
                animator.SetTrigger("CoverPointReached");
            }
        }
        // Flee from enemy
        else
        {
            Vector2 moveDir = thisPos - enemyPos;
            orient.lookAtStep(enemyPos);
            movement.Move(moveDir);
        }
    }
    // 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();
        AI_Memory   aiMemory          = helper.getMemory();

        List <Memory> memoryCache = aiMemory.getMemoryCache();

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

        // Go to last combat location
        Vector2 lastSeenEnemyPos = memoryCache[0].lastSeenPosition;

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

        // Combat point reach
        bool positionReached = movement.positionReached(lastSeenEnemyPos);

        if (positionReached)
        {
            memoryCache.RemoveAt(0);
            animator.SetTrigger("CombatPointReached");
        }
    }
    // 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();
        int         patrolIndex       = animator.GetInteger("PatrolLocationIndex");

        // Patrol if location available
        if (patrolIndex > 0)
        {
            Transform randomPatrolSpot = Navigation_manual.getPatrolLocation(patrolIndex);
            Vector2   visitPos         = randomPatrolSpot.position;
            orient.lookAtStep(visitPos);
            aiMovement.Move(visitPos);

            // Patrol point reach
            bool positionReached = movement.positionReached(visitPos);
            if (positionReached)
            {
                animator.SetTrigger("PatrolPointReached");
            }
        }
        else
        {
            Debug.LogWarning("Patrol location not found");
            animator.SetTrigger("PatrolPointReached");
        }
    }
    // 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();
        Orient   orient  = helper.getOrient();
        Vector2  thisPos = movement.getPosition();

        bool feelPain = animator.GetBool("FeelPain");

        //List<Vector2> locationMemory = helper.getMemory().getLocations();
        if (feelPain)
        {
            AI_Sense_Pain painRecepter  = helper.getPainRecepter();
            Vector2       painDirection = painRecepter.getLatestDamageDirection();
            Vector2       respondToDir  = thisPos + painDirection;
            orient.lookAtStep(respondToDir);
        }
        //else if(locationMemory.Count > 0)
        //{
        //    Vector2 targetLocation = locationMemory[locationMemory.Count - 1];
        //    orient.lookAtStep(targetLocation);

        //    //bool weaponized =
        //    //movement.Move(targetLocation - thisPos);
        //}
    }
    // 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();
        }
    }
Example #6
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>();
        Orient    orient   = helper.getOrient();
        AI_Memory aiMemory = helper.getMemory();

        List <Memory> memoryCache = aiMemory.getMemoryCache();

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

        // Go to last combat location
        Vector2 lastSeenEnemyPos = memoryCache[0].lastSeenPosition;

        orient.lookAtStep(lastSeenEnemyPos);
    }
    // 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();
    }