78 lines
2.7 KiB
C#
78 lines
2.7 KiB
C#
using AsteroidGame.ScriptableObjects;
|
|
using GameDev.CoreSystems;
|
|
using UnityEngine;
|
|
|
|
namespace AsteroidGame.Entities
|
|
{
|
|
public class Targeter : MonoBehaviour
|
|
{
|
|
[SerializeField] private float _range;
|
|
[SerializeField] private SoTargeterConfig.TargetStrategy _targetStrategy;
|
|
[SerializeField] private SoTargetableRuntimeSet _activeEntities;
|
|
[SerializeField] private Targetable _targeterParent;
|
|
[SerializeField] private Targetable _targetEntity;
|
|
|
|
public void SetParent(Transform newParent) => _targeterParent.transform.parent = newParent;
|
|
|
|
public void SetConfig(SoTargeterConfig config)
|
|
{
|
|
_range = config._range;
|
|
_targetStrategy = config._selectedTargetStrategy;
|
|
_activeEntities = config._activeEntities;
|
|
}
|
|
|
|
public Targetable GetTarget()
|
|
{
|
|
return _targetEntity;
|
|
}
|
|
|
|
public bool FoundTarget()
|
|
{
|
|
float currentBestValue = Mathf.Infinity;
|
|
var targetFound = false;
|
|
|
|
foreach (var 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.transform.parent.GetComponent<Damageable>()
|
|
.GetCurrentHealth();
|
|
|
|
bool isLowestHealth = enemyHealth < currentBestValue;
|
|
if (isLowestHealth)
|
|
{
|
|
targetFound = true;
|
|
currentBestValue = enemyHealth;
|
|
_targetEntity = targetEntity;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return targetFound;
|
|
}
|
|
}
|
|
} |