| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.ECSComponents.Utils; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | | using DCL.Models; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.ECSComponents |
| | 11 | | { |
| | 12 | | public class AvatarModifierAreaComponentHandler : IECSComponentHandler<PBAvatarModifierArea> |
| | 13 | | { |
| | 14 | | private event Action<GameObject> OnAvatarEnter; |
| | 15 | | private event Action<GameObject> OnAvatarExit; |
| | 16 | |
|
| 4 | 17 | | internal HashSet<GameObject> avatarsInArea = new HashSet<GameObject>(); |
| | 18 | | internal HashSet<Collider> excludedColliders; |
| | 19 | |
|
| | 20 | | internal PBAvatarModifierArea model; |
| | 21 | | private IDCLEntity entity; |
| | 22 | | private UnityEngine.Vector3 boxArea; |
| | 23 | |
|
| | 24 | | private readonly AvatarModifierFactory factory; |
| | 25 | | private readonly IUpdateEventHandler updateEventHandler; |
| | 26 | | private readonly DataStore_Player dataStore; |
| | 27 | |
|
| 4 | 28 | | public AvatarModifierAreaComponentHandler(IUpdateEventHandler updateEventHandler,DataStore_Player dataStore, Ava |
| | 29 | | { |
| 4 | 30 | | this.factory = factory; |
| 4 | 31 | | this.dataStore = dataStore; |
| 4 | 32 | | this.updateEventHandler = updateEventHandler; |
| | 33 | |
|
| 4 | 34 | | updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, Update); |
| 4 | 35 | | dataStore.otherPlayers.OnRemoved += OnOtherPlayersRemoved; |
| 4 | 36 | | } |
| | 37 | |
|
| 0 | 38 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 39 | |
|
| | 40 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 41 | | { |
| | 42 | | // We detect current affected avatars |
| 5 | 43 | | var toRemove = avatarsInArea; |
| 5 | 44 | | var currentInArea = DetectAllAvatarsInArea(); |
| 5 | 45 | | if (currentInArea != null) |
| 3 | 46 | | toRemove.UnionWith(currentInArea); |
| | 47 | |
|
| | 48 | | // Remove the modifiers on all avatars |
| 5 | 49 | | RemoveAllModifiers(toRemove); |
| 5 | 50 | | if(avatarsInArea != null) |
| 5 | 51 | | avatarsInArea.Clear(); |
| | 52 | |
|
| | 53 | | // We unsubscribe from events |
| 5 | 54 | | dataStore.ownPlayer.OnChange -= OwnPlayerOnOnChange; |
| 5 | 55 | | dataStore.otherPlayers.OnAdded -= OtherPlayersOnOnAdded; |
| 5 | 56 | | dataStore.otherPlayers.OnRemoved -= OnOtherPlayersRemoved; |
| | 57 | |
|
| 5 | 58 | | updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update); |
| 5 | 59 | | } |
| | 60 | |
|
| | 61 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAvatarModifierArea model) |
| | 62 | | { |
| 2 | 63 | | if (model.Equals(this.model)) |
| 0 | 64 | | return; |
| | 65 | |
|
| | 66 | | // Clean up |
| 2 | 67 | | RemoveAllModifiers(); |
| 2 | 68 | | OnAvatarEnter = null; |
| 2 | 69 | | OnAvatarExit = null; |
| | 70 | |
|
| 2 | 71 | | this.entity = entity; |
| 2 | 72 | | ApplyCurrentModel(model); |
| 2 | 73 | | } |
| | 74 | |
|
| | 75 | | private void Update() |
| | 76 | | { |
| 2 | 77 | | if (model == null) |
| 0 | 78 | | return; |
| | 79 | |
|
| | 80 | | // Find avatars currently on the area |
| 2 | 81 | | HashSet<GameObject> newAvatarsInArea = DetectAllAvatarsInArea(); |
| 2 | 82 | | if (AreSetEquals(avatarsInArea, newAvatarsInArea)) |
| 2 | 83 | | return; |
| | 84 | |
|
| | 85 | | // Call event for avatars that just entered the area |
| 0 | 86 | | foreach (GameObject avatarThatEntered in newAvatarsInArea.Except(avatarsInArea)) |
| | 87 | | { |
| 0 | 88 | | OnAvatarEnter?.Invoke(avatarThatEntered); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | // Call events for avatars that just exited the area |
| 0 | 92 | | foreach (GameObject avatarThatExited in avatarsInArea.Except(newAvatarsInArea)) |
| | 93 | | { |
| 0 | 94 | | OnAvatarExit?.Invoke(avatarThatExited); |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | avatarsInArea = newAvatarsInArea; |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | private bool AreSetEquals(HashSet<GameObject> set1, HashSet<GameObject> set2) |
| | 101 | | { |
| 2 | 102 | | if (set1 == null && set2 == null) |
| 0 | 103 | | return true; |
| | 104 | |
|
| 2 | 105 | | if (set1 == null || set2 == null) |
| 0 | 106 | | return false; |
| | 107 | |
|
| 2 | 108 | | return set1.SetEquals(set2); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | private HashSet<GameObject> DetectAllAvatarsInArea() |
| | 112 | | { |
| 9 | 113 | | if (entity?.gameObject == null) |
| 4 | 114 | | return null; |
| | 115 | |
|
| 5 | 116 | | UnityEngine.Vector3 center = entity.gameObject.transform.position; |
| 5 | 117 | | Quaternion rotation = entity.gameObject.transform.rotation; |
| 5 | 118 | | return ECSAvatarUtils.DetectAvatars(boxArea, center, rotation, excludedColliders); |
| | 119 | | } |
| | 120 | |
|
| | 121 | | private void RemoveAllModifiers() |
| | 122 | | { |
| 2 | 123 | | RemoveAllModifiers(DetectAllAvatarsInArea()); |
| 2 | 124 | | avatarsInArea.Clear(); |
| 2 | 125 | | } |
| | 126 | |
|
| | 127 | | private void RemoveAllModifiers(HashSet<GameObject> avatars) |
| | 128 | | { |
| 7 | 129 | | if (avatars == null) |
| 2 | 130 | | return; |
| | 131 | |
|
| 12 | 132 | | foreach (GameObject avatar in avatars) |
| | 133 | | { |
| 1 | 134 | | OnAvatarExit?.Invoke(avatar); |
| | 135 | | } |
| 5 | 136 | | } |
| | 137 | |
|
| | 138 | | private void ApplyCurrentModel(PBAvatarModifierArea model) |
| | 139 | | { |
| 2 | 140 | | dataStore.ownPlayer.OnChange -= OwnPlayerOnOnChange; |
| 2 | 141 | | dataStore.otherPlayers.OnAdded -= OtherPlayersOnOnAdded; |
| | 142 | |
|
| 2 | 143 | | if (model.Modifiers.Count > 0) |
| | 144 | | { |
| | 145 | | // We set the unity engine Vector3 here, so we don't allocate a Vector3 each time we use it |
| 2 | 146 | | boxArea = ProtoConvertUtils.PBVectorToUnityVector(model.Area); |
| | 147 | |
|
| | 148 | | // Add all listeners |
| 8 | 149 | | foreach (AvatarModifier modifierKey in model.Modifiers) |
| | 150 | | { |
| 2 | 151 | | var modifier = factory.GetOrCreateAvatarModifier(modifierKey); |
| | 152 | |
|
| 2 | 153 | | OnAvatarEnter += modifier.ApplyModifier; |
| 2 | 154 | | OnAvatarExit += modifier.RemoveModifier; |
| | 155 | | } |
| | 156 | |
|
| | 157 | | // Set excluded colliders |
| 2 | 158 | | excludedColliders = GetExcludedColliders(model); |
| | 159 | |
|
| 2 | 160 | | if (model.ExcludeIds.Count > 0) |
| | 161 | | { |
| 2 | 162 | | dataStore.ownPlayer.OnChange += OwnPlayerOnOnChange; |
| 2 | 163 | | dataStore.otherPlayers.OnAdded += OtherPlayersOnOnAdded; |
| | 164 | | } |
| | 165 | |
|
| 2 | 166 | | this.model = model; |
| | 167 | |
|
| | 168 | | // Force update due to after model update modifiers are removed and re-added |
| | 169 | | // leaving a frame with the avatar without the proper modifications |
| 2 | 170 | | Update(); |
| | 171 | | } |
| 2 | 172 | | } |
| | 173 | |
|
| | 174 | | internal HashSet<Collider> GetExcludedColliders(PBAvatarModifierArea model) |
| | 175 | | { |
| 3 | 176 | | if (model.ExcludeIds.Count == 0) |
| 0 | 177 | | return null; |
| | 178 | |
|
| 3 | 179 | | var ownPlayer = dataStore.ownPlayer.Get(); |
| 3 | 180 | | var otherPlayers = dataStore.otherPlayers; |
| | 181 | |
|
| 3 | 182 | | HashSet<Collider> result = new HashSet<Collider>(); |
| 14 | 183 | | for (int i = 0; i < model.ExcludeIds.Count; i++) |
| | 184 | | { |
| 4 | 185 | | if (ownPlayer != null && model.ExcludeIds[i] == ownPlayer.id) |
| 0 | 186 | | result.Add(ownPlayer.collider); |
| 4 | 187 | | else if (otherPlayers.TryGetValue(model.ExcludeIds[i], out Player player)) |
| 1 | 188 | | result.Add(player.collider); |
| | 189 | | } |
| 3 | 190 | | return result; |
| | 191 | | } |
| | 192 | |
|
| 0 | 193 | | private void OtherPlayersOnOnAdded(string id, Player player) { RefreshExclusionList(player); } |
| | 194 | |
|
| 0 | 195 | | private void OwnPlayerOnOnChange(Player current, Player previous) { RefreshExclusionList(current); } |
| | 196 | |
|
| 0 | 197 | | private void OnOtherPlayersRemoved(string id, Player player) { excludedColliders?.Remove(player.collider); } |
| | 198 | |
|
| | 199 | | private void RefreshExclusionList(Player player) |
| | 200 | | { |
| 0 | 201 | | if (model.ExcludeIds.Count == 0 || !model.ExcludeIds.Contains(player.id)) |
| 0 | 202 | | return; |
| | 203 | |
|
| 0 | 204 | | if (excludedColliders == null) |
| 0 | 205 | | excludedColliders = new HashSet<Collider>(); |
| | 206 | |
|
| 0 | 207 | | excludedColliders.Add(player.collider); |
| 0 | 208 | | } |
| | 209 | | } |
| | 210 | | } |