Example #1
0
    /// <summary>
    /// Try to place a building.
    /// </summary>
    /// <param name="prefab">The GameObject to place</param>
    public void Build(GameObject prefab)
    {
        prefab.layer = 0;                                                                       //Set to default Layer
        InHand inHand = prefab.GetComponent <InHand>();                                         //Get a reference to the in-hand component

        if (inHand == null || !inHand.validPlacement || inHand.CheckCollision())                //Check if everything is clear
        {
            if (buildKeyDownTime > InvalidBuildTimeThreshold)                                   //Have we been trying to build here long?
            {
                ShowMessage("Can't build here");                                                //Give player more feedback
            }
            return;                                                                             //Don't do anything else
        }
        string Pay = CanWePayFor(prefab);                                                       //Create a new string, will return what we are missing if we can't build

        if (Pay == "Done")                                                                      //If we do have enough to build this building
        {
            GameObject building = Instantiate(prefab);                                          //Clone the prefab as a new building
            building.layer = LayerMask.NameToLayer("Building");                                 //Set this to be in the building layer (so we can't build on this anymore)
            building.transform.SetParent(FolderBuildings);                                      //Sort the building in the right folder
            building.GetComponent <BuildingOption>().StartTimer();                              //Start the 'Used' after timer if this object
            building.GetComponent <BuildingOption>().OwnerID = ThisPlayerID;                    //Set the current player to be the owner of this building
            Destroy(building.GetComponent <InHand>());                                          //Remove the in-hand component
        }
        else
        {
            ShowMessage("Not enough " + Pay + " to build that");                                //Give the user the warning message
        }
    }
Example #2
0
    /// <summary>
    /// Try to place a building.
    /// </summary>
    /// <param name="prefab">The GameObject to place</param>
    public void Build(GameObject prefab)
    {
        prefab.layer = 0;                                                                       //Set to default Layer
        InHand inHand = prefab.GetComponent <InHand>();                                         //Get a reference to the in-hand component

        if (inHand == null || !inHand.validPlacement || inHand.CheckCollision())                //Check if everything is clear
        {
            if (BuildFirstTry)                                                                  //If the user is starting to trying to build
            {
                ShowMessage("Can't build here ");                                               //Give player more feedback
                BuildFirstTry = false;                                                          //Flag that we have processed the start of this button press
            }
            if (buildKeyDownTime > InvalidBuildTimeThreshold)                                   //Have we been trying to build here long?
            {
                ShowMessage("Can't build here ");                                               //Give player more feedback
            }
            return;                                                                             //Don't do anything else
        }
        string Pay = CanWePayFor(prefab);                                                       //Create a new string, will return what we are missing if we can't build

        if (Pay == "Done")                                                                      //If we do have enough to build this building
        {
            BuildFirstTry    = false;                                                           //Flag that we have processed the start of this button press
            buildKeyDownTime = 0;                                                               //Reset the message timer (this if for keep dragging support)
            if (inHand.RemoveObjectsHit.Count > 0)                                              //If there are objects we need to remove
            {
                for (int i = 0; i < inHand.RemoveObjectsHit.Count; i++)                         //Do for each object
                {
                    DeconstructBuilding(inHand.RemoveObjectsHit[i]);                            //Deconstruct the building
                }
            }
            GameObject building = Instantiate(prefab);                                          //Clone the prefab as a new building
            building.layer = LayerMask.NameToLayer("Building");                                 //Set this to be in the building layer (so we can't build on this anymore)
            building.transform.SetParent(FolderBuildings);                                      //Sort the building in the right folder
            building.GetComponent <BuildingOption>().StartTimer();                              //Start the 'Used' after timer if this object
            building.GetComponent <BuildingOption>().OwnerID = ThisPlayerID;                    //Set the current player to be the owner of this building
            Destroy(building.GetComponent <InHand>());                                          //Remove the in-hand component
        }
        else
        {
            ShowMessage("Not enough " + Pay + " to build that");                                //Give the user the warning message
        }
    }