enemy ai
This commit is contained in:
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