| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Emotes; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace AvatarSystem |
| | 10 | | { |
| | 11 | | public class AvatarEmotesController : IAvatarEmotesController |
| | 12 | | { |
| | 13 | | private const string IN_HIDE_AREA = "IN_HIDE_AREA"; |
| | 14 | |
|
| | 15 | | public event Action<string, IEmoteReference> OnEmoteEquipped; |
| | 16 | | public event Action<string> OnEmoteUnequipped; |
| | 17 | |
|
| 518 | 18 | | private string bodyShapeId = ""; |
| | 19 | | private readonly IAnimator animator; |
| | 20 | | private readonly IEmotesService emotesService; |
| 518 | 21 | | private readonly Dictionary<EmoteBodyId, IEmoteReference> equippedEmotes = new (); |
| 518 | 22 | | private readonly CancellationTokenSource cts = new (); |
| | 23 | | private readonly HashSet<string> visibilityConstraints; |
| | 24 | |
|
| 518 | 25 | | public AvatarEmotesController(IAnimator animator, IEmotesService emotesService) |
| | 26 | | { |
| 518 | 27 | | this.animator = animator; |
| 518 | 28 | | this.emotesService = emotesService; |
| 518 | 29 | | visibilityConstraints = new HashSet<string>(); |
| 518 | 30 | | } |
| | 31 | |
|
| | 32 | | public bool TryGetEquippedEmote(string bodyShape, string emoteId, out IEmoteReference emoteReference) => |
| 0 | 33 | | equippedEmotes.TryGetValue(new EmoteBodyId(bodyShape, emoteId), out emoteReference); |
| | 34 | |
|
| | 35 | | public void AddVisibilityConstraint(string key) |
| | 36 | | { |
| 0 | 37 | | visibilityConstraints.Add(key); |
| | 38 | |
|
| 0 | 39 | | if (!CanPlayEmote()) |
| 0 | 40 | | StopEmote(true); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public void RemoveVisibilityConstraint(string key) |
| | 44 | | { |
| 0 | 45 | | visibilityConstraints.Remove(key); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Prepare(string bodyShapeId, GameObject container) |
| | 49 | | { |
| 0 | 50 | | this.bodyShapeId = bodyShapeId; |
| 0 | 51 | | animator.Prepare(bodyShapeId, container); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | // ReSharper disable once PossibleMultipleEnumeration (its intended) |
| | 55 | | public void LoadEmotes(string bodyShapeId, IEnumerable<WearableItem> newEmotes) |
| | 56 | | { |
| 0 | 57 | | foreach (WearableItem emote in newEmotes) |
| 0 | 58 | | LoadEmote(bodyShapeId, emote); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void LoadEmote(string bodyShapeId, WearableItem emote) |
| | 62 | | { |
| 0 | 63 | | var emoteKey = new EmoteBodyId(bodyShapeId, emote.id); |
| 0 | 64 | | if (equippedEmotes.ContainsKey(emoteKey)) return; |
| 0 | 65 | | equippedEmotes.Add(emoteKey, null); |
| 0 | 66 | | AsyncEmoteLoad(bodyShapeId, emote.id).Forget(); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | private async UniTask AsyncEmoteLoad(string bodyShapeId, string emoteId) |
| | 70 | | { |
| 0 | 71 | | var emoteKey = new EmoteBodyId(bodyShapeId, emoteId); |
| | 72 | |
|
| | 73 | | try |
| | 74 | | { |
| 0 | 75 | | IEmoteReference emoteReference = await emotesService.RequestEmote(emoteKey, cts.Token); |
| 0 | 76 | | if (emoteReference == null) return; |
| 0 | 77 | | animator.EquipEmote(emoteId, emoteReference.GetData()); |
| 0 | 78 | | equippedEmotes[emoteKey] = emoteReference; |
| 0 | 79 | | OnEmoteEquipped?.Invoke(emoteId, emoteReference); |
| 0 | 80 | | } |
| 0 | 81 | | catch (OperationCanceledException) { } |
| 0 | 82 | | catch (Exception e) { Debug.LogException(e); } |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public void PlayEmote(string emoteId, long timestamps, bool spatial = true, bool occlude = true, bool forcePlay |
| | 86 | | { |
| 1 | 87 | | bool isPlayingEmote = !string.IsNullOrEmpty(animator.GetCurrentEmoteId()); |
| 1 | 88 | | bool emoteIsValid = !string.IsNullOrEmpty(emoteId); |
| | 89 | |
|
| 1 | 90 | | if (isPlayingEmote && !emoteIsValid) |
| 0 | 91 | | animator.StopEmote(false); |
| | 92 | |
|
| 1 | 93 | | if (!emoteIsValid) return; |
| 1 | 94 | | if (!CanPlayEmote()) return; |
| | 95 | |
|
| 1 | 96 | | var emoteKey = new EmoteBodyId(bodyShapeId, emoteId); |
| 2 | 97 | | if (!equippedEmotes.ContainsKey(emoteKey)) return; |
| | 98 | |
|
| 0 | 99 | | animator.PlayEmote(emoteId, timestamps, spatial, occlude, forcePlay); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | private bool CanPlayEmote() => |
| 1 | 103 | | !visibilityConstraints.Contains(IN_HIDE_AREA); |
| | 104 | |
|
| | 105 | | public void StopEmote(bool immediate) |
| | 106 | | { |
| 0 | 107 | | animator.StopEmote(immediate); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | public void EquipEmote(string emoteId, IEmoteReference emoteReference) |
| | 111 | | { |
| 0 | 112 | | if (emoteReference == null) return; |
| 0 | 113 | | var emoteKey = new EmoteBodyId(bodyShapeId, emoteId); |
| | 114 | |
|
| 0 | 115 | | if (equippedEmotes.ContainsKey(emoteKey)) |
| | 116 | | { |
| | 117 | | // we avoid dangling references in case an emote was loaded twice |
| 0 | 118 | | emoteReference.Dispose(); |
| 0 | 119 | | return; |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | equippedEmotes.Add(emoteKey, emoteReference); |
| 0 | 123 | | animator.EquipEmote(emoteId, emoteReference.GetData()); |
| 0 | 124 | | OnEmoteEquipped?.Invoke(emoteId, emoteReference); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | public void UnEquipEmote(string emoteId) |
| | 128 | | { |
| 0 | 129 | | var emoteKey = new EmoteBodyId(bodyShapeId, emoteId); |
| | 130 | |
|
| 0 | 131 | | if (equippedEmotes.ContainsKey(emoteKey)) |
| | 132 | | { |
| 0 | 133 | | animator.UnequipEmote(emoteId); |
| 0 | 134 | | equippedEmotes[emoteKey].Dispose(); |
| 0 | 135 | | equippedEmotes.Remove(emoteKey); |
| | 136 | | } |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | public void Dispose() |
| | 140 | | { |
| 0 | 141 | | cts.SafeCancelAndDispose(); |
| | 142 | |
|
| 0 | 143 | | foreach (var kvp in equippedEmotes) |
| 0 | 144 | | kvp.Value.Dispose(); |
| | 145 | |
|
| 0 | 146 | | equippedEmotes.Clear(); |
| 0 | 147 | | } |
| | 148 | | } |
| | 149 | | } |