#if COM_UNITY_MODULES_PHYSICS2D using System.Collections; using NUnit.Framework; using Unity.Netcode.Components; using UnityEngine; using UnityEngine.TestTools; using Unity.Netcode.TestHelpers.Runtime; namespace Unity.Netcode.RuntimeTests { public class NetworkRigidbody2DDynamicTest : NetworkRigidbody2DTestBase { public override bool Kinematic => false; } public class NetworkRigidbody2DKinematicTest : NetworkRigidbody2DTestBase { public override bool Kinematic => true; } public abstract class NetworkRigidbody2DTestBase : NetcodeIntegrationTest { protected override int NumberOfClients => 1; public abstract bool Kinematic { get; } protected override void OnCreatePlayerPrefab() { m_PlayerPrefab.AddComponent(); m_PlayerPrefab.AddComponent(); m_PlayerPrefab.AddComponent(); m_PlayerPrefab.GetComponent().interpolation = RigidbodyInterpolation2D.Interpolate; m_PlayerPrefab.GetComponent().isKinematic = Kinematic; } /// /// Tests that a server can destroy a NetworkObject and that it gets despawned correctly. /// /// [UnityTest] public IEnumerator TestRigidbodyKinematicEnableDisable() { // This is the *SERVER VERSION* of the *CLIENT PLAYER* var serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper(); yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation((x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId), m_ServerNetworkManager, serverClientPlayerResult); var serverPlayer = serverClientPlayerResult.Result.gameObject; // This is the *CLIENT VERSION* of the *CLIENT PLAYER* var clientClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper(); yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation((x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId), m_ClientNetworkManagers[0], clientClientPlayerResult); var clientPlayer = clientClientPlayerResult.Result.gameObject; Assert.IsNotNull(serverPlayer); Assert.IsNotNull(clientPlayer); yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5); // server rigidbody has authority and should have a kinematic mode of false Assert.True(serverPlayer.GetComponent().isKinematic == Kinematic); Assert.AreEqual(RigidbodyInterpolation2D.Interpolate, serverPlayer.GetComponent().interpolation); // client rigidbody has no authority and should have a kinematic mode of true Assert.True(clientPlayer.GetComponent().isKinematic); Assert.AreEqual(RigidbodyInterpolation2D.None, clientPlayer.GetComponent().interpolation); // despawn the server player, (but keep it around on the server) serverPlayer.GetComponent().Despawn(false); yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5); // This should equal Kinematic Assert.IsTrue(serverPlayer.GetComponent().isKinematic == Kinematic); yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5); Assert.IsTrue(clientPlayer == null); // safety check that object is actually despawned. } } } #endif // COM_UNITY_MODULES_PHYSICS2D