| | 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 | | // Time it takes to determine if a character is grounded when vertical velocity is 0 |
| | 30 | | const float FORCE_GROUND_TIME = 0.05f; |
| | 31 | |
|
| | 32 | | // Minimum vertical speed used to consider whether an avatar is on air |
| | 33 | | const float MIN_VERTICAL_SPEED_AIR = 0.025f; |
| | 34 | |
|
| | 35 | |
|
| | 36 | | [System.Serializable] |
| | 37 | | public class AvatarLocomotion |
| | 38 | | { |
| | 39 | | public AnimationClip idle; |
| | 40 | | public AnimationClip walk; |
| | 41 | | public AnimationClip run; |
| | 42 | | public AnimationClip jump; |
| | 43 | | public AnimationClip fall; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | [System.Serializable] |
| | 47 | | public class BlackBoard |
| | 48 | | { |
| | 49 | | public float walkSpeedFactor; |
| | 50 | | public float runSpeedFactor; |
| | 51 | | public float movementSpeed; |
| | 52 | | public float verticalSpeed; |
| | 53 | | public bool isGrounded; |
| | 54 | | public string expressionTriggerId; |
| | 55 | | public long expressionTriggerTimestamp; |
| | 56 | | public float deltaTime; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | [SerializeField] internal AvatarLocomotion femaleLocomotions; |
| | 60 | | [SerializeField] internal AvatarLocomotion maleLocomotions; |
| | 61 | | AvatarLocomotion currentLocomotions; |
| | 62 | |
|
| | 63 | | public new Animation animation; |
| | 64 | | public BlackBoard blackboard; |
| | 65 | | public Transform target; |
| | 66 | |
|
| 1040 | 67 | | [SerializeField] float runMinSpeed = 6f; |
| 1040 | 68 | | [SerializeField] float walkMinSpeed = 0.1f; |
| | 69 | |
|
| | 70 | | internal System.Action<BlackBoard> currentState; |
| | 71 | |
|
| | 72 | | Vector3 lastPosition; |
| | 73 | | bool isOwnPlayer = false; |
| | 74 | | private AvatarAnimationEventHandler animEventHandler; |
| | 75 | |
|
| | 76 | | private float lastOnAirTime = 0; |
| | 77 | |
|
| 1122 | 78 | | public void Start() { OnPoolGet(); } |
| | 79 | |
|
| | 80 | | public void OnPoolGet() |
| | 81 | | { |
| 561 | 82 | | if (DCLCharacterController.i != null) |
| | 83 | | { |
| 561 | 84 | | isOwnPlayer = DCLCharacterController.i.transform == transform.parent; |
| | 85 | |
|
| | 86 | | // NOTE: disable MonoBehaviour's update to use DCLCharacterController event instead |
| 561 | 87 | | this.enabled = !isOwnPlayer; |
| | 88 | |
|
| 561 | 89 | | if (isOwnPlayer) |
| | 90 | | { |
| 559 | 91 | | DCLCharacterController.i.OnUpdateFinish += Update; |
| | 92 | | } |
| | 93 | | } |
| | 94 | |
|
| 561 | 95 | | currentState = State_Init; |
| 561 | 96 | | } |
| | 97 | |
|
| | 98 | | public void OnPoolRelease() |
| | 99 | | { |
| 0 | 100 | | if (isOwnPlayer && DCLCharacterController.i) |
| | 101 | | { |
| 0 | 102 | | DCLCharacterController.i.OnUpdateFinish -= Update; |
| | 103 | | } |
| 0 | 104 | | } |
| | 105 | |
|
| 4 | 106 | | void Update() { Update(Time.deltaTime); } |
| | 107 | |
|
| | 108 | | void Update(float deltaTime) |
| | 109 | | { |
| 4694 | 110 | | if (target == null || animation == null) |
| 4694 | 111 | | return; |
| | 112 | |
|
| 0 | 113 | | blackboard.deltaTime = deltaTime; |
| 0 | 114 | | UpdateInterface(); |
| 0 | 115 | | currentState?.Invoke(blackboard); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | void UpdateInterface() |
| | 119 | | { |
| 0 | 120 | | Vector3 velocityTargetPosition = target.position; |
| 0 | 121 | | Vector3 flattenedVelocity = velocityTargetPosition - lastPosition; |
| | 122 | |
|
| | 123 | | //NOTE(Brian): Vertical speed |
| 0 | 124 | | float verticalVelocity = flattenedVelocity.y; |
| | 125 | |
|
| | 126 | | //NOTE(Kinerius): if we have more or less than zero we consider that we are either jumping or falling |
| 0 | 127 | | if (Mathf.Abs(verticalVelocity) > MIN_VERTICAL_SPEED_AIR) |
| | 128 | | { |
| 0 | 129 | | lastOnAirTime = Time.time; |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | blackboard.verticalSpeed = verticalVelocity; |
| | 133 | |
|
| 0 | 134 | | flattenedVelocity.y = 0; |
| | 135 | |
|
| 0 | 136 | | if (isOwnPlayer) |
| 0 | 137 | | blackboard.movementSpeed = flattenedVelocity.magnitude - DCLCharacterController.i.movingPlatformSpeed; |
| | 138 | | else |
| 0 | 139 | | blackboard.movementSpeed = flattenedVelocity.magnitude; |
| | 140 | |
|
| 0 | 141 | | Vector3 rayOffset = Vector3.up * RAY_OFFSET_LENGTH; |
| | 142 | |
|
| | 143 | | //NOTE(Kinerius): This check is just for the playing character, it uses a combination of collision flags and ray |
| 0 | 144 | | bool isGroundedByCharacterController = isOwnPlayer && DCLCharacterController.i.isGrounded; |
| | 145 | |
|
| | 146 | | //NOTE(Kinerius): This check is for interpolated avatars (the other players) as we dont have a Character Control |
| | 147 | | // this check is cheap and fast but not precise |
| 0 | 148 | | bool isGroundedByVelocity = !isOwnPlayer && Time.time - lastOnAirTime > FORCE_GROUND_TIME; |
| | 149 | |
|
| | 150 | | //NOTE(Kinerius): This additional check is both for the player and interpolated avatars, we cast an additional r |
| 0 | 151 | | bool isGroundedByRaycast = Physics.Raycast(target.transform.position + rayOffset, |
| | 152 | | Vector3.down, |
| | 153 | | RAY_OFFSET_LENGTH - ELEVATION_OFFSET, |
| | 154 | | DCLCharacterController.i.groundLayers); |
| | 155 | |
|
| 0 | 156 | | blackboard.isGrounded = isGroundedByCharacterController || isGroundedByVelocity || isGroundedByRaycast; |
| | 157 | | #if UNITY_EDITOR |
| 0 | 158 | | Debug.DrawRay(target.transform.position + rayOffset, Vector3.down * (RAY_OFFSET_LENGTH - ELEVATION_OFFSET), blac |
| | 159 | | #endif |
| | 160 | |
|
| 0 | 161 | | lastPosition = velocityTargetPosition; |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | void State_Init(BlackBoard bb) |
| | 165 | | { |
| 0 | 166 | | if (bb.isGrounded == true) |
| | 167 | | { |
| 0 | 168 | | currentState = State_Ground; |
| 0 | 169 | | } |
| | 170 | | else |
| | 171 | | { |
| 0 | 172 | | currentState = State_Air; |
| | 173 | | } |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | void State_Ground(BlackBoard bb) |
| | 177 | | { |
| 0 | 178 | | if (bb.deltaTime <= 0) |
| | 179 | | { |
| 0 | 180 | | Debug.LogError("deltaTime should be > 0", gameObject); |
| 0 | 181 | | return; |
| | 182 | | } |
| | 183 | |
|
| 0 | 184 | | animation[currentLocomotions.run.name].normalizedSpeed = bb.movementSpeed / bb.deltaTime * bb.runSpeedFactor; |
| 0 | 185 | | animation[currentLocomotions.walk.name].normalizedSpeed = bb.movementSpeed / bb.deltaTime * bb.walkSpeedFactor; |
| | 186 | |
|
| 0 | 187 | | float movementSpeed = bb.movementSpeed / bb.deltaTime; |
| | 188 | |
|
| 0 | 189 | | if (movementSpeed > runMinSpeed) |
| | 190 | | { |
| 0 | 191 | | animation.CrossFade(currentLocomotions.run.name, RUN_TRANSITION_TIME); |
| 0 | 192 | | } |
| 0 | 193 | | else if (movementSpeed > walkMinSpeed) |
| | 194 | | { |
| 0 | 195 | | animation.CrossFade(currentLocomotions.walk.name, WALK_TRANSITION_TIME); |
| 0 | 196 | | } |
| | 197 | | else |
| | 198 | | { |
| 0 | 199 | | animation.CrossFade(currentLocomotions.idle.name, IDLE_TRANSITION_TIME); |
| | 200 | | } |
| | 201 | |
|
| 0 | 202 | | if (!bb.isGrounded) |
| | 203 | | { |
| 0 | 204 | | currentState = State_Air; |
| 0 | 205 | | Update(bb.deltaTime); |
| | 206 | | } |
| 0 | 207 | | } |
| | 208 | |
|
| | 209 | | void State_Air(BlackBoard bb) |
| | 210 | | { |
| 0 | 211 | | if (bb.verticalSpeed > 0) |
| | 212 | | { |
| 0 | 213 | | animation.CrossFade(currentLocomotions.jump.name, JUMP_TRANSITION_TIME, PlayMode.StopAll); |
| 0 | 214 | | } |
| | 215 | | else |
| | 216 | | { |
| 0 | 217 | | animation.CrossFade(currentLocomotions.fall.name, FALL_TRANSITION_TIME, PlayMode.StopAll); |
| | 218 | | } |
| | 219 | |
|
| 0 | 220 | | if (bb.isGrounded) |
| | 221 | | { |
| 0 | 222 | | animation.Blend(currentLocomotions.jump.name, 0, AIR_EXIT_TRANSITION_TIME); |
| 0 | 223 | | animation.Blend(currentLocomotions.fall.name, 0, AIR_EXIT_TRANSITION_TIME); |
| 0 | 224 | | currentState = State_Ground; |
| 0 | 225 | | Update(bb.deltaTime); |
| | 226 | | } |
| 0 | 227 | | } |
| | 228 | |
|
| | 229 | | internal void State_Expression(BlackBoard bb) |
| | 230 | | { |
| 0 | 231 | | var animationInfo = animation[bb.expressionTriggerId]; |
| 0 | 232 | | animation.CrossFade(bb.expressionTriggerId, EXPRESSION_TRANSITION_TIME, PlayMode.StopAll); |
| | 233 | | bool mustExit; |
| | 234 | |
|
| | 235 | | //Introduced the isMoving variable that is true if there is user input, substituted the old Math.Abs(bb.movement |
| 0 | 236 | | if (isOwnPlayer) |
| 0 | 237 | | mustExit = DCLCharacterController.i.isMovingByUserInput || animationInfo.length - animationInfo.time < EXPRE |
| | 238 | | else |
| 0 | 239 | | mustExit = Math.Abs(bb.movementSpeed) > 0.07f || animationInfo.length - animationInfo.time < EXPRESSION_TRAN |
| | 240 | |
|
| 0 | 241 | | if (mustExit) |
| | 242 | | { |
| 0 | 243 | | animation.Blend(bb.expressionTriggerId, 0, EXPRESSION_TRANSITION_TIME); |
| 0 | 244 | | bb.expressionTriggerId = null; |
| 0 | 245 | | if (!bb.isGrounded) |
| 0 | 246 | | currentState = State_Air; |
| | 247 | | else |
| 0 | 248 | | currentState = State_Ground; |
| | 249 | |
|
| 0 | 250 | | Update(bb.deltaTime); |
| 0 | 251 | | } |
| | 252 | | else |
| | 253 | | { |
| 0 | 254 | | animation.Blend(bb.expressionTriggerId, 1, EXPRESSION_TRANSITION_TIME / 2f); |
| | 255 | | } |
| 0 | 256 | | } |
| | 257 | |
|
| | 258 | | public void SetExpressionValues(string expressionTriggerId, long expressionTriggerTimestamp) |
| | 259 | | { |
| 2 | 260 | | if (animation == null) |
| 2 | 261 | | return; |
| | 262 | |
|
| 0 | 263 | | if (string.IsNullOrEmpty(expressionTriggerId)) |
| 0 | 264 | | return; |
| | 265 | |
|
| 0 | 266 | | if (animation.GetClip(expressionTriggerId) == null) |
| 0 | 267 | | return; |
| | 268 | |
|
| 0 | 269 | | var mustTriggerAnimation = !string.IsNullOrEmpty(expressionTriggerId) && blackboard.expressionTriggerTimestamp ! |
| 0 | 270 | | blackboard.expressionTriggerId = expressionTriggerId; |
| 0 | 271 | | blackboard.expressionTriggerTimestamp = expressionTriggerTimestamp; |
| | 272 | |
|
| 0 | 273 | | if (mustTriggerAnimation) |
| | 274 | | { |
| 0 | 275 | | if (!string.IsNullOrEmpty(expressionTriggerId)) |
| | 276 | | { |
| 0 | 277 | | animation.Stop(expressionTriggerId); |
| | 278 | | } |
| 0 | 279 | | currentState = State_Expression; |
| 0 | 280 | | Update(); |
| | 281 | | } |
| 0 | 282 | | } |
| | 283 | |
|
| | 284 | | public void Reset() |
| | 285 | | { |
| 0 | 286 | | if (animation == null) |
| 0 | 287 | | return; |
| | 288 | |
|
| | 289 | | //It will set the animation to the first frame, but due to the nature of the script and its Update. It wont stop |
| 0 | 290 | | animation.Stop(); |
| 0 | 291 | | } |
| | 292 | |
|
| 0 | 293 | | public void SetIdleFrame() { animation.Play(currentLocomotions.idle.name); } |
| | 294 | |
|
| | 295 | | public void PrepareLocomotionAnims(string bodyshapeId) |
| | 296 | | { |
| 0 | 297 | | if (bodyshapeId.Contains(WearableLiterals.BodyShapes.MALE)) |
| | 298 | | { |
| 0 | 299 | | currentLocomotions = maleLocomotions; |
| 0 | 300 | | } |
| 0 | 301 | | else if (bodyshapeId.Contains(WearableLiterals.BodyShapes.FEMALE)) |
| | 302 | | { |
| 0 | 303 | | currentLocomotions = femaleLocomotions; |
| | 304 | | } |
| | 305 | |
|
| 0 | 306 | | EquipEmote(currentLocomotions.idle.name, currentLocomotions.idle); |
| 0 | 307 | | EquipEmote(currentLocomotions.walk.name, currentLocomotions.walk); |
| 0 | 308 | | EquipEmote(currentLocomotions.run.name, currentLocomotions.run); |
| 0 | 309 | | EquipEmote(currentLocomotions.jump.name, currentLocomotions.jump); |
| 0 | 310 | | EquipEmote(currentLocomotions.fall.name, currentLocomotions.fall); |
| 0 | 311 | | } |
| | 312 | |
|
| | 313 | | // AvatarSystem entry points |
| | 314 | | public bool Prepare(string bodyshapeId, GameObject container) |
| | 315 | | { |
| 0 | 316 | | if (!container.transform.TryFindChildRecursively("Armature", out Transform armature)) |
| | 317 | | { |
| 0 | 318 | | Debug.LogError($"Couldn't find Armature for AnimatorLegacy in path: {transform.GetHierarchyPath()}"); |
| 0 | 319 | | return false; |
| | 320 | | } |
| 0 | 321 | | Transform armatureParent = armature.parent; |
| 0 | 322 | | animation = armatureParent.gameObject.GetOrCreateComponent<Animation>(); |
| 0 | 323 | | armatureParent.gameObject.GetOrCreateComponent<StickerAnimationListener>(); |
| | 324 | |
|
| 0 | 325 | | PrepareLocomotionAnims(bodyshapeId); |
| 0 | 326 | | SetIdleFrame(); |
| 0 | 327 | | animation.Sample(); |
| 0 | 328 | | InitializeAvatarAudioAndParticleHandlers(animation); |
| 0 | 329 | | return true; |
| | 330 | | } |
| | 331 | |
|
| 4 | 332 | | public void PlayEmote(string emoteId, long timestamps) { SetExpressionValues(emoteId, timestamps); } |
| | 333 | |
|
| | 334 | | public void EquipEmote(string emoteId, AnimationClip clip) |
| | 335 | | { |
| 0 | 336 | | if (animation == null) |
| 0 | 337 | | return; |
| | 338 | |
|
| 0 | 339 | | if (animation.GetClip(emoteId) != null) |
| 0 | 340 | | animation.RemoveClip(emoteId); |
| 0 | 341 | | animation.AddClip(clip, emoteId); |
| 0 | 342 | | } |
| | 343 | |
|
| | 344 | | public void UnequipEmote(string emoteId) |
| | 345 | | { |
| 0 | 346 | | if (animation == null) |
| 0 | 347 | | return; |
| | 348 | |
|
| 0 | 349 | | if (animation.GetClip(emoteId) == null) |
| 0 | 350 | | return; |
| 0 | 351 | | animation.RemoveClip(emoteId); |
| 0 | 352 | | } |
| | 353 | |
|
| | 354 | | private void InitializeAvatarAudioAndParticleHandlers(Animation createdAnimation) |
| | 355 | | { |
| | 356 | | //NOTE(Mordi): Adds handler for animation events, and passes in the audioContainer for the avatar |
| 0 | 357 | | AvatarAnimationEventHandler animationEventHandler = createdAnimation.gameObject.GetOrCreateComponent<AvatarAnima |
| 0 | 358 | | AudioContainer audioContainer = transform.GetComponentInChildren<AudioContainer>(); |
| 0 | 359 | | if (audioContainer != null) |
| | 360 | | { |
| 0 | 361 | | animationEventHandler.Init(audioContainer); |
| | 362 | |
|
| | 363 | | //NOTE(Mordi): If this is a remote avatar, pass the animation component so we can keep track of whether it i |
| 0 | 364 | | AvatarAudioHandlerRemote audioHandlerRemote = audioContainer.GetComponent<AvatarAudioHandlerRemote>(); |
| 0 | 365 | | if (audioHandlerRemote != null) |
| | 366 | | { |
| 0 | 367 | | audioHandlerRemote.Init(createdAnimation.gameObject); |
| | 368 | | } |
| | 369 | | } |
| | 370 | |
|
| 0 | 371 | | animEventHandler = animationEventHandler; |
| 0 | 372 | | } |
| | 373 | |
|
| | 374 | | private void OnDestroy() |
| | 375 | | { |
| 1025 | 376 | | if (animEventHandler != null) |
| 0 | 377 | | Destroy(animEventHandler); |
| 1025 | 378 | | } |
| | 379 | | } |