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