Skip to content

Commit

Permalink
tests pulled out
Browse files Browse the repository at this point in the history
  • Loading branch information
dooly123 committed Jan 9, 2025
1 parent 722f31c commit 9e1fc2d
Show file tree
Hide file tree
Showing 43 changed files with 5,948 additions and 4 deletions.
29 changes: 29 additions & 0 deletions Basis/Packages/com.basis.tests/BasisExamples.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "BasisExamples",
"rootNamespace": "",
"references": [
"GUID:8c9aa0e006f5b5347af5c5470971dfae",
"GUID:75c7734b9034adc43990fe961f586496",
"GUID:9e24947de15b9834991c9d8411ea37cf",
"GUID:b4ae365a73764ee478adc74cb89832fe",
"GUID:e0cd26848372d4e5c891c569017e11f1",
"GUID:2665a8d13d1b3f18800f46e256720795",
"GUID:84651a3751eca9349aac36a66bba901b",
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:15fc0a57446b3144c949da3e2b9737a9",
"GUID:a2bda89a3179a5324b88c0a9d3224e44",
"GUID:1a5076741edc2f145aafefca837072cf",
"GUID:da668404f8bf3b14ebd4691cc0a6d67c",
"GUID:42c34c05b8d434943ad2cf21483d9cd0",
"GUID:784bec3882b21ab4d8407cc870539fa9"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Basis/Packages/com.basis.tests/BasisExamples.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

150 changes: 150 additions & 0 deletions Basis/Packages/com.basis.tests/BasisObjectSyncNetworking.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using Basis.Scripts.BasisSdk;
using Basis.Scripts.BasisSdk.Players;
using Basis.Scripts.Networking;
using Basis.Scripts.Networking.NetworkedAvatar;
using BasisSerializer.OdinSerializer;
using LiteNetLib;
using UnityEngine;
public class BasisObjectSyncNetworking : MonoBehaviour
{
public ushort MessageIndex = 3321;
public byte[] byteArray = new byte[28];
public bool IsOwner = false;
public float LerpMultiplier = 3;
private float sendInterval = 0.2f; // Send data every 0.2 seconds
private float timeSinceLastSend = 0.0f;
public BasisNetworkPlayer LocalNetworkPlayer;
public DeliveryMethod DeliveryMethod = DeliveryMethod.Unreliable;
public Vector3 previousPosition;
public Quaternion previousRotation;
public Vector3 previousScale;
public bool NeedsAUpdate = false;
public Rigidbody Rigidbody;
public BasisPositionRotationScale Storeddata = new BasisPositionRotationScale();
public void Awake()
{
BasisScene.OnNetworkMessageReceived += OnNetworkMessageReceived;
BasisNetworkManagement.OnLocalPlayerJoined += OnLocalPlayerJoined;
BasisNetworkManagement.OnRemotePlayerJoined += OnRemotePlayerJoined;
BasisNetworkManagement.OnLocalPlayerLeft += OnLocalPlayerLeft;
BasisNetworkManagement.OnRemotePlayerLeft += OnRemotePlayerLeft;
}
public void OnDestroy()
{
BasisScene.OnNetworkMessageReceived -= OnNetworkMessageReceived;
BasisNetworkManagement.OnLocalPlayerJoined -= OnLocalPlayerJoined;
BasisNetworkManagement.OnRemotePlayerJoined -= OnRemotePlayerJoined;
BasisNetworkManagement.OnLocalPlayerLeft -= OnLocalPlayerLeft;
BasisNetworkManagement.OnRemotePlayerLeft -= OnRemotePlayerLeft;
}
public void OnRemotePlayerLeft(BasisNetworkPlayer player1, BasisRemotePlayer player2)
{
// Handle the remote player leaving
ComputeCurrentOwner();
}

public void OnLocalPlayerLeft(BasisNetworkPlayer player1, BasisLocalPlayer player2)
{
// Handle the local player leaving
ComputeCurrentOwner();
}

public void OnRemotePlayerJoined(BasisNetworkPlayer player1, BasisRemotePlayer player2)
{
// Handle a remote player joining
ComputeCurrentOwner();
NeedsAUpdate = true;
}

public void OnLocalPlayerJoined(BasisNetworkPlayer player1, BasisLocalPlayer player2)
{
LocalNetworkPlayer = player1;
// Handle the local player joining
ComputeCurrentOwner();
}

public void ComputeCurrentOwner()
{
ushort OldestPlayerInInstance = BasisNetworkManagement.Instance.GetOldestAvailablePlayerUshort();
IsOwner = OldestPlayerInInstance == LocalNetworkPlayer.NetId;
Rigidbody.isKinematic = !IsOwner;
}

public void Update()
{
if (IsOwner)
{
// Accumulate time since last send
timeSinceLastSend += Time.deltaTime;

// Only send data if the time since last send exceeds the interval
if (timeSinceLastSend >= sendInterval)
{
// Get current position, rotation, and scale
transform.GetPositionAndRotation(out Storeddata.Position, out Storeddata.Rotation);
Storeddata.Scale = transform.localScale;

// Check if position, rotation, or scale has changed since last sync
if (NeedsAUpdate || HasPositionRotationOrScaleChanged())
{
// Reset the timer
timeSinceLastSend = 0.0f;

// Convert to byte array
byte[] byteArray = SerializationUtility.SerializeValue(Storeddata, DataFormat.Binary);
// Send network message with position, rotation, and scale data
BasisScene.NetworkMessageSend(MessageIndex, byteArray, DeliveryMethod);

// Reset previous values to the current ones
previousPosition = Storeddata.Position;
previousRotation = Storeddata.Rotation;
previousScale = Storeddata.Scale;
NeedsAUpdate = false;
}

// Check if the object is below the respawn height and reset position if necessary
if (Storeddata.Position.y < BasisSceneFactory.Instance.BasisScene.RespawnHeight)
{
this.transform.position = BasisSceneFactory.Instance.BasisScene.SpawnPoint.position;
}
}
}
else
{
// Get the current position, rotation, and scale
transform.GetPositionAndRotation(out Vector3 CurrentPosition, out Quaternion CurrentRotation);
Vector3 CurrentScale = transform.localScale;
float DeltaTime = Time.deltaTime;
float CurrentLerp = LerpMultiplier * DeltaTime;

// Interpolate position, rotation, and scale for smooth transitions
Vector3 DesiredCurrentPosition = Vector3.Lerp(CurrentPosition, Storeddata.Position, CurrentLerp);
Quaternion DesiredCurrentRotation = Quaternion.Slerp(CurrentRotation, Storeddata.Rotation, CurrentLerp);
Vector3 DesiredCurrentScale = Vector3.Lerp(CurrentScale, Storeddata.Scale, CurrentLerp);

transform.SetPositionAndRotation(DesiredCurrentPosition, DesiredCurrentRotation);
transform.localScale = DesiredCurrentScale;
}
}

private bool HasPositionRotationOrScaleChanged()
{
// Check if the position, rotation, or scale has changed
bool positionChanged = previousPosition != Storeddata.Position;
bool rotationChanged = previousRotation != Storeddata.Rotation;
bool scaleChanged = previousScale != Storeddata.Scale;

// Return true if any have changed
return positionChanged || rotationChanged || scaleChanged;
}

public void OnNetworkMessageReceived(ushort PlayerID, ushort MessageIndex, byte[] buffer, DeliveryMethod DeliveryMethod = DeliveryMethod.ReliableSequenced)
{
// Check if this is the correct message
if (MessageIndex == this.MessageIndex)
{
byteArray = buffer;
Storeddata = SerializationUtility.DeserializeValue<BasisPositionRotationScale>(byteArray, DataFormat.Binary);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Basis/Packages/com.basis.tests/BasisPositionRotationScale.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using UnityEngine;
public struct BasisPositionRotationScale
{
public Vector3 Position;
public Quaternion Rotation;
public Vector3 Scale;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Basis/Packages/com.basis.tests/BasisTestClampedRotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using UnityEngine;

namespace Basis.Scripts.Tests
{
public class BasisTestClampedRotation : MonoBehaviour
{
public Transform lookTarget;
public Quaternion CalibrationRotation;
public Vector3 RotationLimits;
public float clampAngle;
public void Start()
{
CalibrationRotation = this.transform.rotation;
RotationLimits = CalibrationRotation.eulerAngles;
}
public void Update()
{
UpdateRotation();
}

private void UpdateRotation()
{
Quaternion targetRotation = lookTarget.rotation;

float xAngle = Mathf.Clamp(targetRotation.eulerAngles.x, CalibrationRotation.eulerAngles.x - RotationLimits.x, CalibrationRotation.eulerAngles.x + RotationLimits.x);
float yAngle = Mathf.Clamp(targetRotation.eulerAngles.y, CalibrationRotation.eulerAngles.y - RotationLimits.y, CalibrationRotation.eulerAngles.y + RotationLimits.y);
float zAngle = Mathf.Clamp(targetRotation.eulerAngles.z, CalibrationRotation.eulerAngles.z - RotationLimits.z, CalibrationRotation.eulerAngles.z + RotationLimits.z);

Quaternion newRotation = Quaternion.Euler(xAngle, yAngle, zAngle);
Quaternion finalRotation = Quaternion.RotateTowards(CalibrationRotation, newRotation, clampAngle);

transform.rotation = finalRotation;
}
}
}
11 changes: 11 additions & 0 deletions Basis/Packages/com.basis.tests/BasisTestClampedRotation.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions Basis/Packages/com.basis.tests/BasisTestNetwork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Basis.Scripts.BasisSdk;
using Basis.Scripts.BasisSdk.Players;
using Basis.Scripts.Networking;
using Basis.Scripts.Networking.NetworkedAvatar;
using LiteNetLib;
using UnityEngine;

public class BasisTestNetwork : MonoBehaviour
{
//tests network apis
public BasisAvatar avatar;
public DeliveryMethod Method = DeliveryMethod.ReliableSequenced;
public bool Send = false;
public ushort[] Players;
public void OnEnable()
{
avatar = BasisLocalPlayer.Instance.BasisAvatar;

foreach (BasisNetworkPlayer Player in BasisNetworkManagement.Players.Values)
{
if (Player != null && Player.Player != null & Player.Player.BasisAvatar != null)
{
Player.Player.BasisAvatar.OnNetworkMessageReceived += OnNetworkMessageReceived;
}
else
{
Debug.LogError("This should not have occured!");
}
}
BasisScene.OnNetworkMessageReceived += OnNetworkMessageReceived;
}
public void OnDisable()
{
avatar = BasisLocalPlayer.Instance.BasisAvatar;

foreach (BasisNetworkPlayer Player in BasisNetworkManagement.Players.Values)
{
if (Player != null && Player.Player != null & Player.Player.BasisAvatar != null)
{
Player.Player.BasisAvatar.OnNetworkMessageReceived -= OnNetworkMessageReceived;
}
else
{
Debug.LogError("This should not have occured!");
}
}
BasisScene.OnNetworkMessageReceived -= OnNetworkMessageReceived;
}
public void LateUpdate()
{
if (Send)
{
// ushort[] Players = BasisNetworkManagement.RemotePlayers.Keys.ToArray();
byte[] Bytes = new byte[16];
Debug.Log("sending Avatar");

// avatar.NetworkMessageSend(1, Method);
// avatar.NetworkMessageSend(2, null, Method);
avatar.NetworkMessageSend(3, null, Method, Players);

// avatar.NetworkMessageSend(4, null, Method, null);

// avatar.NetworkMessageSend(5, Bytes, Method);
// avatar.NetworkMessageSend(6, Bytes, Method, Players);
// avatar.NetworkMessageSend(7, Bytes, Method, null);
// Debug.Log("sent Avatar");
// Debug.Log("Sending Scene");
// BasisScene.NetworkMessageSend(8, Method);
// BasisScene.NetworkMessageSend(9, null, Method);
// BasisScene.NetworkMessageSend(10, null, Method, Players);
//
// BasisScene.NetworkMessageSend(11, null, Method, null);

/// BasisScene.NetworkMessageSend(12, Bytes, Method);
// BasisScene.NetworkMessageSend(13, Bytes, Method, Players);
// BasisScene.NetworkMessageSend(14, Bytes, Method, null);
// Debug.Log("sent Scene");
Send = false;
}
}
private void OnNetworkMessageReceived(ushort PlayerID, ushort MessageIndex, byte[] buffer, DeliveryMethod Method = DeliveryMethod.ReliableSequenced)
{
string bufferLength = buffer != null ? buffer.Length.ToString() : "null";
Debug.Log($"Scene: Interpreting {PlayerID} Stage {MessageIndex} BufferLength: {bufferLength} ReliableSequenced: {Method}");
}

private void OnNetworkMessageReceived(ushort PlayerID, byte MessageIndex, byte[] buffer, DeliveryMethod Method = DeliveryMethod.ReliableSequenced)
{
string bufferLength = buffer != null ? buffer.Length.ToString() : "null";
Debug.Log($"Avatar: Interpreting {PlayerID} Stage {MessageIndex} BufferLength: {bufferLength} ReliableSequenced: {Method}");
}
}
2 changes: 2 additions & 0 deletions Basis/Packages/com.basis.tests/BasisTestNetwork.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9e1fc2d

Please sign in to comment.