public void CmdAddPlayerToList(int NewFactionID)
 {
     //the server will share all the lobby settings for the new player
     foreach (NetworkLobbyPlayer NetworkPlayer in NetworkMgr.lobbySlots) //go through all the lobby slots
     {
         if (NetworkPlayer != null)
         {
             MFactionLobby_UNET LobbyPlayer = NetworkPlayer.gameObject.GetComponent <MFactionLobby_UNET>();
             //remove any slots for players who disconnected and are not anymore present in the lobby
             if (LobbyPlayer != null)
             {
                 if (LobbyPlayer.FactionID != NewFactionID) //making sure that this is not the new player that just joined
                 {
                     LobbyPlayer.CmdServerUpdateMap();
                     LobbyPlayer.CmdUpdateFactionColor(LobbyPlayer.FactionColorID, NetworkMgr.AllowedColors[LobbyPlayer.FactionColorID]);
                     LobbyPlayer.CmdUpdateFactionName(LobbyPlayer.FactionNameInput.text);
                     LobbyPlayer.CmdUpdateFactionType(LobbyPlayer.FactionTypeMenu.value);
                     LobbyPlayer.CmdUpdateReadyStatus();
                     LobbyPlayer.CmdUpdateFactionTeam(LobbyPlayer.TeamDropDown.value);
                     LobbyPlayer.CmdUpdatePlayerType(LobbyPlayer.PlayerType.value);
                 }
             }
         }
     }
 }
        //Start the game:
        public void StartGame()
        {
            bool rpgFirst       = false;
            bool rpgSecond      = false;
            bool strategyFirst  = false;
            bool strategySecond = false;

            if (numPlayers > 0)   //make sure there's more than one player:
            {
                int ReadyCount = 0;
                for (int i = 0; i < numPlayers; i++)                   //check if all the players are ready but the host:
                {
                    MFactionLobby_UNET ThisFaction = lobbySlots[i].GetComponent <MFactionLobby_UNET>();

                    if (lobbySlots [i].readyToBegin == true)
                    {
                        ReadyCount++;
                    }
                    if (ThisFaction.TeamDropDown.value == 0 && ThisFaction.PlayerType.value == 0)
                    {
                        strategyFirst = true;
                    }
                    else if (ThisFaction.TeamDropDown.value == 1 && ThisFaction.PlayerType.value == 0)
                    {
                        strategySecond = true;
                    }
                    else if (ThisFaction.TeamDropDown.value == 0 && ThisFaction.PlayerType.value == 1)
                    {
                        rpgFirst = true;
                    }
                    else
                    {
                        rpgSecond = true;
                    }
                }

                if (ReadyCount == numPlayers - 1)
                {
                    if (1 == 1)
                    {
                        lobbySlots[0].SendReadyToBeginMessage(); //send this ready message to start the game.
                    }
                    else
                    {
                        ShowInfoMsg("There has to be rpg and strategy in each team", 1.0f);
                    }
                }
                else
                {
                    ShowInfoMsg("Not all players are ready!", 1.0f);
                }
            }
            else
            {
                ShowInfoMsg("Can't start the game with one player!", 1.0f);
            }
        }
Exemple #3
0
        public void RpcUpdateMapInfo(int ID)
        {
            NetworkMgr.MapDropDownMenu.value = ID; //set the new map ID

            NetworkMgr.UpdateMapUIInfo();          //update the map info from lobby manager

            //the server will share all the lobby settings for the new player
            foreach (NetworkLobbyPlayer NetworkPlayer in NetworkMgr.lobbySlots) //go through all the lobby slots
            {
                if (NetworkPlayer != null)
                {
                    MFactionLobby_UNET LobbyPlayer = NetworkPlayer.gameObject.GetComponent <MFactionLobby_UNET>();
                    //do not consider any slots for players who disconnected and are not anymore present in the lobby
                    if (LobbyPlayer != null)
                    {
                        LobbyPlayer.ResetFactionTypes(); //reset factions
                    }
                }
            }
        }
Exemple #4
0
 public void CmdAddPlayerToList(int NewFactionID)
 {
     //the server will share all the lobby settings for the new player
     foreach (NetworkLobbyPlayer NetworkPlayer in NetworkMgr.lobbySlots) //go through all the lobby slots
     {
         if (NetworkPlayer != null)
         {
             MFactionLobby_UNET LobbyPlayer = NetworkPlayer.gameObject.GetComponent <MFactionLobby_UNET>();
             //remove any slots for players who disconnected and are not anymore present in the lobby
             if (LobbyPlayer != null)
             {
                 if (LobbyPlayer.FactionID != NewFactionID) //making sure that this is not the new player that just joined
                 {
                     LobbyPlayer.CmdServerUpdateMap();      //ask server to share the current map ID
                     LobbyPlayer.CmdUpdateFactionColor(LobbyPlayer.FactionColorID);
                     LobbyPlayer.CmdUpdateFactionName(LobbyPlayer.FactionName);
                     LobbyPlayer.CmdUpdateFactionType(LobbyPlayer.FactionTypeID);
                     LobbyPlayer.CmdUpdateReadyStatus();
                 }
             }
         }
     }
 }
Exemple #5
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;
        }
Exemple #6
0
        //a method that initializes a multiplayer game
        private bool InitMultiplayerGame()
        {
            //if there's network manager in the scene
            if (NetworkManager_UNET.instance == null)
            {
                return(false); //do not proceed.
            }
            //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.

            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 NetworkManager_UNET.instance.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].UpdateMaxPopulation(NetworkManager_UNET.instance.Maps[NetworkManager_UNET.instance.CurrentMapID].InitialPopulation, false);
                    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].TypeInfo = NetworkManager_UNET.instance.Maps[NetworkManager_UNET.instance.CurrentMapID].FactionTypes[ThisFaction.FactionTypeID];

                    Factions[i].npcMgr = null; //mark as non NPC faction by setting the npc manager prefab to null

                    //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.
                        Factions[i].playerControlled = true;
                    }
                    else
                    {
                        //all other factions will be defined as NPC but in the reality, they are controlled by other players through the network.
                        Factions[i].playerControlled = 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)
                {
                    Destroy(Factions[Factions.Count - 1].FactionMgr); //destroy the faction manager component
                    //remove the extra slots:
                    Factions.RemoveAt(Factions.Count - 1);
                }

                return(true);
            }
            else
            {
                Debug.LogError("[Game Manager]: Not enough slots available for all the factions coming from the multiplayer menu.");
                return(false);
            }
        }