| | 1 | | using System; |
| | 2 | | using AvatarSystem; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Components; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public class AvatarAnimatorLegacy : MonoBehaviour, IPoolLifecycleHandler, IAnimator |
| | 9 | | { |
| | 10 | | const float IDLE_TRANSITION_TIME = 0.2f; |
| | 11 | | const float STRAFE_TRANSITION_TIME = 0.25f; |
| | 12 | | const float RUN_TRANSITION_TIME = 0.15f; |
| | 13 | | const float WALK_TRANSITION_TIME = 0.15f; |
| | 14 | | const float JUMP_TRANSITION_TIME = 0.01f; |
| | 15 | | const float FALL_TRANSITION_TIME = 0.5f; |
| | 16 | | const float EXPRESSION_TRANSITION_TIME = 0.2f; |
| | 17 | |
|
| | 18 | | const float AIR_EXIT_TRANSITION_TIME = 0.2f; |
| | 19 | | const float GROUND_BLENDTREE_TRANSITION_TIME = 0.15f; |
| | 20 | |
|
| | 21 | | const float RUN_SPEED_THRESHOLD = 0.05f; |
| | 22 | | const float WALK_SPEED_THRESHOLD = 0.03f; |
| | 23 | |
|
| | 24 | | const float ELEVATION_OFFSET = 0.6f; |
| | 25 | | const float RAY_OFFSET_LENGTH = 3.0f; |
| | 26 | |
|
| | 27 | | const float MAX_VELOCITY = 6.25f; |
| | 28 | |
|
| | 29 | | [System.Serializable] |
| | 30 | | public class BaseClipsIds |
| | 31 | | { |
| | 32 | | public string idle; |
| | 33 | | public string walk; |
| | 34 | | public string run; |
| | 35 | | public string jump; |
| | 36 | | public string fall; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | [System.Serializable] |
| | 40 | | public class BlackBoard |
| | 41 | | { |
| | 42 | | public float walkSpeedFactor; |
| | 43 | | public float runSpeedFactor; |
| | 44 | | public float movementSpeed; |
| | 45 | | public float verticalSpeed; |
| | 46 | | public bool isGrounded; |
| | 47 | | public string expressionTriggerId; |
| | 48 | | public long expressionTriggerTimestamp; |
| | 49 | | public float deltaTime; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | [SerializeField] internal AvatarAnimationsVariable maleAnimations; |
| | 53 | | [SerializeField] internal AvatarAnimationsVariable femaleAnimations; |
| | 54 | |
|
| | 55 | | public new Animation animation; |
| | 56 | | public BaseClipsIds baseClipsIds; |
| | 57 | | public BlackBoard blackboard; |
| | 58 | | public Transform target; |
| | 59 | |
|
| 1332 | 60 | | [SerializeField] float runMinSpeed = 6f; |
| 1332 | 61 | | [SerializeField] float walkMinSpeed = 0.1f; |
| | 62 | |
|
| | 63 | | internal System.Action<BlackBoard> currentState; |
| | 64 | |
|
| | 65 | | Vector3 lastPosition; |
| | 66 | | AvatarAnimationsVariable currentAnimations; |
| | 67 | | bool isOwnPlayer = false; |
| | 68 | | private AvatarAnimationEventHandler animEventHandler; |
| | 69 | |
|
| 1146 | 70 | | public void Start() { OnPoolGet(); } |
| | 71 | |
|
| | 72 | | public void OnPoolGet() |
| | 73 | | { |
| 573 | 74 | | if (DCLCharacterController.i != null) |
| | 75 | | { |
| 573 | 76 | | isOwnPlayer = DCLCharacterController.i.transform == transform.parent; |
| | 77 | |
|
| | 78 | | // NOTE: disable MonoBehaviour's update to use DCLCharacterController event instead |
| 573 | 79 | | this.enabled = !isOwnPlayer; |
| | 80 | |
|
| 573 | 81 | | if (isOwnPlayer) |
| | 82 | | { |
| 571 | 83 | | DCLCharacterController.i.OnUpdateFinish += Update; |
| | 84 | | } |
| | 85 | | } |
| | 86 | |
|
| 573 | 87 | | currentState = State_Init; |
| 573 | 88 | | } |
| | 89 | |
|
| | 90 | | public void OnPoolRelease() |
| | 91 | | { |
| 0 | 92 | | if (isOwnPlayer && DCLCharacterController.i) |
| | 93 | | { |
| 0 | 94 | | DCLCharacterController.i.OnUpdateFinish -= Update; |
| | 95 | | } |
| 0 | 96 | | } |
| | 97 | |
|
| 6 | 98 | | void Update() { Update(Time.deltaTime); } |
| | 99 | |
|
| | 100 | | void Update(float deltaTime) |
| | 101 | | { |
| 6002 | 102 | | if (target == null || animation == null) |
| 6002 | 103 | | return; |
| | 104 | |
|
| 0 | 105 | | blackboard.deltaTime = deltaTime; |
| 0 | 106 | | UpdateInterface(); |
| 0 | 107 | | currentState?.Invoke(blackboard); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | void UpdateInterface() |
| | 111 | | { |
| 0 | 112 | | Vector3 velocityTargetPosition = target.position; |
| 0 | 113 | | Vector3 flattenedVelocity = velocityTargetPosition - lastPosition; |
| | 114 | |
|
| | 115 | | //NOTE(Brian): Vertical speed |
| 0 | 116 | | float verticalVelocity = flattenedVelocity.y; |
| 0 | 117 | | blackboard.verticalSpeed = verticalVelocity; |
| | 118 | |
|
| 0 | 119 | | flattenedVelocity.y = 0; |
| | 120 | |
|
| 0 | 121 | | if (isOwnPlayer) |
| 0 | 122 | | blackboard.movementSpeed = flattenedVelocity.magnitude - DCLCharacterController.i.movingPlatformSpeed; |
| | 123 | | else |
| 0 | 124 | | blackboard.movementSpeed = flattenedVelocity.magnitude; |
| | 125 | |
|
| 0 | 126 | | Vector3 rayOffset = Vector3.up * RAY_OFFSET_LENGTH; |
| | 127 | | //NOTE(Brian): isGrounded? |
| 0 | 128 | | blackboard.isGrounded = Physics.Raycast(target.transform.position + rayOffset, |
| | 129 | | Vector3.down, |
| | 130 | | RAY_OFFSET_LENGTH - ELEVATION_OFFSET, |
| | 131 | | DCLCharacterController.i.groundLayers); |
| | 132 | |
|
| | 133 | | #if UNITY_EDITOR |
| 0 | 134 | | Debug.DrawRay(target.transform.position + rayOffset, Vector3.down * (RAY_OFFSET_LENGTH - ELEVATION_OFFSET), blac |
| | 135 | | #endif |
| | 136 | |
|
| 0 | 137 | | lastPosition = velocityTargetPosition; |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | void State_Init(BlackBoard bb) |
| | 141 | | { |
| 0 | 142 | | if (bb.isGrounded == true) |
| | 143 | | { |
| 0 | 144 | | currentState = State_Ground; |
| 0 | 145 | | } |
| | 146 | | else |
| | 147 | | { |
| 0 | 148 | | currentState = State_Air; |
| | 149 | | } |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | void State_Ground(BlackBoard bb) |
| | 153 | | { |
| 0 | 154 | | if (bb.deltaTime <= 0) |
| | 155 | | { |
| 0 | 156 | | Debug.LogError("deltaTime should be > 0", gameObject); |
| 0 | 157 | | return; |
| | 158 | | } |
| | 159 | |
|
| 0 | 160 | | animation[baseClipsIds.run].normalizedSpeed = bb.movementSpeed / bb.deltaTime * bb.runSpeedFactor; |
| 0 | 161 | | animation[baseClipsIds.walk].normalizedSpeed = bb.movementSpeed / bb.deltaTime * bb.walkSpeedFactor; |
| | 162 | |
|
| 0 | 163 | | float movementSpeed = bb.movementSpeed / bb.deltaTime; |
| | 164 | |
|
| 0 | 165 | | if (movementSpeed > runMinSpeed) |
| | 166 | | { |
| 0 | 167 | | animation.CrossFade(baseClipsIds.run, RUN_TRANSITION_TIME); |
| 0 | 168 | | } |
| 0 | 169 | | else if (movementSpeed > walkMinSpeed) |
| | 170 | | { |
| 0 | 171 | | animation.CrossFade(baseClipsIds.walk, WALK_TRANSITION_TIME); |
| 0 | 172 | | } |
| | 173 | | else |
| | 174 | | { |
| 0 | 175 | | animation.CrossFade(baseClipsIds.idle, IDLE_TRANSITION_TIME); |
| | 176 | | } |
| | 177 | |
|
| 0 | 178 | | if (!bb.isGrounded) |
| | 179 | | { |
| 0 | 180 | | currentState = State_Air; |
| 0 | 181 | | Update(bb.deltaTime); |
| | 182 | | } |
| 0 | 183 | | } |
| | 184 | |
|
| | 185 | | void State_Air(BlackBoard bb) |
| | 186 | | { |
| 0 | 187 | | if (bb.verticalSpeed > 0) |
| | 188 | | { |
| 0 | 189 | | animation.CrossFade(baseClipsIds.jump, JUMP_TRANSITION_TIME, PlayMode.StopAll); |
| 0 | 190 | | } |
| | 191 | | else |
| | 192 | | { |
| 0 | 193 | | animation.CrossFade(baseClipsIds.fall, FALL_TRANSITION_TIME, PlayMode.StopAll); |
| | 194 | | } |
| | 195 | |
|
| 0 | 196 | | if (bb.isGrounded) |
| | 197 | | { |
| 0 | 198 | | animation.Blend(baseClipsIds.jump, 0, AIR_EXIT_TRANSITION_TIME); |
| 0 | 199 | | animation.Blend(baseClipsIds.fall, 0, AIR_EXIT_TRANSITION_TIME); |
| 0 | 200 | | currentState = State_Ground; |
| 0 | 201 | | Update(bb.deltaTime); |
| | 202 | | } |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | internal void State_Expression(BlackBoard bb) |
| | 206 | | { |
| 0 | 207 | | var animationInfo = animation[bb.expressionTriggerId]; |
| 0 | 208 | | animation.CrossFade(bb.expressionTriggerId, EXPRESSION_TRANSITION_TIME, PlayMode.StopAll); |
| | 209 | |
|
| 0 | 210 | | var mustExit = Math.Abs(bb.movementSpeed) > Mathf.Epsilon || animationInfo.length - animationInfo.time < EXPRESS |
| 0 | 211 | | if (mustExit) |
| | 212 | | { |
| 0 | 213 | | animation.Blend(bb.expressionTriggerId, 0, EXPRESSION_TRANSITION_TIME); |
| 0 | 214 | | bb.expressionTriggerId = null; |
| 0 | 215 | | if (!bb.isGrounded) |
| 0 | 216 | | currentState = State_Air; |
| | 217 | | else |
| 0 | 218 | | currentState = State_Ground; |
| | 219 | |
|
| 0 | 220 | | Update(bb.deltaTime); |
| 0 | 221 | | } |
| | 222 | | else |
| | 223 | | { |
| 0 | 224 | | animation.Blend(bb.expressionTriggerId, 1, EXPRESSION_TRANSITION_TIME / 2f); |
| | 225 | | } |
| 0 | 226 | | } |
| | 227 | |
|
| | 228 | | public void SetExpressionValues(string expressionTriggerId, long expressionTriggerTimestamp) |
| | 229 | | { |
| 2 | 230 | | if (animation == null) |
| 2 | 231 | | return; |
| | 232 | |
|
| 0 | 233 | | if (string.IsNullOrEmpty(expressionTriggerId)) |
| 0 | 234 | | return; |
| | 235 | |
|
| 0 | 236 | | var mustTriggerAnimation = !string.IsNullOrEmpty(expressionTriggerId) && blackboard.expressionTriggerTimestamp ! |
| | 237 | |
|
| 0 | 238 | | blackboard.expressionTriggerId = expressionTriggerId; |
| 0 | 239 | | blackboard.expressionTriggerTimestamp = expressionTriggerTimestamp; |
| | 240 | |
|
| 0 | 241 | | if (mustTriggerAnimation) |
| | 242 | | { |
| 0 | 243 | | if (!string.IsNullOrEmpty(expressionTriggerId)) |
| | 244 | | { |
| 0 | 245 | | animation.Stop(expressionTriggerId); |
| | 246 | | } |
| | 247 | |
|
| 0 | 248 | | currentState = State_Expression; |
| 0 | 249 | | Update(); |
| | 250 | | } |
| 0 | 251 | | } |
| | 252 | |
|
| | 253 | | public void Reset() |
| | 254 | | { |
| 0 | 255 | | if (animation == null) |
| 0 | 256 | | return; |
| | 257 | |
|
| | 258 | | //It will set the animation to the first frame, but due to the nature of the script and its Update. It wont stop |
| 0 | 259 | | animation.Stop(); |
| 0 | 260 | | } |
| | 261 | |
|
| 0 | 262 | | public void SetIdleFrame() { animation.Play(baseClipsIds.idle); } |
| | 263 | |
|
| | 264 | | public void BindBodyShape(Animation animation, string bodyShapeType, Transform target) |
| | 265 | | { |
| 0 | 266 | | this.target = target; |
| 0 | 267 | | this.animation = animation; |
| | 268 | |
|
| 0 | 269 | | if (bodyShapeType.Contains(WearableLiterals.BodyShapes.MALE)) |
| | 270 | | { |
| 0 | 271 | | currentAnimations = maleAnimations; |
| 0 | 272 | | } |
| 0 | 273 | | else if (bodyShapeType.Contains(WearableLiterals.BodyShapes.FEMALE)) |
| | 274 | | { |
| 0 | 275 | | currentAnimations = femaleAnimations; |
| | 276 | | } |
| | 277 | |
|
| 0 | 278 | | for (var i = 0; i < currentAnimations.Get().Length; i++) |
| | 279 | | { |
| 0 | 280 | | var animationToId = currentAnimations.Get()[i]; |
| 0 | 281 | | if (this.animation.GetClip(animationToId.id) == null) |
| | 282 | | { |
| | 283 | | // animationToId.id and animationToId.clip.name must be the same or we get big performance drop here |
| | 284 | | // Already coordinated with art team to have the animations with the correct ids |
| 0 | 285 | | this.animation.AddClip(animationToId.clip, animationToId.id); |
| | 286 | | } |
| | 287 | | } |
| | 288 | |
|
| 0 | 289 | | SetIdleFrame(); |
| 0 | 290 | | animation.Sample(); |
| 0 | 291 | | } |
| | 292 | |
|
| | 293 | | // AvatarSystem entry points |
| | 294 | | public bool Prepare(string bodyshapeId, GameObject container) |
| | 295 | | { |
| 0 | 296 | | if (!container.transform.TryFindChildRecursively("Armature", out Transform armature)) |
| | 297 | | { |
| 0 | 298 | | Debug.LogError($"Couldn't find Armature for AnimatorLegacy in path: {transform.GetHierarchyPath()}"); |
| 0 | 299 | | return false; |
| | 300 | | } |
| 0 | 301 | | Transform armatureParent = armature.parent; |
| 0 | 302 | | animation = armatureParent.gameObject.GetOrCreateComponent<Animation>(); |
| 0 | 303 | | armatureParent.gameObject.GetOrCreateComponent<StickerAnimationListener>(); |
| | 304 | |
|
| 0 | 305 | | BindBodyShape(animation, bodyshapeId, target); |
| 0 | 306 | | InitializeAvatarAudioAndParticleHandlers(animation); |
| 0 | 307 | | return true; |
| | 308 | | } |
| | 309 | |
|
| 4 | 310 | | public void PlayExpression(string expressionId, long timestamps) { SetExpressionValues(expressionId, timestamps); } |
| | 311 | |
|
| | 312 | | private void InitializeAvatarAudioAndParticleHandlers(Animation createdAnimation) |
| | 313 | | { |
| | 314 | | //NOTE(Mordi): Adds handler for animation events, and passes in the audioContainer for the avatar |
| 0 | 315 | | AvatarAnimationEventHandler animationEventHandler = createdAnimation.gameObject.AddComponent<AvatarAnimationEven |
| 0 | 316 | | AudioContainer audioContainer = transform.GetComponentInChildren<AudioContainer>(); |
| 0 | 317 | | if (audioContainer != null) |
| | 318 | | { |
| 0 | 319 | | animationEventHandler.Init(audioContainer); |
| | 320 | |
|
| | 321 | | //NOTE(Mordi): If this is a remote avatar, pass the animation component so we can keep track of whether it i |
| 0 | 322 | | AvatarAudioHandlerRemote audioHandlerRemote = audioContainer.GetComponent<AvatarAudioHandlerRemote>(); |
| 0 | 323 | | if (audioHandlerRemote != null) |
| | 324 | | { |
| 0 | 325 | | audioHandlerRemote.Init(createdAnimation.gameObject); |
| | 326 | | } |
| | 327 | | } |
| | 328 | |
|
| 0 | 329 | | animEventHandler = animationEventHandler; |
| 0 | 330 | | } |
| | 331 | |
|
| | 332 | | private void OnDestroy() |
| | 333 | | { |
| 1329 | 334 | | if (animEventHandler != null) |
| 0 | 335 | | Destroy(animEventHandler); |
| 1329 | 336 | | } |
| | 337 | | } |