//called when a unit requests support from nearby units: void OnUnitSupportRequest(Vector3 pos, GameObject target) { //if the unit support feature is disabled if (unitSupportEnabled == false) { return; //do not proceed. } //go through the faction's attack units: foreach (Unit u in factionMgr.Army) { //check if the unit is within the required distance: if (Vector3.Distance(u.transform.position, pos) < unitSupportRange.getRandomValue()) { //request support (as long as the unit isn't busy): mvtMgr.LaunchAttack(u, target, MovementManager.AttackModes.None); } } }
//attack the assigned target building. void AttackTargetBuilding() { //making sure that the NPC faction in an attack in progress and that there's a valid target building: if (currentTargetBuilding == null || isAttacking == false) { return; } GameObject target = currentTargetBuilding.gameObject; MovementManager.AttackModes attackMode = MovementManager.AttackModes.None; //if the current target building is being constructed: if (currentTargetBuilding.WorkerMgr.CurrentWorkers > 0) { //attack the workers first: go through the workers positions for (int i = 0; i < currentTargetBuilding.WorkerMgr.WorkerPositions.Length; i++) { //find worker: if (currentTargetBuilding.WorkerMgr.WorkerPositions[i].CurrentUnit != null) { //is the worker actually at the building constructing it: if (currentTargetBuilding.WorkerMgr.WorkerPositions[i].CurrentUnit.BuilderMgr.IsBuilding == true) { //assign it as target. target = currentTargetBuilding.WorkerMgr.WorkerPositions[i].CurrentUnit.gameObject; //force attack units to attack it: attackMode = MovementManager.AttackModes.Change; } } } } //launch the actual attack: mvtMgr.LaunchAttack(currentAttackUnits, target, attackMode); }
//Send units to attack a center: public void SendUnitsToTargetBuilding() { if (TargetBuilding != null) //if there's a target building { GameObject Target = TargetBuilding.gameObject; //if the target building is currently being fixed by builders if (TargetBuilding.WorkerMgr.CurrentWorkers > 0) { int n = 0; //attack these builders instead. while (n < TargetBuilding.WorkerMgr.WorkerPositions.Length) { //first check if there's a worker in this slot: if (TargetBuilding.WorkerMgr.WorkerPositions[n].CurrentUnit != null) { //if the worker is constructing if (TargetBuilding.WorkerMgr.WorkerPositions[n].CurrentUnit.BuilderMgr.IsBuilding == true) { //set him as target Target = TargetBuilding.WorkerMgr.GetWorker().gameObject; TargetingBuilders = true; } } n++; } } else { TargetingBuilders = false; } if (Army.Count > 0) { for (int i = 0; i < Army.Count; i++) { if (Army [i] != null) { if (Army [i].AttackMgr.AttackTarget == null) //set the attacking support range that will call nearby units for support. { Army [i].AttackMgr.SearchRange = AttackingSupportRange; } } } MvtMgr.LaunchAttack(Army, Target.gameObject, (TargetingBuilders == true) ? MovementManager.AttackModes.Change : MovementManager.AttackModes.None); //launch the attack. //move healers and converters to the target building //Healers: int HealersAmount = Mathf.RoundToInt(FactionMgr.Healers.Count * HealersRatio); int ConvertersAmount = Mathf.RoundToInt(FactionMgr.Converters.Count * ConvertersRatio); //if there are healers to send: int j = 0; while (HealersAmount > 0 && j < FactionMgr.Healers.Count) { if (FactionMgr.Healers [j].TargetUnit == null) //only move the unit if it has no target { MvtMgr.Move(FactionMgr.Healers[j].UnitMvt, Target.transform.position, TargetBuilding.Radius, Target, InputTargetMode.Building); } HealersAmount--; ExtraArmyUnits.Add(FactionMgr.Healers [j].UnitMvt); j++; } j = 0; //if there are converters to send, do it: while (ConvertersAmount > 0 && j < FactionMgr.Converters.Count) { if (FactionMgr.Converters [j].TargetUnit == null) { MvtMgr.Move(FactionMgr.Converters[j].UnitMvt, Target.transform.position, TargetBuilding.Radius, Target, InputTargetMode.Building); } ConvertersAmount--; ExtraArmyUnits.Add(FactionMgr.Converters [j].UnitMvt); j++; } } } }
//method to search for a target bool SearchForTargetToAttack() { //Search if there are enemy units in range: bool Found = false; float Size = SearchRange; Vector3 SearchFrom = transform.position; //only for NPC factions: //if there's no city center to protect: if (AttackRangeCenter == null) { AttackRangeFromCenter = false; //we're not defending any city center then: } //if there's a city center to protect if (AttackRangeFromCenter == true && AttackRangeCenter != null) { SearchFrom = AttackRangeCenter.transform.position; //the search pos is the city center Size = AttackRangeCenter.GetComponent <Border>().Size; //and the search size is the whole city border size: } Collider[] ObjsInRange = Physics.OverlapSphere(SearchFrom, Size); int i = 0; //counter while (i < ObjsInRange.Length && Found == false) { Unit UnitInRange = ObjsInRange[i].gameObject.GetComponent <Unit>(); if (UnitInRange && UnitInRange != UnitMgr) //make sure that there's a unit in range and that unit is not this one. { //If it's a unit object if (UnitInRange.enabled == true) { //if the unit comp is enabled //If this unit and the target have different teams and make sure it's not dead. if (CanAttackFaction(UnitInRange.FactionID) && UnitInRange.Dead == false) { //if the unit is visible: if (UnitInRange.IsInvisible == false) { if (AttackTarget == null) { if (AttackMgr.CanAttackTarget(this, ObjsInRange[i].gameObject)) { //if the unit can attack the target. //Set this unit as the target //if this is a unit: if (AttackerType == AttackerTypes.Unit) { MvtMgr.LaunchAttack(UnitMgr, ObjsInRange[i].gameObject, MovementManager.AttackModes.Assigned); } else //if this is a building { SetAttackTarget(ObjsInRange[i].gameObject); } Found = true; } } } } } } i++; } return(Found); }
//method to search for a target bool SearchForTargetToAttack() { if (AttackTarget != null) { return(true); } //Search if there are enemy units in range: bool Found = false; float Size = SearchRange; Vector3 SearchFrom = transform.position; //only for NPC factions: Collider[] ObjsInRange = Physics.OverlapSphere(SearchFrom, Size); print(ObjsInRange); int i = 0; //counter if (AttackTarget == null) { while (i < ObjsInRange.Length && Found == false) { Unit UnitInRange = ObjsInRange[i].gameObject.GetComponent <Unit>(); stats RpgPlayer = ObjsInRange[i].gameObject.GetComponent <stats>(); if (UnitInRange && UnitInRange != UnitMgr) //make sure that there's a unit in range and that unit is not this one. { //If it's a unit object if (UnitInRange.enabled == true) { //if the unit comp is enabled //If this unit and the target have different teams and make sure it's not dead. if (CanAttackFaction(UnitInRange.FactionID) && UnitInRange.Dead == false) { //if the unit is visible: if (UnitInRange.IsInvisible == false) { if (AttackTarget == null) { if (AttackMgr.CanAttackTarget(this, ObjsInRange[i].gameObject, AttackExceptionList)) { //if the unit can attack the target. //Set this unit as the target //if this is a unit: if (AttackerType == AttackerTypes.Unit) { if (Vector3.Distance(transform.position, ObjsInRange[i].gameObject.transform.position) > Size) { return(false); } Debug.Log("hello"); MvtMgr.LaunchAttack(UnitMgr, ObjsInRange[i].gameObject, MovementManager.AttackModes.Assigned); } else //if this is a building { SetAttackTarget(ObjsInRange[i].gameObject); } Found = true; } } } } } } else if (RpgPlayer) { if (AttackTarget == null) { if (AttackMgr.CanAttackTarget(this, ObjsInRange[i].gameObject, AttackExceptionList)) { if (AttackerType == AttackerTypes.Unit) { Debug.Log("hello"); MvtMgr.LaunchAttack(UnitMgr, ObjsInRange[i].gameObject, MovementManager.AttackModes.Assigned); } else //if this is a building { SetAttackTarget(ObjsInRange[i].gameObject); } Found = true; } } } i++; } } return(Found); }