Exemple #1
0
        //a method called when the player places the building
        public void PlaceBuilding()
        {
            if (!IsActive) //if this component is not active, do not place building
            {
                return;
            }

            //Building is now placed:
            building.Placed = true;

            if (building.NavObstacle) //enable the nav mesh obstacle comp, if it exists
            {
                building.NavObstacle.enabled = true;
            }

            building.BoundaryCollider.isTrigger = true;                                   //allow the boundaries collider to be trigger now

            building.GetSelection().gameObject.GetComponent <Collider>().enabled = false; //Disable the selection collider so that it won't get auto selected as soon as it's spawned
            building.GetSelection().gameObject.SetActive(true);                           //and activate the object.

            gameMgr.MinimapIconMgr?.Assign(building.GetSelection());                      //ask the minimap icon manager to create the a minimap icon for this building

            building.DisableSelectionPlane();                                             //hide the building's plane

            if (building.BorderComp)                                                      //if the building includes a border comp, then enable it as well
            {
                building.BorderComp.enabled = true;
            }

            building.HealthComp.CurrHealth = 0;                                                                                     //Set the building's health to 0 so that builders can start adding health to it

            building.GetSelection().gameObject.GetComponent <Collider>().enabled = true;                                            //enable the selection's collider so that it becomes selectable

            CustomEvents.OnBuildingPlaced(building);                                                                                //trigger custom event

            if (building.IsFree() == true)                                                                                          //if this is a free building
            {
                return;                                                                                                             //do not proceed
            }
            building.ToggleModel(false);                                                                                            //hide the building's model initially to show the construction objects:

            if (placeOutsideBorder == false && building.BorderComp == null)                                                         //if the building is to be placed inside the faction's border and this is not a center building
            {
                building.CurrentCenter.RegisterBuilding(building);                                                                  //register building in the territory that it belongs to.
            }
            if (building.FactionID == GameManager.PlayerFactionID && GodMode.Enabled == false && building.PlacedByDefault == false) //if the player owns this building and this is a single player game
            {
                List <Unit> selectedUnits = gameMgr.SelectionMgr.Selected.GetEntitiesList(EntityTypes.unit, true, true).Cast <Unit>().ToList();

                if (selectedUnits.Count > 0)
                {
                    //If God Mode is not enabled, make builders move to the building to construct it if building isn't supposed to be placed by default
                    foreach (Unit u in selectedUnits)                                                    //go through the selected units and look for builders
                    {
                        if (u.BuilderComp)                                                               //check if this unit has a builder comp (can actually build).
                        {
                            if (building.WorkerMgr.currWorkers < building.WorkerMgr.GetAvailableSlots()) //make sure that the maximum amount of builders has not been reached
                            {
                                u.BuilderComp.SetTarget(building);                                       //Make the units construct the building:
                            }
                            else //max amount is reached
                            {
                                break; //leave loop
                            }
                        }
                    }
                }
            }

            building.HealthComp.CheckState(true); //Check the building's construction state

            enabled = false;                      //disable this component when the building is placed.
        }