| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using Categories = WearableLiterals.Categories; |
| | 6 | | using Rarity = WearableLiterals.ItemRarity; |
| | 7 | |
|
| | 8 | | public class AvatarEditorHUDAudioHandler : MonoBehaviour |
| | 9 | | { |
| | 10 | | [SerializeField] |
| | 11 | | AvatarEditorHUDView view; |
| | 12 | |
|
| | 13 | | [SerializeField] |
| | 14 | | ItemSelector nftItemSelector; |
| | 15 | |
|
| | 16 | | [SerializeField] |
| | 17 | | Button randomizeButton; |
| | 18 | |
|
| | 19 | | [SerializeField] |
| | 20 | | AudioEvent eventMusic, eventRarity, eventAvatarAppear, eventReactionMale, eventReactionFemale, eventWearableClothing |
| | 21 | | eventWearableFootwear, eventWearableHair, eventWearableHatMask, eventWearableRarity; |
| | 22 | |
|
| | 23 | | WearableItem lastSelectedWearable = null; |
| | 24 | | bool hasClickedRandomize = false, wearableIsSameAsPrevious = false; |
| | 25 | |
|
| | 26 | | IEnumerator musicFadeOut; |
| | 27 | |
|
| | 28 | | private void Start() |
| | 29 | | { |
| 1 | 30 | | int nPairs = view.wearableGridPairs.Length; |
| 34 | 31 | | for (int i = 0; i < nPairs; i++) |
| | 32 | | { |
| 16 | 33 | | view.wearableGridPairs[i].selector.OnItemClicked += OnSelectWearable; |
| | 34 | | } |
| | 35 | |
|
| 1 | 36 | | nftItemSelector.OnItemClicked += OnSelectWearable; |
| | 37 | |
|
| 1 | 38 | | view.OnSetVisibility += OnSetAvatarEditorVisibility; |
| 1 | 39 | | view.OnRandomize += OnClickRandomize; |
| 1 | 40 | | } |
| | 41 | |
|
| | 42 | | void OnSelectWearable(string wearableId) |
| | 43 | | { |
| 0 | 44 | | CatalogController.wearableCatalog.TryGetValue(wearableId, out var wearable); |
| 0 | 45 | | wearableIsSameAsPrevious = (wearable == lastSelectedWearable); |
| 0 | 46 | | if (wearableIsSameAsPrevious) |
| 0 | 47 | | return; |
| | 48 | |
|
| 0 | 49 | | lastSelectedWearable = wearable; |
| 0 | 50 | | if (wearable == null) |
| 0 | 51 | | return; |
| | 52 | |
|
| 0 | 53 | | switch (wearable.data.category) |
| | 54 | | { |
| | 55 | | case Categories.EYEBROWS: |
| 0 | 56 | | eventWearableHair.Play(true); |
| 0 | 57 | | break; |
| | 58 | | case "facial_hair": |
| 0 | 59 | | eventWearableHair.Play(true); |
| 0 | 60 | | break; |
| | 61 | | case Categories.FEET: |
| 0 | 62 | | eventWearableFootwear.Play(true); |
| 0 | 63 | | break; |
| | 64 | | case Categories.HAIR: |
| 0 | 65 | | eventWearableHair.Play(true); |
| 0 | 66 | | break; |
| | 67 | | case Categories.LOWER_BODY: |
| 0 | 68 | | eventWearableClothing.Play(true); |
| 0 | 69 | | break; |
| | 70 | | case Categories.UPPER_BODY: |
| 0 | 71 | | eventWearableClothing.Play(true); |
| 0 | 72 | | break; |
| | 73 | | case "eyewear": |
| 0 | 74 | | eventWearableEyewear.Play(true); |
| 0 | 75 | | break; |
| | 76 | | case "tiara": |
| 0 | 77 | | eventWearableJewelry.Play(true); |
| 0 | 78 | | break; |
| | 79 | | case "earring": |
| 0 | 80 | | eventWearableJewelry.Play(true); |
| 0 | 81 | | break; |
| | 82 | | case "hat": |
| 0 | 83 | | eventWearableHatMask.Play(true); |
| 0 | 84 | | break; |
| | 85 | | case "top_head": |
| 0 | 86 | | eventWearableFootwear.Play(true); |
| 0 | 87 | | break; |
| | 88 | | case "helmet": |
| 0 | 89 | | eventWearableFootwear.Play(true); |
| 0 | 90 | | break; |
| | 91 | | case "mask": |
| 0 | 92 | | eventWearableHatMask.Play(true); |
| | 93 | | break; |
| | 94 | | default: |
| | 95 | | break; |
| | 96 | | } |
| 0 | 97 | | } |
| | 98 | |
|
| 0 | 99 | | void OnEyeColorChanged(Color color) { ResetLastClickedWearable(); } |
| | 100 | |
|
| 0 | 101 | | void OnSkinColorChanged(Color color) { ResetLastClickedWearable(); } |
| | 102 | |
|
| 0 | 103 | | void OnHairColorChanged(Color color) { ResetLastClickedWearable(); } |
| | 104 | |
|
| | 105 | | void OnClickRandomize() |
| | 106 | | { |
| 0 | 107 | | hasClickedRandomize = true; |
| 0 | 108 | | ResetLastClickedWearable(); |
| 0 | 109 | | } |
| | 110 | |
|
| 0 | 111 | | void ResetLastClickedWearable() { lastSelectedWearable = null; } |
| | 112 | |
|
| | 113 | | void OnAvatarAppear(AvatarModel model) |
| | 114 | | { |
| 0 | 115 | | if (!view.isOpen) |
| 0 | 116 | | return; |
| | 117 | |
|
| 0 | 118 | | eventAvatarAppear.Play(true); |
| | 119 | |
|
| 0 | 120 | | if (!wearableIsSameAsPrevious) |
| | 121 | | { |
| 0 | 122 | | if (lastSelectedWearable != null) |
| 0 | 123 | | PlayRarity(); |
| | 124 | |
|
| 0 | 125 | | if (lastSelectedWearable != null || hasClickedRandomize) |
| 0 | 126 | | PlayVoiceReaction(model.bodyShape); |
| | 127 | | } |
| | 128 | |
|
| 0 | 129 | | hasClickedRandomize = false; |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | void PlayRarity() |
| | 133 | | { |
| 0 | 134 | | if (lastSelectedWearable.rarity == null) |
| 0 | 135 | | return; |
| | 136 | |
|
| | 137 | | /*switch (lastClickedWearable.rarity) |
| | 138 | | { |
| | 139 | | case Rarity.RARE: |
| | 140 | | eventRarity.SetIndex(0); |
| | 141 | | break; |
| | 142 | | case Rarity.EPIC: |
| | 143 | | eventRarity.SetIndex(1); |
| | 144 | | break; |
| | 145 | | case Rarity.LEGENDARY: |
| | 146 | | eventRarity.SetIndex(2); |
| | 147 | | break; |
| | 148 | | case Rarity.MYTHIC: |
| | 149 | | eventRarity.SetIndex(3); |
| | 150 | | break; |
| | 151 | | case Rarity.UNIQUE: |
| | 152 | | eventRarity.SetIndex(4); |
| | 153 | | break; |
| | 154 | | default: |
| | 155 | | eventRarity.SetIndex(0); |
| | 156 | | break; |
| | 157 | | }*/ |
| | 158 | |
|
| 0 | 159 | | if (lastSelectedWearable.rarity == Rarity.UNIQUE) |
| 0 | 160 | | eventRarity.SetIndex(1); |
| | 161 | | else |
| 0 | 162 | | eventRarity.SetIndex(0); |
| | 163 | |
|
| 0 | 164 | | eventRarity.Play(true); |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | void PlayVoiceReaction(string bodyShape) |
| | 168 | | { |
| 0 | 169 | | float chanceToPlay = 0.75f; |
| 0 | 170 | | AudioEvent eventReaction = null; |
| | 171 | |
|
| 0 | 172 | | if (bodyShape.Contains("Female")) |
| 0 | 173 | | eventReaction = eventReactionFemale; |
| | 174 | | else |
| 0 | 175 | | eventReaction = eventReactionMale; |
| | 176 | |
|
| 0 | 177 | | if (lastSelectedWearable != null) |
| | 178 | | { |
| 0 | 179 | | if (lastSelectedWearable.rarity == Rarity.EPIC |
| | 180 | | || lastSelectedWearable.rarity == Rarity.LEGENDARY |
| | 181 | | || lastSelectedWearable.rarity == Rarity.MYTHIC |
| | 182 | | || lastSelectedWearable.rarity == Rarity.UNIQUE) |
| | 183 | | { |
| 0 | 184 | | eventReaction.RandomizeIndex(14, 28); |
| 0 | 185 | | chanceToPlay = 1f; |
| 0 | 186 | | } |
| | 187 | | else |
| | 188 | | { |
| 0 | 189 | | eventReaction.RandomizeIndex(0, 14); |
| | 190 | | } |
| | 191 | | } |
| | 192 | |
|
| 0 | 193 | | if (eventReaction != null && Random.Range(0f, 1f) <= chanceToPlay) |
| | 194 | | { |
| 0 | 195 | | if (!eventReaction.source.isPlaying) |
| | 196 | | { |
| 0 | 197 | | eventReaction.PlayScheduled(0.6f); |
| | 198 | | } |
| | 199 | | } |
| 0 | 200 | | } |
| | 201 | |
|
| | 202 | | void OnSetAvatarEditorVisibility(bool visible) |
| | 203 | | { |
| 0 | 204 | | AudioScriptableObjects.listItemAppear.ResetPitch(); |
| | 205 | |
|
| 0 | 206 | | if (visible) |
| | 207 | | { |
| 0 | 208 | | if (musicFadeOut != null) |
| | 209 | | { |
| 0 | 210 | | StopCoroutine(musicFadeOut); |
| 0 | 211 | | StartCoroutine(eventMusic.FadeIn(1f)); |
| | 212 | | } |
| | 213 | |
|
| 0 | 214 | | if (!eventMusic.source.isPlaying) |
| 0 | 215 | | eventMusic.Play(); |
| | 216 | |
|
| 0 | 217 | | view.eyeColorSelector.OnColorChanged += OnEyeColorChanged; |
| 0 | 218 | | view.skinColorSelector.OnColorChanged += OnSkinColorChanged; |
| 0 | 219 | | view.hairColorSelector.OnColorChanged += OnHairColorChanged; |
| 0 | 220 | | view.OnAvatarAppear += OnAvatarAppear; |
| 0 | 221 | | } |
| | 222 | | else |
| | 223 | | { |
| 0 | 224 | | musicFadeOut = eventMusic.FadeOut(2f); |
| 0 | 225 | | StartCoroutine(musicFadeOut); |
| | 226 | |
|
| 0 | 227 | | view.eyeColorSelector.OnColorChanged -= OnEyeColorChanged; |
| 0 | 228 | | view.skinColorSelector.OnColorChanged -= OnSkinColorChanged; |
| 0 | 229 | | view.hairColorSelector.OnColorChanged -= OnHairColorChanged; |
| 0 | 230 | | view.OnAvatarAppear -= OnAvatarAppear; |
| | 231 | | } |
| 0 | 232 | | } |
| | 233 | | } |