예제 #1
0
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3      mousePos   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2      mousePos2D = new Vector2(mousePos.x, mousePos.y);
            RaycastHit2D hit        = Physics2D.Raycast(mousePos2D, Vector2.zero);

            PointerEventData     pointerData = new PointerEventData(EventSystem.current);
            List <RaycastResult> results     = new List <RaycastResult>();
            pointerData.position = Input.mousePosition;
            this.raycaster.Raycast(pointerData, results);

            if (barracks.GetPlaced() && (gameObject.activeSelf == true && barracksMenu.activeSelf == true))
            {
                // if player hasnt clicked on UI
                if (results.Count == 0 || results == null)
                {
                    if (hit.collider != null)
                    {
                        // if player doesnt click on current barracks (but click on another barracks)
                        if (hit.collider.gameObject != gameObject.transform.parent.gameObject)
                        {
                            Hide();
                        }
                    }
                    else
                    {
                        Hide();
                    }
                }
            }
        }
    }
예제 #2
0
    void Update()
    {
        if (barracks.GetPlaced() == false)
        {
            transform.position  = GridManager.instance.ValidateWorldGridPosition(Utilities.GetMouseWorldPosition());
            transform.position += new Vector3(1, 1, 0) * GridManager.instance.grid.GetCellSize() * .5f;
        }

        if (Input.GetMouseButtonDown(1))
        {
            if (barracks.GetPlaced() == false)
            {
                Destroy(gameObject);
            }
        }

        if (Input.GetMouseButtonDown(0))
        {
            if (barracks.GetPlaced() == false)
            {
                if (Vector3.Distance(transform.position, barracks.GetBase().transform.position) <= barracks.GetBase().getBuildRange())
                {
                    if (!GridManager.instance.GetStructureMap().ContainsKey(transform.position))
                    {
                        barracks.SetPlaced(true);
                        barracks.AddPathPosition(transform.position);
                        barracks.GetBase().AddStrucureToBase(barracks);
                        GridManager.instance.AddStructure(transform.position, barracks);
                    }
                    else
                    {
                        // can higlight red if over an existing structure
                        Debug.Log("There already exists a structure at that location");
                    }
                }
                else
                {
                    //later down the line. Can highlight red when out of range
                    Debug.Log("barracks not in range");
                }
            }
        }
    }