< Summary

Class:AvatarAnimationEventHandler
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarAnimationEventHandler.cs
Covered lines:0
Uncovered lines:86
Coverable lines:86
Total lines:195
Line coverage:0% (0 of 86)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:19
Method coverage:0% (0 of 19)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Init(...)0%6200%
AnimEvent_FootstepLight()0%2100%
AnimEvent_FootstepSlide()0%2100%
AnimEvent_FootstepWalkLeft()0%2100%
AnimEvent_FootstepWalkRight()0%2100%
AnimEvent_FootstepRunLeft()0%2100%
AnimEvent_FootstepRunRight()0%2100%
AnimEvent_ClothesRustleShort()0%2100%
AnimEvent_Clap()0%12300%
AnimEvent_ThrowMoney()0%12300%
AnimEvent_BlowKiss()0%12300%
EmitHeartParticle()0%12300%
AnimEvent_Snowflakes()0%12300%
AnimEvent_Hohoho()0%12300%
PlayAudioEvent(...)0%6200%
AnimationWeightIsOverThreshold(...)0%56700%
UpdateEventTime()0%2100%
LastEventWasTooRecent()0%2100%
PlaySticker(...)0%6200%

File(s)

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

#LineLine coverage
 1using System.Collections;
 2using UnityEngine;
 3using DCL;
 4
 5public class AvatarAnimationEventHandler : MonoBehaviour
 6{
 7    private const string FOOTSTEP_NAME = "footstep", HEART_NAME = "heart";
 8    private const string ANIM_NAME_KISS = "kiss", ANIM_NAME_MONEY = "money", ANIM_NAME_CLAP = "clap", ANIM_NAME_SNOWFLAK
 9    private const float MIN_EVENT_WAIT_TIME = 0.1f;
 10    private const float HOHOHO_OFFSET = 1.5f;
 11
 12    private AudioEvent footstepLight;
 13    private AudioEvent footstepSlide;
 14    private AudioEvent footstepWalk;
 15    private AudioEvent footstepRun;
 16    private AudioEvent clothesRustleShort;
 17    private AudioEvent clap;
 18    private AudioEvent throwMoney;
 19    private AudioEvent blowKiss;
 20
 21    private Animation anim;
 22
 23    private float lastEventTime;
 24
 25    private StickersController stickersController;
 26
 27    private int renderingLayer;
 28
 29    private Transform footL;
 30    private Transform footR;
 31    private Transform handL;
 32    private Transform handR;
 33
 34    public void Init(AudioContainer audioContainer, int renderingLayer)
 35    {
 036        if (audioContainer == null)
 037            return;
 38
 039        this.renderingLayer = renderingLayer;
 40
 041        footstepLight = audioContainer.GetEvent("FootstepLight");
 042        footstepSlide = audioContainer.GetEvent("FootstepSlide");
 043        footstepWalk = audioContainer.GetEvent("FootstepWalk");
 044        footstepRun = audioContainer.GetEvent("FootstepRun");
 045        clothesRustleShort = audioContainer.GetEvent("ClothesRustleShort");
 046        clap = audioContainer.GetEvent("ExpressionClap");
 047        throwMoney = audioContainer.GetEvent("ExpressionThrowMoney");
 048        blowKiss = audioContainer.GetEvent("ExpressionBlowKiss");
 49
 050        anim = GetComponent<Animation>();
 051        stickersController = GetComponentInParent<StickersController>();
 52
 053        Transform[] children = GetComponentsInChildren<Transform>();
 054        footL = AvatarBodyPartReferenceUtility.GetLeftToe(children);
 055        footR = AvatarBodyPartReferenceUtility.GetRightToe(children);
 056        handL = AvatarBodyPartReferenceUtility.GetLeftHand(children);
 057        handR = AvatarBodyPartReferenceUtility.GetRightHand(children);
 058    }
 59
 060    public void AnimEvent_FootstepLight() { PlayAudioEvent(footstepLight); }
 61
 062    public void AnimEvent_FootstepSlide() { PlayAudioEvent(footstepSlide); }
 63
 064    public void AnimEvent_FootstepWalkLeft() { PlayAudioEvent(footstepWalk); }
 65
 066    public void AnimEvent_FootstepWalkRight() { PlayAudioEvent(footstepWalk); }
 67
 68    public void AnimEvent_FootstepRunLeft()
 69    {
 070        PlayAudioEvent(footstepRun);
 071        PlaySticker(FOOTSTEP_NAME, footL.position, Vector3.up, renderingLayer: renderingLayer);
 072    }
 73
 74    public void AnimEvent_FootstepRunRight()
 75    {
 076        PlayAudioEvent(footstepRun);
 077        PlaySticker(FOOTSTEP_NAME, footR.position, Vector3.up, renderingLayer: renderingLayer);
 078    }
 79
 080    public void AnimEvent_ClothesRustleShort() { PlayAudioEvent(clothesRustleShort); }
 81
 82    public void AnimEvent_Clap()
 83    {
 084        if (LastEventWasTooRecent())
 085            return;
 86
 087        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_CLAP))
 088            return;
 89
 090        PlayAudioEvent(clap);
 091        PlaySticker(ANIM_NAME_CLAP, handR.position, Vector3.up, true, renderingLayer: renderingLayer);
 092        UpdateEventTime();
 093    }
 94
 95    public void AnimEvent_ThrowMoney()
 96    {
 097        if (LastEventWasTooRecent())
 098            return;
 99
 0100        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_MONEY))
 0101            return;
 102
 0103        PlayAudioEvent(throwMoney);
 0104        PlaySticker(ANIM_NAME_MONEY, handL.position, handL.rotation.eulerAngles, true, renderingLayer: renderingLayer);
 0105        UpdateEventTime();
 0106    }
 107
 108    public void AnimEvent_BlowKiss()
 109    {
 0110        if (LastEventWasTooRecent())
 0111            return;
 112
 0113        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_KISS))
 0114            return;
 115
 0116        PlayAudioEvent(blowKiss);
 0117        StartCoroutine(EmitHeartParticle());
 0118        UpdateEventTime();
 0119    }
 120
 121    private IEnumerator EmitHeartParticle()
 122    {
 0123        yield return new WaitForSeconds(0.8f);
 0124        PlaySticker(HEART_NAME, handR.position, transform.rotation.eulerAngles, true, renderingLayer: renderingLayer);
 0125    }
 126
 127    public void AnimEvent_Snowflakes()
 128    {
 0129        if (LastEventWasTooRecent())
 0130            return;
 131
 0132        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_SNOWFLAKE))
 0133            return;
 134
 0135        PlaySticker("snowflakes", transform.position, Vector3.zero, true, renderingLayer: renderingLayer);
 0136    }
 137
 138    public void AnimEvent_Hohoho()
 139    {
 0140        if (LastEventWasTooRecent())
 0141            return;
 142
 0143        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_HOHOHO))
 0144            return;
 145
 0146        PlaySticker(ANIM_NAME_HOHOHO, transform.position + Vector3.up * HOHOHO_OFFSET, Vector3.zero, true, renderingLaye
 0147    }
 148
 149    private void PlayAudioEvent(AudioEvent audioEvent)
 150    {
 0151        if (audioEvent != null)
 0152            audioEvent.Play(true);
 0153    }
 154
 155    private bool AnimationWeightIsOverThreshold(float threshold, string animationName)
 156    {
 0157        if (anim != null)
 158        {
 0159            if (anim.isPlaying)
 160            {
 0161                foreach (AnimationState state in anim)
 162                {
 0163                    if (state.name == animationName)
 164                    {
 0165                        if (state.weight > threshold)
 0166                            return true;
 167                        break;
 168                    }
 169                }
 170            }
 171        }
 172
 0173        return false;
 0174    }
 175
 176    private void UpdateEventTime()
 177    {
 0178        lastEventTime = Time.realtimeSinceStartup;
 0179    }
 180
 181    private bool LastEventWasTooRecent() =>
 0182        lastEventTime + MIN_EVENT_WAIT_TIME >= Time.realtimeSinceStartup;
 183
 184    /// <summary>
 185    /// Plays a sticker.
 186    /// </summary>
 187    /// <param name="id">ID string of sticker</param>
 188    /// <param name="position">Position in world space</param>
 189    /// <param name="rotation">Euler angles</param>
 190    private void PlaySticker(string id, Vector3 position, Vector3 direction, bool followTransform = false, int rendering
 191    {
 0192        if (stickersController != null)
 0193            stickersController.PlaySticker(id, position, direction, followTransform, renderingLayer);
 0194    }
 195}