Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (_flyingBuilding != null)
        {
            var groundPlace = new Plane(Vector3.up, Vector3.zero);
            Ray ray         = _camera.ScreenPointToRay(Input.mousePosition);
            if (groundPlace.Raycast(ray, out float position))
            {
                Vector3 worldPosition = ray.GetPoint(position);
                int     x             = Mathf.RoundToInt(worldPosition.x);
                int     y             = Mathf.RoundToInt(worldPosition.z);
                if (x != _oldXPosition || y != _oldYPosition)
                {
                    _oldXPosition   = x;
                    _oldYPosition   = y;
                    _canUpdateCheck = true;
                    Debug.Log(_canUpdateCheck);
                }

                if (_canUpdateCheck)
                {
                    _flyingBuilding.transform.position = new Vector3(_oldXPosition, 0, _oldYPosition);
                    _availableForConstruction          = IsPlaceFreeForConstruction();
                    Building building   = null;
                    var      isBuilding = _flyingBuilding.gameObject.TryGetComponent <Building>(out building);
                    if (isBuilding && _availableForConstruction && !CheckObjectUnderFlyingBuilding(building.checkRoad.position, "Road"))
                    {
                        _availableForConstruction = false;                                                                                                                  // можно добавить надпись для понятности
                    }
                    _flyingBuilding.SetTransparent(_availableForConstruction);
                    _canUpdateCheck = false;
                }

                if (_availableForConstruction && Input.GetMouseButtonDown(0))
                {
                    if (EventSystem.current.IsPointerOverGameObject())
                    {
                        return;                                                // чтобы не кликать сквозь кнопку
                    }
                    else
                    {
                        Road     road       = null;
                        var      isRoad     = _flyingBuilding.gameObject.TryGetComponent <Road>(out road);
                        Building building   = null;
                        var      isBuilding = _flyingBuilding.gameObject.TryGetComponent <Building>(out building);
                        if (isBuilding)
                        {
                            if (_construction.BuyBuilding(building))
                            {
                                PlaceFlyingBuilding(x, y);
                            }
                        }
                        else if (isRoad)
                        {
                            if (_construction.BuyRoad(road))
                            {
                                PlaceFlyingBuilding(x, y);
                            }
                        }
                    }
                }
            }
        }
    }