| | 1 | | using System.Collections; |
| | 2 | | using UnityEngine; |
| | 3 | | using DCL; |
| | 4 | |
|
| | 5 | | public 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 | | { |
| 0 | 36 | | if (audioContainer == null) |
| 0 | 37 | | return; |
| | 38 | |
|
| 0 | 39 | | this.renderingLayer = renderingLayer; |
| | 40 | |
|
| 0 | 41 | | footstepLight = audioContainer.GetEvent("FootstepLight"); |
| 0 | 42 | | footstepSlide = audioContainer.GetEvent("FootstepSlide"); |
| 0 | 43 | | footstepWalk = audioContainer.GetEvent("FootstepWalk"); |
| 0 | 44 | | footstepRun = audioContainer.GetEvent("FootstepRun"); |
| 0 | 45 | | clothesRustleShort = audioContainer.GetEvent("ClothesRustleShort"); |
| 0 | 46 | | clap = audioContainer.GetEvent("ExpressionClap"); |
| 0 | 47 | | throwMoney = audioContainer.GetEvent("ExpressionThrowMoney"); |
| 0 | 48 | | blowKiss = audioContainer.GetEvent("ExpressionBlowKiss"); |
| | 49 | |
|
| 0 | 50 | | anim = GetComponent<Animation>(); |
| 0 | 51 | | stickersController = GetComponentInParent<StickersController>(); |
| | 52 | |
|
| 0 | 53 | | Transform[] children = GetComponentsInChildren<Transform>(); |
| 0 | 54 | | footL = AvatarBodyPartReferenceUtility.GetLeftToe(children); |
| 0 | 55 | | footR = AvatarBodyPartReferenceUtility.GetRightToe(children); |
| 0 | 56 | | handL = AvatarBodyPartReferenceUtility.GetLeftHand(children); |
| 0 | 57 | | handR = AvatarBodyPartReferenceUtility.GetRightHand(children); |
| 0 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | public void AnimEvent_FootstepLight() { PlayAudioEvent(footstepLight); } |
| | 61 | |
|
| 0 | 62 | | public void AnimEvent_FootstepSlide() { PlayAudioEvent(footstepSlide); } |
| | 63 | |
|
| 0 | 64 | | public void AnimEvent_FootstepWalkLeft() { PlayAudioEvent(footstepWalk); } |
| | 65 | |
|
| 0 | 66 | | public void AnimEvent_FootstepWalkRight() { PlayAudioEvent(footstepWalk); } |
| | 67 | |
|
| | 68 | | public void AnimEvent_FootstepRunLeft() |
| | 69 | | { |
| 0 | 70 | | PlayAudioEvent(footstepRun); |
| 0 | 71 | | PlaySticker(FOOTSTEP_NAME, footL.position, Vector3.up, renderingLayer: renderingLayer); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public void AnimEvent_FootstepRunRight() |
| | 75 | | { |
| 0 | 76 | | PlayAudioEvent(footstepRun); |
| 0 | 77 | | PlaySticker(FOOTSTEP_NAME, footR.position, Vector3.up, renderingLayer: renderingLayer); |
| 0 | 78 | | } |
| | 79 | |
|
| 0 | 80 | | public void AnimEvent_ClothesRustleShort() { PlayAudioEvent(clothesRustleShort); } |
| | 81 | |
|
| | 82 | | public void AnimEvent_Clap() |
| | 83 | | { |
| 0 | 84 | | if (LastEventWasTooRecent()) |
| 0 | 85 | | return; |
| | 86 | |
|
| 0 | 87 | | if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_CLAP)) |
| 0 | 88 | | return; |
| | 89 | |
|
| 0 | 90 | | PlayAudioEvent(clap); |
| 0 | 91 | | PlaySticker(ANIM_NAME_CLAP, handR.position, Vector3.up, true, renderingLayer: renderingLayer); |
| 0 | 92 | | UpdateEventTime(); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | public void AnimEvent_ThrowMoney() |
| | 96 | | { |
| 0 | 97 | | if (LastEventWasTooRecent()) |
| 0 | 98 | | return; |
| | 99 | |
|
| 0 | 100 | | if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_MONEY)) |
| 0 | 101 | | return; |
| | 102 | |
|
| 0 | 103 | | PlayAudioEvent(throwMoney); |
| 0 | 104 | | PlaySticker(ANIM_NAME_MONEY, handL.position, handL.rotation.eulerAngles, true, renderingLayer: renderingLayer); |
| 0 | 105 | | UpdateEventTime(); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | public void AnimEvent_BlowKiss() |
| | 109 | | { |
| 0 | 110 | | if (LastEventWasTooRecent()) |
| 0 | 111 | | return; |
| | 112 | |
|
| 0 | 113 | | if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_KISS)) |
| 0 | 114 | | return; |
| | 115 | |
|
| 0 | 116 | | PlayAudioEvent(blowKiss); |
| 0 | 117 | | StartCoroutine(EmitHeartParticle()); |
| 0 | 118 | | UpdateEventTime(); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | private IEnumerator EmitHeartParticle() |
| | 122 | | { |
| 0 | 123 | | yield return new WaitForSeconds(0.8f); |
| 0 | 124 | | PlaySticker(HEART_NAME, handR.position, transform.rotation.eulerAngles, true, renderingLayer: renderingLayer); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | public void AnimEvent_Snowflakes() |
| | 128 | | { |
| 0 | 129 | | if (LastEventWasTooRecent()) |
| 0 | 130 | | return; |
| | 131 | |
|
| 0 | 132 | | if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_SNOWFLAKE)) |
| 0 | 133 | | return; |
| | 134 | |
|
| 0 | 135 | | PlaySticker("snowflakes", transform.position, Vector3.zero, true, renderingLayer: renderingLayer); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | public void AnimEvent_Hohoho() |
| | 139 | | { |
| 0 | 140 | | if (LastEventWasTooRecent()) |
| 0 | 141 | | return; |
| | 142 | |
|
| 0 | 143 | | if (!AnimationWeightIsOverThreshold(0.2f, ANIM_NAME_HOHOHO)) |
| 0 | 144 | | return; |
| | 145 | |
|
| 0 | 146 | | PlaySticker(ANIM_NAME_HOHOHO, transform.position + Vector3.up * HOHOHO_OFFSET, Vector3.zero, true, renderingLaye |
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | private void PlayAudioEvent(AudioEvent audioEvent) |
| | 150 | | { |
| 0 | 151 | | if (audioEvent != null) |
| 0 | 152 | | audioEvent.Play(true); |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | private bool AnimationWeightIsOverThreshold(float threshold, string animationName) |
| | 156 | | { |
| 0 | 157 | | if (anim != null) |
| | 158 | | { |
| 0 | 159 | | if (anim.isPlaying) |
| | 160 | | { |
| 0 | 161 | | foreach (AnimationState state in anim) |
| | 162 | | { |
| 0 | 163 | | if (state.name == animationName) |
| | 164 | | { |
| 0 | 165 | | if (state.weight > threshold) |
| 0 | 166 | | return true; |
| | 167 | | break; |
| | 168 | | } |
| | 169 | | } |
| | 170 | | } |
| | 171 | | } |
| | 172 | |
|
| 0 | 173 | | return false; |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | private void UpdateEventTime() |
| | 177 | | { |
| 0 | 178 | | lastEventTime = Time.realtimeSinceStartup; |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | private bool LastEventWasTooRecent() => |
| 0 | 182 | | 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 | | { |
| 0 | 192 | | if (stickersController != null) |
| 0 | 193 | | stickersController.PlaySticker(id, position, direction, followTransform, renderingLayer); |
| 0 | 194 | | } |
| | 195 | | } |