Gamesparks - a network view approximation (w.i.p.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using GameSparks.RT; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class NetworkView : MonoBehaviour | |
{ | |
public int viewID = 0; | |
public enum ViewSynchModes | |
{ | |
NONE, POSITION,ROTATION,POSITION_ROTATION,POSITION_ROTATION_SCALE | |
} | |
public ViewSynchModes ViewSynchMode = ViewSynchModes.NONE; | |
public GameSparksRT.DeliveryIntent DeliveryIntent = GameSparksRT.DeliveryIntent.UNRELIABLE; | |
public float positionSensitivity = 0.01f; | |
public float rotationSensitivity = 0.01f; | |
public float scaleSensitivity = 0.01f; | |
private Transform _transform = null; | |
private Vector3 _lastPosition = Vector3.zero; | |
private Quaternion _lastRotation = Quaternion.identity; | |
private Vector3 _lastScale = Vector3.zero; | |
private Transform mTransform | |
{ | |
get { if(!_transform) _transform = GetComponent<Transform>(); return _transform;} | |
} | |
void Start () | |
{ | |
NetworkController.RegisterView(this); | |
enabled = false; | |
} | |
private void OnDestroy() | |
{ | |
NetworkController.UnRegisterView(this); | |
} | |
RTData GetData() | |
{ | |
RTData data = RTData.Get(); | |
data.SetInt(0, viewID); | |
return data; | |
} | |
bool PositionChanged() | |
{ | |
return (Vector3.Distance(mTransform.position,_lastPosition)>=positionSensitivity); | |
} | |
bool RotationChanged() | |
{ | |
return (Quaternion.Angle(mTransform.rotation,_lastRotation)>=rotationSensitivity); | |
} | |
bool ScaleChanged() | |
{ | |
return (Vector3.Distance(mTransform.localScale,_lastScale)>=scaleSensitivity); | |
} | |
void UpdateAndSendPosition() | |
{ | |
if (PositionChanged()) | |
{ | |
_lastPosition = mTransform.position; | |
NetworkController.Instance.SendRTData( | |
NetworkController.RAISE_EVENTS.POSITION, | |
DeliveryIntent, | |
GetData().SetVector3(1, _lastPosition) | |
); | |
} | |
} | |
void UpdateAndSendPositionRotation() | |
{ | |
if (PositionChanged()|| | |
RotationChanged()) | |
{ | |
_lastPosition = mTransform.position; | |
_lastRotation = mTransform.rotation; | |
NetworkController.Instance.SendRTData( | |
NetworkController.RAISE_EVENTS.POSITION_ROTATION, | |
DeliveryIntent, | |
GetData() | |
.SetVector3(1, _lastPosition) | |
.SetRTVector(2,new RTVector(_lastRotation.x,_lastRotation.y,_lastRotation.z,_lastRotation.w)) | |
); | |
} | |
} | |
void UpdateAndSendPositionRotationScale() | |
{ | |
if (PositionChanged() || | |
RotationChanged() || | |
ScaleChanged() | |
) | |
{ | |
_lastPosition = mTransform.position; | |
_lastRotation = mTransform.rotation; | |
_lastScale = mTransform.localScale; | |
NetworkController.Instance.SendRTData( | |
NetworkController.RAISE_EVENTS.POSITION_ROTATION_SCALE, | |
DeliveryIntent, | |
GetData() | |
.SetVector3(1, _lastPosition) | |
.SetRTVector(2,new RTVector(_lastRotation.x,_lastRotation.y,_lastRotation.z,_lastRotation.w)) | |
.SetVector3(3, _lastScale) | |
); | |
} | |
} | |
internal void SendState() | |
{ | |
switch (ViewSynchMode) | |
{ | |
case ViewSynchModes.POSITION: | |
UpdateAndSendPosition(); | |
break; | |
case ViewSynchModes.POSITION_ROTATION: | |
UpdateAndSendPositionRotation(); | |
break; | |
case ViewSynchModes.POSITION_ROTATION_SCALE: | |
UpdateAndSendPositionRotationScale(); | |
break; | |
} | |
} | |
void AddData(ref RTData data,uint index, object val) | |
{ | |
Type type = val.GetType(); | |
if(type==typeof(int)) | |
{ | |
data.SetInt(index,(int)val); | |
} else if (type == typeof(float)) | |
{ | |
data.SetFloat(index,(float)val); | |
} else if (type == typeof(double)) | |
{ | |
data.SetDouble(index,(double)val); | |
} else if(type==typeof(string)) | |
{ | |
data.SetString(index,(string)val); | |
} else if(type==typeof(long)) | |
{ | |
data.SetLong(index,(long)val); | |
} else if(type==typeof(Vector2)) | |
{ | |
data.SetVector2(index,(Vector2)val); | |
} else if (type==typeof(Vector3)) | |
{ | |
data.SetVector3(index,(Vector3)val); | |
} else if(type==typeof(Vector4)) | |
{ | |
data.SetVector4(index,(Vector4)val); | |
} | |
} | |
void AddDataArrayToData<T>(ref RTData data, T[] array) | |
{ | |
for(uint i=0;i<array.Length;i++) | |
{ | |
AddData(ref data,i,array[i]); | |
} | |
} | |
void AddDataArray(ref RTData data, uint index , object arraypointer) | |
{ | |
if (arraypointer.GetType().GetArrayRank()==1) //support only 1-dimensional arrays | |
{ | |
RTData aRTData = new RTData(); | |
Type type = arraypointer.GetType(); | |
bool added = false; | |
if (type == typeof(int[])) | |
{ | |
AddDataArrayToData(ref aRTData,(int[])arraypointer); | |
added = true; | |
} else if (type == typeof(float[])) | |
{ | |
AddDataArrayToData(ref aRTData,(float[])arraypointer); | |
added = true; | |
} else if (type == typeof(double[])) | |
{ | |
AddDataArrayToData(ref aRTData, (double[])arraypointer); | |
added = true; | |
} else if (type == typeof(string[])) | |
{ | |
AddDataArrayToData(ref aRTData, (string[])arraypointer); | |
added = true; | |
} else if (type == typeof(long[])) | |
{ | |
AddDataArrayToData(ref aRTData, (long[])arraypointer); | |
added = true; | |
} | |
if (added) data.SetData(index,aRTData); | |
} | |
} | |
public void RPC_Send(NetworkController.RAISE_EVENTS RaiseEvent, int[] playersPeerIDs = null, params object[] parameters) | |
{ | |
RTData data = GetData(); | |
for (uint i = 0; i < parameters.Length; i++) | |
{ | |
if (parameters[i].GetType().IsArray) | |
{ | |
AddDataArray(ref data,i,parameters[i]); | |
} else | |
{ | |
AddData(ref data, i, parameters[i]); | |
} | |
} | |
NetworkController.Instance.SendRTData(RaiseEvent, DeliveryIntent, data, playersPeerIDs); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment