예제 #1
0
 private void OnDrawGizmos()
 {
     if (Application.isPlaying && showSight)
     {
         if (AiMaths.SightSphere(agentCollider, agentInfo.sightRange, playerMask))
         {
             Gizmos.color = Color.green;
         }
         else
         {
             Gizmos.color = Color.red;
         }
         Gizmos.DrawWireSphere(transform.position, agentInfo.sightRange);
     }
 }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        //Debug.DrawRay(agentCollider.bounds.center, Vector2.right, Color.red);
        //Get TerrainMap
        if (!getTerrain)
        {
            terrainMap = GameObject.FindGameObjectWithTag("TerrainMap").GetComponent <GridSettings>().GetGrid();
            getTerrain = true;
        }

        if (currentState == AiStates.Patrol)
        {
            path.Clear();
            AiAnimations.Walk(agentAnimator);
            if (AiMaths.SightSphere(agentCollider, agentInfo.sightRange, playerMask))
            {
                currentState = AiStates.Chase;
            }

            else if (AiMaths.raycastSides(agentCollider.GetComponent <Collider2D>(), floorMask, (int)transform.localScale.x, gameObject))
            {
                transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
            }

            GetComponent <Rigidbody2D>().velocity = new Vector2(3 * (int)transform.localScale.x, GetComponent <Rigidbody2D>().velocity.y);
        }
        else if (currentState == AiStates.Chase)
        {
            if (KylesFunctions.IsNextToWall2D(transform.localScale.x, agentCollider, 0.5f, playerMask))
            {
                path.Clear();
                currentState = AiStates.Attack;
            }
            else
            {
                AiAnimations.Walk(agentAnimator);



                if (AiMaths.SightSphere(agentCollider, agentInfo.sightRange, playerMask))
                {
                    Vector2 agentNewHeight = new Vector2(GetComponent <CapsuleCollider2D>().bounds.center.x, GetComponent <CapsuleCollider2D>().bounds.center.y - GetComponent <CapsuleCollider2D>().bounds.extents.y);
                    if (!getPlayerPos)
                    {
                        // KylesFunctions.GetXY(agentNewHeight, terrainMap.GetOriginPos(), terrainMap.GetCellSize(), out playerGridPos);
                        KylesFunctions.GetXY(player.transform.position, terrainMap.GetOriginPos(), terrainMap.GetCellSize(), out playerGridPos);
                        getPlayerPos = true;
                    }

                    KylesFunctions.GetXY(agentNewHeight, terrainMap.GetOriginPos(), terrainMap.GetCellSize(), out agentGridPos);
                    KylesFunctions.GetXY(player.transform.position, terrainMap.GetOriginPos(), terrainMap.GetCellSize(), out newPlayerPos);


                    if (newPlayerPos != playerGridPos)
                    {
                        // playerGridPos = newPlayerPos;
                        getPlayerPos = false;
                        path.Clear();
                        findPath = true;
                    }

                    if (path.Count == 0 && !KylesFunctions.IsNextToWall2D(transform.localScale.x, agentCollider, 0.5f, playerMask))
                    {
                        findPath = true;
                    }

                    if (findPath)
                    {
                        KylesFunctions.AStar(terrainMap, agentGridPos.x, agentGridPos.y, playerGridPos.x, playerGridPos.y, 2, false, ref path);
                        findPath = false;
                    }
                }
                else
                {
                    currentState = AiStates.Patrol;
                }
            }
        }
        else if (currentState == AiStates.Attack)
        {
            timer = Time.deltaTime + timer;
            if (!AiMaths.SightSphere(agentCollider, 1.0f, playerMask))
            {
                currentState = AiStates.Chase;
                timer        = 3;
            }

            else if (!isAttacking && timer > 3.0f)
            {
                isAttacking = true;
                audioManager.Play("MantisAttack", gameObject);

                StartCoroutine(DoAttack());
                timer = 0;
            }
            else
            {
                AiAnimations.Walk(agentAnimator);
            }
        }
        else if (currentState == AiStates.AmountOfStates)
        {
        }
    }