Beispiel #1
0
    private void LateUpdate()
    {
        if (canMove)
        {
            actions act = atb.GetActions();

            //Pegando posição do mouse no mundo
            Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            //Comparando com a do jogador
            Vector2 delta = mousePos;
            delta.x -= transform.position.x;
            delta.y -= transform.position.y;
            //Ajustando mira
            float distTarget = delta.magnitude;
            //Caso o mouse estaja mais distance que alcance da arma
            if (distTarget > act.ReachNow())
            {
                distTarget = act.ReachNow();
            }
            Vector2 posTarget = Vector2.up * distTarget;
            //transformTarget.localPosition = posTarget;

            //Fazer camera seguir
            Vector3 posCam = Camera.main.transform.position;
            posCam.x = transform.position.x;
            posCam.y = transform.position.y;
            Camera.main.transform.position = posCam;
        }
    }
Beispiel #2
0
    IEnumerator IAAtive()
    {
        while (true)
        {
            if (atb && !atb.IsDead())
            {
                if (stateNow == statesIa.awareness)
                {
                    if (!moveTarget)
                    {
                        //Dando direção aletoria para patrulhar
                        float   walkDist = Random.Range(3f, 9f), angZ = Random.Range(0f, 360f);
                        Vector2 dire = Quaternion.Euler(Vector3.forward * angZ) * Vector2.up;
                        //Debug.Log(dire);
                        targetPosition  = transform.position;
                        targetPosition += dire * walkDist;
                        distTarget      = 1f;
                        moveTarget      = true;
                        //Olhar apara direção do movimento
                        //atb.GetMovement().TurnTo(Vector2.SignedAngle(Vector2.up, dire));
                    }

                    //Visão da IA
                    //Area da que a camera pega
                    Camera  cam = Camera.main;
                    Vector2 size;
                    size.y = 2f * cam.orthographicSize;
                    size.x = size.y * cam.aspect;
                    Vector2 point1 = transform.position, point2 = point1 + size / 2f;
                    point1 -= size / 2f;

                    Collider2D[] colls = Physics2D.OverlapAreaAll(point1, point2, repository.GetLayerMaskChars());
                    //Todos os personagens na area
                    for (int i = 0; i < colls.Length; i++)
                    {
                        Collider2D c = colls[i];
                        if (!c.CompareTag(tag))
                        {
                            //Angulo de visão
                            Vector2 delta = c.transform.position - transform.position;
                            float   angZ  = Vector2.SignedAngle(transform.up, delta);

                            if (angZ * angZ <= 90 * 90)
                            {
                                //Dizendo que é o alvo
                                attributes atbX = c.GetComponent <attributes>();

                                if (!atbX.IsDead())
                                {
                                    atbTarget = atbX;
                                    stateNow  = statesIa.chase;
                                }
                            }
                        }
                    }
                }
                else if (stateNow == statesIa.chase)
                {
                    actions act = atb.GetActions();
                    act.StopFire();

                    if (atbTarget.IsDead())
                    {
                        atbTarget = null;
                        stateNow  = statesIa.awareness;
                    }
                    else
                    {
                        Vector2 delta = atbTarget.transform.position - transform.position;
                        if (delta.magnitude < act.ReachNow() && renderer.isVisible)
                        {
                            stateNow   = statesIa.shooting;
                            moveTarget = false;
                        }
                        else
                        {
                            targetPosition = atbTarget.transform.position;
                            Vector2 dire = targetPosition;
                            dire.x -= transform.position.x;
                            dire.y -= transform.position.y;
                            //atb.GetMovement().TurnTo(Vector2.SignedAngle(Vector2.up, dire));
                            distTarget = 2f;
                            moveTarget = true;
                        }
                    }
                }
                else if (stateNow == statesIa.shooting)
                {
                    Vector2 delta = atbTarget.transform.position - transform.position;
                    actions act   = atb.GetActions();

                    if (delta.magnitude < act.ReachNow() && renderer.isVisible && !atbTarget.IsDead())
                    {
                        /*float angZNeed = Vector2.SignedAngle(Vector2.up, delta), angZNow = transform.eulerAngles.z;
                         * if (angZNeed < 0f)
                         *  angZNeed += 360f;
                         * if (angZNow < 0f)
                         *  angZNow += 360f;
                         *
                         * float angZDif = angZNeed - angZNow;
                         *
                         * if (angZDif*angZDif > 400)
                         * {
                         *  atb.GetMovement().TurnTo(angZNeed);
                         * }
                         * else
                         * {
                         *  act.Fire();
                         * }*/
                    }
                    else
                    {
                        stateNow = statesIa.chase;
                    }
                }
            }

            yield return(new WaitForSeconds(timeAction));
        }
    }