more action callbacks for various events, removing objects on leave room, made some stuff static in VelNetManager
parent
96e3e762eb
commit
1b5be601ed
|
|
@ -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
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ namespace VelNet
|
|||
public VelNetPlayer()
|
||||
{
|
||||
manager = VelNetManager.instance;
|
||||
manager.OnPlayerJoined += HandlePlayerJoined;
|
||||
VelNetManager.OnPlayerJoined += HandlePlayerJoined;
|
||||
}
|
||||
|
||||
public void HandlePlayerJoined(VelNetPlayer player)
|
||||
|
|
|
|||
Loading…
Reference in New Issue