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