371 lines
7.4 KiB
C#
371 lines
7.4 KiB
C#
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Enemy))]
|
|
public class EnemyPathing : MonoBehaviour
|
|
{
|
|
[Header("Movement")]
|
|
[SerializeField]
|
|
private float moveSpeed = 0.5f;
|
|
|
|
[SerializeField]
|
|
private float targetReachedDistance = 0.1f;
|
|
|
|
|
|
[Header("Enemy Separation")]
|
|
[SerializeField]
|
|
private CircleCollider2D separationCollider;
|
|
|
|
[SerializeField]
|
|
private LayerMask separationLayer;
|
|
|
|
/*[SerializeField]
|
|
[Min(0.1f)]
|
|
private float obstacleDetectionRadius = 1.5f;
|
|
|
|
|
|
[Header("Stuck Detection")]
|
|
[SerializeField]
|
|
[Min(0.1f)]
|
|
private float progressCheckInterval = 0.5f;
|
|
|
|
[SerializeField]
|
|
[Min(0f)]
|
|
private float minimumTargetProgress = 0.03f;
|
|
|
|
[SerializeField]
|
|
[Min(0.1f)]
|
|
private float maxNoProgressTime = 1.5f;
|
|
|
|
|
|
[Header("Movement Evaluation")]
|
|
[SerializeField]
|
|
private float movementProbeDistance = 0.25f;
|
|
|
|
[SerializeField]
|
|
private float overlapPenaltyWeight = 3f;*/
|
|
|
|
|
|
// Referenzen
|
|
private Enemy enemy;
|
|
private EnemyTargetManager targetManager;
|
|
private CharacterRotation characterRotation;
|
|
|
|
private Transform currentTarget;
|
|
private CircleCollider2D baseCollider;
|
|
|
|
|
|
// Öffentliche Schnittstellen
|
|
public Vector2 MovementDirection { get; private set; }
|
|
|
|
public Transform CurrentTarget => currentTarget;
|
|
|
|
|
|
private EnemySeparation separation;
|
|
|
|
|
|
// INITIALISIERUNG
|
|
//
|
|
|
|
private void Awake()
|
|
{
|
|
enemy = GetComponent<Enemy>();
|
|
|
|
characterRotation =
|
|
GetComponent<CharacterRotation>();
|
|
|
|
targetManager =
|
|
FindFirstObjectByType<EnemyTargetManager>();
|
|
|
|
if (targetManager == null)
|
|
{
|
|
Debug.LogError(
|
|
"EnemyPathing: Kein EnemyTargetManager gefunden!",
|
|
this
|
|
);
|
|
}
|
|
|
|
if (separationCollider == null)
|
|
{
|
|
Debug.LogError(
|
|
"EnemyPathing: Kein SeparationCollider zugewiesen!",
|
|
this
|
|
);
|
|
}
|
|
|
|
separation = GetComponent<EnemySeparation>();
|
|
|
|
if (separation == null)
|
|
{
|
|
Debug.LogError(
|
|
"EnemyPathing: EnemySeparation fehlt!",
|
|
this
|
|
);
|
|
}
|
|
|
|
FindBaseCollider();
|
|
}
|
|
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (targetManager != null)
|
|
{
|
|
targetManager.OnTargetsChanged +=
|
|
SelectClosestTarget;
|
|
}
|
|
}
|
|
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (targetManager != null)
|
|
{
|
|
targetManager.OnTargetsChanged -=
|
|
SelectClosestTarget;
|
|
}
|
|
}
|
|
|
|
|
|
private void Start()
|
|
{
|
|
if (targetManager == null)
|
|
return;
|
|
|
|
SelectClosestTarget();
|
|
|
|
if (characterRotation != null)
|
|
{
|
|
characterRotation.SetInitialRotation();
|
|
}
|
|
}
|
|
|
|
|
|
// BEWEGUNGSSTEUERUNG
|
|
//
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (targetManager == null || enemy.IsDead)
|
|
return;
|
|
|
|
if (currentTarget == null)
|
|
{
|
|
MovementDirection = Vector2.zero;
|
|
|
|
SelectClosestTarget();
|
|
|
|
return;
|
|
}
|
|
|
|
if (!currentTarget.gameObject.activeInHierarchy)
|
|
{
|
|
SelectClosestTarget();
|
|
return;
|
|
}
|
|
|
|
Physics2D.SyncTransforms();
|
|
|
|
MoveTowardsTarget();
|
|
|
|
if (HasReachedTarget())
|
|
{
|
|
SelectClosestTarget();
|
|
}
|
|
}
|
|
|
|
|
|
// TARGETPOINT-AUSWAHL
|
|
//
|
|
|
|
private void SelectClosestTarget()
|
|
{
|
|
if (targetManager == null)
|
|
return;
|
|
|
|
currentTarget =
|
|
targetManager.GetClosestActiveTarget(
|
|
transform.position
|
|
);
|
|
|
|
UpdateMovementDirection();
|
|
}
|
|
|
|
|
|
// BASISBEGRENZUNG
|
|
//
|
|
|
|
private void FindBaseCollider()
|
|
{
|
|
int baseLayer = LayerMask.NameToLayer("Base");
|
|
|
|
if (baseLayer == -1)
|
|
{
|
|
Debug.LogError(
|
|
"EnemyPathing: Layer Base existiert nicht!",
|
|
this
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
Collider2D[] colliders =
|
|
FindObjectsByType<Collider2D>(
|
|
FindObjectsSortMode.None
|
|
);
|
|
|
|
foreach (Collider2D col in colliders)
|
|
{
|
|
if (col.gameObject.layer == baseLayer &&
|
|
col is CircleCollider2D circle)
|
|
{
|
|
baseCollider = circle;
|
|
return;
|
|
}
|
|
}
|
|
|
|
Debug.LogError(
|
|
"EnemyPathing: Kein CircleCollider2D auf Layer Base gefunden!",
|
|
this
|
|
);
|
|
}
|
|
|
|
|
|
private Vector2 ClampPositionToBase(Vector2 position)
|
|
{
|
|
if (baseCollider == null)
|
|
return position;
|
|
|
|
Vector2 baseCenter =
|
|
baseCollider.transform.TransformPoint(
|
|
baseCollider.offset
|
|
);
|
|
|
|
Vector3 scale =
|
|
baseCollider.transform.lossyScale;
|
|
|
|
float baseRadius =
|
|
baseCollider.radius *
|
|
Mathf.Max(
|
|
Mathf.Abs(scale.x),
|
|
Mathf.Abs(scale.y)
|
|
);
|
|
|
|
float allowedRadius = Mathf.Max(
|
|
0f,
|
|
baseRadius - 0.02f
|
|
);
|
|
|
|
Vector2 offset = position - baseCenter;
|
|
|
|
if (offset.sqrMagnitude >
|
|
allowedRadius * allowedRadius)
|
|
{
|
|
position =
|
|
baseCenter +
|
|
offset.normalized * allowedRadius;
|
|
}
|
|
|
|
return position;
|
|
}
|
|
|
|
|
|
// BEWEGUNGSRICHTUNG
|
|
//
|
|
|
|
private void UpdateMovementDirection()
|
|
{
|
|
if (currentTarget == null)
|
|
{
|
|
MovementDirection = Vector2.zero;
|
|
return;
|
|
}
|
|
|
|
Vector2 direction =
|
|
(Vector2)currentTarget.position -
|
|
(Vector2)transform.position;
|
|
|
|
if (direction.sqrMagnitude < 0.001f)
|
|
{
|
|
MovementDirection = Vector2.zero;
|
|
return;
|
|
}
|
|
|
|
MovementDirection = direction.normalized;
|
|
}
|
|
|
|
|
|
// GEGNER BEWEGEN
|
|
//
|
|
|
|
private void MoveTowardsTarget()
|
|
{
|
|
if (currentTarget == null ||
|
|
separation == null)
|
|
return;
|
|
|
|
Vector2 currentPosition =
|
|
ClampPositionToBase(transform.position);
|
|
|
|
transform.position = currentPosition;
|
|
|
|
UpdateMovementDirection();
|
|
|
|
if (MovementDirection.sqrMagnitude < 0.001f)
|
|
return;
|
|
|
|
float movementDistance =
|
|
moveSpeed * Time.fixedDeltaTime;
|
|
|
|
Vector2 safeDirection;
|
|
|
|
bool canMove =
|
|
separation.TryGetSafeDirection(
|
|
MovementDirection,
|
|
movementDistance,
|
|
out safeDirection
|
|
);
|
|
|
|
if (!canMove)
|
|
{
|
|
MovementDirection = Vector2.zero;
|
|
return;
|
|
}
|
|
|
|
Vector2 nextPosition =
|
|
currentPosition +
|
|
safeDirection * movementDistance;
|
|
|
|
nextPosition =
|
|
ClampPositionToBase(nextPosition);
|
|
|
|
transform.position = nextPosition;
|
|
|
|
Vector2 actualMovement =
|
|
nextPosition - currentPosition;
|
|
|
|
if (actualMovement.sqrMagnitude > 0.0001f)
|
|
{
|
|
MovementDirection =
|
|
actualMovement.normalized;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// TARGET ERREICHT?
|
|
//
|
|
|
|
private bool HasReachedTarget()
|
|
{
|
|
if (currentTarget == null)
|
|
return false;
|
|
|
|
return Vector2.Distance(
|
|
transform.position,
|
|
currentTarget.position
|
|
) <= targetReachedDistance;
|
|
}
|
|
}
|