private void FindAttackTarget() { var neighbourCellsOfRange = MyGrid.Instance.GetNeighbourCellsOfRange(ParentCell, GetResource().targetSearchRadius); CreatureController targetCreature = neighbourCellsOfRange .Select(cell => cell.GetCreature()) .FirstOrDefault(creature => creature != null && !creature.IsDead); if (targetCreature != null) { MarkCreatureAsTarget(targetCreature); RotateSpriteTo(Utils.GetDirection(transform.position, targetCreature.transform.position)); Debug.Log($"Found target creature {targetCreature}"); } else { UnmarkCreatureAsTarget(); } }
private IEnumerator StartAttacking(CreatureController target) { do { var neighbourCellsOfRange = MyGrid.Instance.GetNeighbourCellsOfRange(ParentCell, GetResource().attackRange); bool isInAttackRange = neighbourCellsOfRange .Select(cell => cell.GetCreature()) .Any(creature => creature == target); if (isInAttackRange) { DealDamage(GetResource().attackDamage, target); if (target.IsDead) { UnmarkCreatureAsTarget(); yield break; } } yield return(new WaitForSeconds(1 / GetResource().attackPerSecond)); } while (IsInAttackMode && target == AttackTargetCreature); }
public void DealDamage(int damage, CreatureController attackedCreature) { attackedCreature.TakeDamage(damage); }