示例#1
0
        //a method that initializes a single player game:
        private bool InitSinglePlayerGame()
        {
            //if there's no single player manager then
            if (SinglePlayerManager.instance == null)
            {
                return(false); //do not proceed.
            }
            //If there's a map manager script in the scene, it means that we just came from the single player menu, so we need to set the NPC players settings!
            SinglePlayerManager singlePlayerMgr = SinglePlayerManager.instance;

            //This where we will set the NPC settings using the info from the single player manager:
            //First check if we have enough faction slots available:
            if (singlePlayerMgr.Factions.Count <= Factions.Count)
            {
                //loop through the factions slots of this map:
                for (int i = 0; i < singlePlayerMgr.Factions.Count; i++)
                {
                    //Set the info for the factions that we will use:
                    Factions[i].Name             = singlePlayerMgr.Factions[i].FactionName;                                  //name
                    Factions[i].FactionColor     = singlePlayerMgr.Factions[i].FactionColor;                                 //color
                    Factions[i].playerControlled = singlePlayerMgr.Factions[i].playerControlled;                             //is this faction controlled by the player?
                    Factions[i].UpdateMaxPopulation(singlePlayerMgr.Factions[i].InitialPopulation, false);                   //initial maximum population (which can be increased in the game).
                    Factions[i].TypeInfo   = singlePlayerMgr.Factions[i].TypeInfo;                                           //the faction's code.
                    Factions[i].CapitalPos = Factions[i].CapitalBuilding.transform.position;                                 //setting the capital pos to spawn the capital building object at later.

                    Factions[i].Lost = false;                                                                                //the game just started.

                    Factions[i].npcMgr = (Factions[i].playerControlled == true) ? null : singlePlayerMgr.Factions[i].npcMgr; //set the npc mgr for this faction.
                }

                //if there are more slots than required.
                while (singlePlayerMgr.Factions.Count < Factions.Count)
                {
                    //remove the extra slots:
                    Destroy(Factions[Factions.Count - 1].FactionMgr); //destroy the faction manager component
                    DestroyImmediate(Factions[Factions.Count - 1].CapitalBuilding.gameObject);
                    Factions.RemoveAt(Factions.Count - 1);
                }

                //Destroy the map manager script because we don't really need it anymore:
                DestroyImmediate(singlePlayerMgr.gameObject);

                return(true);
            }
            else
            {
                Debug.LogError("[Game Manager]: Not enough slots available for all the factions coming from the single player menu.");
                return(false);
            }
        }
示例#2
0
        public FactionUIInfo FactionUISample; //A child object of the "FactionUIParent", that includs the "FactionUIInfo.cs" component. This represents a faction slot and holds all the information of the faction (color, name, population, etc).
        //This object will be used a sample as it will be duplicated to the number of the faction that the player chooses.

        void Awake()
        {
            //singleton:
            if (instance == null)
            {
                instance = this;
            }
            else if (instance != null)
            {
                Destroy(instance);
            }

            DontDestroyOnLoad(this.gameObject);  //we want this object to be passed to the map's scene.

            int i = 0;

            //go through all the maps:
            if (Maps.Length > 0)
            {
                CurrentMapID = 0;
                List <string> MapNames = new List <string>();

                for (i = 0; i < Maps.Length; i++)
                {
                    //If we forgot to put the map's scene or we have less than 2 as max players, then show an error:
                    if (Maps [i].MapScene == null)
                    {
                        Debug.LogError("[Single Player Manager]: Map ID " + i.ToString() + " is invalid.");
                    }
                    if (Maps [i].MaxFactions < 2)
                    {
                        Debug.LogError("[Single Player Manager]: Map ID " + i.ToString() + " max factions (" + Maps [i].MaxFactions.ToString() + ") is lower than 2.");
                    }
                    MapNames.Add(Maps [i].MapName);
                }

                //set the map drop down menu options to the names of the maps.
                if (MapDropDownMenu != null)
                {
                    MapDropDownMenu.ClearOptions();
                    MapDropDownMenu.AddOptions(MapNames);
                }
                else
                {
                    Debug.LogError("[Single Player Manager]: You must add a drop down menu to pick the maps.");
                }
            }
            else
            {
                //At least one map must be included:
                Debug.LogError("[Single Player Manager]: You must include at least one map.");
            }

            //go through all the npc managers:
            if (npcManagers.Length > 0)
            {
                for (i = 0; i < npcManagers.Length; i++)
                {
                    //Show an error if one of the npc manager components has not been assigned:
                    if (npcManagers[i] == null)
                    {
                        Debug.LogError("[Single Player Manager]: NPC Manager ID " + i.ToString() + " has not been defined.");
                    }
                }
            }
            else
            {
                //We need at least one npc manager:
                Debug.LogError("[Single Player Manager]: You must include at least one NPC Manager.");
            }


            Factions = new List <FactionVars>();            //Initialize the factions list.
            //add the default two factions:
            for (i = 0; i < 2; i++)
            {
                AddFaction();
            }

            UpdateMap();              //update the map's settings
        }
