| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using Newtonsoft.Json; |
| | 5 | | using NUnit.Framework; |
| | 6 | | using System.Collections; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.TestTools; |
| | 9 | |
|
| | 10 | | namespace Tests |
| | 11 | | { |
| | 12 | | public class CharacterControllerTests : IntegrationTestSuite_Legacy |
| | 13 | | { |
| | 14 | | [UnityTest] |
| 2 | 15 | | public IEnumerator CharacterTeleportReposition() { yield return InitCharacterPosition(10, 2, 10); } |
| | 16 | |
|
| 4 | 17 | | public IEnumerator InitCharacterPosition(float x, float y, float z, bool pauseGravity = true) { yield return Ini |
| | 18 | |
|
| | 19 | | public IEnumerator InitCharacterPosition(Vector3 position, bool pauseGravity = true) |
| | 20 | | { |
| 5 | 21 | | if (pauseGravity) |
| 5 | 22 | | DCLCharacterController.i.PauseGravity(); |
| | 23 | | else |
| 0 | 24 | | DCLCharacterController.i.ResumeGravity(); |
| | 25 | |
|
| 5 | 26 | | DCLCharacterController.i.Teleport(JsonUtility.ToJson(position)); |
| | 27 | |
|
| 5 | 28 | | UnityEngine.Assertions.Assert.AreApproximatelyEqual(0, Vector3.Distance(DCLCharacterController.i.characterPo |
| | 29 | |
|
| 5 | 30 | | yield return null; |
| 5 | 31 | | } |
| | 32 | |
|
| | 33 | | public IEnumerator WaitUntilGrounded() |
| | 34 | | { |
| | 35 | | // Let the character *fall* onto the ground shape |
| 0 | 36 | | yield return new WaitUntil(() => DCLCharacterController.i.isGrounded); |
| 0 | 37 | | yield return null; |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | [UnityTest] |
| | 41 | | public IEnumerator CharacterAdjustPosition() |
| | 42 | | { |
| 1 | 43 | | UnityEngine.Assertions.Assert.AreApproximatelyEqual(0, |
| | 44 | | Vector3.Distance(Vector3.zero, |
| | 45 | | CommonScriptableObjects.worldOffset), 0.05f); |
| | 46 | |
|
| 1 | 47 | | Vector3 originalCharacterPosition = new Vector3 |
| | 48 | | { |
| | 49 | | x = 50f, |
| | 50 | | y = 2f, |
| | 51 | | z = 0f |
| | 52 | | }; |
| | 53 | |
|
| 1 | 54 | | yield return InitCharacterPosition(originalCharacterPosition, true); |
| | 55 | |
|
| 1 | 56 | | var pos2 = new Vector3 |
| | 57 | | { |
| | 58 | | x = 50f + PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE, |
| | 59 | | y = 2f, |
| | 60 | | z = 50f + PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE |
| | 61 | | }; |
| | 62 | |
|
| 1 | 63 | | yield return InitCharacterPosition(pos2, true); |
| 1 | 64 | | UnityEngine.Assertions.Assert.AreApproximatelyEqual(0, Vector3.Distance(new Vector3(50f, 2f, 50f), DCLCharac |
| | 65 | |
|
| 1 | 66 | | var pos3 = new Vector3 |
| | 67 | | { |
| | 68 | | x = -50f - PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE, |
| | 69 | | y = 2f, |
| | 70 | | z = -50f - PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE |
| | 71 | | }; |
| | 72 | |
|
| 1 | 73 | | yield return InitCharacterPosition(pos3, true); |
| 1 | 74 | | Assert.AreEqual(new Vector3(-50f, 2f, -50f), DCLCharacterController.i.transform.position); |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | [UnityTest] |
| | 78 | | [Explicit] |
| | 79 | | [Category("Explicit")] |
| | 80 | | public IEnumerator CharacterIsNotParentedOnWorldReposition() |
| | 81 | | { |
| | 82 | | // We use a shape that represents a static ground and has collisions |
| 0 | 83 | | TestHelpers.InstantiateEntityWithShape(scene, "groundShape", DCL.Models.CLASS_ID.PLANE_SHAPE, Vector3.zero); |
| 0 | 84 | | var shapeEntity = scene.entities["groundShape"]; |
| | 85 | |
|
| | 86 | | // Reposition ground shape to be on the world-reposition-limit |
| 0 | 87 | | TestHelpers.SetEntityTransform(scene, shapeEntity, |
| | 88 | | new DCLTransform.Model |
| | 89 | | { |
| | 90 | | position = new Vector3(PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE, 1f, PlayerSettings.WORLD_RE |
| | 91 | | rotation = Quaternion.Euler(90f, 0f, 0f), |
| | 92 | | scale = new Vector3(20, 20, 1) |
| | 93 | | }); |
| | 94 | |
|
| | 95 | | // Place character on the ground shape and check if it's detected as ground |
| 0 | 96 | | DCLCharacterController.i.Teleport(JsonConvert.SerializeObject(new |
| | 97 | | { |
| | 98 | | x = PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE - 2f, |
| | 99 | | y = 3f, |
| | 100 | | z = PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE - 2f |
| | 101 | | })); |
| | 102 | |
|
| 0 | 103 | | yield return WaitUntilGrounded(); |
| | 104 | |
|
| 0 | 105 | | Assert.IsTrue(DCLCharacterController.i.groundTransform == shapeEntity.meshRootGameObject.transform); |
| | 106 | |
|
| | 107 | | // Place the character barely passing the limits to trigger the world repositioning |
| 0 | 108 | | DCLCharacterController.i.Teleport(JsonConvert.SerializeObject(new |
| | 109 | | { |
| | 110 | | x = PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE + 1f, |
| | 111 | | y = DCLCharacterController.i.transform.position.y, |
| | 112 | | z = PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE + 1f |
| | 113 | | })); |
| | 114 | |
|
| 0 | 115 | | yield return null; |
| | 116 | |
|
| | 117 | | // check if the character got repositioned correctly |
| 0 | 118 | | Assert.AreEqual(new Vector3(1f, DCLCharacterController.i.transform.position.y, 1f), DCLCharacterController.i |
| | 119 | |
|
| | 120 | | // check it's not parented but still has the same ground |
| 0 | 121 | | Assert.IsTrue(DCLCharacterController.i.groundTransform == shapeEntity.meshRootGameObject.transform); |
| 0 | 122 | | Assert.IsTrue(DCLCharacterController.i.transform.parent == null); |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | [UnityTest] |
| | 126 | | public IEnumerator Character_UpdateSOPosition() |
| | 127 | | { |
| 1 | 128 | | yield return InitCharacterPosition(50, 2, 0); |
| 1 | 129 | | Assert.AreEqual(new Vector3(50f, 2f, 0f), CommonScriptableObjects.playerUnityPosition.Get()); |
| 1 | 130 | | } |
| | 131 | |
|
| | 132 | | [UnityTest] |
| | 133 | | [Explicit] |
| | 134 | | [Category("Explicit")] |
| | 135 | | public IEnumerator Character_UpdateSORotation() |
| | 136 | | { |
| 0 | 137 | | DCLCharacterController.i.PauseGravity(); |
| | 138 | |
|
| 0 | 139 | | var newEulerAngle = 10f; |
| 0 | 140 | | CommonScriptableObjects.characterForward.Set(Quaternion.Euler(new Vector3(0, newEulerAngle, 0)) * Vector3.fo |
| 0 | 141 | | Cursor.lockState = CursorLockMode.Locked; |
| 0 | 142 | | yield return new WaitForSeconds(0.1f); |
| | 143 | |
|
| 0 | 144 | | Assert.AreEqual(DCLCharacterController.i.transform.eulerAngles, CommonScriptableObjects.playerUnityEulerAngl |
| 0 | 145 | | DCLCharacterController.i.ResumeGravity(); |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | [UnityTest] |
| | 149 | | [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme |
| | 150 | | [Category("Explicit")] |
| | 151 | | public IEnumerator CharacterSupportsMovingPlatforms() |
| | 152 | | { |
| 0 | 153 | | Vector3 originalCharacterPosition = new Vector3 |
| | 154 | | { |
| | 155 | | x = 2f, |
| | 156 | | y = 3f, |
| | 157 | | z = 8f |
| | 158 | | }; |
| | 159 | |
|
| 0 | 160 | | yield return InitCharacterPosition(originalCharacterPosition); |
| | 161 | |
|
| 0 | 162 | | string platformEntityId = "movingPlatform"; |
| 0 | 163 | | TestHelpers.InstantiateEntityWithShape(scene, platformEntityId, DCL.Models.CLASS_ID.BOX_SHAPE, new Vector3(2 |
| | 164 | |
|
| 0 | 165 | | Transform platformTransform = scene.entities[platformEntityId].gameObject.transform; |
| 0 | 166 | | platformTransform.localScale = new Vector3(2f, 0.5f, 2f); |
| | 167 | |
|
| 0 | 168 | | yield return null; |
| 0 | 169 | | Assert.IsTrue(Vector3.Distance(platformTransform.position, new Vector3(2f, 1f, 8f)) < 0.1f); |
| | 170 | |
|
| | 171 | | // enable character gravity |
| 0 | 172 | | DCLCharacterController.i.ResumeGravity(); |
| | 173 | |
|
| 0 | 174 | | yield return WaitUntilGrounded(); |
| | 175 | |
|
| 0 | 176 | | Assert.IsFalse(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be true only if the p |
| | 177 | |
|
| | 178 | | // Lerp the platform's position |
| 0 | 179 | | float lerpTime = 0f; |
| 0 | 180 | | float lerpSpeed = 2f; |
| 0 | 181 | | Vector3 originalPosition = platformTransform.position; |
| 0 | 182 | | Vector3 targetPosition = new Vector3(10f, 1f, 8f); |
| | 183 | |
|
| 0 | 184 | | bool checkedParent = false; |
| | 185 | |
|
| 0 | 186 | | while (lerpTime < 1f) |
| | 187 | | { |
| 0 | 188 | | yield return null; |
| 0 | 189 | | lerpTime += Time.deltaTime * lerpSpeed; |
| | 190 | |
|
| 0 | 191 | | if (lerpTime > 1f) |
| 0 | 192 | | lerpTime = 1f; |
| | 193 | |
|
| 0 | 194 | | platformTransform.position = Vector3.Lerp(originalPosition, targetPosition, lerpTime); |
| | 195 | |
|
| 0 | 196 | | DCLCharacterController.i.LateUpdate(); |
| | 197 | |
|
| 0 | 198 | | if (!checkedParent && lerpTime >= 0.25f) |
| | 199 | | { |
| 0 | 200 | | Assert.IsTrue(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be true when t |
| 0 | 201 | | checkedParent = true; |
| | 202 | | } |
| | 203 | | } |
| | 204 | |
|
| | 205 | | // check positions |
| 0 | 206 | | Assert.IsTrue(Vector3.Distance(platformTransform.position, targetPosition) < 0.1f); |
| | 207 | |
|
| 0 | 208 | | float dist1 = Vector3.Distance(originalCharacterPosition, DCLCharacterController.i.transform.position); |
| 0 | 209 | | float dist2 = Vector3.Distance(originalPosition, targetPosition); |
| | 210 | |
|
| 0 | 211 | | UnityEngine.Assertions.Assert.AreApproximatelyEqual(dist1, dist2, 1f); |
| 0 | 212 | | } |
| | 213 | |
|
| | 214 | | [UnityTest] |
| | 215 | | [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme |
| | 216 | | [Category("Explicit")] |
| | 217 | | public IEnumerator CharacterSupportsRotatingPlatforms() |
| | 218 | | { |
| 0 | 219 | | Vector3 originalCharacterPosition = new Vector3 |
| | 220 | | { |
| | 221 | | x = 5f, |
| | 222 | | y = 3f, |
| | 223 | | z = 5f |
| | 224 | | }; |
| | 225 | |
|
| 0 | 226 | | yield return InitCharacterPosition(originalCharacterPosition); |
| | 227 | |
|
| 0 | 228 | | string platformEntityId = "rotatingPlatform"; |
| 0 | 229 | | TestHelpers.InstantiateEntityWithShape(scene, platformEntityId, DCL.Models.CLASS_ID.BOX_SHAPE, new Vector3(8 |
| | 230 | |
|
| 0 | 231 | | Transform platformTransform = scene.entities[platformEntityId].gameObject.transform; |
| 0 | 232 | | platformTransform.localScale = new Vector3(8f, 0.5f, 8f); |
| | 233 | |
|
| 0 | 234 | | yield return null; |
| 0 | 235 | | Assert.IsTrue(Vector3.Distance(platformTransform.position, new Vector3(8f, 1f, 8f)) < 0.1f); |
| | 236 | |
|
| | 237 | | // enable character gravity |
| 0 | 238 | | DCLCharacterController.i.ResumeGravity(); |
| | 239 | |
|
| 0 | 240 | | yield return WaitUntilGrounded(); |
| | 241 | |
|
| 0 | 242 | | Assert.IsFalse(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be true only if the p |
| | 243 | |
|
| | 244 | | // Lerp the platform's rotation |
| 0 | 245 | | float lerpTime = 0f; |
| 0 | 246 | | float lerpSpeed = 1f; |
| 0 | 247 | | Quaternion initialRotation = Quaternion.identity; |
| 0 | 248 | | Quaternion targetRotation = Quaternion.Euler(0, 180f, 0f); |
| | 249 | |
|
| 0 | 250 | | bool checkedParent = false; |
| 0 | 251 | | while (lerpTime < 1f) |
| | 252 | | { |
| 0 | 253 | | yield return null; |
| 0 | 254 | | lerpTime += Time.deltaTime * lerpSpeed; |
| | 255 | |
|
| 0 | 256 | | if (lerpTime > 1f) |
| 0 | 257 | | lerpTime = 1f; |
| | 258 | |
|
| 0 | 259 | | platformTransform.rotation = Quaternion.Lerp(initialRotation, targetRotation, lerpTime); |
| | 260 | |
|
| 0 | 261 | | if (!checkedParent && lerpTime >= 0.5f) |
| | 262 | | { |
| 0 | 263 | | Assert.IsTrue(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be true when t |
| | 264 | |
|
| 0 | 265 | | checkedParent = true; |
| | 266 | | } |
| | 267 | | } |
| | 268 | |
|
| | 269 | | // check positions |
| 0 | 270 | | Assert.IsTrue(Vector3.Distance(platformTransform.rotation.eulerAngles, targetRotation.eulerAngles) < 0.1f); |
| | 271 | |
|
| 0 | 272 | | UnityEngine.Assertions.Assert.AreApproximatelyEqual(DCLCharacterController.i.transform.position.x, 11f, 1f); |
| 0 | 273 | | UnityEngine.Assertions.Assert.AreApproximatelyEqual(DCLCharacterController.i.transform.position.z, 11f, 1f); |
| | 274 | |
|
| | 275 | | // remove platform and check character parent |
| 0 | 276 | | TestHelpers.RemoveSceneEntity(scene, platformEntityId); |
| 0 | 277 | | yield return null; |
| | 278 | |
|
| 0 | 279 | | Assert.IsFalse(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be false as there's n |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | [UnityTest] |
| | 283 | | [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme |
| | 284 | | [Category("Explicit")] |
| | 285 | | public IEnumerator CharacterIsReleasedOnEntityRemoval() |
| | 286 | | { |
| 0 | 287 | | yield return CharacterSupportsMovingPlatforms(); |
| | 288 | |
|
| | 289 | | // remove platform and check character parent |
| 0 | 290 | | string platformEntityId = "movingPlatform"; |
| 0 | 291 | | TestHelpers.RemoveSceneEntity(scene, platformEntityId); |
| 0 | 292 | | yield return null; |
| 0 | 293 | | yield return null; |
| 0 | 294 | | yield return null; |
| | 295 | |
|
| 0 | 296 | | Assert.IsNull(DCLCharacterController.i.transform.parent, "The character shouldn't be parented as there's no |
| 0 | 297 | | } |
| | 298 | |
|
| | 299 | | [UnityTest] |
| | 300 | | [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme |
| | 301 | | [Category("Explicit")] |
| | 302 | | public IEnumerator CharacterIsReleasedOnPlatformCollisionToggle() |
| | 303 | | { |
| 0 | 304 | | yield return CharacterSupportsMovingPlatforms(); |
| | 305 | |
|
| | 306 | | // Disable shape colliders |
| 0 | 307 | | string platformEntityId = "movingPlatform"; |
| 0 | 308 | | var shapeComponent = scene.entities[platformEntityId].GetSharedComponent(typeof(BaseShape)); |
| 0 | 309 | | yield return TestHelpers.SharedComponentUpdate(shapeComponent, new BaseShape.Model() |
| | 310 | | { |
| | 311 | | withCollisions = false |
| | 312 | | }); |
| | 313 | |
|
| 0 | 314 | | yield return null; |
| 0 | 315 | | yield return null; |
| | 316 | |
|
| 0 | 317 | | Assert.IsNull(DCLCharacterController.i.transform.parent, "The character shouldn't be parented as the shape c |
| 0 | 318 | | } |
| | 319 | |
|
| | 320 | | [UnityTest] |
| | 321 | | [NUnit.Framework.Explicit("This test started failing on the CI out of the blue. Will be re-enabled after impleme |
| | 322 | | [Category("Explicit")] |
| | 323 | | public IEnumerator CharacterIsReleasedOnShapeRemoval() |
| | 324 | | { |
| 0 | 325 | | yield return CharacterSupportsMovingPlatforms(); |
| | 326 | |
|
| | 327 | | // remove shape component |
| 0 | 328 | | string platformEntityId = "movingPlatform"; |
| 0 | 329 | | var shapeComponent = scene.entities[platformEntityId].GetSharedComponent(typeof(BaseShape)); |
| 0 | 330 | | TestHelpers.DetachSharedComponent(scene, platformEntityId, shapeComponent.id); |
| | 331 | |
|
| 0 | 332 | | yield return null; |
| 0 | 333 | | yield return null; |
| | 334 | |
|
| 0 | 335 | | Assert.IsFalse(DCLCharacterController.i.isOnMovingPlatform, "isOnMovingPlatform should be false when the sha |
| 0 | 336 | | } |
| | 337 | | } |
| | 338 | | } |