| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL; |
| | 5 | |
|
| | 6 | | public class AvatarAudioHandlerRemote : MonoBehaviour |
| | 7 | | { |
| | 8 | | const float WALK_INTERVAL_SEC = 0.37f, RUN_INTERVAL_SEC = 0.25f; |
| | 9 | | float nextFootstepTime = 0f; |
| | 10 | |
|
| | 11 | | AudioEvent footstepJump; |
| | 12 | | AudioEvent footstepLand; |
| | 13 | | AudioEvent footstepWalk; |
| | 14 | | AudioEvent footstepRun; |
| | 15 | | AudioEvent clothesRustleShort; |
| | 16 | |
|
| | 17 | | GameObject rendererContainer; |
| | 18 | | new Renderer renderer; |
| | 19 | |
|
| | 20 | | AvatarAnimatorLegacy.BlackBoard blackBoard; |
| 687 | 21 | | bool isGroundedPrevious = true; |
| | 22 | |
|
| | 23 | | public AvatarAnimatorLegacy avatarAnimatorLegacy; |
| | 24 | | bool globalRendererIsReady; |
| | 25 | |
|
| | 26 | | private Camera mainCamera; |
| | 27 | | private StickersController stickersController; |
| | 28 | |
|
| | 29 | | Transform footL; |
| | 30 | | Transform footR; |
| | 31 | |
|
| | 32 | | private void Start() |
| | 33 | | { |
| 1 | 34 | | AudioContainer ac = GetComponent<AudioContainer>(); |
| 1 | 35 | | footstepJump = ac.GetEvent("FootstepJump"); |
| 1 | 36 | | footstepLand = ac.GetEvent("FootstepLand"); |
| 1 | 37 | | footstepWalk = ac.GetEvent("FootstepWalk"); |
| 1 | 38 | | footstepRun = ac.GetEvent("FootstepRun"); |
| 1 | 39 | | clothesRustleShort = ac.GetEvent("ClothesRustleShort"); |
| | 40 | |
|
| | 41 | | // Lower volume of jump/land/clothes |
| 1 | 42 | | footstepJump.source.volume = footstepJump.source.volume * 0.5f; |
| 1 | 43 | | footstepLand.source.volume = footstepLand.source.volume * 0.5f; |
| 1 | 44 | | clothesRustleShort.source.volume = clothesRustleShort.source.volume * 0.5f; |
| | 45 | |
|
| 1 | 46 | | if (avatarAnimatorLegacy != null) |
| | 47 | | { |
| 1 | 48 | | blackBoard = avatarAnimatorLegacy.blackboard; |
| | 49 | | } |
| | 50 | |
|
| 1 | 51 | | globalRendererIsReady = CommonScriptableObjects.rendererState.Get(); |
| 1 | 52 | | CommonScriptableObjects.rendererState.OnChange += OnGlobalRendererStateChange; |
| 1 | 53 | | } |
| | 54 | |
|
| 20 | 55 | | void OnGlobalRendererStateChange(bool current, bool previous) { globalRendererIsReady = current; } |
| | 56 | |
|
| | 57 | | public void Init(GameObject rendererContainer) |
| | 58 | | { |
| 0 | 59 | | this.rendererContainer = rendererContainer; |
| 0 | 60 | | stickersController = rendererContainer.GetComponentInParent<StickersController>(); |
| | 61 | |
|
| | 62 | | // Get references to body parts |
| 0 | 63 | | Transform[] children = rendererContainer.GetComponentsInChildren<Transform>(); |
| 0 | 64 | | footL = AvatarBodyPartReferenceUtility.GetLeftToe(children); |
| 0 | 65 | | footR = AvatarBodyPartReferenceUtility.GetRightToe(children); |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | private void Update() |
| | 69 | | { |
| 2 | 70 | | if (blackBoard == null || !globalRendererIsReady) |
| 0 | 71 | | return; |
| | 72 | |
|
| | 73 | | // Jumped |
| 2 | 74 | | if (!blackBoard.isGrounded && isGroundedPrevious) |
| | 75 | | { |
| 0 | 76 | | if (footstepJump != null) |
| 0 | 77 | | footstepJump.Play(true); |
| 0 | 78 | | if (stickersController != null && footR != null) |
| 0 | 79 | | stickersController.PlaySticker("footstepJump", footR.position, Vector3.up, false); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | // Landed |
| 2 | 83 | | if (blackBoard.isGrounded && !isGroundedPrevious) |
| | 84 | | { |
| 0 | 85 | | if (footstepLand != null) |
| 0 | 86 | | footstepLand.Play(true); |
| | 87 | |
|
| 0 | 88 | | if (stickersController != null && footL != null && footR != null) |
| | 89 | | { |
| 0 | 90 | | stickersController.PlaySticker("footstepLand", |
| | 91 | | Vector3.Lerp(footL.position, footR.position, 0.5f), |
| | 92 | | Vector3.up, |
| | 93 | | false); |
| | 94 | | } |
| | 95 | | } |
| | 96 | |
|
| | 97 | | // Simulate footsteps when avatar is not visible |
| 2 | 98 | | if (renderer != null) |
| | 99 | | { |
| 0 | 100 | | SimulateFootsteps(); |
| 0 | 101 | | } |
| | 102 | | else |
| | 103 | | { |
| 2 | 104 | | if (rendererContainer != null) |
| | 105 | | { |
| | 106 | | //NOTE(Mordi): The renderer takes a while to get ready, so we need to check it continually until it can |
| 0 | 107 | | renderer = rendererContainer.GetComponent<Renderer>(); |
| | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| 2 | 111 | | isGroundedPrevious = blackBoard.isGrounded; |
| 2 | 112 | | } |
| | 113 | |
|
| | 114 | | bool AvatarIsInView() |
| | 115 | | { |
| 0 | 116 | | if (renderer.isVisible) |
| 0 | 117 | | return true; |
| | 118 | |
|
| 0 | 119 | | if (Camera.main == null) |
| 0 | 120 | | return false; |
| | 121 | |
|
| | 122 | | // NOTE(Mordi): In some cases, the renderer will report false even if the avatar is visible. |
| | 123 | | // Therefore we must check whether or not the avatar is in the camera's view. |
| | 124 | |
|
| 0 | 125 | | if ( mainCamera == null ) |
| 0 | 126 | | mainCamera = Camera.main; |
| | 127 | |
|
| 0 | 128 | | if (mainCamera == null) |
| 0 | 129 | | return false; |
| | 130 | |
|
| 0 | 131 | | Vector3 point = mainCamera.WorldToViewportPoint(transform.position); |
| | 132 | |
|
| 0 | 133 | | if (point.z > 0f) |
| | 134 | | { |
| 0 | 135 | | if (point.x >= 0f && point.x <= 1f) |
| | 136 | | { |
| 0 | 137 | | if (point.y >= 0f && point.y <= 1f) |
| | 138 | | { |
| 0 | 139 | | return true; |
| | 140 | | } |
| | 141 | | } |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | return false; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | void SimulateFootsteps() |
| | 148 | | { |
| 0 | 149 | | if (!AvatarIsInView() && (blackBoard.movementSpeed / Time.deltaTime) > 1f && blackBoard.isGrounded) |
| | 150 | | { |
| 0 | 151 | | if (Time.time >= nextFootstepTime) |
| | 152 | | { |
| 0 | 153 | | if ((blackBoard.movementSpeed / Time.deltaTime) > 6f) |
| | 154 | | { |
| 0 | 155 | | if (footstepRun != null) |
| 0 | 156 | | footstepRun.Play(true); |
| | 157 | |
|
| 0 | 158 | | if (clothesRustleShort != null) |
| 0 | 159 | | clothesRustleShort.Play(true); |
| | 160 | |
|
| 0 | 161 | | nextFootstepTime = Time.time + RUN_INTERVAL_SEC; |
| 0 | 162 | | } |
| | 163 | | else |
| | 164 | | { |
| 0 | 165 | | if (footstepWalk != null) |
| 0 | 166 | | footstepWalk.Play(true); |
| | 167 | |
|
| 0 | 168 | | if (clothesRustleShort != null) |
| 0 | 169 | | clothesRustleShort.PlayScheduled(Random.Range(0.05f, 0.1f)); |
| | 170 | |
|
| 0 | 171 | | nextFootstepTime = Time.time + WALK_INTERVAL_SEC; |
| | 172 | | } |
| | 173 | | } |
| | 174 | | } |
| 0 | 175 | | } |
| | 176 | | } |