Example #1
0
    private void Update()
    {
        var canAfford = ResourceManager.GetInstance().CanAfford(-buildingPrefab.GetWoodCost(), -buildingPrefab.GetStoneCost(), -buildingPrefab.GetMetalCost(), 0);

        button.interactable = canAfford;
        for (int i = 0; i < additionalGraphics.Count; i++)
        {
            additionalGraphics[i].color = canAfford ? originalColors[i] : button.colors.disabledColor;
        }
    }
Example #2
0
    private void Update()
    {
        var        cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        if (placeableInstance != null)
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                placeableInstance.transform.Rotate(new Vector3(0, 90, 0), Space.World);
            }

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                ClearPlaceable();
                return;
            }

            if (CanPlace())
            {
                placeableInstance.GetComponent <Selectable>().ChangeColor(Color.green);
            }
            else
            {
                placeableInstance.GetComponent <Selectable>().ChangeColor(Color.red);
            }

            var selectionManager = SelectionManager.GetInstance();
            if (Physics.Raycast(cameraRay, out hitInfo, float.MaxValue, LayerMask.GetMask("Ground")))
            {
                placeableInstance.transform.position = hitInfo.point;
                if (CanPlace() && Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject())
                {
                    if (ResourceManager.GetInstance().OffsetAll(-placeableInstance.GetWoodCost(), -placeableInstance.GetStoneCost(), -placeableInstance.GetMetalCost(), 0))
                    {
                        var buildSite = BuildSite.Instantiate(placeableInstance).GetComponent <Assignable>();
                        foreach (var worker in selectionManager.GetSelected().Where(s => s.GetComponent <Worker>() != null).Select(s => s.GetComponent <Worker>()).OrderBy(s => s.IsAssigned()))
                        {
                            if (!buildSite.AddWorker(worker))
                            {
                                break;
                            }
                        }
                        placeableInstance.GetComponent <Selectable>().ChangeColor(Color.clear);
                        placeableInstance = null;
                        selectionManager.DeselectAll();
                        selectionManager.Enable();
                    }
                }
                else if (Input.GetMouseButtonUp(1))
                {
                    ClearPlaceable();
                    return;
                }
            }
        }
    }