< Summary

Class:AvatarAudioHandlerRemote
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarAudioHandlerRemote.cs
Covered lines:1
Uncovered lines:60
Coverable lines:61
Total lines:153
Line coverage:1.6% (1 of 61)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarAudioHandlerRemote()0%110100%
Start()0%6200%
OnGlobalRendererStateChange(...)0%2100%
Init(...)0%2100%
Update()0%1321100%
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;
 65521    bool isGroundedPrevious = true;
 22
 23    public AvatarAnimatorLegacy avatarAnimatorLegacy;
 24    bool globalRendererIsReady;
 25
 26    private Camera mainCamera;
 27
 28    private void Start()
 29    {
 030        AudioContainer ac = GetComponent<AudioContainer>();
 031        footstepJump = ac.GetEvent("FootstepJump");
 032        footstepLand = ac.GetEvent("FootstepLand");
 033        footstepWalk = ac.GetEvent("FootstepWalk");
 034        footstepRun = ac.GetEvent("FootstepRun");
 035        clothesRustleShort = ac.GetEvent("ClothesRustleShort");
 36
 37        // Lower volume of jump/land/clothes
 038        footstepJump.source.volume = footstepJump.source.volume * 0.5f;
 039        footstepLand.source.volume = footstepLand.source.volume * 0.5f;
 040        clothesRustleShort.source.volume = clothesRustleShort.source.volume * 0.5f;
 41
 042        if (avatarAnimatorLegacy != null)
 43        {
 044            blackBoard = avatarAnimatorLegacy.blackboard;
 45        }
 46
 047        globalRendererIsReady = CommonScriptableObjects.rendererState.Get();
 048        CommonScriptableObjects.rendererState.OnChange += OnGlobalRendererStateChange;
 049    }
 50
 051    void OnGlobalRendererStateChange(bool current, bool previous) { globalRendererIsReady = current; }
 52
 053    public void Init(GameObject rendererContainer) { this.rendererContainer = rendererContainer; }
 54
 55    private void Update()
 56    {
 057        if (blackBoard == null || !globalRendererIsReady)
 058            return;
 59
 60        // Jumped
 061        if (!blackBoard.isGrounded && isGroundedPrevious)
 62        {
 063            if (footstepJump != null)
 064                footstepJump.Play(true);
 65        }
 66
 67        // Landed
 068        if (blackBoard.isGrounded && !isGroundedPrevious)
 69        {
 070            if (footstepLand != null)
 071                footstepLand.Play(true);
 72        }
 73
 74        // Simulate footsteps when avatar is not visible
 075        if (renderer != null)
 76        {
 077            SimulateFootsteps();
 078        }
 79        else
 80        {
 081            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
 088        isGroundedPrevious = blackBoard.isGrounded;
 089    }
 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}