< 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:85
Coverable lines:85
Total lines:191
Line coverage:0% (0 of 85)
Covered branches:0
Total branches:0

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 System.Collections.Generic;
 3using UnityEngine;
 4using DCL;
 5
 6public class AvatarAnimationEventHandler : MonoBehaviour
 7{
 8    const string ANIM_NAME_KISS = "kiss", ANIM_NAME_MONEY = "money", ANIM_NAME_CLAP = "clap", ANIM_NAME_SNOWFLAKE = "sno
 9    const float MIN_EVENT_WAIT_TIME = 0.1f;
 10
 11    AudioEvent footstepLight;
 12    AudioEvent footstepSlide;
 13    AudioEvent footstepWalk;
 14    AudioEvent footstepRun;
 15    AudioEvent clothesRustleShort;
 16    AudioEvent clap;
 17    AudioEvent throwMoney;
 18    AudioEvent blowKiss;
 19
 20    Animation anim;
 21
 22    float lastEventTime;
 23
 24    StickersController stickersController;
 25
 26    Transform footL;
 27    Transform footR;
 28    Transform handL;
 29    Transform handR;
 30
 31    public void Init(AudioContainer audioContainer)
 32    {
 033        if (audioContainer == null)
 034            return;
 35
 036        footstepLight = audioContainer.GetEvent("FootstepLight");
 037        footstepSlide = audioContainer.GetEvent("FootstepSlide");
 038        footstepWalk = audioContainer.GetEvent("FootstepWalk");
 039        footstepRun = audioContainer.GetEvent("FootstepRun");
 040        clothesRustleShort = audioContainer.GetEvent("ClothesRustleShort");
 041        clap = audioContainer.GetEvent("ExpressionClap");
 042        throwMoney = audioContainer.GetEvent("ExpressionThrowMoney");
 043        blowKiss = audioContainer.GetEvent("ExpressionBlowKiss");
 44
 045        anim = GetComponent<Animation>();
 046        stickersController = GetComponentInParent<StickersController>();
 47
 048        Transform[] children = GetComponentsInChildren<Transform>();
 049        footL = AvatarBodyPartReferenceUtility.GetLeftToe(children);
 050        footR = AvatarBodyPartReferenceUtility.GetRightToe(children);
 051        handL = AvatarBodyPartReferenceUtility.GetLeftHand(children);
 052        handR = AvatarBodyPartReferenceUtility.GetRightHand(children);
 053    }
 54
 055    public void AnimEvent_FootstepLight() { PlayAudioEvent(footstepLight); }
 56
 057    public void AnimEvent_FootstepSlide() { PlayAudioEvent(footstepSlide); }
 58
 059    public void AnimEvent_FootstepWalkLeft() { PlayAudioEvent(footstepWalk); }
 60
 061    public void AnimEvent_FootstepWalkRight() { PlayAudioEvent(footstepWalk); }
 62
 63    public void AnimEvent_FootstepRunLeft()
 64    {
 065        PlayAudioEvent(footstepRun);
 066        PlaySticker("footstep", footL.position, Vector3.up);
 067    }
 68
 69    public void AnimEvent_FootstepRunRight()
 70    {
 071        PlayAudioEvent(footstepRun);
 072        PlaySticker("footstep", footR.position, Vector3.up);
 073    }
 74
 075    public void AnimEvent_ClothesRustleShort() { PlayAudioEvent(clothesRustleShort); }
 76
 77    public void AnimEvent_Clap()
 78    {
 079        if (LastEventWasTooRecent())
 080            return;
 81
 082        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_CLAP))
 083            return;
 84
 085        PlayAudioEvent(clap);
 086        PlaySticker("clap", handR.position, Vector3.up);
 087        UpdateEventTime();
 088    }
 89
 90    public void AnimEvent_ThrowMoney()
 91    {
 092        if (LastEventWasTooRecent())
 093            return;
 94
 095        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_MONEY))
 096            return;
 97
 098        PlayAudioEvent(throwMoney);
 099        PlaySticker("money", handL.position, handL.rotation.eulerAngles);
 0100        UpdateEventTime();
 0101    }
 102
 103    public void AnimEvent_BlowKiss()
 104    {
 0105        if (LastEventWasTooRecent())
 0106            return;
 107
 0108        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_KISS))
 0109            return;
 110
 0111        PlayAudioEvent(blowKiss);
 0112        StartCoroutine(EmitHeartParticle());
 0113        UpdateEventTime();
 0114    }
 115
 116    IEnumerator EmitHeartParticle()
 117    {
 0118        yield return new WaitForSeconds(0.8f);
 0119        PlaySticker("heart", handR.position, transform.rotation.eulerAngles);
 0120    }
 121
 122    public void AnimEvent_Snowflakes()
 123    {
 0124        if (LastEventWasTooRecent())
 0125            return;
 126
 0127        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_SNOWFLAKE))
 0128            return;
 129
 0130        PlaySticker("snowflakes", transform.position, Vector3.zero);
 0131    }
 132
 133    public void AnimEvent_Hohoho() {
 0134        if (LastEventWasTooRecent())
 0135            return;
 136
 0137        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_HOHOHO))
 0138            return;
 139
 0140        PlaySticker("hohoho", transform.position + transform.up * 1.5f, Vector3.zero);
 0141    }
 142
 143    void PlayAudioEvent(AudioEvent audioEvent)
 144    {
 0145        if (audioEvent != null)
 0146            audioEvent.Play(true);
 0147    }
 148
 149    bool AnimationWeightIsOverThreshold(float threshold, string animationName)
 150    {
 0151        if (anim != null)
 152        {
 0153            if (anim.isPlaying)
 154            {
 0155                foreach (AnimationState state in anim)
 156                {
 0157                    if (state.name == animationName)
 158                    {
 0159                        if (state.weight > threshold)
 0160                            return true;
 161                        break;
 162                    }
 163                }
 164            }
 165        }
 166
 0167        return false;
 0168    }
 169
 170    void UpdateEventTime()
 171    {
 0172        lastEventTime = Time.realtimeSinceStartup;
 0173    }
 174
 175    bool LastEventWasTooRecent()
 176    {
 0177        return lastEventTime + MIN_EVENT_WAIT_TIME >= Time.realtimeSinceStartup;
 178    }
 179
 180    /// <summary>
 181    /// Plays a sticker.
 182    /// </summary>
 183    /// <param name="id">ID string of sticker</param>
 184    /// <param name="position">Position in world space</param>
 185    /// <param name="rotation">Euler angles</param>
 186    void PlaySticker(string id, Vector3 position, Vector3 direction)
 187    {
 0188        if (stickersController != null)
 0189            stickersController.PlaySticker(id, position, direction, false);
 0190    }
 191}