75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using UnityEngine;
|
|
|
|
namespace AsteroidGame.Entities
|
|
{
|
|
public class Targeter : MonoBehaviour
|
|
{
|
|
[SerializeField] private float _range;
|
|
[SerializeField] private EntityBase _targeterParent;
|
|
[SerializeField] private EntityBase _targetEntity;
|
|
[SerializeField] private SoTargeterConfig.TargetStrategy _targetStrategy;
|
|
[SerializeField] private SoEntityBaseRuntimeSet _activeEntities;
|
|
|
|
public void SetParent(EntityBase newParent) => _targeterParent = newParent;
|
|
|
|
public void SetConfig(SoTargeterConfig 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 SoTargeterConfig.TargetStrategy.LowestRange:
|
|
{
|
|
bool isClosest = distanceToTarget < currentBestValue;
|
|
if (isClosest)
|
|
{
|
|
targetFound = true;
|
|
currentBestValue = distanceToTarget;
|
|
_targetEntity = targetEntity;
|
|
}
|
|
|
|
break;
|
|
}
|
|
case SoTargeterConfig.TargetStrategy.LowestHealth:
|
|
{
|
|
float enemyHealth = targetEntity.GetHealth();
|
|
|
|
bool isLowestHealth = enemyHealth < currentBestValue;
|
|
if (isLowestHealth)
|
|
{
|
|
targetFound = true;
|
|
currentBestValue = enemyHealth;
|
|
_targetEntity = targetEntity;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return targetFound;
|
|
}
|
|
}
|
|
} |