< 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:193
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    const float AVATAR_OFFSET = 0.755f; //USE IT WHEN DEALING WITH LOCAL SPACE
 11
 12    AudioEvent footstepLight;
 13    AudioEvent footstepSlide;
 14    AudioEvent footstepWalk;
 15    AudioEvent footstepRun;
 16    AudioEvent clothesRustleShort;
 17    AudioEvent clap;
 18    AudioEvent throwMoney;
 19    AudioEvent blowKiss;
 20
 21    Animation anim;
 22
 23    float lastEventTime;
 24
 25    StickersController stickersController;
 26
 27    Transform footL;
 28    Transform footR;
 29    Transform handL;
 30    Transform handR;
 31
 32    public void Init(AudioContainer audioContainer)
 33    {
 034        if (audioContainer == null)
 035            return;
 36
 037        footstepLight = audioContainer.GetEvent("FootstepLight");
 038        footstepSlide = audioContainer.GetEvent("FootstepSlide");
 039        footstepWalk = audioContainer.GetEvent("FootstepWalk");
 040        footstepRun = audioContainer.GetEvent("FootstepRun");
 041        clothesRustleShort = audioContainer.GetEvent("ClothesRustleShort");
 042        clap = audioContainer.GetEvent("ExpressionClap");
 043        throwMoney = audioContainer.GetEvent("ExpressionThrowMoney");
 044        blowKiss = audioContainer.GetEvent("ExpressionBlowKiss");
 45
 046        anim = GetComponent<Animation>();
 047        stickersController = GetComponentInParent<StickersController>();
 48
 049        Transform[] children = GetComponentsInChildren<Transform>();
 050        footL = AvatarBodyPartReferenceUtility.GetLeftToe(children);
 051        footR = AvatarBodyPartReferenceUtility.GetRightToe(children);
 052        handL = AvatarBodyPartReferenceUtility.GetLeftHand(children);
 053        handR = AvatarBodyPartReferenceUtility.GetRightHand(children);
 054    }
 55
 056    public void AnimEvent_FootstepLight() { PlayAudioEvent(footstepLight); }
 57
 058    public void AnimEvent_FootstepSlide() { PlayAudioEvent(footstepSlide); }
 59
 060    public void AnimEvent_FootstepWalkLeft() { PlayAudioEvent(footstepWalk); }
 61
 062    public void AnimEvent_FootstepWalkRight() { PlayAudioEvent(footstepWalk); }
 63
 64    public void AnimEvent_FootstepRunLeft()
 65    {
 066        PlayAudioEvent(footstepRun);
 067        PlaySticker("footstep", footL.position, Vector3.up);
 068    }
 69
 70    public void AnimEvent_FootstepRunRight()
 71    {
 072        PlayAudioEvent(footstepRun);
 073        PlaySticker("footstep", footR.position, Vector3.up);
 074    }
 75
 076    public void AnimEvent_ClothesRustleShort() { PlayAudioEvent(clothesRustleShort); }
 77
 78    public void AnimEvent_Clap()
 79    {
 080        if (LastEventWasTooRecent())
 081            return;
 82
 083        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_CLAP))
 084            return;
 85
 086        PlayAudioEvent(clap);
 087        PlaySticker("clap", handR.position - transform.position + (Vector3.up * AVATAR_OFFSET), Vector3.up, true);
 088        UpdateEventTime();
 089    }
 90
 91    public void AnimEvent_ThrowMoney()
 92    {
 093        if (LastEventWasTooRecent())
 094            return;
 95
 096        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_MONEY))
 097            return;
 98
 099        PlayAudioEvent(throwMoney);
 0100        PlaySticker("money", handL.position - transform.position + (Vector3.up * AVATAR_OFFSET), handL.rotation.eulerAng
 0101        UpdateEventTime();
 0102    }
 103
 104    public void AnimEvent_BlowKiss()
 105    {
 0106        if (LastEventWasTooRecent())
 0107            return;
 108
 0109        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_KISS))
 0110            return;
 111
 0112        PlayAudioEvent(blowKiss);
 0113        StartCoroutine(EmitHeartParticle());
 0114        UpdateEventTime();
 0115    }
 116
 117    IEnumerator EmitHeartParticle()
 118    {
 0119        yield return new WaitForSeconds(0.8f);
 0120        PlaySticker("heart", handR.position - transform.position + (Vector3.up * AVATAR_OFFSET), transform.rotation.eule
 0121    }
 122
 123    public void AnimEvent_Snowflakes()
 124    {
 0125        if (LastEventWasTooRecent())
 0126            return;
 127
 0128        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_SNOWFLAKE))
 0129            return;
 130
 0131        PlaySticker("snowflakes", Vector3.up * AVATAR_OFFSET, Vector3.zero, true);
 0132    }
 133
 134    public void AnimEvent_Hohoho()
 135    {
 0136        if (LastEventWasTooRecent())
 0137            return;
 138
 0139        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_HOHOHO))
 0140            return;
 141
 0142        PlaySticker("hohoho", Vector3.up * (1.5f + AVATAR_OFFSET), Vector3.zero, true);
 0143    }
 144
 145    void PlayAudioEvent(AudioEvent audioEvent)
 146    {
 0147        if (audioEvent != null)
 0148            audioEvent.Play(true);
 0149    }
 150
 151    bool AnimationWeightIsOverThreshold(float threshold, string animationName)
 152    {
 0153        if (anim != null)
 154        {
 0155            if (anim.isPlaying)
 156            {
 0157                foreach (AnimationState state in anim)
 158                {
 0159                    if (state.name == animationName)
 160                    {
 0161                        if (state.weight > threshold)
 0162                            return true;
 163                        break;
 164                    }
 165                }
 166            }
 167        }
 168
 0169        return false;
 0170    }
 171
 172    void UpdateEventTime()
 173    {
 0174        lastEventTime = Time.realtimeSinceStartup;
 0175    }
 176
 177    bool LastEventWasTooRecent()
 178    {
 0179        return lastEventTime + MIN_EVENT_WAIT_TIME >= Time.realtimeSinceStartup;
 180    }
 181
 182    /// <summary>
 183    /// Plays a sticker.
 184    /// </summary>
 185    /// <param name="id">ID string of sticker</param>
 186    /// <param name="position">Position in world space</param>
 187    /// <param name="rotation">Euler angles</param>
 188    void PlaySticker(string id, Vector3 position, Vector3 direction, bool followTransform = false)
 189    {
 0190        if (stickersController != null)
 0191            stickersController.PlaySticker(id, position, direction, followTransform);
 0192    }
 193}