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