< 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:38
Coverable lines:61
Total lines:153
Line coverage:37.7% (23 of 61)
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%29.3511046.67%
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;
 65621    bool isGroundedPrevious = true;
 22
 23    public AvatarAnimatorLegacy avatarAnimatorLegacy;
 24    bool globalRendererIsReady;
 25
 26    private Camera mainCamera;
 27
 28    private void Start()
 29    {
 130        AudioContainer ac = GetComponent<AudioContainer>();
 131        footstepJump = ac.GetEvent("FootstepJump");
 132        footstepLand = ac.GetEvent("FootstepLand");
 133        footstepWalk = ac.GetEvent("FootstepWalk");
 134        footstepRun = ac.GetEvent("FootstepRun");
 135        clothesRustleShort = ac.GetEvent("ClothesRustleShort");
 36
 37        // Lower volume of jump/land/clothes
 138        footstepJump.source.volume = footstepJump.source.volume * 0.5f;
 139        footstepLand.source.volume = footstepLand.source.volume * 0.5f;
 140        clothesRustleShort.source.volume = clothesRustleShort.source.volume * 0.5f;
 41
 142        if (avatarAnimatorLegacy != null)
 43        {
 144            blackBoard = avatarAnimatorLegacy.blackboard;
 45        }
 46
 147        globalRendererIsReady = CommonScriptableObjects.rendererState.Get();
 148        CommonScriptableObjects.rendererState.OnChange += OnGlobalRendererStateChange;
 149    }
 50
 43651    void OnGlobalRendererStateChange(bool current, bool previous) { globalRendererIsReady = current; }
 52
 053    public void Init(GameObject rendererContainer) { this.rendererContainer = rendererContainer; }
 54
 55    private void Update()
 56    {
 157        if (blackBoard == null || !globalRendererIsReady)
 058            return;
 59
 60        // Jumped
 161        if (!blackBoard.isGrounded && isGroundedPrevious)
 62        {
 063            if (footstepJump != null)
 064                footstepJump.Play(true);
 65        }
 66
 67        // Landed
 168        if (blackBoard.isGrounded && !isGroundedPrevious)
 69        {
 070            if (footstepLand != null)
 071                footstepLand.Play(true);
 72        }
 73
 74        // Simulate footsteps when avatar is not visible
 175        if (renderer != null)
 76        {
 077            SimulateFootsteps();
 078        }
 79        else
 80        {
 181            if (rendererContainer != null)
 82            {
 83                //NOTE(Mordi): The renderer takes a while to get ready, so we need to check it continually until it can 
 084                renderer = rendererContainer.GetComponent<Renderer>();
 85            }
 86        }
 87
 188        isGroundedPrevious = blackBoard.isGrounded;
 189    }
 90
 91    bool AvatarIsInView()
 92    {
 093        if (renderer.isVisible)
 094            return true;
 95
 096        if (Camera.main == null)
 097            return false;
 98
 99        // NOTE(Mordi): In some cases, the renderer will report false even if the avatar is visible.
 100        // Therefore we must check whether or not the avatar is in the camera's view.
 101
 0102        if ( mainCamera == null )
 0103            mainCamera = Camera.main;
 104
 0105        if (mainCamera == null)
 0106            return false;
 107
 0108        Vector3 point = mainCamera.WorldToViewportPoint(transform.position);
 109
 0110        if (point.z > 0f)
 111        {
 0112            if (point.x >= 0f && point.x <= 1f)
 113            {
 0114                if (point.y >= 0f && point.y <= 1f)
 115                {
 0116                    return true;
 117                }
 118            }
 119        }
 120
 0121        return false;
 122    }
 123
 124    void SimulateFootsteps()
 125    {
 0126        if (!AvatarIsInView() && (blackBoard.movementSpeed / Time.deltaTime) > 1f && blackBoard.isGrounded)
 127        {
 0128            if (Time.time >= nextFootstepTime)
 129            {
 0130                if ((blackBoard.movementSpeed / Time.deltaTime) > 6f)
 131                {
 0132                    if (footstepRun != null)
 0133                        footstepRun.Play(true);
 134
 0135                    if (clothesRustleShort != null)
 0136                        clothesRustleShort.Play(true);
 137
 0138                    nextFootstepTime = Time.time + RUN_INTERVAL_SEC;
 0139                }
 140                else
 141                {
 0142                    if (footstepWalk != null)
 0143                        footstepWalk.Play(true);
 144
 0145                    if (clothesRustleShort != null)
 0146                        clothesRustleShort.PlayScheduled(Random.Range(0.05f, 0.1f));
 147
 0148                    nextFootstepTime = Time.time + WALK_INTERVAL_SEC;
 149                }
 150            }
 151        }
 0152    }
 153}