void Update()
 {
     moneyText.text = PlayerResources.GetMoney().ToString();
     ironText.text  = PlayerResources.GetIron().ToString();
     coalText.text  = PlayerResources.GetCoal().ToString();
     leadText.text  = PlayerResources.GetLead().ToString();
 }
예제 #2
0
        public void OnDrag(PointerEventData eventData)
        {
            //dont do anything until the player has dragged their cursor onto the map - do not place towers through the UI
            if (EventSystem.current.IsPointerOverGameObject())
            {
                if (Tower)               //
                {                        // player can cancel placing a tower by dragging it back onto the UI
                    Destroy(Tower);      //
                    readyToPlace = true; //
                }                        //
                return;
            }
            // on the first frame that the player has dragged their cursor onto the map, create the object
            if (readyToPlace)
            {
                Tower        = Instantiate(prefab, Vector3.zero, Quaternion.identity);
                readyToPlace = false;
            }

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out RaycastHit hit))
            {
                // convert cursor position into worldspace and tilespace
                Vector3 clickInWorldspace = hit.collider.gameObject.transform.position;
                Vector3 clickInTilespace  = new Vector3(Mathf.RoundToInt(clickInWorldspace.x), 0.0f, Mathf.RoundToInt(clickInWorldspace.z));

                // postion the tower under the cursor
                Tower.transform.position = clickInTilespace;

                allowPlacement = false;
                // if tower is walkable, change model colour to green
                if (isPlacableWithOrthogonal(clickInTilespace))
                {
                    if (developerMode ||                                                                    // if dev mode turned on, place tower OR
                        (PlayerResources.GetMoney() >= moneyCost && PlayerResources.GetIron() >= ironCost)) // of player can afford it, place tower
                    {
                        Renderer[] rend = Tower.GetComponentsInChildren <Renderer>();
                        foreach (Renderer r in rend)
                        {
                            r.material.SetColor("_BaseColor", Color.green);
                        }
                        allowPlacement = true;
                    }
                    // if player can not afford tower, show as red, and do not allow placement when click released
                    else
                    {
                        Renderer[] rend = Tower.GetComponentsInChildren <Renderer>();
                        foreach (Renderer r in rend)
                        {
                            r.material.SetColor("_BaseColor", Color.red);
                        }
                    }
                }
                // if tower is NOT walkable, change model colour to red
                else
                {
                    Renderer[] rend = Tower.GetComponentsInChildren <Renderer>();
                    foreach (Renderer r in rend)
                    {
                        r.material.SetColor("_BaseColor", Color.red);
                    }
                    allowPlacement = false;
                }

                if (Tile.Vector3ToTile(clickInTilespace).towerObject != null)
                {
                    Renderer[] rend = Tower.GetComponentsInChildren <Renderer>();
                    foreach (Renderer r in rend)
                    {
                        r.material.SetColor("_BaseColor", Color.red);
                    }
                    allowPlacement = false;

                    //Debug.LogWarning("Player attempted to place a tower ontop of another tower. cancelling placement");
                }
            }
        }