Skip to content

Commit 368d719

Browse files
committed
Added Proximity to NetworkedAnimator
1 parent cf4db38 commit 368d719

File tree

1 file changed

+78
-8
lines changed

1 file changed

+78
-8
lines changed

MLAPI/MonoBehaviours/Prototyping/NetworkedAnimator.cs

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
using System.IO;
1+
using System.Collections.Generic;
2+
using System.IO;
23
using UnityEngine;
34

45
namespace MLAPI.MonoBehaviours.Prototyping
56
{
67
public class NetworkedAnimator : NetworkedBehaviour
78
{
9+
public bool EnableProximity = false;
10+
public float ProximityRange = 50f;
11+
812
[SerializeField]
913
private Animator _animator;
1014
[SerializeField]
1115
private uint parameterSendBits;
1216
[SerializeField]
13-
private float sendRate = 0.1f;
17+
private readonly float sendRate = 0.1f;
1418
private AnimatorControllerParameter[] animatorParameters;
1519

1620
private int animationHash;
@@ -98,7 +102,18 @@ private void FixedUpdate()
98102
}
99103
if(isServer)
100104
{
101-
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
105+
if(EnableProximity)
106+
{
107+
List<int> clientsInProximity = new List<int>();
108+
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
109+
{
110+
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
111+
clientsInProximity.Add(client.Key);
112+
}
113+
SendToClientsTarget(clientsInProximity ,"MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
114+
}
115+
else
116+
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
102117
}
103118
else
104119
{
@@ -156,7 +171,18 @@ private void CheckSendRate()
156171
}
157172
if (isServer)
158173
{
159-
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
174+
if (EnableProximity)
175+
{
176+
List<int> clientsInProximity = new List<int>();
177+
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
178+
{
179+
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
180+
clientsInProximity.Add(client.Key);
181+
}
182+
SendToClientsTarget(clientsInProximity, "MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
183+
}
184+
else
185+
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
160186
}
161187
else
162188
{
@@ -196,7 +222,18 @@ private void HandleAnimMsg(int clientId, byte[] data)
196222

197223
if(isServer)
198224
{
199-
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", data);
225+
if (EnableProximity)
226+
{
227+
List<int> clientsInProximity = new List<int>();
228+
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
229+
{
230+
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
231+
clientsInProximity.Add(client.Key);
232+
}
233+
SendToClientsTarget(clientsInProximity, "MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", data);
234+
}
235+
else
236+
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", data);
200237
}
201238
using(MemoryStream stream = new MemoryStream(data))
202239
{
@@ -217,7 +254,18 @@ private void HandleAnimParamsMsg(int clientId, byte[] data)
217254
{
218255
if (isServer)
219256
{
220-
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", data);
257+
if (EnableProximity)
258+
{
259+
List<int> clientsInProximity = new List<int>();
260+
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
261+
{
262+
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
263+
clientsInProximity.Add(client.Key);
264+
}
265+
SendToClientsTarget(clientsInProximity, "MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", data);
266+
}
267+
else
268+
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", data);
221269
}
222270
using (MemoryStream stream = new MemoryStream(data))
223271
{
@@ -232,7 +280,18 @@ private void HandleAnimTriggerMsg(int clientId, byte[] data)
232280
{
233281
if (isServer)
234282
{
235-
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", data);
283+
if (EnableProximity)
284+
{
285+
List<int> clientsInProximity = new List<int>();
286+
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
287+
{
288+
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
289+
clientsInProximity.Add(client.Key);
290+
}
291+
SendToClientsTarget(clientsInProximity, "MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", data);
292+
}
293+
else
294+
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", data);
236295
}
237296
using (MemoryStream stream = new MemoryStream(data))
238297
{
@@ -331,7 +390,18 @@ public void SetTrigger(int hash)
331390
}
332391
if (isServer)
333392
{
334-
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
393+
if (EnableProximity)
394+
{
395+
List<int> clientsInProximity = new List<int>();
396+
foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients)
397+
{
398+
if (Vector3.Distance(transform.position, client.Value.PlayerObject.transform.position) <= ProximityRange)
399+
clientsInProximity.Add(client.Key);
400+
}
401+
SendToClientsTarget(clientsInProximity, "MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
402+
}
403+
else
404+
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
335405
}
336406
else
337407
{

0 commit comments

Comments
 (0)