// Get the total "value" sum for a list of affected units private int GetValueFor(List <Unit> affectedUnits) { int value = 0; foreach (var unit in affectedUnits) { value += ValueOf(AttachedSkill.PreviewEffectOnUnit(unit)); } return(value); }
public IEnumerator CommitActions() { IsBusy = true; // First move the AI if (FoundMovePos.HasValue) { AttachedEntity.StartMoveAnimation(FoundMovePos.Value); yield return(new WaitWhile(() => AttachedEntity.IsMoving)); } // Then attack the target if (FoundTarget.HasValue) { AttachedSkill.ActivateSkill(AttachedUnit, FoundTarget.Value); } IsBusy = false; }
// Returns a list of possible target points for the Unit, sorted by value (descending order) and distance (ascending order) private List <AITarget> GetPossibleTargets() { List <AITarget> targets = new List <AITarget>(); switch (AttachedSkill.targetType) { // Only units can be targeted, thus check the value of targeting all valid Units case Skill.TargetType.Unit: { List <Unit> allUnits = Grid.ActiveGrid.GetAllUnits(); foreach (var unit in allUnits) { // Filter out only valid targets if (AttachedSkill.IsAffected(AttachedUnit, unit)) { // Check all units that will be affected from targeting this unit AITarget target; target.coordinates = unit.GetCoordinates(); target.value = GetValueFor(AttachedSkill.GetAffectedUnits(AttachedUnit, target.coordinates, true)); target.distance = Vector2Int.Distance(AttachedUnit.GetCoordinates(), target.coordinates); // If there is value to this target, add it to the list if (target.value > 0) { targets.Add(target); } } } } break; // Any tile can be targeted, thus check the values of all tiles case Skill.TargetType.Tile: { for (int x = 0; x < Grid.ActiveGrid.gridSize.x; x++) { for (int y = 0; y < Grid.ActiveGrid.gridSize.y; y++) { // Check all units that will be affected from targeting this tile AITarget target; target.coordinates = new Vector2Int(x, y); target.value = GetValueFor(AttachedSkill.GetAffectedUnits(AttachedUnit, target.coordinates, true)); target.distance = Vector2Int.Distance(AttachedUnit.GetCoordinates(), target.coordinates); // If there is value to this target, add it to the list if (target.value > 0) { targets.Add(target); } } } } break; } targets.Sort((x, y) => { int i = y.value.CompareTo(x.value); if (i == 0) { return(x.distance.CompareTo(y.distance)); } return(i); }); return(targets); }