From 0167ca3003d42edc3264544abc466eab03d11301 Mon Sep 17 00:00:00 2001 From: Kyle Johnsen Date: Mon, 3 Jan 2022 17:16:25 -0500 Subject: [PATCH] better concept for the player now that is more generic --- TestVelGameServer/Assets/NetworkObject.cs | 12 ++-- TestVelGameServer/Assets/NetworkSyncable.cs | 13 ++++ .../Assets/NetworkSyncable.cs.meta | 11 ++++ TestVelGameServer/Assets/SyncTransform.cs | 12 +++- .../Assets/VelGameServer/NetworkManager.cs | 7 ++- .../Assets/VelGameServer/NetworkPlayer.cs | 61 ++++++++----------- .../Assets/VelGameServer/PlayerPrefab.prefab | 21 ++++++- 7 files changed, 91 insertions(+), 46 deletions(-) create mode 100644 TestVelGameServer/Assets/NetworkSyncable.cs create mode 100644 TestVelGameServer/Assets/NetworkSyncable.cs.meta diff --git a/TestVelGameServer/Assets/NetworkObject.cs b/TestVelGameServer/Assets/NetworkObject.cs index 085ebab..64e5a76 100644 --- a/TestVelGameServer/Assets/NetworkObject.cs +++ b/TestVelGameServer/Assets/NetworkObject.cs @@ -2,10 +2,14 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public abstract class NetworkObject: MonoBehaviour +/// +/// This is a base class for all objects that a player can instantiated/owned +/// +public abstract class NetworkObject: MonoBehaviour, NetworkSyncable { public NetworkPlayer owner; - public string networkId; - public abstract byte[] getSyncMessage(); //local owner asks for this and sends it periodically - public abstract void handleSyncMessage(byte[] message); //remote owner will call this + public string networkId; //this is forged from the combination of the creator's id (-1 in the case of a scene object) and an object id, so it's always unique for a room + + public abstract byte[] getSyncMessage(); + public abstract void handleSyncMessage(byte[] message); } diff --git a/TestVelGameServer/Assets/NetworkSyncable.cs b/TestVelGameServer/Assets/NetworkSyncable.cs new file mode 100644 index 0000000..24e68db --- /dev/null +++ b/TestVelGameServer/Assets/NetworkSyncable.cs @@ -0,0 +1,13 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// +/// This makes serialization somewhat uniform +/// +public interface NetworkSyncable +{ + public byte[] getSyncMessage(); //local owner asks for this and sends it periodically + public void handleSyncMessage(byte[] message); //remote owner will call this + +} diff --git a/TestVelGameServer/Assets/NetworkSyncable.cs.meta b/TestVelGameServer/Assets/NetworkSyncable.cs.meta new file mode 100644 index 0000000..f444aa7 --- /dev/null +++ b/TestVelGameServer/Assets/NetworkSyncable.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8e4266960bfa444998dda57f911448f0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/TestVelGameServer/Assets/SyncTransform.cs b/TestVelGameServer/Assets/SyncTransform.cs index cdee0b3..9253c6e 100644 --- a/TestVelGameServer/Assets/SyncTransform.cs +++ b/TestVelGameServer/Assets/SyncTransform.cs @@ -6,13 +6,16 @@ using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using UnityEngine; +/// +/// A simple class that will sync the position and rotation of a network object +/// public class SyncTransform : NetworkObject { public Vector3 targetPosition; public Quaternion targetRotation; - + public override byte[] getSyncMessage() { float[] data = new float[7]; @@ -50,7 +53,9 @@ public class SyncTransform : NetworkObject { while (true) { - if (owner != null && owner.isLocal) { + if (owner != null && owner.isLocal) + { + owner.syncObject(this); } yield return new WaitForSeconds(.1f); @@ -59,10 +64,11 @@ public class SyncTransform : NetworkObject // Update is called once per frame void Update() { - if(owner != null && !owner.isLocal) + if (owner != null && !owner.isLocal) { transform.position = Vector3.Lerp(transform.position, targetPosition, .1f); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, .1f); } } + } diff --git a/TestVelGameServer/Assets/VelGameServer/NetworkManager.cs b/TestVelGameServer/Assets/VelGameServer/NetworkManager.cs index 55efe59..a02b498 100644 --- a/TestVelGameServer/Assets/VelGameServer/NetworkManager.cs +++ b/TestVelGameServer/Assets/VelGameServer/NetworkManager.cs @@ -24,7 +24,7 @@ public class NetworkManager : MonoBehaviour public Action onPlayerLeft = delegate { }; public List prefabs = new List(); - + NetworkObject[] sceneObjects; public Dictionary objects = new Dictionary(); //maintains a list of all known objects on the server (ones that have ids) NetworkPlayer masterPlayer = null; #endregion @@ -39,7 +39,8 @@ public class NetworkManager : MonoBehaviour void Start() { ConnectToTcpServer(); - + sceneObjects = GameObject.FindObjectsOfType(); //add all local network objects + } @@ -122,7 +123,7 @@ public class NetworkManager : MonoBehaviour masterPlayer = players[m.sender]; //no master player yet, add the scene objects - NetworkObject[] sceneObjects = GameObject.FindObjectsOfType(); //add all local network objects + for (int i = 0; i < sceneObjects.Length; i++) { sceneObjects[i].networkId = -1 + "-" + i; diff --git a/TestVelGameServer/Assets/VelGameServer/NetworkPlayer.cs b/TestVelGameServer/Assets/VelGameServer/NetworkPlayer.cs index 485afa0..5566606 100644 --- a/TestVelGameServer/Assets/VelGameServer/NetworkPlayer.cs +++ b/TestVelGameServer/Assets/VelGameServer/NetworkPlayer.cs @@ -5,53 +5,44 @@ using System.Text; using System; using Dissonance; +[RequireComponent(typeof(NetworkObject))] public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer { + + public NetworkObject myObject; public int userid; public string username; public string room; public NetworkManager manager; - Vector3 networkPosition; + public bool isLocal = false; + public int lastObjectId=0; //for instantiation - + public Dissonance.VelCommsNetwork commsNetwork; bool isSpeaking = false; uint lastAudioId = 0; public string dissonanceID; //required by dissonance for spatial audio public string PlayerId => dissonanceID; - public Vector3 Position => transform.position; - public Quaternion Rotation => transform.rotation; + public Vector3 Position => myObject.transform.position; + public Quaternion Rotation => myObject.transform.rotation; public NetworkPlayerType Type => isLocal?NetworkPlayerType.Local:NetworkPlayerType.Remote; public bool IsTracking => true; bool isMaster = false; - void Start() + void Awake() { - if (manager.userid == userid) //this is me, I send updates - { - StartCoroutine(syncTransformCoroutine()); - } + myObject.owner = this; } // Update is called once per frame void Update() { - - if (!isLocal) //not me, I move to wherever I should + //handle dissonance comms + if(isLocal) { - transform.position = Vector3.Lerp(transform.position, networkPosition, .1f); - - } - else - { - float h = Input.GetAxis("Horizontal"); - float v = Input.GetAxis("Vertical"); - Vector3 delta = h * Vector3.right + v * Vector3.up; - transform.position = transform.position + delta * Time.deltaTime; - //if we're not speaking, and the comms say we are, send a speaking event, which will be received on other network players and sent to their comms accordingly if(commsNetwork.comms.FindPlayer(dissonanceID).IsSpeaking != isSpeaking) //unfortunately, there does not seem to be an event for this { @@ -67,14 +58,6 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer } - IEnumerator syncTransformCoroutine() - { - while (true) - { - manager.sendTo(0, "1," + transform.position.x + "," + transform.position.y + "," + transform.position.z + ";"); - yield return new WaitForSeconds(.1f); - } - } public void handleMessage(NetworkManager.Message m) { //these are generally things that come from the "owner" and should be enacted locally, where appropriate @@ -89,12 +72,10 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer switch (sections[0]) { - case "1": //update transform of self + case "1": //update my object's data { - float x = float.Parse(sections[1]); - float y = float.Parse(sections[2]); - float z = float.Parse(sections[3]); - networkPosition = new Vector3(x, y, z); + byte[] message = Convert.FromBase64String(sections[1]); + myObject.handleSyncMessage(message); break; } case "2": //audio data @@ -226,11 +207,21 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer public void syncObject(NetworkObject obj) { byte[] data = obj.getSyncMessage(); - manager.sendTo(0, "5," + obj.networkId + "," + Convert.ToBase64String(data)); + if (obj == myObject) + { + manager.sendTo(0, "1," + Convert.ToBase64String(data)); + } + else + { + + manager.sendTo(0, "5," + obj.networkId + "," + Convert.ToBase64String(data)); + } } public void instantiateObject(string prefab) { } + + } diff --git a/TestVelGameServer/Assets/VelGameServer/PlayerPrefab.prefab b/TestVelGameServer/Assets/VelGameServer/PlayerPrefab.prefab index 9c236d9..b74458b 100644 --- a/TestVelGameServer/Assets/VelGameServer/PlayerPrefab.prefab +++ b/TestVelGameServer/Assets/VelGameServer/PlayerPrefab.prefab @@ -13,6 +13,7 @@ GameObject: - component: {fileID: 6854617867369839} - component: {fileID: 5845716565458182149} - component: {fileID: 5713671764962751473} + - component: {fileID: 2513801069545108573} m_Layer: 0 m_Name: PlayerPrefab m_TagString: Untagged @@ -108,10 +109,28 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d8d3b6de660834e3e898725928251405, type: 3} m_Name: m_EditorClassIdentifier: + myObject: {fileID: 2513801069545108573} userid: 0 username: - dissonanceID: room: manager: {fileID: 0} isLocal: 0 + lastObjectId: 0 commsNetwork: {fileID: 0} + dissonanceID: +--- !u!114 &2513801069545108573 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6139051692386484099} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3f1f9b0bbd93a484a987c51f1107ebe5, type: 3} + m_Name: + m_EditorClassIdentifier: + owner: {fileID: 5713671764962751473} + networkId: + targetPosition: {x: 0, y: 0, z: 0} + targetRotation: {x: 0, y: 0, z: 0, w: 0}