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