enemy ai
This commit is contained in:
@@ -19,6 +19,9 @@ namespace MegaKoop.Game.Enemy
|
||||
public class SteamEnemyController : MonoBehaviour
|
||||
{
|
||||
private static readonly List<Health> SharedHealthBuffer = new(32);
|
||||
private static int nextEnemyNetworkId = StartingEnemyNetworkId;
|
||||
|
||||
private const int StartingEnemyNetworkId = 10000;
|
||||
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float moveSpeed = 3.5f;
|
||||
@@ -49,6 +52,7 @@ namespace MegaKoop.Game.Enemy
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
EnsureIdentity();
|
||||
spawnPosition = transform.position;
|
||||
|
||||
if (navMeshAgent == null)
|
||||
@@ -66,11 +70,6 @@ namespace MegaKoop.Game.Enemy
|
||||
health = GetComponent<Health>();
|
||||
}
|
||||
|
||||
if (identity == null)
|
||||
{
|
||||
identity = GetComponent<NetworkIdentity>();
|
||||
}
|
||||
|
||||
if (networkTransform == null)
|
||||
{
|
||||
networkTransform = GetComponent<SteamNetworkTransform>();
|
||||
@@ -89,6 +88,30 @@ namespace MegaKoop.Game.Enemy
|
||||
retargetTimer = Random.Range(0f, Mathf.Max(0.05f, retargetInterval));
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
private static void ResetNetworkIdCounter()
|
||||
{
|
||||
nextEnemyNetworkId = StartingEnemyNetworkId;
|
||||
}
|
||||
|
||||
private void EnsureIdentity()
|
||||
{
|
||||
if (identity == null)
|
||||
{
|
||||
identity = GetComponent<NetworkIdentity>();
|
||||
}
|
||||
|
||||
if (identity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (identity.NetworkId == 0)
|
||||
{
|
||||
identity.SetNetworkId(nextEnemyNetworkId++);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
networkManager = SteamCoopNetworkManager.Instance;
|
||||
|
||||
2
Game/Scripts/Networking/SteamCharacterStateCache.cs.meta
Normal file
2
Game/Scripts/Networking/SteamCharacterStateCache.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a79d3f26acea4110aa80d048994e8b93
|
||||
@@ -10,6 +10,8 @@ namespace MegaKoop.Game.Networking
|
||||
|
||||
private SteamCoopNetworkManager networkManager;
|
||||
private bool isRegistered;
|
||||
private bool hasPendingBroadcast;
|
||||
private float pendingNormalizedHealth;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -37,6 +39,13 @@ namespace MegaKoop.Game.Networking
|
||||
{
|
||||
health.NormalizedHealthChanged += OnHealthChanged;
|
||||
}
|
||||
|
||||
if (health != null && identity != null && IsAuthority())
|
||||
{
|
||||
QueueBroadcast(health.CurrentHealth / health.MaxHealth);
|
||||
}
|
||||
|
||||
FlushPendingBroadcast();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
@@ -51,6 +60,8 @@ namespace MegaKoop.Game.Networking
|
||||
{
|
||||
health.NormalizedHealthChanged -= OnHealthChanged;
|
||||
}
|
||||
|
||||
networkManager = null;
|
||||
}
|
||||
|
||||
private bool IsAuthority()
|
||||
@@ -65,9 +76,8 @@ namespace MegaKoop.Game.Networking
|
||||
return;
|
||||
}
|
||||
|
||||
var message = new HealthSyncMessage(identity.NetworkId, normalized);
|
||||
byte[] payload = HealthSyncMessage.Serialize(message);
|
||||
networkManager.SendToAll(NetworkMessageType.HealthSync, payload, Steamworks.EP2PSend.k_EP2PSendReliable);
|
||||
QueueBroadcast(normalized);
|
||||
FlushPendingBroadcast();
|
||||
}
|
||||
|
||||
private void HandleHealthSync(NetworkMessage message)
|
||||
@@ -85,5 +95,44 @@ namespace MegaKoop.Game.Networking
|
||||
|
||||
health.ForceSetNormalizedHealth(syncMessage.NormalizedHealth);
|
||||
}
|
||||
|
||||
private void QueueBroadcast(float normalized)
|
||||
{
|
||||
pendingNormalizedHealth = normalized;
|
||||
hasPendingBroadcast = true;
|
||||
}
|
||||
|
||||
private void FlushPendingBroadcast()
|
||||
{
|
||||
if (!hasPendingBroadcast)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
networkManager ??= SteamCoopNetworkManager.Instance;
|
||||
if (networkManager == null || identity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!IsAuthority())
|
||||
{
|
||||
hasPendingBroadcast = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var message = new HealthSyncMessage(identity.NetworkId, pendingNormalizedHealth);
|
||||
byte[] payload = HealthSyncMessage.Serialize(message);
|
||||
networkManager.SendToAll(NetworkMessageType.HealthSync, payload, Steamworks.EP2PSend.k_EP2PSendReliable);
|
||||
hasPendingBroadcast = false;
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (hasPendingBroadcast)
|
||||
{
|
||||
FlushPendingBroadcast();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ namespace MegaKoop.Game.Networking
|
||||
}
|
||||
else if (trackedRigidbody != null)
|
||||
{
|
||||
velocity = trackedRigidbody.velocity;
|
||||
velocity = trackedRigidbody.linearVelocity;
|
||||
}
|
||||
|
||||
SteamCharacterStateCache.ReportLocalState(identity.NetworkId, targetTransform.position, targetTransform.rotation, velocity);
|
||||
|
||||
Reference in New Issue
Block a user