more action callbacks for various events, removing objects on leave room, made some stuff static in VelNetManager

handTracking
Anton Franzluebbers 2022-01-07 23:23:03 -05:00
parent 7e90b6d8cc
commit 4d7513f6da
5 changed files with 234 additions and 138 deletions

View File

@ -46,7 +46,7 @@ namespace VelNet
{
comms = FindObjectOfType<DissonanceComms>();
microphones.AddOptions(new List<string>(Microphone.devices));
velNetManager.MessageReceived += (m) =>
VelNetManager.MessageReceived += (m) =>
{
string s = m.type + ":" + m.sender + ":" + m.text;
messageBuffer.Add(s);

View File

@ -10,7 +10,7 @@ public class VelNetMan : MonoBehaviour
// Start is called before the first frame update
private void Start()
{
VelNetManager.instance.OnJoinedRoom += player =>
VelNetManager.OnJoinedRoom += player =>
{
VelNetManager.InstantiateNetworkObject(playerPrefab.name);
};

View File

@ -301,7 +301,7 @@ RectTransform:
- {fileID: 1557879931}
- {fileID: 927188573}
m_Father: {fileID: 244561620}
m_RootOrder: 7
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@ -596,14 +596,14 @@ RectTransform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_Children:
- {fileID: 1278634767}
- {fileID: 711524767}
- {fileID: 1894247853}
- {fileID: 626742070}
- {fileID: 117638565}
- {fileID: 1760805525}
- {fileID: 1278634767}
- {fileID: 626742070}
- {fileID: 711524767}
- {fileID: 945446556}
- {fileID: 1843597586}
- {fileID: 117638565}
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -870,7 +870,7 @@ RectTransform:
- {fileID: 2034439}
- {fileID: 1560686264}
m_Father: {fileID: 244561620}
m_RootOrder: 3
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@ -1041,7 +1041,7 @@ RectTransform:
- {fileID: 162005664}
- {fileID: 1484033256}
m_Father: {fileID: 244561620}
m_RootOrder: 1
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@ -1609,7 +1609,7 @@ RectTransform:
- {fileID: 1954037272}
- {fileID: 1235343401}
m_Father: {fileID: 244561620}
m_RootOrder: 5
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@ -1839,12 +1839,12 @@ MonoBehaviour:
udpConnected: 0
userid: -1
room:
connected: 0
prefabs:
- {fileID: 3951900052977689805, guid: 6e4a023f70e01405e8b249a4488fe319, type: 3}
- {fileID: 9102273340480352682, guid: d4158ab9c4a204cdbba28d3273fc1fb3, type: 3}
sceneObjects: []
deletedSceneObjects: []
connected: 0
--- !u!1 &1154194181
GameObject:
m_ObjectHideFlags: 0
@ -2082,7 +2082,7 @@ RectTransform:
m_Children:
- {fileID: 1295274441}
m_Father: {fileID: 244561620}
m_RootOrder: 0
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@ -2350,7 +2350,7 @@ MonoBehaviour:
_metadataExpanded: 1
_activationModeExpanded: 1
_tokensExpanded: 1
_ampExpanded: 0
_ampExpanded: 1
_activationFaderSettings:
_volume: 1
_fadeInTicks: 0
@ -2685,7 +2685,7 @@ RectTransform:
m_Children:
- {fileID: 2118429759}
m_Father: {fileID: 244561620}
m_RootOrder: 4
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@ -2818,7 +2818,7 @@ RectTransform:
m_Children:
- {fileID: 1852007163}
m_Father: {fileID: 244561620}
m_RootOrder: 6
m_RootOrder: 7
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@ -3028,7 +3028,7 @@ RectTransform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 244561620}
m_RootOrder: 2
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 1}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
@ -37,9 +38,34 @@ namespace VelNet
public readonly Dictionary<int, VelNetPlayer> players = new Dictionary<int, VelNetPlayer>();
public Action<VelNetPlayer> OnJoinedRoom;
public Action<VelNetPlayer> OnPlayerJoined;
public Action<VelNetPlayer> OnPlayerLeft;
/// <summary>
/// We just joined a room
/// string - the room name
/// </summary>
public static Action<string> OnJoinedRoom;
/// <summary>
/// We just left a room
/// string - the room name we left
/// </summary>
public static Action<string> OnLeftRoom;
/// <summary>
/// Somebody else just joined our room
/// </summary>
public static Action<VelNetPlayer> OnPlayerJoined;
/// <summary>
/// Somebody else just left our room
/// </summary>
public static Action<VelNetPlayer> OnPlayerLeft;
public static Action OnConnectedToServer;
public static Action<Message> MessageReceived;
public static Action LoggedIn;
public static Action<string[], int> RoomsReceived;
public bool connected;
public List<NetworkObject> prefabs = new List<NetworkObject>();
public NetworkObject[] sceneObjects;
@ -47,6 +73,7 @@ namespace VelNet
public readonly Dictionary<string, NetworkObject> objects = new Dictionary<string, NetworkObject>(); //maintains a list of all known objects on the server (ones that have ids)
private VelNetPlayer masterPlayer;
public static VelNetPlayer LocalPlayer => instance.players.Where(p => p.Value.isLocal).Select(p => p.Value).FirstOrDefault();
public static bool InRoom => LocalPlayer != null && LocalPlayer.room != "-1" && LocalPlayer.room != "";
// Use this for initialization
@ -69,10 +96,21 @@ namespace VelNet
instance = this;
}
private void Start()
private IEnumerator Start()
{
ConnectToTcpServer();
sceneObjects = FindObjectsOfType<NetworkObject>(); //add all local network objects
yield return null;
try
{
OnConnectedToServer?.Invoke();
}
// prevent errors in subscribers from breaking our code
catch (Exception e)
{
Debug.LogError(e);
}
}
@ -92,25 +130,35 @@ namespace VelNet
//the main thread, which can do Unity stuff
foreach (Message m in receivedMessages)
{
if (m.type == 0) //when you join the server
switch (m.type)
{
// when you join the server
case 0:
userid = m.sender;
Debug.Log("joined server");
try
{
LoggedIn?.Invoke();
}
// prevent errors in subscribers from breaking our code
catch (Exception e)
{
Debug.LogError(e);
}
//start the udp thread
clientReceiveThreadUDP = new Thread(ListenForDataUDP);
clientReceiveThreadUDP.IsBackground = true;
clientReceiveThreadUDP.Start();
}
if (m.type == 2)
{
break;
// if this message is for me, that means I joined a new room...
if (userid == m.sender)
case 2 when userid == m.sender:
{
// TODO delete all old objects when joining a new room
string oldRoom = LocalPlayer?.room;
players.Clear(); //we clear the list, but will recreate as we get messages from people in our room
// we clear the list, but will recreate as we get messages from people in our room
players.Clear();
if (m.text != "")
{
@ -122,10 +170,44 @@ namespace VelNet
};
players.Add(userid, player);
OnJoinedRoom?.Invoke(player);
if (m.text != "")
{
try
{
OnJoinedRoom?.Invoke(m.text);
}
// prevent errors in subscribers from breaking our code
catch (Exception e)
{
Debug.LogError(e);
}
}
else // not for me, a player is joining or leaving
}
// we just left a room
else
{
// delete all networkobjects that aren't sceneobjects or are null now
objects
.Where(kvp => kvp.Value == null || !kvp.Value.isSceneObject)
.Select(o => o.Key)
.ToList().ForEach(DeleteNetworkObject);
Debug.Log("Left VelNet Room: " + oldRoom);
try
{
OnLeftRoom?.Invoke(oldRoom);
}
// prevent errors in subscribers from breaking our code
catch (Exception e)
{
Debug.LogError(e);
}
}
break;
}
// not for me, a player is joining or leaving
case 2:
{
VelNetPlayer me = players[userid];
@ -171,17 +253,25 @@ namespace VelNet
userid = m.sender
};
players.Add(m.sender, player);
try
{
OnPlayerJoined?.Invoke(player);
}
}
}
if (m.type == 3) // generic message
// prevent errors in subscribers from breaking our code
catch (Exception e)
{
players[m.sender]?.HandleMessage(m);
Debug.LogError(e);
}
}
if (m.type == 4) // change master player (this should only happen when the first player joins or if the master player leaves)
break;
}
// generic message
case 3:
players[m.sender]?.HandleMessage(m);
break;
// change master player (this should only happen when the first player joins or if the master player leaves)
case 4:
{
if (masterPlayer == null)
{
@ -213,9 +303,12 @@ namespace VelNet
kvp.Value.owner = masterPlayer;
}
}
break;
}
}
MessageReceived(m);
MessageReceived?.Invoke(m);
}
receivedMessages.Clear();
@ -227,13 +320,6 @@ namespace VelNet
socketConnection.Close();
}
public Action<string, int> JoinedRoom = delegate { };
public Action<Message> MessageReceived = delegate { };
public Action<string, int> LoggedIn = delegate { };
public Action<string[], int> RoomsReceived = delegate { };
public bool connected;
/// <summary>
/// Setup socket connection.
/// </summary>
@ -253,10 +339,11 @@ namespace VelNet
private void HandleMessage(string s) // this parses messages from the server, and adds them to a queue to be processed on the main thread
{
// Debug.Log("Received: " + s);
Message m = new Message();
string[] sections = s.Split(':');
if (sections.Length > 0)
{
if (sections.Length <= 0) return;
int type = int.Parse(sections[0]);
switch (type)
@ -317,7 +404,6 @@ namespace VelNet
}
}
}
}
/// <summary>
/// Runs in background clientReceiveThread; Listens for incomming data.
@ -452,6 +538,7 @@ namespace VelNet
/// </summary>
private static void SendNetworkMessage(string clientMessage)
{
// Debug.Log("Sent: " + clientMessage);
if (instance.socketConnection == null)
{
return;
@ -476,19 +563,29 @@ namespace VelNet
}
}
/// <summary>
/// Connects to the server with a username
/// </summary>
public static void Login(string username, string password)
{
SendNetworkMessage("0:" + username + ":" + password);
}
/// <summary>
/// Joins a room by name
/// </summary>
/// <param name="roomname">The name of the room to join</param>
public static void Join(string roomname)
{
SendNetworkMessage("2:" + roomname);
}
/// <summary>
/// Leaves a room if we're in one
/// </summary>
public static void Leave()
{
SendNetworkMessage("2:-1");
if (InRoom) SendNetworkMessage("2:-1");
}
public static void SendTo(MessageType type, string message, bool reliable = true)
@ -557,9 +654,9 @@ namespace VelNet
public void DeleteNetworkObject(string networkId)
{
if (objects.ContainsKey(networkId))
{
if (!objects.ContainsKey(networkId)) return;
NetworkObject obj = objects[networkId];
if (obj == null) return;
if (obj.isSceneObject)
{
deletedSceneObjects.Add(networkId);
@ -570,4 +667,3 @@ namespace VelNet
}
}
}
}

View File

@ -29,7 +29,7 @@ namespace VelNet
public VelNetPlayer()
{
manager = VelNetManager.instance;
manager.OnPlayerJoined += HandlePlayerJoined;
VelNetManager.OnPlayerJoined += HandlePlayerJoined;
}
public void HandlePlayerJoined(VelNetPlayer player)