| | 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) |
| 0 | 46 | | toRemove.UnionWith(currentInArea); |
| | 47 | |
|
| | 48 | | // Remove the modifiers on all avatars |
| 5 | 49 | | RemoveAllModifiers(toRemove); |
| 5 | 50 | | if(avatarsInArea != null) |
| 4 | 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.LateUpdate, Update); |
| 5 | 59 | | } |
| | 60 | |
|
| | 61 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAvatarModifierArea model) |
| | 62 | | { |
| | 63 | | // Clean up |
| 2 | 64 | | RemoveAllModifiers(); |
| 2 | 65 | | OnAvatarEnter = null; |
| 2 | 66 | | OnAvatarExit = null; |
| | 67 | |
|
| 2 | 68 | | this.entity = entity; |
| 2 | 69 | | ApplyCurrentModel(model); |
| 2 | 70 | | } |
| | 71 | |
|
| | 72 | | private void Update() |
| | 73 | | { |
| 2 | 74 | | if (model == null) |
| 0 | 75 | | return; |
| | 76 | |
|
| | 77 | | // Find avatars currently on the area |
| 2 | 78 | | HashSet<GameObject> newAvatarsInArea = DetectAllAvatarsInArea(); |
| 2 | 79 | | if (AreSetEquals(avatarsInArea, newAvatarsInArea)) |
| 2 | 80 | | return; |
| | 81 | |
|
| | 82 | | // Call event for avatars that just entered the area |
| 0 | 83 | | foreach (GameObject avatarThatEntered in newAvatarsInArea.Except(avatarsInArea)) |
| | 84 | | { |
| 0 | 85 | | OnAvatarEnter?.Invoke(avatarThatEntered); |
| | 86 | | } |
| | 87 | |
|
| | 88 | | // Call events for avatars that just exited the area |
| 0 | 89 | | foreach (GameObject avatarThatExited in avatarsInArea.Except(newAvatarsInArea)) |
| | 90 | | { |
| 0 | 91 | | OnAvatarExit?.Invoke(avatarThatExited); |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | avatarsInArea = newAvatarsInArea; |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | private bool AreSetEquals(HashSet<GameObject> set1, HashSet<GameObject> set2) |
| | 98 | | { |
| 2 | 99 | | if (set1 == null && set2 == null) |
| 2 | 100 | | return true; |
| | 101 | |
|
| 0 | 102 | | if (set1 == null || set2 == null) |
| 0 | 103 | | return false; |
| | 104 | |
|
| 0 | 105 | | return set1.SetEquals(set2); |
| | 106 | | } |
| | 107 | |
|
| | 108 | | private HashSet<GameObject> DetectAllAvatarsInArea() |
| | 109 | | { |
| 9 | 110 | | if (entity?.gameObject == null) |
| 4 | 111 | | return null; |
| | 112 | |
|
| 5 | 113 | | UnityEngine.Vector3 center = entity.gameObject.transform.position; |
| 5 | 114 | | Quaternion rotation = entity.gameObject.transform.rotation; |
| 5 | 115 | | return ECSAvatarUtils.DetectAvatars(boxArea, center, rotation, excludedColliders); |
| | 116 | | } |
| | 117 | |
|
| | 118 | | private void RemoveAllModifiers() |
| | 119 | | { |
| 2 | 120 | | RemoveAllModifiers(DetectAllAvatarsInArea()); |
| 2 | 121 | | avatarsInArea = null; |
| 2 | 122 | | } |
| | 123 | |
|
| | 124 | | private void RemoveAllModifiers(HashSet<GameObject> avatars) |
| | 125 | | { |
| 7 | 126 | | if (avatars == null) |
| 3 | 127 | | return; |
| | 128 | |
|
| 10 | 129 | | foreach (GameObject avatar in avatars) |
| | 130 | | { |
| 1 | 131 | | OnAvatarExit?.Invoke(avatar); |
| | 132 | | } |
| 4 | 133 | | } |
| | 134 | |
|
| | 135 | | private void ApplyCurrentModel(PBAvatarModifierArea model) |
| | 136 | | { |
| 2 | 137 | | dataStore.ownPlayer.OnChange -= OwnPlayerOnOnChange; |
| 2 | 138 | | dataStore.otherPlayers.OnAdded -= OtherPlayersOnOnAdded; |
| | 139 | |
|
| 2 | 140 | | if (model.Modifiers.Count > 0) |
| | 141 | | { |
| | 142 | | // We set the unity engine Vector3 here, so we don't allocate a Vector3 each time we use it |
| 2 | 143 | | boxArea = ProtoConvertUtils.PBVectorToUnityVector(model.Area); |
| | 144 | |
|
| | 145 | | // Add all listeners |
| 8 | 146 | | foreach (AvatarModifier modifierKey in model.Modifiers) |
| | 147 | | { |
| 2 | 148 | | var modifier = factory.GetOrCreateAvatarModifier(modifierKey); |
| | 149 | |
|
| 2 | 150 | | OnAvatarEnter += modifier.ApplyModifier; |
| 2 | 151 | | OnAvatarExit += modifier.RemoveModifier; |
| | 152 | | } |
| | 153 | |
|
| | 154 | | // Set excluded colliders |
| 2 | 155 | | excludedColliders = GetExcludedColliders(model); |
| | 156 | |
|
| 2 | 157 | | if (model.ExcludeIds.Count > 0) |
| | 158 | | { |
| 2 | 159 | | dataStore.ownPlayer.OnChange += OwnPlayerOnOnChange; |
| 2 | 160 | | dataStore.otherPlayers.OnAdded += OtherPlayersOnOnAdded; |
| | 161 | | } |
| | 162 | |
|
| 2 | 163 | | this.model = model; |
| | 164 | |
|
| | 165 | | // Force update due to after model update modifiers are removed and re-added |
| | 166 | | // leaving a frame with the avatar without the proper modifications |
| 2 | 167 | | Update(); |
| | 168 | | } |
| 2 | 169 | | } |
| | 170 | |
|
| | 171 | | internal HashSet<Collider> GetExcludedColliders(PBAvatarModifierArea model) |
| | 172 | | { |
| 3 | 173 | | if (model.ExcludeIds.Count == 0) |
| 0 | 174 | | return null; |
| | 175 | |
|
| 3 | 176 | | var ownPlayer = dataStore.ownPlayer.Get(); |
| 3 | 177 | | var otherPlayers = dataStore.otherPlayers; |
| | 178 | |
|
| 3 | 179 | | HashSet<Collider> result = new HashSet<Collider>(); |
| 14 | 180 | | for (int i = 0; i < model.ExcludeIds.Count; i++) |
| | 181 | | { |
| 4 | 182 | | if (ownPlayer != null && model.ExcludeIds[i] == ownPlayer.id) |
| 0 | 183 | | result.Add(ownPlayer.collider); |
| 4 | 184 | | else if (otherPlayers.TryGetValue(model.ExcludeIds[i], out Player player)) |
| 1 | 185 | | result.Add(player.collider); |
| | 186 | | } |
| 3 | 187 | | return result; |
| | 188 | | } |
| | 189 | |
|
| 0 | 190 | | private void OtherPlayersOnOnAdded(string id, Player player) { RefreshExclusionList(player); } |
| | 191 | |
|
| 0 | 192 | | private void OwnPlayerOnOnChange(Player current, Player previous) { RefreshExclusionList(current); } |
| | 193 | |
|
| 0 | 194 | | private void OnOtherPlayersRemoved(string id, Player player) { excludedColliders?.Remove(player.collider); } |
| | 195 | |
|
| | 196 | | private void RefreshExclusionList(Player player) |
| | 197 | | { |
| 0 | 198 | | if (model.ExcludeIds.Count == 0 || !model.ExcludeIds.Contains(player.id)) |
| 0 | 199 | | return; |
| | 200 | |
|
| 0 | 201 | | if (excludedColliders == null) |
| 0 | 202 | | excludedColliders = new HashSet<Collider>(); |
| | 203 | |
|
| 0 | 204 | | excludedColliders.Add(player.collider); |
| 0 | 205 | | } |
| | 206 | | } |
| | 207 | | } |