public void OnClientConnected(int connectionId) { try { Console.WriteLine($"OnClientConnected: {connectionId}"); var message = new NetworkMessage(); message.AddTagPacket(NetworkTagPacket.PlayerPositionsArray); message.AddUInt32((uint)networkPlayersDictionary.Count); foreach (var player in networkPlayersDictionary) { message.AddUInt32((uint)player.Key); message.AddFloat(player.Value.X); message.AddFloat(player.Value.Y); message.AddFloat(player.Value.Z); } server.Send(connectionId, message.Buffer); if (!networkPlayersDictionary.ContainsKey(connectionId)) { networkPlayersDictionary.Add(connectionId, new NetworkPlayer(connectionId)); } networkPlayersDictionary[connectionId].Moved = true; } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } }
public void SendPlayerPositions() { try { Dictionary <long, NetworkPlayer> sendPosDict = new Dictionary <long, NetworkPlayer>(networkPlayersDictionary); foreach (var sendToPlayer in sendPosDict) { if (sendToPlayer.Value == null) { continue; } var message = new NetworkMessage(); message.AddTagPacket(NetworkTagPacket.PlayerPositionsArray); message.AddUInt32((uint)sendPosDict.Count(c => c.Key != sendToPlayer.Key && c.Value.Moved)); int amountPlayersMoved = 0; foreach (var posPlayers in sendPosDict) { if (sendToPlayer.Key == posPlayers.Key) { continue; } if (!posPlayers.Value.Moved) { continue; } message.AddUInt32((uint)posPlayers.Key); message.AddFloat(posPlayers.Value.X); message.AddFloat(posPlayers.Value.Y); message.AddFloat(posPlayers.Value.Z); amountPlayersMoved++; } if (amountPlayersMoved > 0) { server.Send(sendToPlayer.Value.ConnectionID, message.Buffer); } } foreach (var player in networkPlayersDictionary) { player.Value.Moved = false; } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } }