Пример #1
0
    public void BuildCurrent()
    {
        GameObject building = null;
        Buildable  temp     = CurrentBuildable;

        switch (CurrentBuildable)
        {
        case Buildable.Powerstation_down:
        case Buildable.Powerstation_left:
        case Buildable.Powerstation_right:
        case Buildable.Powerstation_up: {
            //Check Validator
            bool    valid        = current.GetComponent <RoadValidityChecker>().IsValid();
            World   w            = FindObjectOfType <World>();
            Vector2 newPlacement = Vector2.zero;

            if (valid || PossibleToHelp(CurrentBuildable, current, out newPlacement))
            {
                //build
                if (Game.Instance.SubtractResource(CurrentBuildable.GetCost()))
                {
                    Road r = w.roadmap.BuildPowerstation(CurrentBuildable, current.transform.position.ToVector2(), w);
                    building = r.gameObject;
                    ResetState();
                    ConnectConnectibles(LevelManager.properties.PoleRadius, r.GetComponentsInChildren <Connectible>());
                }
                else
                {
                    Game.Instance.Warn(LocaleManager.locale.InsufficientResources);
                    ResetState();
                }
            }
            else
            {
                //do nothing
                Game.Instance.Warn(LocaleManager.locale.InvalidPlacement);
            }
            break;
        }

        case Buildable.Pole: {
            //check validity
            if (Game.Instance.SubtractResource(CurrentBuildable.GetCost()))
            {
                Pole p = Pole.BuildPole(current.transform.position.ToVector2());
                //Destroy the dummy first, else it will try to connect it.
                building = p.gameObject;
                ResetState();
                ConnectConnectibles(LevelManager.properties.PoleRadius, p.GetComponent <Connectible>());
            }
            else
            {
                Game.Instance.Warn(LocaleManager.locale.InsufficientResources);
                ResetState();
            }
            break;
        }

        case Buildable.Plant: {
            bool valid = current.GetComponent <RoadValidityChecker>().IsValid();
            if (valid)
            {
                if (Game.Instance.SubtractResource(CurrentBuildable.GetCost()))
                {
                    Powerplant p = FindObjectOfType <World>().BuildPowerplant(current.transform.position.ToVector2());
                    //Destroy the dummy first, else it will try to connect it.
                    building = p.gameObject;
                    ResetState();
                    ConnectConnectibles(LevelManager.properties.PoleRadius, p.GetComponent <Connectible>());
                }
                else
                {
                    Game.Instance.Warn(LocaleManager.locale.InsufficientResources);
                    ResetState();
                }
            }
            else
            {
                Game.Instance.Warn(LocaleManager.locale.InvalidPlacement);
            }
            break;
        }
        }

        //Add to action stack if successful
        if (building != null)
        {
            Deconstructible d = building.AddComponent <Deconstructible>();
            d.building = temp;
            actionStack.Push(d);
        }
        UnfoldDecorations();
    }