< 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:73
Coverable lines:73
Total lines:170
Line coverage:0% (0 of 73)
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%
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";
 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    void PlayAudioEvent(AudioEvent audioEvent)
 123    {
 0124        if (audioEvent != null)
 0125            audioEvent.Play(true);
 0126    }
 127
 128    bool AnimationWeightIsOverThreshold(float threshold, string animationName)
 129    {
 0130        if (anim != null)
 131        {
 0132            if (anim.isPlaying)
 133            {
 0134                foreach (AnimationState state in anim)
 135                {
 0136                    if (state.name == animationName)
 137                    {
 0138                        if (state.weight > threshold)
 0139                            return true;
 140                        break;
 141                    }
 142                }
 143            }
 144        }
 145
 0146        return false;
 0147    }
 148
 149    void UpdateEventTime()
 150    {
 0151        lastEventTime = Time.realtimeSinceStartup;
 0152    }
 153
 154    bool LastEventWasTooRecent()
 155    {
 0156        return lastEventTime + MIN_EVENT_WAIT_TIME >= Time.realtimeSinceStartup;
 157    }
 158
 159    /// <summary>
 160    /// Plays a sticker.
 161    /// </summary>
 162    /// <param name="id">ID string of sticker</param>
 163    /// <param name="position">Position in world space</param>
 164    /// <param name="rotation">Euler angles</param>
 165    void PlaySticker(string id, Vector3 position, Vector3 direction)
 166    {
 0167        if (stickersController != null)
 0168            stickersController.PlaySticker(id, position, direction, false);
 0169    }
 170}