示例#3
0
        public AudioSource GeneralAudioSource; //The audio source where audio will be played generally unless the audio is local. In that case, it will be played

        void Awake()
        {
            //set the instance:
            if (Instance == null)
            {
                Instance = this;
            }
            else if (Instance != this)
            {
                Destroy(gameObject);
            }

            AllFactionsReady = false; //faction stats are not ready, yet

            //Randomize player controlled faction:
            RandomizePlayerFaction();

            CamMov      = FindObjectOfType(typeof(CameraMovement)) as CameraMovement;   //Find the camera movement script.
            ResourceMgr = FindObjectOfType(typeof(ResourceManager)) as ResourceManager; //Find the resource manager script.
            if (ResourceMgr != null)
            {
                ResourceMgr.GameMgr = this;
            }
            UIMgr       = FindObjectOfType(typeof(UIManager)) as UIManager; //Find the UI manager script.
            BuildingMgr = FindObjectOfType(typeof(BuildingPlacement)) as BuildingPlacement;
            Events      = FindObjectOfType(typeof(CustomEvents)) as CustomEvents;
            TaskMgr     = FindObjectOfType(typeof(TaskManager)) as TaskManager;
            if (TaskMgr != null)
            {
                TaskMgr.GameMgr = this;
            }
            UnitMgr      = FindObjectOfType(typeof(UnitManager)) as UnitManager;
            SelectionMgr = FindObjectOfType(typeof(SelectionManager)) as SelectionManager;
            PlacementMgr = FindObjectOfType(typeof(BuildingPlacement)) as BuildingPlacement;
            TerrainMgr   = FindObjectOfType(typeof(TerrainManager)) as TerrainManager;
            MvtMgr       = FindObjectOfType(typeof(MovementManager)) as MovementManager;

            MultiplayerGame = false; //We start by assuming it's a simple single player game.

            //First check if there's a network manager component in the scene:
            NetworkMgr_UNET = NetworkManager_UNET.NetworkMgr;
            if (NetworkMgr_UNET != null)
            {                           //If there's actually a network map manager, it means that the map was loaded from the multiplayer menu, meaning that this is a MP game.
                ClearNPCManagers();     //clearing all the npc components in the map since it's a MP game.
                MultiplayerGame = true; //we now recongize that this a multiplayer game.

                //set the network type in the input manager
                InputManager.NetworkType = InputManager.NetworkTypes.UNET;

                List <MFactionLobby_UNET> FactionLobbyInfos = new List <MFactionLobby_UNET>();
                FactionLobbyInfos.Clear();                                               //clear this list
                //we'll add the faction lobby components to it using the lobby slots array from the network manager
                foreach (NetworkLobbyPlayer NetworkPlayer in NetworkMgr_UNET.lobbySlots) //go through all the lobby slots
                {
                    //if the slot is occupied:
                    if (NetworkPlayer != null)
                    {
                        //add the faction lobby component attached to it to the list:
                        FactionLobbyInfos.Add(NetworkPlayer.gameObject.GetComponent <MFactionLobby_UNET>());
                    }
                }
                //This where we will set the settings for all the players:
                //First check if we have enough faction slots available:
                if (FactionLobbyInfos.Count <= Factions.Count)
                {
                    //Loop through all the current factions and set up each faction slot:
                    for (int i = 0; i < FactionLobbyInfos.Count; i++)
                    {
                        MFactionLobby_UNET ThisFaction = FactionLobbyInfos[i]; //this is the faction info that we will get from the faction lobby info.

                        //Set the info for the factions that we will use:
                        Factions[i].Name         = ThisFaction.FactionName;  //get the faction name
                        Factions[i].FactionColor = ThisFaction.FactionColor; //the faction color
                        //get the initial max population from the network manager (making it the same for all the players).
                        Factions[i].MaxPopulation = NetworkMgr_UNET.Maps[ThisFaction.MapID].InitialPopulation;
                        Factions[i].Lost          = false;

                        Factions[i].MFactionLobby_UNET = ThisFaction;                                    //linking the faction with its lobby info script.
                        Factions[i].CapitalPos         = Factions[i].CapitalBuilding.transform.position; //setting the capital pos to spawn the capital building object at later.

                        Factions[i].FactionMgr = Factions[i].FactionMgr;                                 //linking the faction with its faction manager.

                        Factions[i].Code = ThisFaction.FactionCode;

                        //Setting the local player faction ID:
                        if (ThisFaction.isLocalPlayer)
                        { //isLoclPlayer determines which lobby faction info script is owned by the player..
                          //therefore the faction linked to that script is the player controlled one.
                            PlayerFactionID           = i;
                            Factions[i].PlayerControl = true;
                            PlayerFactionMgr          = Factions[i].FactionMgr;
                        }
                        else
                        {
                            //all other factions will be defined as NPC but in the reality, they are controlled by other players through the network.
                            Factions[i].PlayerControl = false;
                        }

                        //Set the master faction ID:
                        if (ThisFaction.IsServer)
                        {
                            MasterFactionID = i;
                        }
                    }

                    //loop through all the factions and destroy the default capital buildings because the server will spawn new ones for each faction.
                    for (int i = 0; i < Factions.Count; i++)
                    {
                        DestroyImmediate(Factions[i].CapitalBuilding.gameObject);
                    }

                    //if there are more slots than required.
                    while (FactionLobbyInfos.Count < Factions.Count)
                    {
                        //remove the extra slots:
                        Factions.RemoveAt(Factions.Count - 1);
                    }
                }
                else
                {
                    Debug.LogError("Not enough slots available for all the factions!");
                }
            }

            SinglePlayerMgr = FindObjectOfType(typeof(SinglePlayerManager)) as SinglePlayerManager; //search for the map manager script.
            //If there's a map manager script in the scene, it means that we just came from the single player menu, so we need to set the NPC players settings!
            if (SinglePlayerMgr != null)
            {
                //This where we will set the NPC settings using the info from the map manager:
                //First check if we have enough faction slots available:
                if (SinglePlayerMgr.Factions.Count <= Factions.Count)
                {
                    ClearNPCManagers(); //remove the current npc managers as they will be replaced by other ones.

                    //loop through the factions slots of this map:
                    for (int i = 0; i < SinglePlayerMgr.Factions.Count; i++)
                    {
                        //Set the info for the factions that we will use:
                        Factions[i].Name          = SinglePlayerMgr.Factions[i].FactionName;        //name
                        Factions[i].FactionColor  = SinglePlayerMgr.Factions[i].FactionColor;       //color
                        Factions[i].PlayerControl = SinglePlayerMgr.Factions[i].ControlledByPlayer; //is this faction controlled by the player?
                        Factions[i].MaxPopulation = SinglePlayerMgr.Factions[i].InitialPopulation;  //initial maximum population (which can be increased in the game).
                        Factions[i].Code          = SinglePlayerMgr.Factions[i].FactionCode;        //the faction's code.
                        Factions[i].CapitalPos    = Factions[i].CapitalBuilding.transform.position; //setting the capital pos to spawn the capital building object at later.

                        Factions[i].Lost = false;

                        int FactionTypeID = GetFactionTypeID(Factions[i].Code);
                        if (FactionTypeID >= 0 && FactionDef[FactionTypeID].CapitalBuilding != null)
                        {                                                             //if the faction to a certain type
                            DestroyImmediate(Factions[i].CapitalBuilding.gameObject); //destroy the default capital and spawn another one:

                            //we will spawn the capital building and remove the one that already came in the scene:
                            GameObject Capital = Instantiate(FactionDef[FactionTypeID].CapitalBuilding.gameObject);

                            //set the capital's settings:
                            Capital.GetComponent <Building>().FactionID       = i;
                            Capital.GetComponent <Building>().FactionCapital  = true;
                            Capital.GetComponent <Building>().PlacedByDefault = true;

                            Capital.transform.position  = Factions[i].CapitalPos; //set the capital's position on the map.
                            Factions[i].CapitalBuilding = Capital.GetComponent <Building>();
                        }

                        //if this faction not controlled by the player
                        if (Factions[i].PlayerControl == false)
                        {
                            //Spawn the NPC managers setinngs for this faction:
                            GameObject NPCMgrObj = (GameObject)Instantiate(SinglePlayerMgr.DifficultyLevels[SinglePlayerMgr.Factions[i].NPCDifficulty], Vector3.zero, Quaternion.identity);

                            //NPC Army manager:
                            NPCMgrObj.GetComponent <NPCArmy>().FactionID  = i;
                            NPCMgrObj.GetComponent <NPCArmy>().FactionMgr = Factions[i].FactionMgr;
                            Factions[i].FactionMgr.ArmyMgr = NPCMgrObj.GetComponent <NPCArmy>();

                            //NPC Building placement manager:
                            NPCMgrObj.GetComponent <NPCBuildingPlacement>().FactionID  = i;
                            NPCMgrObj.GetComponent <NPCBuildingPlacement>().FactionMgr = Factions[i].FactionMgr;
                            Factions[i].FactionMgr.BuildingMgr = NPCMgrObj.GetComponent <NPCBuildingPlacement>();

                            //NPC Resource manager:
                            NPCMgrObj.GetComponent <NPCResource>().FactionID  = i;
                            NPCMgrObj.GetComponent <NPCResource>().FactionMgr = Factions[i].FactionMgr;
                            Factions[i].FactionMgr.ResourceMgr = NPCMgrObj.GetComponent <NPCResource>();

                            //NPC Unit spawner: (optional)
                            if (NPCMgrObj.GetComponent <NPCUnitSpawner>())
                            {
                                NPCMgrObj.GetComponent <NPCUnitSpawner>().FactionID  = i;
                                NPCMgrObj.GetComponent <NPCUnitSpawner>().FactionMgr = Factions[i].FactionMgr;
                                Factions[i].FactionMgr.UnitSpawner = NPCMgrObj.GetComponent <NPCUnitSpawner>();
                            }
                        }
                    }

                    //if there are more slots than required.
                    while (SinglePlayerMgr.Factions.Count < Factions.Count)
                    {
                        //remove the extra slots:
                        DestroyImmediate(Factions[Factions.Count - 1].CapitalBuilding.gameObject);
                        Factions.RemoveAt(Factions.Count - 1);
                    }
                }
                else
                {
                    Debug.LogError("Not enough slots available for all the factions!");
                }

                //Destroy the map manager script because we don't really need it anymore:
                DestroyImmediate(SinglePlayerMgr.gameObject);
            }

            //If it's a multiplayer game, we still need to figure what's the local player faction ID.
            if (MultiplayerGame == false)
            {
                PlayerFactionID = -1;
            }

            if (Factions.Count > 0)
            {
                //Create as many resource info slots as the amount of the spawned factions.
                ResourceMgr.FactionResourcesInfo = new ResourceManager.FactionResourcesVars[Factions.Count];

                //Loop through all the factions:
                for (int i = 0; i < Factions.Count; i++)
                {
                    //if it's not a multiplayer game:
                    if (MultiplayerGame == false)
                    {
                        //and this faction is controlled by the player:
                        if (Factions[i].PlayerControl == true)
                        {
                            //then define this as the player faction:
                            PlayerFactionID  = i;
                            PlayerFactionMgr = Factions[i].FactionMgr;
                        }
                    }

                    //Only if it's a single player game we will be checking if the capital buildings have spawned, because in multiplayer, another script handles spawning these
                    if (MultiplayerGame == false)
                    {
                        if (Factions[i].CapitalBuilding == null)
                        {
                            Debug.LogError("Faction ID: " + i + " is missing the 'Capital Building'");
                        }
                        else
                        {
                            Factions[i].CapitalBuilding.FactionID      = i;
                            Factions[i].CapitalBuilding.FactionCapital = true;
                        }
                    }


                    ResourceMgr.FactionResourcesInfo[i] = new ResourceManager.FactionResourcesVars();
                    //Associate each team with all available resources:
                    ResourceMgr.FactionResourcesInfo[i].ResourcesTypes = new ResourceManager.ResourcesVars[ResourceMgr.ResourcesInfo.Length];

                    //Loop through all the available resources and define them for each team.
                    for (int j = 0; j < ResourceMgr.FactionResourcesInfo[i].ResourcesTypes.Length; j++)
                    {
                        ResourceMgr.FactionResourcesInfo[i].ResourcesTypes[j] = new ResourceManager.ResourcesVars();

                        ResourceMgr.FactionResourcesInfo[i].ResourcesTypes[j].Name   = ResourceMgr.ResourcesInfo[j].Name;           //Name of the resource
                        ResourceMgr.FactionResourcesInfo[i].ResourcesTypes[j].Amount = ResourceMgr.ResourcesInfo[j].StartingAmount; //Starting amount of the resource for each team.
                    }
                }
            }

            ResourceMgr.UpdateResourcesUI(); //right after setting up the resource settings above, refresh the resource UI.

            //In order to avoid having buildings that are being placed by AI players and units collide, we will ignore physics between their two layers:
            Physics.IgnoreLayerCollision(LayerMask.NameToLayer("Hidden"), LayerMask.NameToLayer("Unit"));
            //Physics.IgnoreLayerCollision(LayerMask.NameToLayer("Unit"), LayerMask.NameToLayer("Unit"));

            if (PeaceTime <= 0.0f)
            {
                //If there's no peace make factions pick their targets early:
                SetFactionTargets();
            }

            //Set the amount of the active factions:
            ActiveFactions = Factions.Count;

            GameState = GameStates.Running; //the game state is now set to running

            //reaching this point means that all faction info/stats in the game manager are ready:
            AllFactionsReady = true;
        }