1271 lines
29 KiB
C#
1271 lines
29 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class Enemy : MonoBehaviour, IMovementDirection
|
|
{
|
|
[Header("Movement")]
|
|
[SerializeField]
|
|
private float moveSpeed = 0.5f;
|
|
|
|
[SerializeField]
|
|
private float targetReachedDistance = 0.1f;
|
|
|
|
private EnemyTargetManager targetManager;
|
|
private Transform currentTarget;
|
|
|
|
// Wird von CharacterRotation verwendet.
|
|
public Vector2 MovementDirection { get; private set; }
|
|
|
|
|
|
[Header("Health")]
|
|
[SerializeField]
|
|
private float maxHealth = 100f;
|
|
|
|
private float currentHealth;
|
|
|
|
public float CurrentHealth => currentHealth;
|
|
|
|
public bool IsDead { get; private set; }
|
|
|
|
|
|
[Header("Damage Effect")]
|
|
[SerializeField]
|
|
private SpriteRenderer spriteRenderer;
|
|
|
|
[SerializeField]
|
|
private Color damageColor = Color.red;
|
|
|
|
[SerializeField]
|
|
private float damageFlashDuration = 0.15f;
|
|
|
|
private Color originalColor;
|
|
private Coroutine damageFlashCoroutine;
|
|
|
|
|
|
[Header("Enemy Separation")]
|
|
[SerializeField]
|
|
private CircleCollider2D separationCollider;
|
|
|
|
[SerializeField]
|
|
private LayerMask separationLayer;
|
|
|
|
[SerializeField]
|
|
[Min(0.1f)]
|
|
private float obstacleDetectionRadius = 1.5f;
|
|
|
|
[SerializeField]
|
|
[Min(0f)]
|
|
private float avoidanceReleaseMargin = 0.15f;
|
|
|
|
[SerializeField]
|
|
[Min(0f)]
|
|
private float avoidanceStrength = 1.5f;
|
|
|
|
|
|
[Header("Random Avoidance")]
|
|
[SerializeField]
|
|
[Range(0f, 80f)]
|
|
private float minAvoidanceAngle = 15f;
|
|
|
|
[SerializeField]
|
|
[Range(0f, 80f)]
|
|
private float maxAvoidanceAngle = 45f;
|
|
|
|
|
|
[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 Validation")]
|
|
[SerializeField]
|
|
[Min(0f)]
|
|
private float minimumProgress = 0.001f;
|
|
|
|
[SerializeField]
|
|
[Min(0f)]
|
|
private float movementCheckMargin = 0.01f;
|
|
|
|
[Header("Movement Evaluation")]
|
|
[SerializeField]
|
|
private float movementProbeDistance = 0.25f;
|
|
|
|
[SerializeField]
|
|
private float separationSafetyMargin = 0.01f;
|
|
|
|
[SerializeField]
|
|
private float overlapPenaltyWeight = 3f;
|
|
|
|
|
|
private Collider2D[] nearbyEnemies =
|
|
new Collider2D[32];
|
|
|
|
private Collider2D[] sideCheckEnemies =
|
|
new Collider2D[32];
|
|
|
|
private Enemy blockingEnemy;
|
|
|
|
private bool isAvoiding;
|
|
private float avoidanceSide;
|
|
|
|
private Vector2 avoidanceForwardDirection;
|
|
private Vector2 currentAvoidanceDirection;
|
|
|
|
private float progressCheckTimer;
|
|
private float previousTargetDistance;
|
|
private float noProgressTimer;
|
|
|
|
private int avoidanceAttempts;
|
|
|
|
private CharacterRotation characterRotation;
|
|
|
|
private CircleCollider2D baseCollider;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
currentHealth = maxHealth;
|
|
|
|
// Visual und Rotation finden.
|
|
if (spriteRenderer == null)
|
|
{
|
|
spriteRenderer =
|
|
GetComponentInChildren<SpriteRenderer>();
|
|
}
|
|
|
|
if (spriteRenderer != null)
|
|
{
|
|
originalColor = spriteRenderer.color;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning(
|
|
"Enemy: Kein SpriteRenderer gefunden!",
|
|
this
|
|
);
|
|
}
|
|
|
|
characterRotation =
|
|
GetComponent<CharacterRotation>();
|
|
|
|
if (separationCollider == null)
|
|
{
|
|
Debug.LogError(
|
|
"Enemy: Kein SeparationCollider zugewiesen!",
|
|
this
|
|
);
|
|
}
|
|
|
|
targetManager =
|
|
FindFirstObjectByType<EnemyTargetManager>();
|
|
|
|
if (targetManager == null)
|
|
{
|
|
Debug.LogError(
|
|
"Enemy: Kein EnemyTargetManager gefunden!",
|
|
this
|
|
);
|
|
}
|
|
|
|
// Basis-Collider automatisch über den Layer finden.
|
|
if (baseCollider == null)
|
|
{
|
|
int baseLayer = LayerMask.NameToLayer("Base");
|
|
|
|
if (baseLayer != -1)
|
|
{
|
|
Collider2D[] colliders =
|
|
FindObjectsByType<Collider2D>(
|
|
FindObjectsSortMode.None
|
|
);
|
|
|
|
foreach (Collider2D col in colliders)
|
|
{
|
|
if (col.gameObject.layer == baseLayer &&
|
|
col is CircleCollider2D circle)
|
|
{
|
|
baseCollider = circle;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (baseCollider == null)
|
|
{
|
|
Debug.LogError(
|
|
"Enemy: Kein CircleCollider2D auf Layer Base gefunden!",
|
|
this
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
// Bereits vor der ersten Bewegung ausrichten.
|
|
if (characterRotation != null)
|
|
{
|
|
characterRotation.SetInitialRotation();
|
|
}
|
|
}
|
|
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (targetManager == null || IsDead)
|
|
return;
|
|
|
|
if (currentTarget == null)
|
|
{
|
|
MovementDirection = Vector2.zero;
|
|
|
|
ResetAvoidance();
|
|
|
|
SelectClosestTarget();
|
|
|
|
return;
|
|
}
|
|
|
|
if (!currentTarget.gameObject.activeInHierarchy)
|
|
{
|
|
SelectClosestTarget();
|
|
return;
|
|
}
|
|
|
|
Physics2D.SyncTransforms();
|
|
|
|
MoveTowardsTarget();
|
|
|
|
UpdateAvoidanceProgress();
|
|
|
|
if (HasReachedTarget())
|
|
{
|
|
SelectClosestTarget();
|
|
}
|
|
}
|
|
|
|
|
|
// BEWEGUNGSLOGIK
|
|
//
|
|
|
|
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)
|
|
);
|
|
|
|
// Kleiner Sicherheitsabstand zur Basisgrenze.
|
|
float allowedRadius = Mathf.Max(
|
|
0f,
|
|
baseRadius - 0.02f
|
|
);
|
|
|
|
Vector2 offset = position - baseCenter;
|
|
|
|
if (offset.sqrMagnitude >
|
|
allowedRadius * allowedRadius)
|
|
{
|
|
position =
|
|
baseCenter +
|
|
offset.normalized * allowedRadius;
|
|
}
|
|
|
|
return position;
|
|
}
|
|
|
|
private bool IsMovementAllowed(
|
|
Vector2 nextPosition)
|
|
{
|
|
if (baseCollider == null)
|
|
return false;
|
|
|
|
// Position muss innerhalb der Basis liegen.
|
|
Vector2 clampedPosition =
|
|
ClampPositionToBase(nextPosition);
|
|
|
|
if (Vector2.Distance(
|
|
clampedPosition,
|
|
nextPosition
|
|
) > 0.001f)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Prüfen, ob wir dadurch einem Gegner zu nahe kommen.
|
|
if (separationCollider != null)
|
|
{
|
|
Vector2 ownCenter =
|
|
nextPosition +
|
|
(GetSeparationCenter(separationCollider) -
|
|
(Vector2)transform.position);
|
|
|
|
float ownRadius =
|
|
GetWorldRadius(separationCollider);
|
|
|
|
ContactFilter2D filter =
|
|
new ContactFilter2D();
|
|
|
|
filter.SetLayerMask(separationLayer);
|
|
filter.useTriggers = true;
|
|
|
|
int count = Physics2D.OverlapCircle(
|
|
ownCenter,
|
|
ownRadius + movementCheckMargin,
|
|
filter,
|
|
nearbyEnemies
|
|
);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Collider2D other = nearbyEnemies[i];
|
|
|
|
if (other == null)
|
|
continue;
|
|
|
|
Enemy enemy =
|
|
other.GetComponentInParent<Enemy>();
|
|
|
|
if (enemy == null ||
|
|
enemy == this ||
|
|
enemy.IsDead ||
|
|
enemy.separationCollider == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float otherRadius =
|
|
GetWorldRadius(enemy.separationCollider);
|
|
|
|
Vector2 otherCenter =
|
|
GetSeparationCenter(
|
|
enemy.separationCollider
|
|
);
|
|
|
|
float allowedDistance =
|
|
ownRadius +
|
|
otherRadius +
|
|
movementCheckMargin;
|
|
|
|
if (Vector2.Distance(
|
|
ownCenter,
|
|
otherCenter
|
|
) < allowedDistance)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Während eines Ausweichmanövers darf auch
|
|
// eine seitliche Bewegung stattfinden.
|
|
if (isAvoiding)
|
|
return true;
|
|
|
|
// Bei normaler Bewegung muss Fortschritt entstehen.
|
|
float currentDistance =
|
|
Vector2.Distance(
|
|
transform.position,
|
|
currentTarget.position
|
|
);
|
|
|
|
float nextDistance =
|
|
Vector2.Distance(
|
|
nextPosition,
|
|
currentTarget.position
|
|
);
|
|
|
|
return nextDistance <
|
|
currentDistance - minimumProgress;
|
|
}
|
|
|
|
private void SelectClosestTarget()
|
|
{
|
|
if (targetManager == null)
|
|
return;
|
|
|
|
currentTarget =
|
|
targetManager.GetClosestActiveTarget(
|
|
transform.position
|
|
);
|
|
|
|
ResetAvoidance();
|
|
|
|
UpdateMovementDirection();
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
Vector2 targetDirection = direction.normalized;
|
|
|
|
MovementDirection =
|
|
GetAvoidanceDirection(targetDirection);
|
|
}
|
|
|
|
|
|
|
|
private void MoveTowardsTarget()
|
|
{
|
|
if (currentTarget == null)
|
|
return;
|
|
|
|
Vector2 currentPosition =
|
|
ClampPositionToBase(transform.position);
|
|
|
|
transform.position = currentPosition;
|
|
|
|
UpdateMovementDirection();
|
|
|
|
if (MovementDirection.sqrMagnitude < 0.001f)
|
|
return;
|
|
|
|
Vector2 bestDirection =
|
|
FindBestMovementDirection(MovementDirection);
|
|
|
|
// Keine sinnvolle Bewegung möglich.
|
|
if (bestDirection.sqrMagnitude < 0.001f)
|
|
return;
|
|
|
|
float movementDistance =
|
|
moveSpeed * Time.fixedDeltaTime;
|
|
|
|
Vector2 nextPosition =
|
|
currentPosition +
|
|
bestDirection * movementDistance;
|
|
|
|
nextPosition =
|
|
ClampPositionToBase(nextPosition);
|
|
|
|
transform.position = nextPosition;
|
|
|
|
Vector2 actualMovement =
|
|
nextPosition - currentPosition;
|
|
|
|
if (actualMovement.sqrMagnitude > 0.0001f)
|
|
{
|
|
MovementDirection =
|
|
actualMovement.normalized;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private bool HasReachedTarget()
|
|
{
|
|
if (currentTarget == null)
|
|
return false;
|
|
|
|
return Vector2.Distance(
|
|
transform.position,
|
|
currentTarget.position
|
|
) <= targetReachedDistance;
|
|
}
|
|
|
|
|
|
// GEGNERABSTAND UND AUSWEICHLOGIK
|
|
//
|
|
|
|
// FORTSETZUNG IN TEIL 2
|
|
|
|
|
|
private float CalculateOverlapPenalty(Vector2 position)
|
|
{
|
|
if (separationCollider == null)
|
|
return 0f;
|
|
|
|
Vector2 centerOffset =
|
|
GetSeparationCenter(separationCollider) -
|
|
(Vector2)transform.position;
|
|
|
|
Vector2 checkCenter = position + centerOffset;
|
|
|
|
float ownRadius =
|
|
GetWorldRadius(separationCollider);
|
|
|
|
ContactFilter2D filter = new ContactFilter2D();
|
|
filter.SetLayerMask(separationLayer);
|
|
filter.useTriggers = true;
|
|
|
|
int count = Physics2D.OverlapCircle(
|
|
checkCenter,
|
|
obstacleDetectionRadius + ownRadius,
|
|
filter,
|
|
nearbyEnemies
|
|
);
|
|
|
|
float penalty = 0f;
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Collider2D other = nearbyEnemies[i];
|
|
|
|
if (other == null)
|
|
continue;
|
|
|
|
Enemy enemy =
|
|
other.GetComponentInParent<Enemy>();
|
|
|
|
if (enemy == null ||
|
|
enemy == this ||
|
|
enemy.IsDead ||
|
|
enemy.separationCollider == null)
|
|
continue;
|
|
|
|
Vector2 otherCenter =
|
|
GetSeparationCenter(
|
|
enemy.separationCollider
|
|
);
|
|
|
|
float otherRadius =
|
|
GetWorldRadius(
|
|
enemy.separationCollider
|
|
);
|
|
|
|
float allowedDistance =
|
|
ownRadius +
|
|
otherRadius +
|
|
separationSafetyMargin;
|
|
|
|
float actualDistance =
|
|
Vector2.Distance(checkCenter, otherCenter);
|
|
|
|
float overlap =
|
|
Mathf.Max(0f, allowedDistance - actualDistance);
|
|
|
|
penalty += overlap * overlap;
|
|
}
|
|
|
|
return penalty;
|
|
}
|
|
|
|
|
|
private Vector2 FindBestMovementDirection(
|
|
Vector2 preferredDirection)
|
|
{
|
|
if (preferredDirection.sqrMagnitude < 0.001f)
|
|
return Vector2.zero;
|
|
|
|
Vector2 currentPosition = transform.position;
|
|
|
|
float currentDistance =
|
|
Vector2.Distance(
|
|
currentPosition,
|
|
currentTarget.position
|
|
);
|
|
|
|
float currentPenalty =
|
|
CalculateOverlapPenalty(currentPosition);
|
|
|
|
float bestScore = float.MinValue;
|
|
Vector2 bestDirection = Vector2.zero;
|
|
|
|
float probeDistance = Mathf.Max(
|
|
movementProbeDistance,
|
|
moveSpeed * Time.fixedDeltaTime
|
|
);
|
|
|
|
// Verschiedene Bewegungswinkel ausprobieren.
|
|
for (int angle = -75; angle <= 75; angle += 15)
|
|
{
|
|
Vector2 direction =
|
|
Quaternion.Euler(0f, 0f, angle) *
|
|
preferredDirection.normalized;
|
|
|
|
Vector2 candidatePosition =
|
|
currentPosition +
|
|
direction * probeDistance;
|
|
|
|
Vector2 clampedPosition =
|
|
ClampPositionToBase(candidatePosition);
|
|
|
|
// Bewegungen außerhalb der Basis ablehnen.
|
|
if (Vector2.Distance(
|
|
candidatePosition,
|
|
clampedPosition
|
|
) > 0.001f)
|
|
continue;
|
|
|
|
float candidatePenalty =
|
|
CalculateOverlapPenalty(candidatePosition);
|
|
|
|
// Keine Bewegung wählen, die die Situation
|
|
// bezüglich anderer Gegner verschlechtert.
|
|
if (candidatePenalty > currentPenalty + 0.000001f)
|
|
continue;
|
|
|
|
float candidateDistance =
|
|
Vector2.Distance(
|
|
candidatePosition,
|
|
currentTarget.position
|
|
);
|
|
|
|
float progress =
|
|
currentDistance - candidateDistance;
|
|
|
|
float overlapImprovement =
|
|
currentPenalty - candidatePenalty;
|
|
|
|
float score =
|
|
progress +
|
|
overlapImprovement * overlapPenaltyWeight;
|
|
|
|
if (score > bestScore)
|
|
{
|
|
bestScore = score;
|
|
bestDirection = direction;
|
|
}
|
|
}
|
|
|
|
return bestDirection;
|
|
}
|
|
|
|
|
|
private Vector2 GetAvoidanceDirection(
|
|
Vector2 targetDirection)
|
|
{
|
|
if (separationCollider == null)
|
|
return targetDirection;
|
|
|
|
if (isAvoiding)
|
|
{
|
|
if (blockingEnemy == null ||
|
|
blockingEnemy.IsDead ||
|
|
blockingEnemy.separationCollider == null)
|
|
{
|
|
ResetAvoidance();
|
|
}
|
|
else if (HasPassedBlockingEnemy())
|
|
{
|
|
ResetAvoidance();
|
|
}
|
|
}
|
|
|
|
if (!isAvoiding)
|
|
{
|
|
Enemy detectedEnemy =
|
|
FindBlockingEnemy(targetDirection);
|
|
|
|
if (detectedEnemy != null)
|
|
{
|
|
BeginAvoidance(
|
|
detectedEnemy,
|
|
targetDirection
|
|
);
|
|
}
|
|
}
|
|
|
|
if (!isAvoiding)
|
|
return targetDirection;
|
|
|
|
return currentAvoidanceDirection;
|
|
}
|
|
|
|
|
|
private Enemy FindBlockingEnemy(
|
|
Vector2 targetDirection)
|
|
{
|
|
ContactFilter2D filter =
|
|
new ContactFilter2D();
|
|
|
|
filter.SetLayerMask(separationLayer);
|
|
filter.useTriggers = true;
|
|
|
|
Vector2 ownCenter =
|
|
GetSeparationCenter(separationCollider);
|
|
|
|
float ownRadius =
|
|
GetWorldRadius(separationCollider);
|
|
|
|
int count = Physics2D.OverlapCircle(
|
|
ownCenter,
|
|
obstacleDetectionRadius + ownRadius,
|
|
filter,
|
|
nearbyEnemies
|
|
);
|
|
|
|
Enemy closestEnemy = null;
|
|
float closestDistance = float.MaxValue;
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Collider2D other = nearbyEnemies[i];
|
|
|
|
if (other == null)
|
|
continue;
|
|
|
|
Enemy enemy =
|
|
other.GetComponentInParent<Enemy>();
|
|
|
|
if (enemy == null ||
|
|
enemy == this ||
|
|
enemy.IsDead ||
|
|
enemy.separationCollider == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Vector2 otherCenter =
|
|
GetSeparationCenter(
|
|
enemy.separationCollider
|
|
);
|
|
|
|
float otherRadius =
|
|
GetWorldRadius(
|
|
enemy.separationCollider
|
|
);
|
|
|
|
Vector2 offset = otherCenter - ownCenter;
|
|
|
|
float distance = offset.magnitude;
|
|
|
|
// Nur überlappende SeparationCollider beachten.
|
|
if (distance >= ownRadius + otherRadius)
|
|
continue;
|
|
|
|
if (distance < 0.001f)
|
|
continue;
|
|
|
|
float forwardDot = Vector2.Dot(
|
|
targetDirection,
|
|
offset.normalized
|
|
);
|
|
|
|
// Gegner hinter uns ignorieren.
|
|
if (forwardDot < -0.2f)
|
|
continue;
|
|
|
|
if (distance < closestDistance)
|
|
{
|
|
closestDistance = distance;
|
|
closestEnemy = enemy;
|
|
}
|
|
}
|
|
|
|
return closestEnemy;
|
|
}
|
|
|
|
|
|
private void BeginAvoidance(
|
|
Enemy enemy,
|
|
Vector2 targetDirection)
|
|
{
|
|
if (enemy == null)
|
|
return;
|
|
|
|
blockingEnemy = enemy;
|
|
|
|
avoidanceForwardDirection =
|
|
targetDirection.normalized;
|
|
|
|
avoidanceSide =
|
|
ChooseBestAvoidanceSide(
|
|
enemy,
|
|
avoidanceForwardDirection
|
|
);
|
|
|
|
GenerateAvoidanceDirection();
|
|
|
|
isAvoiding = true;
|
|
|
|
previousTargetDistance =
|
|
Vector2.Distance(
|
|
transform.position,
|
|
currentTarget.position
|
|
);
|
|
|
|
progressCheckTimer = 0f;
|
|
noProgressTimer = 0f;
|
|
avoidanceAttempts = 0;
|
|
}
|
|
|
|
|
|
private float ChooseBestAvoidanceSide(
|
|
Enemy enemy,
|
|
Vector2 targetDirection)
|
|
{
|
|
Vector2 perpendicular = new Vector2(
|
|
-targetDirection.y,
|
|
targetDirection.x
|
|
);
|
|
|
|
Vector2 ownCenter =
|
|
GetSeparationCenter(separationCollider);
|
|
|
|
Vector2 blockerCenter =
|
|
GetSeparationCenter(
|
|
enemy.separationCollider
|
|
);
|
|
|
|
float ownRadius =
|
|
GetWorldRadius(separationCollider);
|
|
|
|
float blockerRadius =
|
|
GetWorldRadius(
|
|
enemy.separationCollider
|
|
);
|
|
|
|
float sideOffset =
|
|
ownRadius + blockerRadius + 0.05f;
|
|
|
|
Vector2 leftPosition =
|
|
blockerCenter - perpendicular * sideOffset;
|
|
|
|
Vector2 rightPosition =
|
|
blockerCenter + perpendicular * sideOffset;
|
|
|
|
int leftObstacles =
|
|
CountSideObstacles(
|
|
leftPosition,
|
|
ownRadius,
|
|
enemy
|
|
);
|
|
|
|
int rightObstacles =
|
|
CountSideObstacles(
|
|
rightPosition,
|
|
ownRadius,
|
|
enemy
|
|
);
|
|
|
|
if (leftObstacles < rightObstacles)
|
|
return -1f;
|
|
|
|
if (rightObstacles < leftObstacles)
|
|
return 1f;
|
|
|
|
Vector2 blockerOffset =
|
|
blockerCenter - ownCenter;
|
|
|
|
float lateralPosition =
|
|
Vector2.Dot(
|
|
blockerOffset,
|
|
perpendicular
|
|
);
|
|
|
|
if (lateralPosition > 0.01f)
|
|
return -1f;
|
|
|
|
if (lateralPosition < -0.01f)
|
|
return 1f;
|
|
|
|
// Bei Gleichstand stabile Entscheidung.
|
|
return GetInstanceID() < enemy.GetInstanceID()
|
|
? -1f
|
|
: 1f;
|
|
}
|
|
|
|
|
|
private void GenerateAvoidanceDirection()
|
|
{
|
|
Vector2 perpendicular = new Vector2(
|
|
-avoidanceForwardDirection.y,
|
|
avoidanceForwardDirection.x
|
|
);
|
|
|
|
// Zufälliger Winkel innerhalb der gewählten Seite.
|
|
float angle = Random.Range(
|
|
minAvoidanceAngle,
|
|
maxAvoidanceAngle
|
|
);
|
|
|
|
angle *= avoidanceSide;
|
|
|
|
currentAvoidanceDirection =
|
|
Quaternion.Euler(0f, 0f, angle) *
|
|
avoidanceForwardDirection;
|
|
|
|
currentAvoidanceDirection =
|
|
(
|
|
avoidanceForwardDirection +
|
|
currentAvoidanceDirection *
|
|
avoidanceStrength
|
|
).normalized;
|
|
}
|
|
|
|
|
|
private int CountSideObstacles(
|
|
Vector2 position,
|
|
float ownRadius,
|
|
Enemy ignoredEnemy)
|
|
{
|
|
ContactFilter2D filter =
|
|
new ContactFilter2D();
|
|
|
|
filter.SetLayerMask(separationLayer);
|
|
filter.useTriggers = true;
|
|
|
|
int count = Physics2D.OverlapCircle(
|
|
position,
|
|
ownRadius + 0.1f,
|
|
filter,
|
|
sideCheckEnemies
|
|
);
|
|
|
|
int obstacleCount = 0;
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Collider2D other = sideCheckEnemies[i];
|
|
|
|
if (other == null)
|
|
continue;
|
|
|
|
Enemy enemy =
|
|
other.GetComponentInParent<Enemy>();
|
|
|
|
if (enemy == null ||
|
|
enemy == this ||
|
|
enemy == ignoredEnemy ||
|
|
enemy.IsDead ||
|
|
enemy.separationCollider == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Vector2 otherCenter =
|
|
GetSeparationCenter(
|
|
enemy.separationCollider
|
|
);
|
|
|
|
float otherRadius =
|
|
GetWorldRadius(
|
|
enemy.separationCollider
|
|
);
|
|
|
|
if (Vector2.Distance(
|
|
position,
|
|
otherCenter
|
|
) < ownRadius + otherRadius)
|
|
{
|
|
obstacleCount++;
|
|
}
|
|
}
|
|
|
|
return obstacleCount;
|
|
}
|
|
|
|
|
|
private bool HasPassedBlockingEnemy()
|
|
{
|
|
if (blockingEnemy == null ||
|
|
blockingEnemy.separationCollider == null)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
Vector2 ownCenter =
|
|
GetSeparationCenter(separationCollider);
|
|
|
|
Vector2 blockerCenter =
|
|
GetSeparationCenter(
|
|
blockingEnemy.separationCollider
|
|
);
|
|
|
|
float combinedRadius =
|
|
GetWorldRadius(separationCollider) +
|
|
GetWorldRadius(
|
|
blockingEnemy.separationCollider
|
|
);
|
|
|
|
float distance =
|
|
Vector2.Distance(
|
|
ownCenter,
|
|
blockerCenter
|
|
);
|
|
|
|
if (distance >
|
|
combinedRadius + avoidanceReleaseMargin)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
float passedDistance = Vector2.Dot(
|
|
ownCenter - blockerCenter,
|
|
avoidanceForwardDirection
|
|
);
|
|
|
|
return passedDistance > 0f &&
|
|
distance > combinedRadius * 0.8f;
|
|
}
|
|
|
|
|
|
// FESTSTECKEN ERKENNEN UND AUSWEICHEN ERNEUERN
|
|
//
|
|
|
|
private void UpdateAvoidanceProgress()
|
|
{
|
|
if (!isAvoiding || currentTarget == null)
|
|
return;
|
|
|
|
progressCheckTimer += Time.fixedDeltaTime;
|
|
|
|
if (progressCheckTimer < progressCheckInterval)
|
|
return;
|
|
|
|
float currentDistance =
|
|
Vector2.Distance(
|
|
transform.position,
|
|
currentTarget.position
|
|
);
|
|
|
|
float progress =
|
|
previousTargetDistance - currentDistance;
|
|
|
|
if (progress < minimumTargetProgress)
|
|
{
|
|
noProgressTimer += progressCheckInterval;
|
|
}
|
|
else
|
|
{
|
|
noProgressTimer = 0f;
|
|
avoidanceAttempts = 0;
|
|
}
|
|
|
|
previousTargetDistance = currentDistance;
|
|
progressCheckTimer = 0f;
|
|
|
|
if (noProgressTimer >= maxNoProgressTime)
|
|
{
|
|
ChangeAvoidanceDirection();
|
|
}
|
|
}
|
|
|
|
|
|
private void ChangeAvoidanceDirection()
|
|
{
|
|
if (blockingEnemy == null ||
|
|
blockingEnemy.separationCollider == null)
|
|
{
|
|
ResetAvoidance();
|
|
return;
|
|
}
|
|
|
|
avoidanceAttempts++;
|
|
|
|
// Bei wiederholtem Feststecken Seite wechseln.
|
|
if (avoidanceAttempts >= 2)
|
|
{
|
|
avoidanceSide =
|
|
Random.value < 0.5f ? -1f : 1f;
|
|
|
|
avoidanceAttempts = 0;
|
|
}
|
|
else
|
|
{
|
|
avoidanceSide *= -1f;
|
|
}
|
|
|
|
avoidanceForwardDirection =
|
|
((Vector2)currentTarget.position -
|
|
(Vector2)transform.position).normalized;
|
|
|
|
GenerateAvoidanceDirection();
|
|
|
|
noProgressTimer = 0f;
|
|
progressCheckTimer = 0f;
|
|
|
|
previousTargetDistance =
|
|
Vector2.Distance(
|
|
transform.position,
|
|
currentTarget.position
|
|
);
|
|
}
|
|
|
|
|
|
private Vector2 GetSeparationCenter(
|
|
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;
|
|
}
|
|
|
|
|
|
private void ResetAvoidance()
|
|
{
|
|
isAvoiding = false;
|
|
blockingEnemy = null;
|
|
avoidanceSide = 0f;
|
|
|
|
avoidanceForwardDirection = Vector2.zero;
|
|
currentAvoidanceDirection = Vector2.zero;
|
|
|
|
progressCheckTimer = 0f;
|
|
noProgressTimer = 0f;
|
|
avoidanceAttempts = 0;
|
|
}
|
|
|
|
|
|
// BASIS-KOLLISION
|
|
//
|
|
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
|
{
|
|
if (collision.gameObject.layer ==
|
|
LayerMask.NameToLayer("Base"))
|
|
{
|
|
// Gegnerbewegung an der Basis stoppen.
|
|
MovementDirection = Vector2.zero;
|
|
}
|
|
}
|
|
|
|
|
|
// LEBENSLOGIK
|
|
//
|
|
|
|
public void TakeDamage(float damage)
|
|
{
|
|
if (IsDead || damage <= 0f)
|
|
return;
|
|
|
|
currentHealth -= damage;
|
|
|
|
Debug.Log(
|
|
gameObject.name +
|
|
" erhält " + damage +
|
|
" Schaden. HP: " +
|
|
currentHealth + "/" + maxHealth
|
|
);
|
|
|
|
StartDamageFlash();
|
|
|
|
if (currentHealth <= 0f)
|
|
{
|
|
Die();
|
|
}
|
|
}
|
|
|
|
|
|
private void Die()
|
|
{
|
|
if (IsDead)
|
|
return;
|
|
|
|
IsDead = true;
|
|
|
|
Debug.Log(
|
|
gameObject.name + " wurde besiegt!"
|
|
);
|
|
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
|
|
// SCHADENSEFFEKT
|
|
//
|
|
|
|
private void StartDamageFlash()
|
|
{
|
|
if (spriteRenderer == null)
|
|
return;
|
|
|
|
if (damageFlashCoroutine != null)
|
|
{
|
|
StopCoroutine(damageFlashCoroutine);
|
|
}
|
|
|
|
damageFlashCoroutine =
|
|
StartCoroutine(DamageFlash());
|
|
}
|
|
|
|
|
|
private IEnumerator DamageFlash()
|
|
{
|
|
Color flashColor = damageColor;
|
|
flashColor.a = originalColor.a;
|
|
|
|
spriteRenderer.color = flashColor;
|
|
|
|
yield return new WaitForSeconds(
|
|
damageFlashDuration
|
|
);
|
|
|
|
spriteRenderer.color = originalColor;
|
|
|
|
damageFlashCoroutine = null;
|
|
}
|
|
|
|
|
|
// ANGRIFFSLOGIK (möglicherweise in anderem Skript)
|
|
//
|
|
|
|
} |