public void Move(Unit RefUnit, Vector3 Destination, float CircleRadius, GameObject TargetObj, InputTargetMode TargetMode) { //if this is an online game if (GameManager.MultiplayerGame == true) { //and this is the owner of the unit if (GameMgr.IsLocalPlayer(RefUnit.gameObject) == true) { //send input action to the input manager InputVars NewInputAction = new InputVars(); //mode: NewInputAction.SourceMode = (byte)InputSourceMode.Unit; NewInputAction.TargetMode = (byte)TargetMode; //initially, this is set to none //source NewInputAction.Source = RefUnit.gameObject; NewInputAction.Target = TargetObj; NewInputAction.InitialPos = RefUnit.transform.position; NewInputAction.TargetPos = Destination; NewInputAction.Value = (int)CircleRadius; //sent input InputManager.SendInput(NewInputAction); } } else //single player game { //directly move the unit MoveLocal(RefUnit, Destination, CircleRadius, TargetObj, TargetMode); } }
//move one single unit to a destination public void MoveLocal(Unit RefUnit, Vector3 Destination, float CircleRadius, GameObject TargetObj, InputTargetMode TargetMode) { if (RefUnit.CanBeMoved == true) { Destination = new Vector3(Destination.x, TerrainMgr.SampleHeight(Destination), Destination.z); Vector3 MvtPos = Vector3.zero; //Make the ref unit's target collider pos so it won't be detected as invalid destination RefUnit.TargetPosColl.isTrigger = false; //trigger if we can move directly to the chosen destination (assuming that there's no target object to move to). if (IsDestinationClear(Destination, RefUnit, out MvtPos) && TargetObj == null) { RefUnit.CheckUnitPathLocal(MvtPos, null, Destination, MvtStoppingDistance, InputTargetMode.None); } else { //the positions that the units will fill List <Vector3> Positions = CircleFormation(Destination, CircleRadius, RefUnit); //get a valid position for the unit to move to while (Positions.Count == 0) { Positions = CircleFormation(Destination, CircleRadius, RefUnit); CircleRadius += RefUnit.NavAgent.radius; } RefUnit.CheckUnitPathLocal(Positions[GetClosestPosID(Positions, RefUnit.transform.position)], TargetObj, Destination, MvtStoppingDistance, TargetMode); } //trigger the ref unit's target collider pos so it will be detected again: RefUnit.TargetPosColl.isTrigger = true; } if (GameMgr.Events) { GameMgr.Events.OnUnitMoveAttempt(RefUnit); //custom event call } }
//move a large amount of units public void MoveLocal(List <Unit> UnitsList, Vector3 Destination, float CircleRadius, GameObject TargetObj, InputTargetMode TargetMode) { //go through the unit list and trigger the target position collider for (int i = 0; i < UnitsList.Count; i++) { UnitsList[i].TargetPosColl.isTrigger = false; } if (UnitsList.Count > 0) { //make sure, at least a unit has been selected //sort the units by their types List <UnitTypes> SelectedUnitTypes = SetSelectedUnitsTypes(UnitsList, false); //sort the unit groups depending on the unit's radius SortSelectedUnitTypes(UnitSortType.Radius, ref SelectedUnitTypes); //go through all unit types: foreach (UnitTypes UnitGroup in SelectedUnitTypes) { List <Unit> CurrentUnitsList = UnitGroup.UnitsList; //sort the units in this group based on their distance from the destination SortUnitsByDistance(ref CurrentUnitsList, Destination); bool UnitMoved = false; //the positions that the units will fill List <Vector3> Positions = new List <Vector3>(); //go through the list for (int i = 0; i < CurrentUnitsList.Count; i++) { if (CurrentUnitsList[i].CanBeMoved == true) { //get a valid position for the unit to move to while (Positions.Count == 0) { Positions = CircleFormation(Destination, CircleRadius, CurrentUnitsList[i]); CircleRadius += CurrentUnitsList[i].NavAgent.radius; } UnitMoved = true; int ID = GetClosestPosID(Positions, CurrentUnitsList[i].transform.position); //Inform the units about the target position to go to and they'll see if there's a valid path to go there: CurrentUnitsList[i].CheckUnitPathLocal(Positions[ID], TargetObj, Destination, MvtStoppingDistance, TargetMode); Positions.RemoveAt(ID); //trigger the target pos collider CurrentUnitsList[i].TargetPosColl.isTrigger = true; } if (GameMgr.Events) { GameMgr.Events.OnUnitMoveAttempt(CurrentUnitsList[i]); //custom event call } } //if units have moved if (UnitMoved == true) { //mvt order audio: AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, CurrentUnitsList[0].MvtOrderAudio, false); //Show the mvt target effect: if (MvtTargetEffectObj != null) { MvtTargetEffectObj.transform.position = Destination; MvtTargetEffectObj.Activate(); } } else { //display a message to let the player know that the units couldn't be moved: UIMgr.ShowPlayerMessage("Can't moved selected unit(s)", UIManager.MessageTypes.Error); } } } }