| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace AvatarSystem |
| | 6 | | { |
| | 7 | | public class EmoteAnimationEquipper : IEmoteAnimationEquipper |
| | 8 | | { |
| | 9 | | internal readonly IAnimator animator; |
| | 10 | | internal readonly DataStore_Emotes dataStoreEmotes; |
| | 11 | |
|
| 1311 | 12 | | internal string bodyShapeId = ""; |
| 1311 | 13 | | internal readonly List<string> emotes = new List<string>(); |
| | 14 | |
|
| 1311 | 15 | | public EmoteAnimationEquipper(IAnimator animator, DataStore_Emotes dataStoreEmotes) |
| | 16 | | { |
| 1311 | 17 | | this.animator = animator; |
| 1311 | 18 | | this.dataStoreEmotes = dataStoreEmotes; |
| 1311 | 19 | | this.dataStoreEmotes.animations.OnAdded += OnAnimationAdded; |
| 1311 | 20 | | this.dataStoreEmotes.animations.OnRemoved += OnAnimationRemoved; |
| 1311 | 21 | | } |
| | 22 | |
|
| | 23 | | private void OnAnimationAdded((string bodyshapeId, string emoteId) values, AnimationClip animationClip) |
| | 24 | | { |
| 4 | 25 | | if (bodyShapeId != values.bodyshapeId) |
| 3 | 26 | | return; |
| | 27 | |
|
| 1 | 28 | | if (!emotes.Contains(values.emoteId)) |
| 0 | 29 | | return; |
| | 30 | |
|
| 1 | 31 | | animator.EquipEmote(values.emoteId, animationClip); |
| 1 | 32 | | } |
| | 33 | |
|
| | 34 | | private void OnAnimationRemoved((string bodyshapeId, string emoteId) values, AnimationClip animationClip) |
| | 35 | | { |
| 0 | 36 | | if (bodyShapeId != values.bodyshapeId) |
| 0 | 37 | | return; |
| | 38 | |
|
| 0 | 39 | | animator.UnequipEmote(values.emoteId); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public void SetEquippedEmotes(string bodyShapeId, IEnumerable<WearableItem> emotes) |
| | 43 | | { |
| 4 | 44 | | this.bodyShapeId = bodyShapeId; |
| 14 | 45 | | foreach (string emoteId in this.emotes) |
| | 46 | | { |
| 3 | 47 | | dataStoreEmotes.emotesOnUse.DecreaseRefCount(emoteId); |
| | 48 | | } |
| 4 | 49 | | this.emotes.Clear(); |
| | 50 | |
|
| 32 | 51 | | foreach (WearableItem emote in emotes) |
| | 52 | | { |
| 12 | 53 | | this.emotes.Add(emote.id); |
| 12 | 54 | | dataStoreEmotes.emotesOnUse.IncreaseRefCount(emote.id); |
| | 55 | |
|
| | 56 | | //If the clip is not ready by the time we equip it |
| | 57 | | //we will receive it once its added to the collection in the DataStore |
| 12 | 58 | | if (dataStoreEmotes.animations.TryGetValue((this.bodyShapeId, emote.id), out AnimationClip clip)) |
| 2 | 59 | | animator.EquipEmote(emote.id, clip); |
| | 60 | | } |
| 4 | 61 | | } |
| | 62 | |
|
| | 63 | | public void Dispose() |
| | 64 | | { |
| 7 | 65 | | dataStoreEmotes.animations.OnAdded -= OnAnimationAdded; |
| 7 | 66 | | dataStoreEmotes.animations.OnRemoved -= OnAnimationRemoved; |
| 50 | 67 | | foreach (string emoteId in this.emotes) |
| | 68 | | { |
| 18 | 69 | | dataStoreEmotes.emotesOnUse.DecreaseRefCount(emoteId); |
| | 70 | | } |
| 7 | 71 | | emotes.Clear(); |
| 7 | 72 | | } |
| | 73 | | } |
| | 74 | | } |