< 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:173
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;
 66321    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
 43655    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    {
 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);
 087            if (stickersController != null && footL != null && footR != null)
 088                    stickersController.PlaySticker("footstepLand",
 89                        Vector3.Lerp(footL.position, footR.position, 0.5f),
 90                        Vector3.up,
 91                        false);
 92        }
 93
 94        // Simulate footsteps when avatar is not visible
 195        if (renderer != null)
 96        {
 097            SimulateFootsteps();
 098        }
 99        else
 100        {
 1101            if (rendererContainer != null)
 102            {
 103                //NOTE(Mordi): The renderer takes a while to get ready, so we need to check it continually until it can 
 0104                renderer = rendererContainer.GetComponent<Renderer>();
 105            }
 106        }
 107
 1108        isGroundedPrevious = blackBoard.isGrounded;
 1109    }
 110
 111    bool AvatarIsInView()
 112    {
 0113        if (renderer.isVisible)
 0114            return true;
 115
 0116        if (Camera.main == null)
 0117            return false;
 118
 119        // NOTE(Mordi): In some cases, the renderer will report false even if the avatar is visible.
 120        // Therefore we must check whether or not the avatar is in the camera's view.
 121
 0122        if ( mainCamera == null )
 0123            mainCamera = Camera.main;
 124
 0125        if (mainCamera == null)
 0126            return false;
 127
 0128        Vector3 point = mainCamera.WorldToViewportPoint(transform.position);
 129
 0130        if (point.z > 0f)
 131        {
 0132            if (point.x >= 0f && point.x <= 1f)
 133            {
 0134                if (point.y >= 0f && point.y <= 1f)
 135                {
 0136                    return true;
 137                }
 138            }
 139        }
 140
 0141        return false;
 142    }
 143
 144    void SimulateFootsteps()
 145    {
 0146        if (!AvatarIsInView() && (blackBoard.movementSpeed / Time.deltaTime) > 1f && blackBoard.isGrounded)
 147        {
 0148            if (Time.time >= nextFootstepTime)
 149            {
 0150                if ((blackBoard.movementSpeed / Time.deltaTime) > 6f)
 151                {
 0152                    if (footstepRun != null)
 0153                        footstepRun.Play(true);
 154
 0155                    if (clothesRustleShort != null)
 0156                        clothesRustleShort.Play(true);
 157
 0158                    nextFootstepTime = Time.time + RUN_INTERVAL_SEC;
 0159                }
 160                else
 161                {
 0162                    if (footstepWalk != null)
 0163                        footstepWalk.Play(true);
 164
 0165                    if (clothesRustleShort != null)
 0166                        clothesRustleShort.PlayScheduled(Random.Range(0.05f, 0.1f));
 167
 0168                    nextFootstepTime = Time.time + WALK_INTERVAL_SEC;
 169                }
 170            }
 171        }
 0172    }
 173}