| | 1 | | using AvatarSystem; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using System; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class AvatarMovementController : MonoBehaviour, IPoolLifecycleHandler, IAvatarMovementController |
| | 10 | | { |
| | 11 | | // Speed values are slightly slower than the player |
| | 12 | | private const float WALK_SPEED = 4f; |
| | 13 | | private const float RUN_SPEED = 10.0f; |
| | 14 | |
|
| | 15 | | private const float SPEED_GRAVITY = 11.0f; |
| | 16 | | private const float ROTATION_SPEED = 6.25f; |
| | 17 | | private const float SPEED_EPSILON = 0.0001f; |
| | 18 | | private const float WALK_DISTANCE = 1.5f; |
| 314 | 19 | | private float movementSpeed = WALK_SPEED; |
| | 20 | |
|
| | 21 | | private Transform avatarTransformValue; |
| | 22 | |
|
| | 23 | | private Quaternion targetRotation; |
| 314 | 24 | | private Vector3 currentWorldPosition = Vector3.zero; |
| | 25 | | private Vector3 targetPosition; |
| | 26 | |
|
| | 27 | | private float movementLerpWait = 0f; |
| | 28 | | private float movementLerpWaitCounter = 0f; |
| | 29 | |
|
| | 30 | |
|
| | 31 | | private Transform AvatarTransform |
| | 32 | | { |
| | 33 | | get |
| | 34 | | { |
| 2 | 35 | | if (avatarTransformValue == null) |
| | 36 | | { |
| 1 | 37 | | avatarTransformValue = GetComponent<AvatarShape>().entity.gameObject.transform; |
| 1 | 38 | | enabled = true; |
| | 39 | | } |
| | 40 | |
|
| 2 | 41 | | return avatarTransformValue; |
| | 42 | | } |
| | 43 | | set |
| | 44 | | { |
| 3 | 45 | | avatarTransformValue = value; |
| | 46 | |
|
| 3 | 47 | | if (value == null) |
| 1 | 48 | | enabled = false; |
| 3 | 49 | | } |
| | 50 | | } |
| | 51 | |
|
| | 52 | | private Vector3 CurrentPosition |
| | 53 | | { |
| 1 | 54 | | get { return currentWorldPosition; } |
| | 55 | | set |
| | 56 | | { |
| 0 | 57 | | currentWorldPosition = value; |
| 0 | 58 | | Vector3 newPosition = PositionUtils.WorldToUnityPosition(currentWorldPosition); |
| 0 | 59 | | if (float.IsNaN(newPosition.x) || float.IsInfinity(newPosition.x) || |
| | 60 | | float.IsNaN(newPosition.z) || float.IsInfinity(newPosition.z) || |
| | 61 | | float.IsNaN(newPosition.y) || float.IsInfinity(newPosition.y)) |
| 0 | 62 | | return; |
| | 63 | |
|
| 0 | 64 | | AvatarTransform.position = newPosition; |
| 0 | 65 | | } |
| | 66 | | } |
| | 67 | |
|
| | 68 | | private Quaternion CurrentRotation |
| | 69 | | { |
| 1 | 70 | | get { return AvatarTransform.rotation; } |
| 2 | 71 | | set { AvatarTransform.rotation = value; } |
| | 72 | | } |
| | 73 | |
|
| 1 | 74 | | public void OnPoolGet() { } |
| | 75 | |
|
| | 76 | | public void OnPoolRelease() |
| | 77 | | { |
| 1 | 78 | | AvatarTransform = null; |
| | 79 | |
|
| 1 | 80 | | currentWorldPosition = Vector3.zero; |
| 1 | 81 | | } |
| | 82 | |
|
| | 83 | | public void SetAvatarTransform(Transform avatarTransform) |
| | 84 | | { |
| 2 | 85 | | AvatarTransform = avatarTransform; |
| 2 | 86 | | } |
| | 87 | |
|
| 622 | 88 | | private void OnEnable() { CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; } |
| | 89 | |
|
| 622 | 90 | | private void OnDisable() { CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; } |
| | 91 | |
|
| | 92 | | private void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 93 | | { |
| 0 | 94 | | AvatarTransform.position = PositionUtils.WorldToUnityPosition(currentWorldPosition); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void OnTransformChanged(Vector3 newPosition, Quaternion newRotation) |
| | 98 | | { |
| 0 | 99 | | OnTransformChanged(newPosition, newRotation, false); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public void OnTransformChanged(Vector3 position, Quaternion rotation, bool immediate) |
| | 103 | | { |
| 0 | 104 | | float characterMinHeight = DCLCharacterController.i.characterController.height * 0.5f; |
| | 105 | |
|
| 0 | 106 | | MoveTo( |
| | 107 | | new Vector3(position.x, Math.Max(position.y - characterMinHeight, -characterMinHeight), position.z), // |
| | 108 | | rotation, |
| | 109 | | immediate); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | public void MoveTo(Vector3 position, Quaternion rotation, bool immediate = false) |
| | 113 | | { |
| 0 | 114 | | if (immediate) |
| | 115 | | { |
| 0 | 116 | | CurrentPosition = position; |
| 0 | 117 | | AvatarTransform.rotation = rotation; |
| | 118 | | } |
| | 119 | |
|
| 0 | 120 | | Vector3 flatEulerRotation = rotation.eulerAngles; |
| 0 | 121 | | flatEulerRotation.z = flatEulerRotation.x = 0; |
| 0 | 122 | | rotation = Quaternion.Euler(flatEulerRotation); |
| | 123 | |
|
| 0 | 124 | | targetPosition = position; |
| 0 | 125 | | targetRotation = rotation; |
| | 126 | |
|
| 0 | 127 | | float distance = Vector3.Distance(targetPosition, CurrentPosition); |
| | 128 | |
|
| 0 | 129 | | if (distance >= 50) |
| | 130 | | { |
| 0 | 131 | | CurrentPosition = position; |
| 0 | 132 | | AvatarTransform.rotation = rotation; |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | if (distance >= WALK_DISTANCE) |
| 0 | 136 | | movementSpeed = Mathf.MoveTowards(movementSpeed, RUN_SPEED, Time.deltaTime * RUN_SPEED * 10); |
| | 137 | | else |
| 0 | 138 | | movementSpeed = Mathf.MoveTowards(movementSpeed, WALK_SPEED, Time.deltaTime * RUN_SPEED * 30); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | void UpdateLerp(float deltaTime) |
| | 142 | | { |
| 1 | 143 | | if (Vector3.SqrMagnitude(CurrentPosition - targetPosition) < SPEED_EPSILON) |
| | 144 | | { |
| 1 | 145 | | UpdateRotation(deltaTime, targetRotation); |
| 1 | 146 | | return; |
| | 147 | | } |
| | 148 | |
|
| | 149 | | //NOTE(Brian): When we update movement we don't update rotation, because the Avatar will face the movement f |
| 0 | 150 | | UpdateMovement(deltaTime); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | private void UpdateRotation(float deltaTime, Quaternion targetRotation) |
| | 154 | | { |
| 1 | 155 | | CurrentRotation = Quaternion.Slerp(CurrentRotation, targetRotation, ROTATION_SPEED * deltaTime); |
| 1 | 156 | | } |
| | 157 | |
|
| | 158 | | private void UpdateMovement(float deltaTime) |
| | 159 | | { |
| 0 | 160 | | Vector3 flattenedDiff = targetPosition - CurrentPosition; |
| 0 | 161 | | flattenedDiff.y = 0; |
| | 162 | |
|
| | 163 | | //NOTE(Brian): Avoid Unity error when computing look rotation for 0 magnitude vectors. |
| | 164 | | // Note that this isn't the same as the previous distance check because this |
| | 165 | | // is computed with a flattened vector. |
| 0 | 166 | | if (flattenedDiff != Vector3.zero) |
| | 167 | | { |
| 0 | 168 | | Quaternion lookRotation = Quaternion.LookRotation(flattenedDiff, Vector3.up); |
| 0 | 169 | | UpdateRotation(deltaTime, lookRotation); |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | Vector3 direction = (targetPosition - CurrentPosition).normalized; |
| 0 | 173 | | Vector3 delta = direction * (movementSpeed * deltaTime); |
| | 174 | |
|
| | 175 | | //NOTE(Brian): We need a separate value for Y movement because the gravity has to be lerped faster. |
| 0 | 176 | | delta.y = direction.y * SPEED_GRAVITY * deltaTime; |
| | 177 | |
|
| | 178 | | //NOTE(Brian): If we overshoot targetPosition we adjust the delta value accordingly. |
| 0 | 179 | | if (delta.sqrMagnitude > Vector3.SqrMagnitude(targetPosition - CurrentPosition)) |
| | 180 | | { |
| 0 | 181 | | delta = targetPosition - CurrentPosition; |
| | 182 | | } |
| | 183 | |
|
| 0 | 184 | | CurrentPosition += delta; |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | private void Update() |
| | 188 | | { |
| 1 | 189 | | movementLerpWaitCounter += Time.deltaTime; |
| 1 | 190 | | if (movementLerpWaitCounter >= movementLerpWait) |
| | 191 | | { |
| 1 | 192 | | UpdateLerp(movementLerpWaitCounter); |
| 1 | 193 | | movementLerpWaitCounter = 0f; |
| | 194 | | } |
| 1 | 195 | | } |
| | 196 | |
|
| 0 | 197 | | public void SetMovementLerpWait(float secondsBetweenUpdates) { movementLerpWait = secondsBetweenUpdates; } |
| | 198 | | } |
| | 199 | | } |