public void LocateBuildingOnMap(GameObject building)
    {
        Vector3 pos = GetRandomPosition();

        if ((GetNearColliders(pos) == null || GetNearColliders(pos).Length == 0) && Move_out_of_zone.OnZone_Characters(pos.x, pos.y))
        {
            Instantiate(building, pos, Quaternion.identity);
        }
        else
        {
            LocateBuildingOnMap(building);
        }
    }
Exemple #2
0
    // Update is called once per frame
    void Update()
    {
        float scrollData = Input.GetAxis("Mouse ScrollWheel");

        targetZoom                       -= scrollData * zoomFactor;
        cam.orthographicSize              = Mathf.Lerp(cam.orthographicSize, targetZoom, Time.deltaTime * zoomSpeed);
        Point_Tool_Resources              = Camera.main.ScreenToWorldPoint(new Vector3(0, Camera.main.scaledPixelHeight, Camera.main.transform.position.z));
        Point_Tool_Resources.z            = Camera.main.transform.position.z + 5;
        Tool_Resources.transform.position = Point_Tool_Resources;
        //true on start
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("vrvr");
            touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            flag       = Move_out_of_zone.OnZone_Camera(touchStart.x, touchStart.y);
        }
        //two touch on screen to zoom in or out
        if (Input.touchCount == 2)
        {
            //store two touch position
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne  = Input.GetTouch(1);

            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
            Vector2 touchOnePrevPos  = touchOne.position - touchOne.deltaPosition;

            //distance between the point
            float prevMagnitude    = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float currentMagnitude = (touchZero.position - touchOne.position).magnitude;

            float difference = currentMagnitude - prevMagnitude;

            zoom(difference * 0.01f);
        }
        else if (Input.GetMouseButton(0) && flag)
        {
            Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            flag = Move_out_of_zone.OnZone_Camera(point.x, point.y);
            if (flag)
            {
                Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector3 v         = Camera.main.transform.position + direction;
                if (Move_out_of_zone.OnZone_Camera(v.x, v.y))
                {
                    Camera.main.transform.position += direction;
                }
            }
        }
    }
Exemple #3
0
    void Update()
    {
        bool moving = GetComponent <Walk>().IsMoving();

        if (attacking && !moving)
        {
            rb.velocity = new Vector3(0, 0, 0);
        }
        if (attackedAnimator != null)//the attacker died
        {
            if (attackedAnimator.GetBool("die"))
            {
                animator.SetBool("toAttack", false);
            }
        }

        if (this.tag.Contains("colony"))
        {
            bool    detectedTouch       = Input.GetMouseButtonDown(0);
            Vector3 p                   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            bool    detectedTouchInZone = Move_out_of_zone.OnZone_Characters(p.x, p.y);

            if (detectedTouch)
            {
                //touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                //Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
                //RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Vector2.zero);
                Ray          ray            = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit2D hitInformation = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
                Vector3      mousePos       = Input.mousePosition;
                mousePos = Camera.main.ScreenToWorldPoint(mousePos);
                Collider2D[] nearSoldiers1 = GetNearSoldiers(mousePos);


                if ((hitInformation.collider != null && nearSoldiers1 != null && nearSoldiers1.Length > 0) && !soldierChosen)
                {
                    Transform t = nearSoldiers1[0].transform;

                    float dis = Mathf.Infinity;
                    foreach (Collider2D collider in nearSoldiers1)
                    {
                        float currDis = Mathf.Abs(Vector3.Distance(collider.transform.position, mousePos));
                        if (currDis < dis)
                        {
                            dis = currDis;
                            t   = collider.transform;
                        }
                    }

                    if (this.tag == t.tag)
                    {
                        soldierChosen = true;
                    }
                }
                else if (soldierChosen)
                {
                    Collider2D[] nearSoldiers2 = GetNearSoldiers(mousePos);

                    if (nearSoldiers2 != null && nearSoldiers2.Length > 0)
                    {
                        targetToAttack = (nearSoldiers2[0].name.Split(' '))[0];
                        targetChosen   = true;
                    }
                    else
                    {
                        targetChosen = false;
                    }
                    soldierChosen = false;
                }
            }
        }



        if (this.tag.Contains("colony"))
        {
            bool solAttacked         = attacked != null;
            bool solAttackedAnimator = attacked != null;
            if (solAttacked && solAttackedAnimator)
            {
                setAttack = true;

                Attack attackCompOfAttacked = attacked.GetComponent <Attack>();
                if (attackCompOfAttacked != null)
                {
                    GameObject attackedOfAttacked = attackCompOfAttacked.GetAttacked();
                    //if (attackCompOfAttacked != null)
                    //{
                    //    AttackCollision(attackedOfAttacked);
                    //}
                }
            }
            else
            {
                setAttack = false;
            }
        }
    }