Exemplo n.º 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) {
    //
    //}

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{

    //}

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        AI_StateMachine_Helper helper = animator.GetComponent <AI_StateMachine_Helper>();
        AI_Memory aiSoundMemory       = helper.getSoundReceiver();

        aiSoundMemory.clearMemory();
    }
    // 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");
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        memory = GetComponent <AI_Memory>();
        vision = GetComponent <AI_Vision>();
        rb     = GetComponent <Rigidbody>();

        rotation.y = 1000.0f;
    }
Exemplo n.º 4
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);
        }
    }
    void Start()
    {
        StartCoroutine(FSM());

        memory    = GetComponent <AI_Memory>();
        vision    = GetComponent <AI_Vision>();
        inventory = GetComponent <Inventory_Control>();
        rb        = GetComponent <Rigidbody>();

        rotation.y = 1000.0f;

        On_Start();
    }
    // 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);
    }
Exemplo n.º 7
0
 // Start is called before the first frame update
 void Start()
 {
     mem = GetComponent <AI_Memory>();
 }