60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
using UnityEngine;
|
|
|
|
public class Enemy : CombatEntity
|
|
{
|
|
[Header("Targetting")]
|
|
public CombatEntity closestTarget; //actually get the detected target first
|
|
public bool detectedPlayer = false;
|
|
[Header("Direction")]
|
|
public Transform[] possibleDirections;
|
|
public float forwardPercent;
|
|
public float strafePercent;
|
|
public float directionVariance; //how much strafe/forward vary
|
|
protected float xSign = -1f;
|
|
protected float ySign = 1f;
|
|
|
|
private void Start()
|
|
{
|
|
if (Random.Range(0f, 2f) > 1f)
|
|
{
|
|
xSign = -xSign;
|
|
ySign = -ySign;
|
|
}
|
|
float newVariance = Random.Range(-directionVariance, directionVariance);
|
|
forwardPercent += newVariance;
|
|
strafePercent -= newVariance;
|
|
}
|
|
|
|
protected override void Movement()
|
|
{
|
|
if (stalled || !closestTarget || !detectedPlayer || closestTarget.isSafe)
|
|
{
|
|
moveDirection = Vector3.zero;
|
|
}
|
|
else
|
|
{
|
|
Vector2 directionToTarget = (closestTarget.transform.position - transform.position).normalized;
|
|
Vector2 directionToMove = directionToTarget;
|
|
float currentHighestScore = float.NegativeInfinity;
|
|
Vector2 strafeDirection = new Vector3(directionToTarget.y * ySign, directionToTarget.x * xSign);
|
|
foreach (Transform direction in possibleDirections)
|
|
{
|
|
Vector3 directionToPoint = (direction.position - transform.position).normalized;
|
|
float forwardScore = Vector3.Dot(directionToPoint, directionToTarget);
|
|
float strafeScore = Vector3.Dot(directionToPoint, strafeDirection);
|
|
float finalScore = (forwardScore * forwardPercent) + (strafeScore * strafePercent);
|
|
//Debug.Log($"Forward: {forwardScore} Strafe: {strafeScore} Final: {finalScore}");
|
|
if (finalScore > currentHighestScore)
|
|
{
|
|
//Debug.Log($"{finalScore} is higher than current score: {currentHighestScore}");
|
|
currentHighestScore = finalScore;
|
|
directionToMove = directionToPoint;
|
|
}
|
|
}
|
|
|
|
moveDirection = directionToMove;
|
|
//FlipSprite(directionToMove);
|
|
// ^ add when you actually draw sprites
|
|
}
|
|
}
|
|
}
|