| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL; |
| | 6 | | using DCL.Components; |
| | 7 | | using DCL.Helpers; |
| | 8 | | using DCL.Models; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | public class AvatarModifierArea : BaseComponent |
| | 12 | | { |
| | 13 | | [Serializable] |
| | 14 | | public class Model : BaseModel |
| | 15 | | { |
| | 16 | | // TODO: Change to TriggerArea and handle deserialization with subclasses |
| | 17 | | public BoxTriggerArea area; |
| | 18 | | public string[] modifiers; |
| | 19 | | public string[] excludeIds; |
| | 20 | |
|
| | 21 | | public override BaseModel GetDataFromJSON(string json) |
| | 22 | | { |
| 20 | 23 | | return Utils.SafeFromJson<Model>(json); |
| | 24 | | } |
| | 25 | | } |
| | 26 | |
|
| 10 | 27 | | private Model cachedModel = new Model(); |
| | 28 | |
|
| 10 | 29 | | private HashSet<GameObject> avatarsInArea = new HashSet<GameObject>(); |
| | 30 | | private event Action<GameObject> OnAvatarEnter; |
| | 31 | | private event Action<GameObject> OnAvatarExit; |
| | 32 | |
|
| | 33 | | internal readonly Dictionary<string, IAvatarModifier> modifiers; |
| | 34 | |
|
| | 35 | | private HashSet<Collider> excludedColliders; |
| 5 | 36 | | public override string componentName => "avatarModifierArea"; |
| | 37 | |
|
| 10 | 38 | | public AvatarModifierArea() |
| | 39 | | { |
| | 40 | | // Configure all available modifiers |
| 10 | 41 | | this.modifiers = new Dictionary<string, IAvatarModifier>() |
| | 42 | | { |
| | 43 | | { "HIDE_AVATARS", new HideAvatarsModifier() }, |
| | 44 | | { "DISABLE_PASSPORTS", new HidePassportModifier() } |
| | 45 | | }; |
| 10 | 46 | | model = new Model(); |
| 10 | 47 | | } |
| | 48 | |
|
| | 49 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 50 | | { |
| | 51 | |
|
| | 52 | | // Clean up |
| 20 | 53 | | RemoveAllModifiers(); |
| 20 | 54 | | OnAvatarEnter = null; |
| 20 | 55 | | OnAvatarExit = null; |
| | 56 | |
|
| 20 | 57 | | ApplyCurrentModel(); |
| | 58 | |
|
| 20 | 59 | | return null; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | private void Awake() |
| | 63 | | { |
| 8 | 64 | | DataStore.i.player.otherPlayers.OnRemoved += OnOtherPlayersRemoved; |
| 8 | 65 | | } |
| | 66 | |
|
| | 67 | | private void OnDestroy() |
| | 68 | | { |
| 8 | 69 | | var toRemove = new HashSet<GameObject>(); |
| 8 | 70 | | if (avatarsInArea != null) |
| 6 | 71 | | toRemove.UnionWith(avatarsInArea); |
| | 72 | |
|
| 8 | 73 | | var currentInArea = DetectAllAvatarsInArea(); |
| 8 | 74 | | if (currentInArea != null) |
| 0 | 75 | | toRemove.UnionWith(currentInArea); |
| | 76 | |
|
| 8 | 77 | | RemoveAllModifiers(toRemove); |
| | 78 | |
|
| 8 | 79 | | DataStore.i.player.ownPlayer.OnChange -= OwnPlayerOnOnChange; |
| 8 | 80 | | DataStore.i.player.otherPlayers.OnAdded -= OtherPlayersOnOnAdded; |
| 8 | 81 | | DataStore.i.player.otherPlayers.OnRemoved -= OnOtherPlayersRemoved; |
| 8 | 82 | | } |
| | 83 | |
|
| | 84 | | private void Update() |
| | 85 | | { |
| 78 | 86 | | if (cachedModel?.area == null) |
| | 87 | | { |
| 0 | 88 | | return; |
| | 89 | | } |
| | 90 | |
|
| | 91 | | // Find avatars currently on the area |
| 78 | 92 | | HashSet<GameObject> newAvatarsInArea = DetectAllAvatarsInArea(); |
| 78 | 93 | | if (AreSetEquals(avatarsInArea, newAvatarsInArea)) |
| 60 | 94 | | return; |
| | 95 | |
|
| 18 | 96 | | if (avatarsInArea == null) |
| 10 | 97 | | avatarsInArea = new HashSet<GameObject>(); |
| | 98 | |
|
| 18 | 99 | | if (newAvatarsInArea == null) |
| 8 | 100 | | newAvatarsInArea = new HashSet<GameObject>(); |
| | 101 | |
|
| | 102 | | // Call event for avatars that just entered the area |
| 68 | 103 | | foreach (GameObject avatarThatEntered in newAvatarsInArea.Except(avatarsInArea)) |
| | 104 | | { |
| 16 | 105 | | OnAvatarEnter?.Invoke(avatarThatEntered); |
| | 106 | | } |
| | 107 | |
|
| | 108 | | // Call events for avatars that just exited the area |
| 44 | 109 | | foreach (GameObject avatarThatExited in avatarsInArea.Except(newAvatarsInArea)) |
| | 110 | | { |
| 4 | 111 | | OnAvatarExit?.Invoke(avatarThatExited); |
| | 112 | | } |
| | 113 | |
|
| 18 | 114 | | avatarsInArea = newAvatarsInArea; |
| 18 | 115 | | } |
| | 116 | |
|
| | 117 | | private bool AreSetEquals(HashSet<GameObject> set1, HashSet<GameObject> set2) |
| | 118 | | { |
| 78 | 119 | | if (set1 == null && set2 == null) |
| 46 | 120 | | return true; |
| | 121 | |
|
| 32 | 122 | | if (set1 == null || set2 == null) |
| 18 | 123 | | return false; |
| | 124 | |
|
| 14 | 125 | | return set1.SetEquals(set2); |
| | 126 | | } |
| | 127 | |
|
| | 128 | | private HashSet<GameObject> DetectAllAvatarsInArea() |
| | 129 | | { |
| 106 | 130 | | if (entity?.gameObject == null) |
| | 131 | | { |
| 5 | 132 | | return null; |
| | 133 | | } |
| | 134 | |
|
| 101 | 135 | | Vector3 center = entity.gameObject.transform.position; |
| 101 | 136 | | Quaternion rotation = entity.gameObject.transform.rotation; |
| 101 | 137 | | return cachedModel.area?.DetectAvatars(center, rotation, excludedColliders); |
| | 138 | | } |
| | 139 | |
|
| | 140 | | private void RemoveAllModifiers() |
| | 141 | | { |
| 20 | 142 | | RemoveAllModifiers(DetectAllAvatarsInArea()); |
| 20 | 143 | | avatarsInArea = null; |
| 20 | 144 | | } |
| | 145 | |
|
| | 146 | | private void RemoveAllModifiers(HashSet<GameObject> avatars) |
| | 147 | | { |
| 28 | 148 | | if (cachedModel?.area == null) |
| | 149 | | { |
| 8 | 150 | | return; |
| | 151 | | } |
| | 152 | |
|
| 20 | 153 | | if (avatars != null) |
| | 154 | | { |
| 56 | 155 | | foreach (GameObject avatar in avatars) |
| | 156 | | { |
| 15 | 157 | | OnAvatarExit?.Invoke(avatar); |
| | 158 | | } |
| | 159 | | } |
| 20 | 160 | | } |
| | 161 | |
|
| | 162 | | private void ApplyCurrentModel() |
| | 163 | | { |
| 20 | 164 | | DataStore.i.player.ownPlayer.OnChange -= OwnPlayerOnOnChange; |
| 20 | 165 | | DataStore.i.player.otherPlayers.OnAdded -= OtherPlayersOnOnAdded; |
| | 166 | |
|
| 20 | 167 | | cachedModel = (Model)this.model; |
| 20 | 168 | | if (cachedModel.modifiers != null) |
| | 169 | | { |
| | 170 | | // Add all listeners |
| 62 | 171 | | foreach (string modifierKey in cachedModel.modifiers) |
| | 172 | | { |
| 12 | 173 | | if (!modifiers.TryGetValue(modifierKey, out IAvatarModifier modifier)) |
| | 174 | | continue; |
| | 175 | |
|
| 12 | 176 | | OnAvatarEnter += modifier.ApplyModifier; |
| 12 | 177 | | OnAvatarExit += modifier.RemoveModifier; |
| | 178 | | } |
| | 179 | |
|
| | 180 | | // Set excluded colliders |
| 19 | 181 | | excludedColliders = GetExcludedColliders(cachedModel); |
| | 182 | |
|
| 19 | 183 | | if (cachedModel.excludeIds != null && cachedModel.excludeIds.Length > 0) |
| | 184 | | { |
| 3 | 185 | | DataStore.i.player.ownPlayer.OnChange += OwnPlayerOnOnChange; |
| 3 | 186 | | DataStore.i.player.otherPlayers.OnAdded += OtherPlayersOnOnAdded; |
| | 187 | | } |
| | 188 | |
|
| | 189 | | // Force update due to after model update modifiers are removed and re-added |
| | 190 | | // leaving a frame with the avatar without the proper modifications |
| 19 | 191 | | Update(); |
| | 192 | | } |
| 20 | 193 | | } |
| | 194 | |
|
| | 195 | | public override int GetClassId() |
| | 196 | | { |
| 1 | 197 | | return (int)CLASS_ID_COMPONENT.AVATAR_MODIFIER_AREA; |
| | 198 | | } |
| | 199 | |
|
| | 200 | | private static HashSet<Collider> GetExcludedColliders(in Model componentModel) |
| | 201 | | { |
| 19 | 202 | | string[] excludeIds = componentModel?.excludeIds; |
| 19 | 203 | | if (excludeIds == null || excludeIds.Length == 0) |
| | 204 | | { |
| 16 | 205 | | return null; |
| | 206 | | } |
| | 207 | |
|
| 3 | 208 | | var ownPlayer = DataStore.i.player.ownPlayer.Get(); |
| 3 | 209 | | var otherPlayers = DataStore.i.player.otherPlayers; |
| | 210 | |
|
| 3 | 211 | | HashSet<Collider> result = new HashSet<Collider>(); |
| 14 | 212 | | for (int i = 0; i < excludeIds.Length; i++) |
| | 213 | | { |
| 4 | 214 | | if (ownPlayer != null && excludeIds[i] == ownPlayer.id) |
| | 215 | | { |
| 1 | 216 | | result.Add(ownPlayer.collider); |
| 1 | 217 | | } |
| 3 | 218 | | else if (otherPlayers.TryGetValue(excludeIds[i], out Player player)) |
| | 219 | | { |
| 2 | 220 | | result.Add(player.collider); |
| | 221 | | } |
| | 222 | | } |
| 3 | 223 | | return result; |
| | 224 | | } |
| | 225 | |
|
| | 226 | | private void OtherPlayersOnOnAdded(string id, Player player) |
| | 227 | | { |
| 1 | 228 | | RefreshExclusionList(player); |
| 1 | 229 | | } |
| | 230 | |
|
| | 231 | | private void OwnPlayerOnOnChange(Player current, Player previous) |
| | 232 | | { |
| 0 | 233 | | RefreshExclusionList(current); |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | private void OnOtherPlayersRemoved(string id, Player player) |
| | 237 | | { |
| 3 | 238 | | excludedColliders?.Remove(player.collider); |
| 0 | 239 | | } |
| | 240 | |
|
| | 241 | | private void RefreshExclusionList(Player player) |
| | 242 | | { |
| 1 | 243 | | string[] excludeIds = cachedModel?.excludeIds; |
| 1 | 244 | | if (excludeIds == null || excludeIds.Length == 0) |
| | 245 | | { |
| 0 | 246 | | return; |
| | 247 | | } |
| | 248 | |
|
| 1 | 249 | | if (!excludeIds.Contains(player.id)) |
| | 250 | | { |
| 0 | 251 | | return; |
| | 252 | | } |
| | 253 | |
|
| 1 | 254 | | if (excludedColliders == null) |
| | 255 | | { |
| 0 | 256 | | excludedColliders = new HashSet<Collider>(); |
| | 257 | | } |
| 1 | 258 | | excludedColliders.Add(player.collider); |
| 1 | 259 | | } |
| | 260 | | } |