< Summary

Class:AvatarAudioHandlerRemote
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarAudioHandlerRemote.cs
Covered lines:23
Uncovered lines:47
Coverable lines:70
Total lines:176
Line coverage:32.8% (23 of 70)
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%80.516036.84%
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    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;
 68721    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    {
 134        AudioContainer ac = GetComponent<AudioContainer>();
 135        footstepJump = ac.GetEvent("FootstepJump");
 136        footstepLand = ac.GetEvent("FootstepLand");
 137        footstepWalk = ac.GetEvent("FootstepWalk");
 138        footstepRun = ac.GetEvent("FootstepRun");
 139        clothesRustleShort = ac.GetEvent("ClothesRustleShort");
 40
 41        // Lower volume of jump/land/clothes
 142        footstepJump.source.volume = footstepJump.source.volume * 0.5f;
 143        footstepLand.source.volume = footstepLand.source.volume * 0.5f;
 144        clothesRustleShort.source.volume = clothesRustleShort.source.volume * 0.5f;
 45
 146        if (avatarAnimatorLegacy != null)
 47        {
 148            blackBoard = avatarAnimatorLegacy.blackboard;
 49        }
 50
 151        globalRendererIsReady = CommonScriptableObjects.rendererState.Get();
 152        CommonScriptableObjects.rendererState.OnChange += OnGlobalRendererStateChange;
 153    }
 54
 2055    void OnGlobalRendererStateChange(bool current, bool previous) { globalRendererIsReady = current; }
 56
 57    public void Init(GameObject rendererContainer)
 58    {
 059        this.rendererContainer = rendererContainer;
 060        stickersController = rendererContainer.GetComponentInParent<StickersController>();
 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    {
 270        if (blackBoard == null || !globalRendererIsReady)
 071            return;
 72
 73        // Jumped
 274        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
 283        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
 298        if (renderer != null)
 99        {
 0100            SimulateFootsteps();
 0101        }
 102        else
 103        {
 2104            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.GetComponent<Renderer>();
 108            }
 109        }
 110
 2111        isGroundedPrevious = blackBoard.isGrounded;
 2112    }
 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;
 0162                }
 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}