using System; using Dissonance; using Dissonance.Networking; using UnityEngine; using UnityEngine.Serialization; namespace VelNet { /// /// Added to the same object as DissonanceComms component. Only one in the scene. /// [RequireComponent(typeof(DissonanceComms))] [AddComponentMenu("VelNet/Dissonance/VelNet Comms Network")] public class VelCommsNetwork : MonoBehaviour, ICommsNetwork { public ConnectionStatus Status => manager.connected ? ConnectionStatus.Connected : ConnectionStatus.Disconnected; public NetworkMode Mode => NetworkMode.Client; public event Action ModeChanged; public event Action PlayerJoined; public event Action PlayerLeft; public event Action VoicePacketReceived; public event Action TextPacketReceived; public event Action PlayerStartedSpeaking; public event Action PlayerStoppedSpeaking; public event Action PlayerEnteredRoom; public event Action PlayerExitedRoom; private ConnectionStatus _status = ConnectionStatus.Disconnected; private CodecSettings initSettings; public string dissonanceId; [HideInInspector] public DissonanceComms dissonanceComms; private VelNetManager manager; /// /// listen to this if you want to send voice /// public Action> VoiceQueued; // Start is called before the first frame update private void Start() { _status = ConnectionStatus.Connected; dissonanceComms = GetComponent(); manager = VelNetManager.instance; } public void Initialize(string playerName, Rooms rooms, PlayerChannels playerChannels, RoomChannels roomChannels, CodecSettings codecSettings) { dissonanceId = playerName; initSettings = codecSettings; dissonanceComms.ResetMicrophoneCapture(); } public void VoiceReceived(string sender, byte[] data) { uint sequenceNumber = BitConverter.ToUInt32(data, 0); VoicePacket vp = new VoicePacket(sender, ChannelPriority.Default, 1, true, new ArraySegment(data, 4, data.Length - 4), sequenceNumber); VoicePacketReceived?.Invoke(vp); } public void SendText(string data, ChannelType recipientType, string recipientId) { Debug.Log("sending text"); } public void SendVoice(ArraySegment data) { VoiceQueued?.Invoke(data); } public void SetPlayerJoined(string id) { Debug.Log("Dissonance player joined"); PlayerJoined?.Invoke(id, initSettings); RoomEvent re = new RoomEvent { Joined = true, Room = "Global", PlayerName = id }; PlayerEnteredRoom?.Invoke(re); } public void SetPlayerLeft(string id) { RoomEvent re = new RoomEvent { Joined = false, Room = "Global", PlayerName = id }; PlayerExitedRoom?.Invoke(re); // only send this event for non-local players if (id != dissonanceId) PlayerLeft?.Invoke(id); } public void SetPlayerStartedSpeaking(string id) { PlayerStartedSpeaking?.Invoke(id); } public void SetPlayerStoppedSpeaking(string id) { PlayerStoppedSpeaking?.Invoke(id); } } }