better concept for the player now that is more generic
parent
a967795b58
commit
0167ca3003
|
|
@ -2,10 +2,14 @@ using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public abstract class NetworkObject: MonoBehaviour
|
/// <summary>
|
||||||
|
/// This is a base class for all objects that a player can instantiated/owned
|
||||||
|
/// </summary>
|
||||||
|
public abstract class NetworkObject: MonoBehaviour, NetworkSyncable
|
||||||
{
|
{
|
||||||
public NetworkPlayer owner;
|
public NetworkPlayer owner;
|
||||||
public string networkId;
|
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(); //local owner asks for this and sends it periodically
|
|
||||||
public abstract void handleSyncMessage(byte[] message); //remote owner will call this
|
public abstract byte[] getSyncMessage();
|
||||||
|
public abstract void handleSyncMessage(byte[] message);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This makes serialization somewhat uniform
|
||||||
|
/// </summary>
|
||||||
|
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
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 8e4266960bfa444998dda57f911448f0
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
|
|
@ -6,6 +6,9 @@ using System.Runtime.Serialization;
|
||||||
using System.Runtime.Serialization.Formatters.Binary;
|
using System.Runtime.Serialization.Formatters.Binary;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A simple class that will sync the position and rotation of a network object
|
||||||
|
/// </summary>
|
||||||
public class SyncTransform : NetworkObject
|
public class SyncTransform : NetworkObject
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -50,7 +53,9 @@ public class SyncTransform : NetworkObject
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (owner != null && owner.isLocal) {
|
if (owner != null && owner.isLocal)
|
||||||
|
{
|
||||||
|
|
||||||
owner.syncObject(this);
|
owner.syncObject(this);
|
||||||
}
|
}
|
||||||
yield return new WaitForSeconds(.1f);
|
yield return new WaitForSeconds(.1f);
|
||||||
|
|
@ -65,4 +70,5 @@ public class SyncTransform : NetworkObject
|
||||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, .1f);
|
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, .1f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ public class NetworkManager : MonoBehaviour
|
||||||
public Action<NetworkPlayer> onPlayerLeft = delegate { };
|
public Action<NetworkPlayer> onPlayerLeft = delegate { };
|
||||||
|
|
||||||
public List<NetworkObject> prefabs = new List<NetworkObject>();
|
public List<NetworkObject> prefabs = new List<NetworkObject>();
|
||||||
|
NetworkObject[] sceneObjects;
|
||||||
public Dictionary<string, NetworkObject> objects = new Dictionary<string, NetworkObject>(); //maintains a list of all known objects on the server (ones that have ids)
|
public Dictionary<string, NetworkObject> objects = new Dictionary<string, NetworkObject>(); //maintains a list of all known objects on the server (ones that have ids)
|
||||||
NetworkPlayer masterPlayer = null;
|
NetworkPlayer masterPlayer = null;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
@ -39,6 +39,7 @@ public class NetworkManager : MonoBehaviour
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
ConnectToTcpServer();
|
ConnectToTcpServer();
|
||||||
|
sceneObjects = GameObject.FindObjectsOfType<NetworkObject>(); //add all local network objects
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,7 +123,7 @@ public class NetworkManager : MonoBehaviour
|
||||||
masterPlayer = players[m.sender];
|
masterPlayer = players[m.sender];
|
||||||
|
|
||||||
//no master player yet, add the scene objects
|
//no master player yet, add the scene objects
|
||||||
NetworkObject[] sceneObjects = GameObject.FindObjectsOfType<NetworkObject>(); //add all local network objects
|
|
||||||
for (int i = 0; i < sceneObjects.Length; i++)
|
for (int i = 0; i < sceneObjects.Length; i++)
|
||||||
{
|
{
|
||||||
sceneObjects[i].networkId = -1 + "-" + i;
|
sceneObjects[i].networkId = -1 + "-" + i;
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,19 @@ using System.Text;
|
||||||
using System;
|
using System;
|
||||||
using Dissonance;
|
using Dissonance;
|
||||||
|
|
||||||
|
[RequireComponent(typeof(NetworkObject))]
|
||||||
public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
|
public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public NetworkObject myObject;
|
||||||
public int userid;
|
public int userid;
|
||||||
public string username;
|
public string username;
|
||||||
|
|
||||||
public string room;
|
public string room;
|
||||||
public NetworkManager manager;
|
public NetworkManager manager;
|
||||||
Vector3 networkPosition;
|
|
||||||
public bool isLocal = false;
|
public bool isLocal = false;
|
||||||
|
|
||||||
public int lastObjectId=0; //for instantiation
|
public int lastObjectId=0; //for instantiation
|
||||||
|
|
||||||
public Dissonance.VelCommsNetwork commsNetwork;
|
public Dissonance.VelCommsNetwork commsNetwork;
|
||||||
|
|
@ -22,36 +26,23 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
|
||||||
public string dissonanceID;
|
public string dissonanceID;
|
||||||
//required by dissonance for spatial audio
|
//required by dissonance for spatial audio
|
||||||
public string PlayerId => dissonanceID;
|
public string PlayerId => dissonanceID;
|
||||||
public Vector3 Position => transform.position;
|
public Vector3 Position => myObject.transform.position;
|
||||||
public Quaternion Rotation => transform.rotation;
|
public Quaternion Rotation => myObject.transform.rotation;
|
||||||
public NetworkPlayerType Type => isLocal?NetworkPlayerType.Local:NetworkPlayerType.Remote;
|
public NetworkPlayerType Type => isLocal?NetworkPlayerType.Local:NetworkPlayerType.Remote;
|
||||||
public bool IsTracking => true;
|
public bool IsTracking => true;
|
||||||
bool isMaster = false;
|
bool isMaster = false;
|
||||||
|
|
||||||
void Start()
|
void Awake()
|
||||||
{
|
{
|
||||||
if (manager.userid == userid) //this is me, I send updates
|
myObject.owner = this;
|
||||||
{
|
|
||||||
StartCoroutine(syncTransformCoroutine());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
|
//handle dissonance comms
|
||||||
if (!isLocal) //not me, I move to wherever I should
|
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 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
|
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)
|
public void handleMessage(NetworkManager.Message m)
|
||||||
{
|
{
|
||||||
//these are generally things that come from the "owner" and should be enacted locally, where appropriate
|
//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])
|
switch (sections[0])
|
||||||
{
|
{
|
||||||
case "1": //update transform of self
|
case "1": //update my object's data
|
||||||
{
|
{
|
||||||
float x = float.Parse(sections[1]);
|
byte[] message = Convert.FromBase64String(sections[1]);
|
||||||
float y = float.Parse(sections[2]);
|
myObject.handleSyncMessage(message);
|
||||||
float z = float.Parse(sections[3]);
|
|
||||||
networkPosition = new Vector3(x, y, z);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "2": //audio data
|
case "2": //audio data
|
||||||
|
|
@ -226,11 +207,21 @@ public class NetworkPlayer : MonoBehaviour, Dissonance.IDissonancePlayer
|
||||||
public void syncObject(NetworkObject obj)
|
public void syncObject(NetworkObject obj)
|
||||||
{
|
{
|
||||||
byte[] data = obj.getSyncMessage();
|
byte[] data = obj.getSyncMessage();
|
||||||
|
if (obj == myObject)
|
||||||
|
{
|
||||||
|
manager.sendTo(0, "1," + Convert.ToBase64String(data));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
manager.sendTo(0, "5," + obj.networkId + "," + Convert.ToBase64String(data));
|
manager.sendTo(0, "5," + obj.networkId + "," + Convert.ToBase64String(data));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void instantiateObject(string prefab)
|
public void instantiateObject(string prefab)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ GameObject:
|
||||||
- component: {fileID: 6854617867369839}
|
- component: {fileID: 6854617867369839}
|
||||||
- component: {fileID: 5845716565458182149}
|
- component: {fileID: 5845716565458182149}
|
||||||
- component: {fileID: 5713671764962751473}
|
- component: {fileID: 5713671764962751473}
|
||||||
|
- component: {fileID: 2513801069545108573}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: PlayerPrefab
|
m_Name: PlayerPrefab
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
|
|
@ -108,10 +109,28 @@ MonoBehaviour:
|
||||||
m_Script: {fileID: 11500000, guid: d8d3b6de660834e3e898725928251405, type: 3}
|
m_Script: {fileID: 11500000, guid: d8d3b6de660834e3e898725928251405, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
|
myObject: {fileID: 2513801069545108573}
|
||||||
userid: 0
|
userid: 0
|
||||||
username:
|
username:
|
||||||
dissonanceID:
|
|
||||||
room:
|
room:
|
||||||
manager: {fileID: 0}
|
manager: {fileID: 0}
|
||||||
isLocal: 0
|
isLocal: 0
|
||||||
|
lastObjectId: 0
|
||||||
commsNetwork: {fileID: 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}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue