public static void CalculateAttack(Unit attacker, Unit defender) { Unit[] participants = Combat.CalculateCombatForUnits(attacker, defender); Debug.LogFormat("attacker.health = {0}", participants[0].health); Debug.LogFormat("defender.health = {0}", participants[1].health); if (attacker.health <= 0) { DestroyUnit(attacker.xy); } if (defender.health <= 0) { DestroyUnit(defender.xy); } }
public Unit ChooseTarget(Unit unit, ref Vector2 attackPosition) { Vector2 startingPosition = unit.xy; Unit chosenTarget = null; float bestAttackValue = -100000; UnitPather pather = new UnitPather(unit); foreach (Vector2 position in pather.BehaviorMovePositions(attacking: true)) { GridManager.MoveUnit(startingPosition, position, () => {}); foreach (Vector2 targetPosition in GridManager.GetCoordsToAttackHighlight(unit.xy, unit.range)) { Unit target = GridManager.GetUnit(targetPosition); if (target) { if (target.owner != unit.owner) { int attackerStartingHealth = unit.health; int defenderStartingHealth = target.health; Unit[] outCome = Combat.CalculateCombatForUnits(unit, target); int damageDone = defenderStartingHealth - outCome[1].health; int damageTaken = attackerStartingHealth - outCome[0].health; bool enoughDamageDone = (damageTaken > 0) ? (damageDone > 10 || damageDone / damageTaken > 2f) : true; bool notTooMuchDamageTaken = damageTaken < 70; if (enoughDamageDone && notTooMuchDamageTaken) { float attackValue = damageDone * target.cost * target.powerConstant - damageTaken * unit.cost * unit.powerConstant; if ((attackValue > bestAttackValue) || (attackValue == bestAttackValue && UnityEngine.Random.Range(0f, 1f) > .5f)) { bestAttackValue = attackValue; chosenTarget = target; attackPosition = position; } } unit.ChangeHealth(damageTaken); target.ChangeHealth(damageDone); } } } GridManager.MoveUnit(position, startingPosition, () => {}); } return(chosenTarget); }