I recently began a new project, which is almost finished. As far as the code goes, all I need to do is add AI to the game. I chose A* pathfinding because it was the best option for me, but I also added enemy personalities to make it more interesting. In essence, based on the enemy's personality, it will either: stay in the fight, go heal, or look for other enemies. I am aware that I can use if statements, but I do not want complicated, difficult-to-read code. Please respond if you have any suggestions for how I could put this into practice, thanks in advance.
Here is the code, P.S. the code is not finished yet this is just a test:
private enum Personality { Proud, Smart, Dumb }
private Personality personality;
public bool lowHP;
public bool inDanger;
public bool shouldMove;
public bool canCallSupport = true;
public Transform target;
public Transform healStation;
public Transform swarm;
public float scanRadius;
public float alertRadius;
public LayerMask whatIsSupport;
public LayerMask whatIsHeal;
private AIDestinationSetter destinationSetter;
private void Awake()
{
destinationSetter = GetComponent<AIDestinationSetter>();
}
private void Update()
{
if (!lowHP)
{
destinationSetter.target = target;
}
else if (lowHP)
{
if (!canCallSupport)
{
if (ScanForHelp(whatIsHeal) == null)
return;
else
destinationSetter.target = ScanForHelp(whatIsHeal);
}
else if(canCallSupport)
{
if (ScanForHelp(whatIsSupport) == null)
return;
else
destinationSetter.target = ScanForHelp(whatIsSupport);
}
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, scanRadius);
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, alertRadius);
}
private Transform ScanForHelp(LayerMask mask)
{
Transform closestHelp;
RaycastHit2D[] hit = Physics2D.CircleCastAll(transform.position, scanRadius, Vector2.right, 0.1f, mask);
Debug.Log(hit.Length);
closestHelp = hit[0].transform;
foreach (RaycastHit2D h in hit)
{
if (Vector3.Distance(transform.position, h.transform.position) < (Vector3.Distance(transform.position, closestHelp.position)))
{
closestHelp = h.transform;
}
}
if (closestHelp == null)
return null;
return closestHelp;
}
private void CallSupport()
{
RaycastHit2D[] hit = Physics2D.CircleCastAll(transform.position, alertRadius, Vector2.right, 0.1f, whatIsSupport);
foreach (RaycastHit2D h in hit)
{
h.collider.TryGetComponent<AIDestinationSetter>(out AIDestinationSetter destinationSetter);
destinationSetter.target = target;
h.collider.TryGetComponent<AIPath>(out AIPath path);
path.canMove = true;
}
lowHP = false;
}