using UnityEngine; namespace AsteroidGame.Entities { public class Targeter : MonoBehaviour { [SerializeField] private float _range; [SerializeField] private STargeterConfig.TargetStrategy _targetStrategy; [SerializeField] private SEntityBaseRuntimeSet _activeEntities; [SerializeField] private EntityBase _targeterParent; [SerializeField] private EntityBase _targetEntity; public void SetParent(EntityBase newParent) => _targeterParent = newParent; public void SetConfig(STargeterConfig config) { _range = config._range; _targetStrategy = config._selectedTargetStrategy; _activeEntities = config._activeEntities; } public EntityBase GetTarget() { return _targetEntity; } public bool FoundTarget() { float currentBestValue = Mathf.Infinity; var targetFound = false; foreach (EntityBase targetEntity in _activeEntities._list) { float distanceToTarget = Vector3.Magnitude(targetEntity.GetCenterPosition() - _targeterParent.GetCenterPosition()); bool withinRange = distanceToTarget < _range; if (withinRange) { switch (_targetStrategy) { case STargeterConfig.TargetStrategy.LowestRange: { bool isClosest = distanceToTarget < currentBestValue; if (isClosest) { targetFound = true; currentBestValue = distanceToTarget; _targetEntity = targetEntity; } break; } case STargeterConfig.TargetStrategy.LowestHealth: { float enemyHealth = targetEntity.GetHealth(); bool isLowestHealth = enemyHealth < currentBestValue; if (isLowestHealth) { targetFound = true; currentBestValue = enemyHealth; _targetEntity = targetEntity; } break; } } } } return targetFound; } } }