| | 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 AvatarLocomotion |
| | 31 | | { |
| | 32 | | public AnimationClip idle; |
| | 33 | | public AnimationClip walk; |
| | 34 | | public AnimationClip run; |
| | 35 | | public AnimationClip jump; |
| | 36 | | public AnimationClip 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 AvatarLocomotion femaleLocomotions; |
| | 53 | | [SerializeField] internal AvatarLocomotion maleLocomotions; |
| | 54 | | AvatarLocomotion currentLocomotions; |
| | 55 | |
|
| | 56 | | public new Animation animation; |
| | 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 | | bool isOwnPlayer = false; |
| | 67 | | private AvatarAnimationEventHandler animEventHandler; |
| | 68 | |
|
| 1148 | 69 | | public void Start() { OnPoolGet(); } |
| | 70 | |
|
| | 71 | | public void OnPoolGet() |
| | 72 | | { |
| 574 | 73 | | if (DCLCharacterController.i != null) |
| | 74 | | { |
| 574 | 75 | | isOwnPlayer = DCLCharacterController.i.transform == transform.parent; |
| | 76 | |
|
| | 77 | | // NOTE: disable MonoBehaviour's update to use DCLCharacterController event instead |
| 574 | 78 | | this.enabled = !isOwnPlayer; |
| | 79 | |
|
| 574 | 80 | | if (isOwnPlayer) |
| | 81 | | { |
| 572 | 82 | | DCLCharacterController.i.OnUpdateFinish += Update; |
| | 83 | | } |
| | 84 | | } |
| | 85 | |
|
| 574 | 86 | | currentState = State_Init; |
| 574 | 87 | | } |
| | 88 | |
|
| | 89 | | public void OnPoolRelease() |
| | 90 | | { |
| 0 | 91 | | if (isOwnPlayer && DCLCharacterController.i) |
| | 92 | | { |
| 0 | 93 | | DCLCharacterController.i.OnUpdateFinish -= Update; |
| | 94 | | } |
| 0 | 95 | | } |
| | 96 | |
|
| 6 | 97 | | void Update() { Update(Time.deltaTime); } |
| | 98 | |
|
| | 99 | | void Update(float deltaTime) |
| | 100 | | { |
| 4979 | 101 | | if (target == null || animation == null) |
| 4979 | 102 | | return; |
| | 103 | |
|
| 0 | 104 | | blackboard.deltaTime = deltaTime; |
| 0 | 105 | | UpdateInterface(); |
| 0 | 106 | | currentState?.Invoke(blackboard); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | void UpdateInterface() |
| | 110 | | { |
| 0 | 111 | | Vector3 velocityTargetPosition = target.position; |
| 0 | 112 | | Vector3 flattenedVelocity = velocityTargetPosition - lastPosition; |
| | 113 | |
|
| | 114 | | //NOTE(Brian): Vertical speed |
| 0 | 115 | | float verticalVelocity = flattenedVelocity.y; |
| 0 | 116 | | blackboard.verticalSpeed = verticalVelocity; |
| | 117 | |
|
| 0 | 118 | | flattenedVelocity.y = 0; |
| | 119 | |
|
| 0 | 120 | | if (isOwnPlayer) |
| 0 | 121 | | blackboard.movementSpeed = flattenedVelocity.magnitude - DCLCharacterController.i.movingPlatformSpeed; |
| | 122 | | else |
| 0 | 123 | | blackboard.movementSpeed = flattenedVelocity.magnitude; |
| | 124 | |
|
| 0 | 125 | | Vector3 rayOffset = Vector3.up * RAY_OFFSET_LENGTH; |
| | 126 | | //NOTE(Brian): isGrounded? |
| 0 | 127 | | blackboard.isGrounded = Physics.Raycast(target.transform.position + rayOffset, |
| | 128 | | Vector3.down, |
| | 129 | | RAY_OFFSET_LENGTH - ELEVATION_OFFSET, |
| | 130 | | DCLCharacterController.i.groundLayers); |
| | 131 | |
|
| | 132 | | #if UNITY_EDITOR |
| 0 | 133 | | Debug.DrawRay(target.transform.position + rayOffset, Vector3.down * (RAY_OFFSET_LENGTH - ELEVATION_OFFSET), blac |
| | 134 | | #endif |
| | 135 | |
|
| 0 | 136 | | lastPosition = velocityTargetPosition; |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | void State_Init(BlackBoard bb) |
| | 140 | | { |
| 0 | 141 | | if (bb.isGrounded == true) |
| | 142 | | { |
| 0 | 143 | | currentState = State_Ground; |
| 0 | 144 | | } |
| | 145 | | else |
| | 146 | | { |
| 0 | 147 | | currentState = State_Air; |
| | 148 | | } |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | void State_Ground(BlackBoard bb) |
| | 152 | | { |
| 0 | 153 | | if (bb.deltaTime <= 0) |
| | 154 | | { |
| 0 | 155 | | Debug.LogError("deltaTime should be > 0", gameObject); |
| 0 | 156 | | return; |
| | 157 | | } |
| | 158 | |
|
| 0 | 159 | | animation[currentLocomotions.run.name].normalizedSpeed = bb.movementSpeed / bb.deltaTime * bb.runSpeedFactor; |
| 0 | 160 | | animation[currentLocomotions.walk.name].normalizedSpeed = bb.movementSpeed / bb.deltaTime * bb.walkSpeedFactor; |
| | 161 | |
|
| 0 | 162 | | float movementSpeed = bb.movementSpeed / bb.deltaTime; |
| | 163 | |
|
| 0 | 164 | | if (movementSpeed > runMinSpeed) |
| | 165 | | { |
| 0 | 166 | | animation.CrossFade(currentLocomotions.run.name, RUN_TRANSITION_TIME); |
| 0 | 167 | | } |
| 0 | 168 | | else if (movementSpeed > walkMinSpeed) |
| | 169 | | { |
| 0 | 170 | | animation.CrossFade(currentLocomotions.walk.name, WALK_TRANSITION_TIME); |
| 0 | 171 | | } |
| | 172 | | else |
| | 173 | | { |
| 0 | 174 | | animation.CrossFade(currentLocomotions.idle.name, IDLE_TRANSITION_TIME); |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | if (!bb.isGrounded) |
| | 178 | | { |
| 0 | 179 | | currentState = State_Air; |
| 0 | 180 | | Update(bb.deltaTime); |
| | 181 | | } |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | void State_Air(BlackBoard bb) |
| | 185 | | { |
| 0 | 186 | | if (bb.verticalSpeed > 0) |
| | 187 | | { |
| 0 | 188 | | animation.CrossFade(currentLocomotions.jump.name, JUMP_TRANSITION_TIME, PlayMode.StopAll); |
| 0 | 189 | | } |
| | 190 | | else |
| | 191 | | { |
| 0 | 192 | | animation.CrossFade(currentLocomotions.fall.name, FALL_TRANSITION_TIME, PlayMode.StopAll); |
| | 193 | | } |
| | 194 | |
|
| 0 | 195 | | if (bb.isGrounded) |
| | 196 | | { |
| 0 | 197 | | animation.Blend(currentLocomotions.jump.name, 0, AIR_EXIT_TRANSITION_TIME); |
| 0 | 198 | | animation.Blend(currentLocomotions.fall.name, 0, AIR_EXIT_TRANSITION_TIME); |
| 0 | 199 | | currentState = State_Ground; |
| 0 | 200 | | Update(bb.deltaTime); |
| | 201 | | } |
| 0 | 202 | | } |
| | 203 | |
|
| | 204 | | internal void State_Expression(BlackBoard bb) |
| | 205 | | { |
| 0 | 206 | | var animationInfo = animation[bb.expressionTriggerId]; |
| 0 | 207 | | animation.CrossFade(bb.expressionTriggerId, EXPRESSION_TRANSITION_TIME, PlayMode.StopAll); |
| | 208 | |
|
| 0 | 209 | | var mustExit = Math.Abs(bb.movementSpeed) > Mathf.Epsilon || animationInfo.length - animationInfo.time < EXPRESS |
| 0 | 210 | | if (mustExit) |
| | 211 | | { |
| 0 | 212 | | animation.Blend(bb.expressionTriggerId, 0, EXPRESSION_TRANSITION_TIME); |
| 0 | 213 | | bb.expressionTriggerId = null; |
| 0 | 214 | | if (!bb.isGrounded) |
| 0 | 215 | | currentState = State_Air; |
| | 216 | | else |
| 0 | 217 | | currentState = State_Ground; |
| | 218 | |
|
| 0 | 219 | | Update(bb.deltaTime); |
| 0 | 220 | | } |
| | 221 | | else |
| | 222 | | { |
| 0 | 223 | | animation.Blend(bb.expressionTriggerId, 1, EXPRESSION_TRANSITION_TIME / 2f); |
| | 224 | | } |
| 0 | 225 | | } |
| | 226 | |
|
| | 227 | | public void SetExpressionValues(string expressionTriggerId, long expressionTriggerTimestamp) |
| | 228 | | { |
| 2 | 229 | | if (animation == null) |
| 2 | 230 | | return; |
| | 231 | |
|
| 0 | 232 | | if (string.IsNullOrEmpty(expressionTriggerId)) |
| 0 | 233 | | return; |
| | 234 | |
|
| 0 | 235 | | var mustTriggerAnimation = !string.IsNullOrEmpty(expressionTriggerId) && blackboard.expressionTriggerTimestamp ! |
| | 236 | |
|
| 0 | 237 | | blackboard.expressionTriggerId = expressionTriggerId; |
| 0 | 238 | | blackboard.expressionTriggerTimestamp = expressionTriggerTimestamp; |
| | 239 | |
|
| 0 | 240 | | if (mustTriggerAnimation) |
| | 241 | | { |
| 0 | 242 | | if (!string.IsNullOrEmpty(expressionTriggerId)) |
| | 243 | | { |
| 0 | 244 | | animation.Stop(expressionTriggerId); |
| | 245 | | } |
| | 246 | |
|
| 0 | 247 | | currentState = State_Expression; |
| 0 | 248 | | Update(); |
| | 249 | | } |
| 0 | 250 | | } |
| | 251 | |
|
| | 252 | | public void Reset() |
| | 253 | | { |
| 0 | 254 | | if (animation == null) |
| 0 | 255 | | return; |
| | 256 | |
|
| | 257 | | //It will set the animation to the first frame, but due to the nature of the script and its Update. It wont stop |
| 0 | 258 | | animation.Stop(); |
| 0 | 259 | | } |
| | 260 | |
|
| 0 | 261 | | public void SetIdleFrame() { animation.Play(currentLocomotions.idle.name); } |
| | 262 | |
|
| | 263 | | public void PrepareLocomotionAnims(string bodyshapeId) |
| | 264 | | { |
| 0 | 265 | | if (bodyshapeId.Contains(WearableLiterals.BodyShapes.MALE)) |
| | 266 | | { |
| 0 | 267 | | currentLocomotions = maleLocomotions; |
| 0 | 268 | | } |
| 0 | 269 | | else if (bodyshapeId.Contains(WearableLiterals.BodyShapes.FEMALE)) |
| | 270 | | { |
| 0 | 271 | | currentLocomotions = femaleLocomotions; |
| | 272 | | } |
| | 273 | |
|
| 0 | 274 | | EquipEmote(currentLocomotions.idle.name, currentLocomotions.idle); |
| 0 | 275 | | EquipEmote(currentLocomotions.walk.name, currentLocomotions.walk); |
| 0 | 276 | | EquipEmote(currentLocomotions.run.name, currentLocomotions.run); |
| 0 | 277 | | EquipEmote(currentLocomotions.jump.name, currentLocomotions.jump); |
| 0 | 278 | | EquipEmote(currentLocomotions.fall.name, currentLocomotions.fall); |
| 0 | 279 | | } |
| | 280 | |
|
| | 281 | | // AvatarSystem entry points |
| | 282 | | public bool Prepare(string bodyshapeId, GameObject container) |
| | 283 | | { |
| 0 | 284 | | if (!container.transform.TryFindChildRecursively("Armature", out Transform armature)) |
| | 285 | | { |
| 0 | 286 | | Debug.LogError($"Couldn't find Armature for AnimatorLegacy in path: {transform.GetHierarchyPath()}"); |
| 0 | 287 | | return false; |
| | 288 | | } |
| 0 | 289 | | Transform armatureParent = armature.parent; |
| 0 | 290 | | animation = armatureParent.gameObject.GetOrCreateComponent<Animation>(); |
| 0 | 291 | | armatureParent.gameObject.GetOrCreateComponent<StickerAnimationListener>(); |
| | 292 | |
|
| 0 | 293 | | PrepareLocomotionAnims(bodyshapeId); |
| 0 | 294 | | SetIdleFrame(); |
| 0 | 295 | | animation.Sample(); |
| 0 | 296 | | InitializeAvatarAudioAndParticleHandlers(animation); |
| 0 | 297 | | return true; |
| | 298 | | } |
| | 299 | |
|
| 4 | 300 | | public void PlayEmote(string emoteId, long timestamps) { SetExpressionValues(emoteId, timestamps); } |
| | 301 | |
|
| | 302 | | public void EquipEmote(string emoteId, AnimationClip clip) |
| | 303 | | { |
| 0 | 304 | | if (animation.GetClip(emoteId) != null) |
| 0 | 305 | | animation.RemoveClip(emoteId); |
| 0 | 306 | | animation.AddClip(clip, emoteId); |
| 0 | 307 | | } |
| | 308 | |
|
| | 309 | | public void UnequipEmote(string emoteId) |
| | 310 | | { |
| 0 | 311 | | if (animation.GetClip(emoteId) == null) |
| 0 | 312 | | return; |
| 0 | 313 | | animation.RemoveClip(emoteId); |
| 0 | 314 | | } |
| | 315 | |
|
| | 316 | | private void InitializeAvatarAudioAndParticleHandlers(Animation createdAnimation) |
| | 317 | | { |
| | 318 | | //NOTE(Mordi): Adds handler for animation events, and passes in the audioContainer for the avatar |
| 0 | 319 | | AvatarAnimationEventHandler animationEventHandler = createdAnimation.gameObject.AddComponent<AvatarAnimationEven |
| 0 | 320 | | AudioContainer audioContainer = transform.GetComponentInChildren<AudioContainer>(); |
| 0 | 321 | | if (audioContainer != null) |
| | 322 | | { |
| 0 | 323 | | animationEventHandler.Init(audioContainer); |
| | 324 | |
|
| | 325 | | //NOTE(Mordi): If this is a remote avatar, pass the animation component so we can keep track of whether it i |
| 0 | 326 | | AvatarAudioHandlerRemote audioHandlerRemote = audioContainer.GetComponent<AvatarAudioHandlerRemote>(); |
| 0 | 327 | | if (audioHandlerRemote != null) |
| | 328 | | { |
| 0 | 329 | | audioHandlerRemote.Init(createdAnimation.gameObject); |
| | 330 | | } |
| | 331 | | } |
| | 332 | |
|
| 0 | 333 | | animEventHandler = animationEventHandler; |
| 0 | 334 | | } |
| | 335 | |
|
| | 336 | | private void OnDestroy() |
| | 337 | | { |
| 1329 | 338 | | if (animEventHandler != null) |
| 0 | 339 | | Destroy(animEventHandler); |
| 1329 | 340 | | } |
| | 341 | | } |