//method that places a building. void PlaceBuilding() { //place the first building in the pending buildings list: //destroy the building instance that was supposed to be placed: Destroy(pendingBuildings[0].buildingInstance.gameObject); //ask the building manager to create a new placed building: BuildingManager.CreatePlacedInstance(pendingBuildings[0].buildingPrefab, pendingBuildings[0].buildingInstance.transform.position, pendingBuildings[0].buildingCenter.BorderMgr, factionMgr.FactionID); //remove the first item in pending buildings list: pendingBuildings.RemoveAt(0); StartPlacingNextBuilding(); //start placing next building }
//the method that allows us to place the building void PlaceBuilding() { TakeBuildingResources(CurrentBuilding); //Remove the resources needed to create the building. if (GameManager.MultiplayerGame == false) { //if it's a single player game. BuildingManager.CreatePlacedInstance(AllBuildings[LastBuildingID], CurrentBuilding.transform.position, CurrentBuilding.CurrentCenter, GameManager.PlayerFactionID); //place the building } else { //in case it's a multiplayer game: //ask the server to spawn the building for all clients: //send input action to the input manager InputVars NewInputAction = new InputVars(); //mode: NewInputAction.SourceMode = (byte)InputSourceMode.Create; NewInputAction.Source = AllBuildings[LastBuildingID].gameObject; NewInputAction.InitialPos = CurrentBuilding.transform.position; NewInputAction.Target = CurrentBuilding.CurrentCenter.gameObject; NewInputAction.Value = 0; InputManager.SendInput(NewInputAction); } //remove instance that was being used to place building Destroy(CurrentBuilding.gameObject); CurrentBuilding = null; //Play building placement audio AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, PlaceBuildingAudio, false); //if holding and spawning is enabled and the player is holding the right key to do that: if (HoldAndSpawn == true && Input.GetKey(HoldAndSpawnKey)) { //start placing the same building again StartPlacingBuilding(LastBuildingID); } else { //Show the tasks again for the builders again: if (SelectionMgr.SelectedUnits.Count > 0) { SelectionMgr.UIMgr.UpdateTaskPanel(); } } }
//initialize the faction capitals. private void InitFactionCapitals() { //only in single player: if (MultiplayerGame == true) { return; } for (int i = 0; i < Factions.Count; i++) //go through the factions list { //if the faction has a valid faction type: if (Factions[i].TypeInfo != null) { if (Factions[i].TypeInfo.capitalBuilding != null) { //if the faction to a certain type DestroyImmediate(Factions[i].CapitalBuilding.gameObject); //destroy the default capital and spawn another one: //create new faction center: Factions[i].CapitalBuilding = BuildingManager.CreatePlacedInstance(Factions[i].TypeInfo.capitalBuilding, Factions[i].CapitalPos, null, i, true); } } //mark as faction capital: Factions[i].CapitalBuilding.FactionCapital = true; } }
public void LaunchCommand(InputVars Command) { float Value = (float)Command.Value; if (Command.SourceMode == (byte)InputSourceMode.FactionSpawn) { //spawning the faction capital building Building capitalBuilding = null; //search for the capital building to spawn for this faction if (GameMgr.Factions[Command.FactionID].TypeInfo != null) //valid faction type info is required { if (GameMgr.Factions[Command.FactionID].TypeInfo.capitalBuilding != null) { //if the faction belongs to a certain type capitalBuilding = BuildingManager.CreatePlacedInstance(GameMgr.Factions[Command.FactionID].TypeInfo.capitalBuilding, Command.InitialPos, null, Command.FactionID, true); //assign as a capital faction capitalBuilding.FactionCapital = true; //if this is the local player? if (Command.FactionID == GameManager.PlayerFactionID) { //Set the player's initial cam position (looking at the faction's capital building): GameMgr.CamMov.LookAt(capitalBuilding.transform.position); GameMgr.CamMov.SetMiniMapCursorPos(capitalBuilding.transform.position); } InputManager.Instance.SpawnedObjects.Add(capitalBuilding.gameObject); //add the new object to the list } } //if the capital building hasn't been set. if (capitalBuilding == null) { Debug.LogError("[Input Manager]: Capital Building hasn't been for faction type of faction ID: " + Command.FactionID); } if (Command.FactionID == GameManager.MasterFactionID) { //if this is the master client //spawn resources if (GameMgr.ResourceMgr.AllResources.Count > 0) { //make sure there are resources to spawn //add the scene resources to list. GameMgr.ResourceMgr.RegisterResourcesMP(); //here resource objects will also be added to the spawn objects list } //register free units and buildings: if (GameMgr.UnitMgr.FreeUnits.Length > 0) { //go through all free units foreach (Unit FreeUnit in GameMgr.UnitMgr.FreeUnits) { //register them InputManager.Instance.SpawnedObjects.Add(FreeUnit.gameObject); } } if (GameMgr.UnitMgr.FreeUnits.Length > 0) { //go through all free buildings foreach (Building FreeBuilding in GameMgr.BuildingMgr.FreeBuildings) { //register them InputManager.Instance.SpawnedObjects.Add(FreeBuilding.gameObject); } } } } else if (Command.SourceMode == (byte)InputSourceMode.Create) { //object creation. //get the prefab to create from GameObject prefab = InputManager.Instance.SpawnablePrefabs[Command.SourceID]; GameObject newInstance = null; //if the prefab is a unit: if (prefab.GetComponent <Unit>()) { Building unitCreator = null; //set the creator: if (Command.TargetID >= 0) { unitCreator = InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject.GetComponent <Building>(); } //create new instance of the unit: newInstance = UnitManager.CreateUnit(prefab.GetComponent <Unit>(), Command.InitialPos, Command.FactionID, unitCreator).gameObject; } //if the prefab is a building: else if (prefab.GetComponent <Building>()) { /* * 0 -> PlacedByDefault = false & Capital = false * 1 -> PlacedByDefault = true & Capital = false * 2 -> PlacedByDefault = false & Capital = true * 3 -> PlacedByDefault = true & Capital = true * */ //TO BE MODIFIED bool placedByDefault = (Value == 1 || Value == 3) ? true : false; bool isCapital = (Value == 2 || Value == 3) ? true : false; Border buildingCenter = null; //set the creator: if (Command.TargetID >= 0) { buildingCenter = InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject.GetComponent <Border>(); } newInstance = BuildingManager.CreatePlacedInstance(prefab.GetComponent <Building>(), Command.InitialPos, buildingCenter, Command.FactionID, placedByDefault).gameObject; newInstance.GetComponent <Building>().FactionCapital = isCapital; } if (newInstance != null) //if a new instance of a unit/building is created: { InputManager.Instance.SpawnedObjects.Add(newInstance); } } else if (Command.SourceMode == (byte)InputSourceMode.Destroy) { if (Command.TargetMode == (byte)InputTargetMode.None) { //this means we're destroying an object (unit, building or resource) GameObject SourceObj = InputManager.Instance.SpawnedObjects[Command.SourceID]; //destroy the object: if (SourceObj.gameObject.GetComponent <Unit>()) { SourceObj.gameObject.GetComponent <Unit>().DestroyUnitLocal(); } else if (SourceObj.gameObject.GetComponent <Building>()) { SourceObj.gameObject.GetComponent <Building>().DestroyBuildingLocal(((int)Value == 1) ? true : false); // 1 means upgrade, 0 means completely destroy } else if (SourceObj.gameObject.GetComponent <Resource>()) { SourceObj.gameObject.GetComponent <Resource>().DestroyResourceLocal(InputManager.Instance.SpawnedObjects[Command.TargetID].GetComponent <GatherResource>()); } //Find an alternative //InputManager.Instance.SpawnedObjects.RemoveAt(Command.SourceID); //remove the ojbect to destroy from the spawn objects list } else if (Command.TargetMode == (byte)InputTargetMode.Faction) { //and this means that a faction gets defeated GameMgr.OnFactionDefeated(Command.Value); GameMgr.UIMgr.ShowPlayerMessage(GameMgr.Factions[Command.Value].Name + " (Faction ID:" + Command.Value.ToString() + ") has been defeated.", UIManager.MessageTypes.Error); //If this is the server: if (GameManager.PlayerFactionID == GameManager.MasterFactionID) { int i = 0; bool ClientFound = false; //mark this faction as disconnected: //go through all the client infos as long as the faction that disconnected or lost while (i < UNET_Mgr.ClientsInfo.Count && ClientFound == false) { //if this faction is the one that disconncted if (UNET_Mgr.ClientsInfo[i].FactionID == Command.Value) { UNET_Mgr.ClientsInfo[i].Disconneted = true; //mark the player as disconnected //stop the while loop ClientFound = true; //if the game is frozen: if (GameManager.GameState == GameStates.Frozen) { //perform a sync test: UNET_Mgr.SyncTest(); } } i++; } } } } else if (Command.SourceMode == (byte)InputSourceMode.CustomCommand) { if (GameMgr.Events) { GameObject Source = null; GameObject Target = null; //get the source and target objects if they exist. if (Command.SourceID >= 0) { Source = InputManager.Instance.SpawnedObjects[Command.SourceID]; } if (Command.TargetID >= 0) { Target = InputManager.Instance.SpawnedObjects[Command.TargetID]; } //Pre defined custom actions for some events that need to be synced between all clients switch (Command.TargetMode) { //switching attack types: case (byte)InputCustomMode.MultipleAttacks: Source.GetComponent <Unit>().MultipleAttacksMgr.EnableAttackTypeLocal(Command.Value); break; //Converting a unit: case (byte)InputCustomMode.Convert: Source.GetComponent <Unit>().ConvertUnitLocal(Target.GetComponent <Unit>()); break; //Toggling invisiblity: case (byte)InputCustomMode.Invisibility: Source.GetComponent <Unit>().InvisibilityMgr.ToggleInvisibility(); break; //APC dropping units case (byte)InputCustomMode.APCDrop: Source.GetComponent <APC>().DropOffUnitsLocal(Command.Value); break; //Triggering a research task in a building: case (byte)InputCustomMode.Research: Source.GetComponent <TaskLauncher>().LaunchResearchTaskLocal(Command.Value); break; //Unit escaping case (byte)InputCustomMode.UnitEscape: Source.GetComponent <Unit>().EscapeLocal(Command.TargetPos); break; case (byte)InputCustomMode.Event: GameMgr.Events.OnCustomCommand(Source, Target, Command.InitialPos, Command.TargetPos, Command.Value); break; default: Debug.LogError("Invalid custom command target mode!"); break; } } } //if a group of units is the source else if (Command.SourceMode == (byte)InputSourceMode.Group) { //get the units list List <Unit> UnitList = StringToUnitList(Command.GroupSourceID); //if there's units in the list: if (UnitList.Count > 0) { //if the target mode is none: if (Command.TargetMode == (byte)InputTargetMode.None) { //move units: MovementManager.Instance.MoveLocal(UnitList, Command.TargetPos, Value, null, InputTargetMode.None); } //if the target mode is attack: else if (Command.TargetMode == (byte)InputTargetMode.Attack) { //group attack: MovementManager.Instance.LaunchAttackLocal(UnitList, InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject, (MovementManager.AttackModes)Value); } } } else if (Command.SourceMode == (byte)InputSourceMode.Unit) { //invalid source id? if (Command.SourceID < 0 || Command.SourceID >= InputManager.Instance.SpawnedObjects.Count) { return; //do not proceed. } Unit SourceUnit = InputManager.Instance.SpawnedObjects[Command.SourceID].gameObject.GetComponent <Unit>(); //see if we need to snap its position or not. if (Vector3.Distance(SourceUnit.transform.position, Command.InitialPos) > SnapDistance) { SourceUnit.transform.position = Command.InitialPos; } if (Command.TargetMode == (byte)InputTargetMode.None) { //if there's no target //move unit. MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, null, InputTargetMode.None); } else { //if there's a target object: GameObject TargetObj = null; if (Command.TargetID >= 0 && Command.TargetID < InputManager.Instance.SpawnedObjects.Count) { TargetObj = InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject; //get the target obj } if (Command.TargetMode == (byte)InputTargetMode.Self) { //if the target mode is self SourceUnit.AddHealthLocal(Value, TargetObj); //update unit health } else if (TargetObj != null) { if (Command.TargetMode == (byte)InputTargetMode.Unit) { //if the target mode is a unit if (TargetObj.GetComponent <Unit>().FactionID != SourceUnit.FactionID) { //if the target unit is from another faction if (SourceUnit.ConvertMgr) { //convert the target unit. SourceUnit.ConvertMgr.SetTargetUnitLocal(TargetObj.GetComponent <Unit>()); } } else { //if hte target unit belongs to the source unit's faction //APC if (TargetObj.GetComponent <APC>()) { SourceUnit.TargetAPC = TargetObj.GetComponent <APC>(); MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, TargetObj, InputTargetMode.Unit); } else if (SourceUnit.HealMgr != null) { //healer: SourceUnit.HealMgr.SetTargetUnitLocal(TargetObj.GetComponent <Unit>()); } } } else if (Command.TargetMode == (byte)InputTargetMode.Building) { //if the target mode is a building if (TargetObj.GetComponent <Building>().FactionID == SourceUnit.FactionID) { //and it belongs to the source's faction if (TargetObj.GetComponent <Building>().Health < TargetObj.GetComponent <Building>().MaxHealth) { //if it doesn't have max health //construct building Builder BuilderComp = SourceUnit.gameObject.GetComponent <Builder>(); BuilderComp.SetTargetBuildingLocal(TargetObj.GetComponent <Building>()); } else if (TargetObj.GetComponent <APC>()) { //if target building is APC SourceUnit.TargetAPC = TargetObj.GetComponent <APC>(); MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, TargetObj, InputTargetMode.Building); } } } else if (Command.TargetMode == (byte)InputTargetMode.Resource) { //if the target mode is a resource GatherResource ResourceComp = SourceUnit.gameObject.GetComponent <GatherResource>(); if (TargetObj.GetComponent <Resource>()) { //if the target obj is a resource //collect resources: ResourceComp.SetTargetResourceLocal(TargetObj.GetComponent <Resource>()); } //but if the target obj is a building else if (TargetObj.GetComponent <Building>()) { //send unit to the drop off building. MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, TargetObj, InputTargetMode.Building); } } else if (Command.TargetMode == (byte)InputTargetMode.Portal) { //if the target mode is a portal //move unit to the portal MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, TargetObj, InputTargetMode.Portal); } else if (Command.TargetMode == (byte)InputTargetMode.Attack) { MovementManager.Instance.LaunchAttackLocal(SourceUnit, InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject, (MovementManager.AttackModes)Value); } } } } else if (Command.SourceMode == (byte)InputSourceMode.Building) { //if the source mode is a building Building SourceBuilding = InputManager.Instance.SpawnedObjects[Command.SourceID].gameObject.GetComponent <Building>(); if (SourceBuilding) { if (Command.TargetMode != (byte)InputTargetMode.None) { //if there's a target object: GameObject TargetObj = null; //get the target obj if (Command.TargetID >= 0) { TargetObj = InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject; //get the target obj } if (Command.TargetMode == (byte)InputTargetMode.Self) { //if the target mode is self = update health SourceBuilding.AddHealthLocal(Value, TargetObj); } else if (Command.TargetMode == (byte)InputTargetMode.Attack && TargetObj != null) { //if the target mode is attack = attack unit //Attack. SourceBuilding.AttackMgr.SetAttackTargetLocal(TargetObj); } } } } else if (Command.SourceMode == (byte)InputSourceMode.Resource) { //if the source mode is a resource Resource SourceResource = InputManager.Instance.SpawnedObjects[Command.SourceID].gameObject.GetComponent <Resource>(); if (Command.TargetMode != (byte)InputTargetMode.None) { //if there's a target object: if (Command.TargetMode == (byte)InputTargetMode.Self) { //if the target mode is self = update health GameObject TargetObj = InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject; //get the target obj SourceResource.AddResourceAmountLocal(Value, TargetObj.GetComponent <GatherResource>()); } } } }