예제 #1
0
        public void SpawnFactionEntities(GameManager gameMgr)
        {
            //if the defeat condition is set to destroy capital then all factions must have a building assigned as their capital building before the game starts.
            Assert.IsFalse(gameMgr.GetDefeatCondition() == DefeatConditions.destroyCapital && capitalBuilding == null
                           , $"[FactionSlot] Defeat condition is set to 'Destroy Capital' but faction ID {ID} does not have its 'Capital Building' field assigned!");

            CapitalPosition = Vector3.zero;

            //if the faction has a valid faction capital building assigned
            if (capitalBuilding != null)
            {
                CapitalPosition = capitalBuilding.transform.position; //assign its position

                //if there's a valid type and a valid capital building assigned to it
                if (typeInfo != null && typeInfo.GetCapitalBuilding() != null)
                {
                    Object.DestroyImmediate(this.capitalBuilding.gameObject); //destroy the default capital and spawn another one:

                    //create new faction center:
                    capitalBuilding = gameMgr.BuildingMgr.CreatePlacedInstanceLocal(
                        typeInfo.GetCapitalBuilding(),
                        CapitalPosition,
                        typeInfo.GetCapitalBuilding().transform.rotation.eulerAngles.y,
                        null, ID, true, true);
                }
                else //if not, just init the pre-placed capital building
                {
                    capitalBuilding.Init(gameMgr, ID, false, null, true);
                }
            }

            if (ID == GameManager.PlayerFactionID) //if this is the local player? (owner of this capital building)
            {
                //make sure either the capital building is assigned or the camera look at position is.
                Assert.IsFalse(capitalBuilding == null && camLookAtPos == null
                               , $"[FactionSlot] Neither the 'Capital Building' nor the 'Camera Look At Position' fields are assigned for the player faction (ID: {ID}, one must be assigned to set the initial position of the camera!");

                //Set the player's initial camera position (looking at the faction's look at position if it's assigned, else the capital building)
                Vector3 lookAtPos = camLookAtPos ? camLookAtPos.position : capitalBuilding.transform.position;
                gameMgr.CamMgr.LookAt(lookAtPos, false);
            }

            foreach (DefaultFactionEntity factionEntity in defaultFactionEntities) //go through the default faction entities
            {
                if (factionEntity.factionTypes.Length == 0 || //if not faction types have been assigned
                    factionEntity.factionTypes.Any(t => t == typeInfo))      //or the faction slot's type is specified in the array
                {
                    if (factionEntity.instance.Type == EntityTypes.building) //if we're spawning a pre placed building, more initializing settings are required
                    {
                        (factionEntity.instance as Building).Init(gameMgr, ID, false, capitalBuilding?.BorderComp, false);
                    }
                    else
                    {
                        (factionEntity.instance as Unit).Init(gameMgr, ID, false, null, factionEntity.instance.transform.position);
                    }

                    //if this is a unit, then update the population and update the faction limits:
                    FactionMgr.UpdateLimitsList(factionEntity.instance.GetCode(), factionEntity.instance.GetCategory(), true);
                    if (factionEntity.instance.Type == EntityTypes.unit)
                    {
                        Unit defaultUnit = (Unit)factionEntity.instance;
                        UpdateCurrentPopulation(defaultUnit.GetPopulationSlots());
                    }
                }
                else
                {
                    Object.DestroyImmediate(factionEntity.instance.gameObject); //destroy the faction entity instance if it's not part of this faction type
                }
            }

            CustomEvents.OnFactionDefaultEntitiesInit(this);
        }