< 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:194
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 FOOTSTEP_NAME = "footstep", HEART_NAME = "heart";
 9    const string ANIM_NAME_KISS = "kiss", ANIM_NAME_MONEY = "money", ANIM_NAME_CLAP = "clap", ANIM_NAME_SNOWFLAKE = "sno
 10    const float MIN_EVENT_WAIT_TIME = 0.1f;
 11    const float HOHOHO_OFFSET = 1.5f;
 12
 13    AudioEvent footstepLight;
 14    AudioEvent footstepSlide;
 15    AudioEvent footstepWalk;
 16    AudioEvent footstepRun;
 17    AudioEvent clothesRustleShort;
 18    AudioEvent clap;
 19    AudioEvent throwMoney;
 20    AudioEvent blowKiss;
 21
 22    Animation anim;
 23
 24    float lastEventTime;
 25
 26    StickersController stickersController;
 27
 28    Transform footL;
 29    Transform footR;
 30    Transform handL;
 31    Transform handR;
 32
 33    public void Init(AudioContainer audioContainer)
 34    {
 035        if (audioContainer == null)
 036            return;
 37
 038        footstepLight = audioContainer.GetEvent("FootstepLight");
 039        footstepSlide = audioContainer.GetEvent("FootstepSlide");
 040        footstepWalk = audioContainer.GetEvent("FootstepWalk");
 041        footstepRun = audioContainer.GetEvent("FootstepRun");
 042        clothesRustleShort = audioContainer.GetEvent("ClothesRustleShort");
 043        clap = audioContainer.GetEvent("ExpressionClap");
 044        throwMoney = audioContainer.GetEvent("ExpressionThrowMoney");
 045        blowKiss = audioContainer.GetEvent("ExpressionBlowKiss");
 46
 047        anim = GetComponent<Animation>();
 048        stickersController = GetComponentInParent<StickersController>();
 49
 050        Transform[] children = GetComponentsInChildren<Transform>();
 051        footL = AvatarBodyPartReferenceUtility.GetLeftToe(children);
 052        footR = AvatarBodyPartReferenceUtility.GetRightToe(children);
 053        handL = AvatarBodyPartReferenceUtility.GetLeftHand(children);
 054        handR = AvatarBodyPartReferenceUtility.GetRightHand(children);
 055    }
 56
 057    public void AnimEvent_FootstepLight() { PlayAudioEvent(footstepLight); }
 58
 059    public void AnimEvent_FootstepSlide() { PlayAudioEvent(footstepSlide); }
 60
 061    public void AnimEvent_FootstepWalkLeft() { PlayAudioEvent(footstepWalk); }
 62
 063    public void AnimEvent_FootstepWalkRight() { PlayAudioEvent(footstepWalk); }
 64
 65    public void AnimEvent_FootstepRunLeft()
 66    {
 067        PlayAudioEvent(footstepRun);
 068        PlaySticker(FOOTSTEP_NAME, footL.position, Vector3.up);
 069    }
 70
 71    public void AnimEvent_FootstepRunRight()
 72    {
 073        PlayAudioEvent(footstepRun);
 074        PlaySticker(FOOTSTEP_NAME, footR.position, Vector3.up);
 075    }
 76
 077    public void AnimEvent_ClothesRustleShort() { PlayAudioEvent(clothesRustleShort); }
 78
 79    public void AnimEvent_Clap()
 80    {
 081        if (LastEventWasTooRecent())
 082            return;
 83
 084        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_CLAP))
 085            return;
 86
 087        PlayAudioEvent(clap);
 088        PlaySticker(ANIM_NAME_CLAP, handR.position, Vector3.up, true);
 089        UpdateEventTime();
 090    }
 91
 92    public void AnimEvent_ThrowMoney()
 93    {
 094        if (LastEventWasTooRecent())
 095            return;
 96
 097        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_MONEY))
 098            return;
 99
 0100        PlayAudioEvent(throwMoney);
 0101        PlaySticker(ANIM_NAME_MONEY, handL.position, handL.rotation.eulerAngles, true);
 0102        UpdateEventTime();
 0103    }
 104
 105    public void AnimEvent_BlowKiss()
 106    {
 0107        if (LastEventWasTooRecent())
 0108            return;
 109
 0110        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_KISS))
 0111            return;
 112
 0113        PlayAudioEvent(blowKiss);
 0114        StartCoroutine(EmitHeartParticle());
 0115        UpdateEventTime();
 0116    }
 117
 118    IEnumerator EmitHeartParticle()
 119    {
 0120        yield return new WaitForSeconds(0.8f);
 0121        PlaySticker(HEART_NAME, handR.position, transform.rotation.eulerAngles, true);
 0122    }
 123
 124    public void AnimEvent_Snowflakes()
 125    {
 0126        if (LastEventWasTooRecent())
 0127            return;
 128
 0129        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_SNOWFLAKE))
 0130            return;
 131
 0132        PlaySticker("snowflakes", transform.position, Vector3.zero, true);
 0133    }
 134
 135    public void AnimEvent_Hohoho()
 136    {
 0137        if (LastEventWasTooRecent())
 0138            return;
 139
 0140        if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_HOHOHO))
 0141            return;
 142
 0143        PlaySticker(ANIM_NAME_HOHOHO, transform.position + Vector3.up * HOHOHO_OFFSET, Vector3.zero, true);
 0144    }
 145
 146    void PlayAudioEvent(AudioEvent audioEvent)
 147    {
 0148        if (audioEvent != null)
 0149            audioEvent.Play(true);
 0150    }
 151
 152    bool AnimationWeightIsOverThreshold(float threshold, string animationName)
 153    {
 0154        if (anim != null)
 155        {
 0156            if (anim.isPlaying)
 157            {
 0158                foreach (AnimationState state in anim)
 159                {
 0160                    if (state.name == animationName)
 161                    {
 0162                        if (state.weight > threshold)
 0163                            return true;
 164                        break;
 165                    }
 166                }
 167            }
 168        }
 169
 0170        return false;
 0171    }
 172
 173    void UpdateEventTime()
 174    {
 0175        lastEventTime = Time.realtimeSinceStartup;
 0176    }
 177
 178    bool LastEventWasTooRecent()
 179    {
 0180        return lastEventTime + MIN_EVENT_WAIT_TIME >= Time.realtimeSinceStartup;
 181    }
 182
 183    /// <summary>
 184    /// Plays a sticker.
 185    /// </summary>
 186    /// <param name="id">ID string of sticker</param>
 187    /// <param name="position">Position in world space</param>
 188    /// <param name="rotation">Euler angles</param>
 189    void PlaySticker(string id, Vector3 position, Vector3 direction, bool followTransform = false)
 190    {
 0191        if (stickersController != null)
 0192            stickersController.PlaySticker(id, position, direction, followTransform);
 0193    }
 194}