// try not to target an already attacked enemy // if all enemies are attacked, target nearest attacked enemy // if there are no enemies at all, return null public CombatFlow findGroundTargetByType(CombatFlow.Type type) { CombatFlow maybeAttackedEnemy = findGroundTargetByType(type, true); CombatFlow uniqueEnemy = findGroundTargetByType(type, false); if (uniqueEnemy == null) { return(maybeAttackedEnemy); } else { return(uniqueEnemy); } }
public CombatFlow findGroundTargetByType(CombatFlow.Type type, bool includeAttackedEnemies) { // start by just finding closest ground target // if any units are within closeRange, use nose angle // --> select the smallest angle unit that is ALSO within closeRange // otherwise, just simply select the closest unit CombatFlow groundUnit = null; bool useNoseAngle = false; bool firstSet = false; float smallestAngle = 0; int smallestAngleIndex = -1; float shortestDist = 0; int shortestDistIndex = -1; for (int i = 0; i < enemyGroundUnitsContainer.Count; i++) { CombatFlow currUnit = enemyGroundUnitsContainer[i]; if (currUnit != null && currUnit.type == type && (includeAttackedEnemies || !aiTgtComp.maxMissilesOnTarget(currUnit))) { float currDist = Vector3.Distance(transform.position, currUnit.transform.position); float currAngle = Vector3.Angle(transform.forward, currUnit.transform.position - transform.position); if (!firstSet) { smallestAngle = currAngle; smallestAngleIndex = i; shortestDist = currDist; shortestDistIndex = i; firstSet = true; } if (currDist < shortestDist) { shortestDist = currDist; shortestDistIndex = i; } if (currAngle < smallestAngle && currDist < closeRange) { smallestAngle = currAngle; smallestAngleIndex = i; useNoseAngle = true; } } } if (firstSet) // return unit will remain null if none are found { if (useNoseAngle) { groundUnit = enemyGroundUnitsContainer[smallestAngleIndex]; } else { groundUnit = enemyGroundUnitsContainer[shortestDistIndex]; } } Debug.Log("Attacking ground unit: " + groundUnit + ", useNoseAngle: " + useNoseAngle); return(groundUnit); }