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