458 lines
11 KiB
C#
458 lines
11 KiB
C#
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Enemy))]
|
|
public class EnemySeparation : MonoBehaviour
|
|
{
|
|
[Header("Separation")]
|
|
[SerializeField]
|
|
private CircleCollider2D separationCollider;
|
|
|
|
[SerializeField]
|
|
private LayerMask separationLayer;
|
|
|
|
[SerializeField]
|
|
[Min(0.1f)]
|
|
private float obstacleDetectionRadius = 1.5f;
|
|
|
|
[SerializeField]
|
|
[Min(0f)]
|
|
private float separationSafetyMargin = 0.01f;
|
|
|
|
|
|
/*[Header("Movement Evaluation")]
|
|
[SerializeField]
|
|
private float movementProbeDistance = 0.25f;
|
|
|
|
[SerializeField]
|
|
private float overlapPenaltyWeight = 3f;*/
|
|
|
|
|
|
[Header("Avoidance")]
|
|
[SerializeField]
|
|
[Range(0f, 80f)]
|
|
private float minAvoidanceAngle = 15f;
|
|
|
|
[SerializeField]
|
|
[Range(0f, 80f)]
|
|
private float maxAvoidanceAngle = 45f;
|
|
|
|
/*[SerializeField]
|
|
[Min(0f)]
|
|
private float avoidanceStrength = 1.5f;*/
|
|
|
|
|
|
[Header("Overlap Resolution")]
|
|
[SerializeField]
|
|
[Min(0.001f)]
|
|
private float exactOverlapThreshold = 0.02f;
|
|
|
|
[SerializeField]
|
|
[Min(0.001f)]
|
|
private float minimumSeparationProgress = 0.001f;
|
|
|
|
|
|
private Enemy enemy;
|
|
|
|
private Collider2D[] nearbyEnemies =
|
|
new Collider2D[64];
|
|
|
|
private Enemy[] detectedEnemies =
|
|
new Enemy[64];
|
|
|
|
private EnemySeparation[] detectedSeparations =
|
|
new EnemySeparation[64];
|
|
|
|
private int detectedCount;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
enemy = GetComponent<Enemy>();
|
|
|
|
if (separationCollider == null)
|
|
{
|
|
Debug.LogError(
|
|
"EnemySeparation: Kein SeparationCollider zugewiesen!",
|
|
this
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
// ÖFFENTLICHE BEWEGUNGSSCHNITTSTELLE
|
|
//
|
|
|
|
public bool TryGetSafeDirection(
|
|
Vector2 preferredDirection,
|
|
float movementDistance,
|
|
out Vector2 safeDirection)
|
|
{
|
|
safeDirection = Vector2.zero;
|
|
|
|
if (separationCollider == null ||
|
|
!separationCollider.enabled ||
|
|
enemy.IsDead ||
|
|
preferredDirection.sqrMagnitude < 0.001f)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
CollectNearbyEnemies();
|
|
|
|
if (detectedCount == 0)
|
|
{
|
|
safeDirection = preferredDirection.normalized;
|
|
return true;
|
|
}
|
|
|
|
// Prüfen, ob eine exakte Überlappung vorliegt.
|
|
if (HasExactOverlap())
|
|
{
|
|
return ResolveExactOverlap(
|
|
preferredDirection,
|
|
movementDistance,
|
|
out safeDirection
|
|
);
|
|
}
|
|
|
|
return FindSafeMovementDirection(
|
|
preferredDirection,
|
|
movementDistance,
|
|
out safeDirection
|
|
);
|
|
}
|
|
|
|
|
|
// GEGNER IN DER UMGEBUNG ERMITTELN
|
|
//
|
|
|
|
private void CollectNearbyEnemies()
|
|
{
|
|
detectedCount = 0;
|
|
|
|
Vector2 ownCenter =
|
|
GetWorldCenter(separationCollider);
|
|
|
|
float ownRadius =
|
|
GetWorldRadius(separationCollider);
|
|
|
|
ContactFilter2D filter =
|
|
new ContactFilter2D();
|
|
|
|
filter.SetLayerMask(separationLayer);
|
|
filter.useTriggers = true;
|
|
|
|
int count = Physics2D.OverlapCircle(
|
|
ownCenter,
|
|
obstacleDetectionRadius + ownRadius,
|
|
filter,
|
|
nearbyEnemies
|
|
);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Collider2D other = nearbyEnemies[i];
|
|
|
|
if (other == null ||
|
|
other == separationCollider)
|
|
continue;
|
|
|
|
Enemy otherEnemy =
|
|
other.GetComponentInParent<Enemy>();
|
|
|
|
if (otherEnemy == null ||
|
|
otherEnemy == enemy ||
|
|
otherEnemy.IsDead)
|
|
continue;
|
|
|
|
// Jeden Gegner nur einmal erfassen.
|
|
bool alreadyAdded = false;
|
|
|
|
for (int j = 0; j < detectedCount; j++)
|
|
{
|
|
if (detectedEnemies[j] == otherEnemy)
|
|
{
|
|
alreadyAdded = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (alreadyAdded)
|
|
continue;
|
|
|
|
EnemySeparation otherSeparation =
|
|
otherEnemy.GetComponent<EnemySeparation>();
|
|
|
|
if (otherSeparation == null ||
|
|
otherSeparation.separationCollider == null)
|
|
continue;
|
|
|
|
if (detectedCount >= detectedEnemies.Length)
|
|
break;
|
|
|
|
detectedEnemies[detectedCount] = otherEnemy;
|
|
|
|
detectedSeparations[detectedCount] =
|
|
otherSeparation;
|
|
|
|
detectedCount++;
|
|
}
|
|
}
|
|
|
|
|
|
// EXAKTE ÜBERLAPPUNG ERKENNEN
|
|
//
|
|
|
|
private bool HasExactOverlap()
|
|
{
|
|
Vector2 ownCenter =
|
|
GetWorldCenter(separationCollider);
|
|
|
|
for (int i = 0; i < detectedCount; i++)
|
|
{
|
|
EnemySeparation other =
|
|
detectedSeparations[i];
|
|
|
|
float distance = Vector2.Distance(
|
|
ownCenter,
|
|
GetWorldCenter(other.separationCollider)
|
|
);
|
|
|
|
if (distance <= exactOverlapThreshold)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
// ENTWIRRUNG EXAKT ÜBEREINANDERLIEGENDER GEGNER
|
|
//
|
|
|
|
private bool ResolveExactOverlap(
|
|
Vector2 preferredDirection,
|
|
float movementDistance,
|
|
out Vector2 safeDirection)
|
|
{
|
|
safeDirection = Vector2.zero;
|
|
|
|
// Der Gegner mit der kleinsten Instance-ID
|
|
// erhält zunächst die Bewegungsfreigabe.
|
|
int ownID = GetInstanceID();
|
|
|
|
for (int i = 0; i < detectedCount; i++)
|
|
{
|
|
EnemySeparation other =
|
|
detectedSeparations[i];
|
|
|
|
Vector2 ownCenter =
|
|
GetWorldCenter(separationCollider);
|
|
|
|
Vector2 otherCenter =
|
|
GetWorldCenter(other.separationCollider);
|
|
|
|
float distance =
|
|
Vector2.Distance(ownCenter, otherCenter);
|
|
|
|
if (distance <= exactOverlapThreshold &&
|
|
other.GetInstanceID() < ownID)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Der priorisierte Gegner sucht eine Richtung,
|
|
// die den Abstand zu allen überlappenden Gegnern
|
|
// vergrößert.
|
|
return FindSafeMovementDirection(
|
|
preferredDirection,
|
|
movementDistance,
|
|
out safeDirection
|
|
);
|
|
}
|
|
|
|
// SICHERE BEWEGUNGSRICHTUNG FINDEN
|
|
//
|
|
|
|
private bool FindSafeMovementDirection(
|
|
Vector2 preferredDirection,
|
|
float movementDistance,
|
|
out Vector2 safeDirection)
|
|
{
|
|
safeDirection = Vector2.zero;
|
|
|
|
Vector2 ownCenter =
|
|
GetWorldCenter(separationCollider);
|
|
|
|
float bestScore = float.MinValue;
|
|
|
|
// Zunächst die bevorzugte Richtung,
|
|
// anschließend verschiedene Ausweichwinkel testen.
|
|
for (int angle = -Mathf.CeilToInt(maxAvoidanceAngle);
|
|
angle <= Mathf.CeilToInt(maxAvoidanceAngle);
|
|
angle += 5)
|
|
{
|
|
if (Mathf.Abs(angle) > 0 &&
|
|
Mathf.Abs(angle) < minAvoidanceAngle)
|
|
continue;
|
|
|
|
Vector2 direction =
|
|
Quaternion.Euler(0f, 0f, angle) *
|
|
preferredDirection.normalized;
|
|
|
|
Vector2 candidatePosition =
|
|
ownCenter + direction * movementDistance;
|
|
|
|
if (!IsCandidateSafe(
|
|
candidatePosition,
|
|
ownCenter))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float alignment =
|
|
Vector2.Dot(
|
|
direction,
|
|
preferredDirection.normalized
|
|
);
|
|
|
|
float score = alignment;
|
|
|
|
if (score > bestScore)
|
|
{
|
|
bestScore = score;
|
|
safeDirection = direction;
|
|
}
|
|
}
|
|
|
|
return safeDirection.sqrMagnitude > 0.001f;
|
|
}
|
|
|
|
|
|
// BEWEGUNG GEGEN ALLE GEGNER PRÜFEN
|
|
//
|
|
|
|
|
|
private bool IsCandidateSafe(
|
|
Vector2 candidateCenter,
|
|
Vector2 currentCenter)
|
|
{
|
|
float ownRadius =
|
|
GetWorldRadius(separationCollider);
|
|
|
|
Vector2 movement =
|
|
candidateCenter - currentCenter;
|
|
|
|
for (int i = 0; i < detectedCount; i++)
|
|
{
|
|
EnemySeparation other =
|
|
detectedSeparations[i];
|
|
|
|
Vector2 otherCenter =
|
|
GetWorldCenter(other.separationCollider);
|
|
|
|
float otherRadius =
|
|
GetWorldRadius(other.separationCollider);
|
|
|
|
float allowedDistance =
|
|
ownRadius +
|
|
otherRadius +
|
|
separationSafetyMargin;
|
|
|
|
Vector2 offset =
|
|
currentCenter - otherCenter;
|
|
|
|
float currentDistance =
|
|
offset.magnitude;
|
|
|
|
float candidateDistance =
|
|
Vector2.Distance(
|
|
candidateCenter,
|
|
otherCenter
|
|
);
|
|
|
|
// Bereits bestehende Überlappung:
|
|
if (currentDistance < allowedDistance)
|
|
{
|
|
// Bei exakt übereinanderliegenden Gegnern
|
|
// reicht jede tatsächliche Entfernung.
|
|
if (currentDistance <= exactOverlapThreshold)
|
|
{
|
|
if (candidateDistance <
|
|
currentDistance + minimumSeparationProgress)
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
// Bei einer normalen Überlappung muss
|
|
// die Bewegung vom anderen Gegner wegführen.
|
|
if (Vector2.Dot(movement, offset) <= 0f)
|
|
return false;
|
|
|
|
if (candidateDistance <
|
|
currentDistance + minimumSeparationProgress)
|
|
return false;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
// Noch keine Überlappung:
|
|
// Auch den gesamten Bewegungsweg überprüfen.
|
|
float movementLengthSquared =
|
|
movement.sqrMagnitude;
|
|
|
|
if (movementLengthSquared > 0.000001f)
|
|
{
|
|
float t = Mathf.Clamp01(
|
|
Vector2.Dot(
|
|
otherCenter - currentCenter,
|
|
movement
|
|
) / movementLengthSquared
|
|
);
|
|
|
|
Vector2 closestPoint =
|
|
currentCenter + movement * t;
|
|
|
|
float closestDistance =
|
|
Vector2.Distance(
|
|
closestPoint,
|
|
otherCenter
|
|
);
|
|
|
|
if (closestDistance < allowedDistance)
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// COLLIDER-HILFSFUNKTIONEN
|
|
//
|
|
|
|
private Vector2 GetWorldCenter(
|
|
CircleCollider2D collider)
|
|
{
|
|
return collider.transform.TransformPoint(
|
|
collider.offset
|
|
);
|
|
}
|
|
|
|
|
|
private float GetWorldRadius(
|
|
CircleCollider2D collider)
|
|
{
|
|
Vector3 scale =
|
|
collider.transform.lossyScale;
|
|
|
|
float maxScale = Mathf.Max(
|
|
Mathf.Abs(scale.x),
|
|
Mathf.Abs(scale.y)
|
|
);
|
|
|
|
return collider.radius * maxScale;
|
|
}
|
|
}
|