enemy ai
This commit is contained in:
@@ -38,9 +38,11 @@ namespace MegaKoop.Game.Enemy
|
||||
[SerializeField] private SteamNetworkTransform networkTransform;
|
||||
|
||||
private SteamCoopNetworkManager networkManager;
|
||||
private Transform currentTarget;
|
||||
private Transform currentTargetTransform;
|
||||
private NetworkIdentity currentTargetIdentity;
|
||||
private Health currentTargetHealth;
|
||||
private Vector3 lastTargetPosition;
|
||||
private Vector3 lastKnownTargetPosition;
|
||||
private float retargetTimer;
|
||||
private Vector3 spawnPosition;
|
||||
private bool cachedNavAgentState;
|
||||
@@ -83,6 +85,7 @@ namespace MegaKoop.Game.Enemy
|
||||
}
|
||||
|
||||
lastTargetPosition = Vector3.positiveInfinity;
|
||||
lastKnownTargetPosition = spawnPosition;
|
||||
retargetTimer = Random.Range(0f, Mathf.Max(0.05f, retargetInterval));
|
||||
}
|
||||
|
||||
@@ -121,7 +124,7 @@ namespace MegaKoop.Game.Enemy
|
||||
AcquireTarget();
|
||||
}
|
||||
|
||||
if (currentTarget == null)
|
||||
if (!TryGetTargetPosition(out _))
|
||||
{
|
||||
HandleIdle();
|
||||
return;
|
||||
@@ -164,7 +167,7 @@ namespace MegaKoop.Game.Enemy
|
||||
|
||||
private bool IsTargetValid()
|
||||
{
|
||||
if (currentTarget == null)
|
||||
if (currentTargetTransform == null && currentTargetIdentity == null && currentTargetHealth == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -174,7 +177,12 @@ namespace MegaKoop.Game.Enemy
|
||||
return false;
|
||||
}
|
||||
|
||||
float sqrDistance = (currentTarget.position - transform.position).sqrMagnitude;
|
||||
if (!TryGetTargetPosition(out Vector3 targetPosition))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
float sqrDistance = (targetPosition - transform.position).sqrMagnitude;
|
||||
if (detectionRadius > 0f && sqrDistance > detectionRadius * detectionRadius)
|
||||
{
|
||||
return false;
|
||||
@@ -182,7 +190,7 @@ namespace MegaKoop.Game.Enemy
|
||||
|
||||
if (leashDistance > 0f)
|
||||
{
|
||||
float sqrLeash = (currentTarget.position - spawnPosition).sqrMagnitude;
|
||||
float sqrLeash = (targetPosition - spawnPosition).sqrMagnitude;
|
||||
if (sqrLeash > leashDistance * leashDistance)
|
||||
{
|
||||
return false;
|
||||
@@ -195,8 +203,7 @@ namespace MegaKoop.Game.Enemy
|
||||
private void AcquireTarget()
|
||||
{
|
||||
retargetTimer = retargetInterval;
|
||||
currentTarget = null;
|
||||
currentTargetHealth = null;
|
||||
ClearTarget();
|
||||
float bestScore = float.MaxValue;
|
||||
|
||||
SharedHealthBuffer.Clear();
|
||||
@@ -215,7 +222,18 @@ namespace MegaKoop.Game.Enemy
|
||||
}
|
||||
|
||||
Transform candidateTransform = candidate.transform;
|
||||
float sqrDistance = (candidateTransform.position - transform.position).sqrMagnitude;
|
||||
NetworkIdentity candidateIdentity = candidate.GetComponent<NetworkIdentity>();
|
||||
Vector3 candidatePosition;
|
||||
if (candidateIdentity != null && SteamCharacterStateCache.TryGetState(candidateIdentity.NetworkId, out var state))
|
||||
{
|
||||
candidatePosition = state.Position;
|
||||
}
|
||||
else
|
||||
{
|
||||
candidatePosition = candidateTransform.position;
|
||||
}
|
||||
|
||||
float sqrDistance = (candidatePosition - transform.position).sqrMagnitude;
|
||||
if (detectionRadius > 0f && sqrDistance > detectionRadius * detectionRadius)
|
||||
{
|
||||
continue;
|
||||
@@ -224,12 +242,15 @@ namespace MegaKoop.Game.Enemy
|
||||
if (sqrDistance < bestScore)
|
||||
{
|
||||
bestScore = sqrDistance;
|
||||
currentTarget = candidateTransform;
|
||||
currentTargetTransform = candidateTransform;
|
||||
currentTargetIdentity = candidateIdentity;
|
||||
currentTargetHealth = candidate;
|
||||
lastKnownTargetPosition = candidatePosition;
|
||||
lastTargetPosition = Vector3.positiveInfinity;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentTarget == null)
|
||||
if (currentTargetTransform == null && currentTargetIdentity == null)
|
||||
{
|
||||
lastTargetPosition = Vector3.positiveInfinity;
|
||||
}
|
||||
@@ -239,7 +260,11 @@ namespace MegaKoop.Game.Enemy
|
||||
|
||||
private void TickMovement(float deltaTime)
|
||||
{
|
||||
Vector3 targetPosition = currentTarget.position;
|
||||
if (!TryGetTargetPosition(out Vector3 targetPosition))
|
||||
{
|
||||
ClearTarget();
|
||||
return;
|
||||
}
|
||||
|
||||
if (navMeshAgent != null && navMeshAgent.enabled && navMeshAgent.isOnNavMesh)
|
||||
{
|
||||
@@ -255,6 +280,7 @@ namespace MegaKoop.Game.Enemy
|
||||
else
|
||||
{
|
||||
ManualMove(targetPosition, deltaTime);
|
||||
lastTargetPosition = targetPosition;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,7 +304,12 @@ namespace MegaKoop.Game.Enemy
|
||||
|
||||
private void FaceTarget(float deltaTime)
|
||||
{
|
||||
Vector3 toTarget = currentTarget.position - transform.position;
|
||||
if (!TryGetTargetPosition(out Vector3 targetPosition))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 toTarget = targetPosition - transform.position;
|
||||
toTarget.y = 0f;
|
||||
if (toTarget.sqrMagnitude < 0.0001f)
|
||||
{
|
||||
@@ -314,6 +345,47 @@ namespace MegaKoop.Game.Enemy
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryGetTargetPosition(out Vector3 position)
|
||||
{
|
||||
if (currentTargetIdentity != null && SteamCharacterStateCache.TryGetState(currentTargetIdentity.NetworkId, out var state))
|
||||
{
|
||||
position = state.Position;
|
||||
lastKnownTargetPosition = position;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (currentTargetTransform != null)
|
||||
{
|
||||
position = currentTargetTransform.position;
|
||||
lastKnownTargetPosition = position;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (currentTargetHealth != null)
|
||||
{
|
||||
position = currentTargetHealth.transform.position;
|
||||
lastKnownTargetPosition = position;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (lastKnownTargetPosition != Vector3.positiveInfinity)
|
||||
{
|
||||
position = lastKnownTargetPosition;
|
||||
return true;
|
||||
}
|
||||
|
||||
position = Vector3.zero;
|
||||
return false;
|
||||
}
|
||||
|
||||
private void ClearTarget()
|
||||
{
|
||||
currentTargetTransform = null;
|
||||
currentTargetIdentity = null;
|
||||
currentTargetHealth = null;
|
||||
lastKnownTargetPosition = Vector3.positiveInfinity;
|
||||
}
|
||||
|
||||
private void FacePoint(Vector3 point, float deltaTime)
|
||||
{
|
||||
Vector3 toPoint = point - transform.position;
|
||||
|
||||
Reference in New Issue
Block a user