< Summary

Class:AvatarAudioHandlerRemote
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarAudioHandlerRemote.cs
Covered lines:22
Uncovered lines:44
Coverable lines:66
Total lines:176
Line coverage:33.3% (22 of 66)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarAudioHandlerRemote()0%110100%
Start()0%220100%
OnGlobalRendererStateChange(...)0%110100%
Init(...)0%2100%
Update()0%74.4216038.89%
AvatarIsInView()0%1101000%
SimulateFootsteps()0%1101000%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarAudioHandlerRemote.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using DCL;
 5
 6public class AvatarAudioHandlerRemote : MonoBehaviour
 7{
 8    [SerializeField] private AudioContainer audioContainer;
 9    [SerializeField] private StickersController stickersController;
 10
 11    const float WALK_INTERVAL_SEC = 0.37f, RUN_INTERVAL_SEC = 0.25f;
 12    float nextFootstepTime = 0f;
 13
 14    AudioEvent footstepJump;
 15    AudioEvent footstepLand;
 16    AudioEvent footstepWalk;
 17    AudioEvent footstepRun;
 18    AudioEvent clothesRustleShort;
 19
 20    GameObject rendererContainer;
 21    new Renderer renderer;
 22
 23    AvatarAnimatorLegacy.BlackBoard blackBoard;
 34024    bool isGroundedPrevious = true;
 25
 26    public AvatarAnimatorLegacy avatarAnimatorLegacy;
 27    bool globalRendererIsReady;
 28
 29    private Camera mainCamera;
 30
 31    Transform footL;
 32    Transform footR;
 33
 34    private void Start()
 35    {
 636        footstepJump = audioContainer.GetEvent("FootstepJump");
 637        footstepLand = audioContainer.GetEvent("FootstepLand");
 638        footstepWalk = audioContainer.GetEvent("FootstepWalk");
 639        footstepRun = audioContainer.GetEvent("FootstepRun");
 640        clothesRustleShort = audioContainer.GetEvent("ClothesRustleShort");
 41
 42        // Lower volume of jump/land/clothes
 643        footstepJump.source.volume *= 0.5f;
 644        footstepLand.source.volume *= 0.5f;
 645        clothesRustleShort.source.volume *= 0.5f;
 46
 647        if (avatarAnimatorLegacy != null)
 48        {
 649            blackBoard = avatarAnimatorLegacy.blackboard;
 50        }
 51
 652        globalRendererIsReady = CommonScriptableObjects.rendererState.Get();
 653        CommonScriptableObjects.rendererState.OnChange += OnGlobalRendererStateChange;
 654    }
 55
 1256    void OnGlobalRendererStateChange(bool current, bool previous) { globalRendererIsReady = current; }
 57
 58    public void Init(GameObject rendererContainer)
 59    {
 060        this.rendererContainer = rendererContainer;
 61
 62        // Get references to body parts
 063        Transform[] children = rendererContainer.GetComponentsInChildren<Transform>();
 064        footL = AvatarBodyPartReferenceUtility.GetLeftToe(children);
 065        footR = AvatarBodyPartReferenceUtility.GetRightToe(children);
 066    }
 67
 68    private void Update()
 69    {
 170        if (blackBoard == null || !globalRendererIsReady)
 071            return;
 72
 73        // Jumped
 174        if (!blackBoard.isGrounded && isGroundedPrevious)
 75        {
 076            if (footstepJump != null)
 077                footstepJump.Play(true);
 078            if (stickersController != null && footR != null)
 079                stickersController.PlaySticker("footstepJump", footR.position, Vector3.up, false);
 80        }
 81
 82        // Landed
 183        if (blackBoard.isGrounded && !isGroundedPrevious)
 84        {
 085            if (footstepLand != null)
 086                footstepLand.Play(true);
 87
 088            if (stickersController != null && footL != null && footR != null)
 89            {
 090                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
 198        if (renderer != null)
 99        {
 0100            SimulateFootsteps();
 101        }
 102        else
 103        {
 1104            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 
 0107                renderer = rendererContainer.GetComponentInChildren<Renderer>();
 108            }
 109        }
 110
 1111        isGroundedPrevious = blackBoard.isGrounded;
 1112    }
 113
 114    bool AvatarIsInView()
 115    {
 0116        if (renderer.isVisible)
 0117            return true;
 118
 0119        if (Camera.main == null)
 0120            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
 0125        if ( mainCamera == null )
 0126            mainCamera = Camera.main;
 127
 0128        if (mainCamera == null)
 0129            return false;
 130
 0131        Vector3 point = mainCamera.WorldToViewportPoint(transform.position);
 132
 0133        if (point.z > 0f)
 134        {
 0135            if (point.x >= 0f && point.x <= 1f)
 136            {
 0137                if (point.y >= 0f && point.y <= 1f)
 138                {
 0139                    return true;
 140                }
 141            }
 142        }
 143
 0144        return false;
 145    }
 146
 147    void SimulateFootsteps()
 148    {
 0149        if (!AvatarIsInView() && (blackBoard.movementSpeed / Time.deltaTime) > 1f && blackBoard.isGrounded)
 150        {
 0151            if (Time.time >= nextFootstepTime)
 152            {
 0153                if ((blackBoard.movementSpeed / Time.deltaTime) > 6f)
 154                {
 0155                    if (footstepRun != null)
 0156                        footstepRun.Play(true);
 157
 0158                    if (clothesRustleShort != null)
 0159                        clothesRustleShort.Play(true);
 160
 0161                    nextFootstepTime = Time.time + RUN_INTERVAL_SEC;
 162                }
 163                else
 164                {
 0165                    if (footstepWalk != null)
 0166                        footstepWalk.Play(true);
 167
 0168                    if (clothesRustleShort != null)
 0169                        clothesRustleShort.PlayScheduled(Random.Range(0.05f, 0.1f));
 170
 0171                    nextFootstepTime = Time.time + WALK_INTERVAL_SEC;
 172                }
 173            }
 174        }
 0175    }
 176}