Пример #1
0
        public Color AttackSelectionColor = Color.red; //The color of the selection texture that will be flashed around the target unit/building that was chosen by the player to attack.


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

            //Randomize player controlled faction:
            RandomizePlayerFaction();

            Time.timeScale = 1.0f;                                                      //In case it was not 1.0f in a previous scene.

            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;

            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 = FindObjectOfType(typeof(NetworkMapManager)) as NetworkMapManager;
            if (NetworkMgr != 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.

                //First we will search for all the lobby objects that held the info for the factions
                MFactionsInfo = FindObjectsOfType(typeof(MFactionLobbyInfo)) as MFactionLobbyInfo[];

                //This where we will set the settings for all the players:
                //First check if we have enough faction slots available:
                if (MFactionsInfo.Length <= Factions.Count)
                {
                    //Loop through all the current factions and set up each faction slot:
                    for (int i = 0; i < MFactionsInfo.Length; i++)
                    {
                        MFactionLobbyInfo ThisFaction = NetworkMgr.lobbySlots [i].GetComponent <MFactionLobbyInfo> ();                        //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.Maps[ThisFaction.MapID].InitialPopulation;
                        Factions [i].Lost          = false;

                        Factions [i].MFactionLobby = 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;
                        }
                    }

                    //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 (MFactionsInfo.Length < Factions.Count)
                    {
                        //remove the extra slots:
                        Factions.RemoveAt(Factions.Count - 1);
                    }
                }
                else
                {
                    Debug.LogError("Not enough slots available for all the factions!");
                }
            }

            MapMgr = FindObjectOfType(typeof(MapManager)) as MapManager;              //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 (MapMgr != 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 (MapMgr.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 < MapMgr.Factions.Count; i++)
                    {
                        //Set the info for the factions that we will use:
                        Factions [i].Name          = MapMgr.Factions[i].FactionName;                  //name
                        Factions [i].FactionColor  = MapMgr.Factions [i].FactionColor;                //color
                        Factions [i].PlayerControl = MapMgr.Factions [i].ControlledByPlayer;          //is this faction controlled by the player?
                        Factions [i].MaxPopulation = MapMgr.Factions [i].InitialPopulation;           //initial maximum population (which can be increased in the game).
                        Factions [i].Code          = MapMgr.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(MapMgr.DifficultyLevels[MapMgr.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 (MapMgr.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(MapMgr.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;


            GameEnded = false;             //false, of course, we just started.
        }
Пример #2
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);
            }

            Time.timeScale = 1.0f;    //unfreeze game if it was frozen from a previous game.

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

            //Randomize player controlled faction:
            RandomizePlayerFaction();

            //Get components:
            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;
            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 single player game.

            InitFactionMgrs();                  //create the faction managers components for the faction slots.

            InitMultiplayerGame();              //to initialize a multiplayer game.

            InitSinglePlayerGame();             //to initialize a single player game.

            SetPlayerFactionID();               //pick the player faction ID.

            InitFactionCapitals();              //init the faction capitals.

            ResourceMgr.InitFactionResources(); //init resources for factions.

            InitFactions();                     //init the faction types.

            //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"));

            //Set the amount of the active factions:
            activeFactionsAmount = 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;
        }