| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Components; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using DCL.Helpers; |
| | 8 | | using UnityEngine; |
| | 9 | | using DCL.Models; |
| | 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 | |
|
| 10 | 20 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 21 | | } |
| | 22 | |
|
| 6 | 23 | | private Model cachedModel = new Model(); |
| | 24 | |
|
| 6 | 25 | | private HashSet<GameObject> avatarsInArea = new HashSet<GameObject>(); |
| | 26 | | private event Action<GameObject> OnAvatarEnter; |
| | 27 | | private event Action<GameObject> OnAvatarExit; |
| | 28 | | internal readonly Dictionary<string, AvatarModifier> modifiers; |
| | 29 | |
|
| 6 | 30 | | public AvatarModifierArea() |
| | 31 | | { |
| | 32 | | // Configure all available modifiers |
| 6 | 33 | | this.modifiers = new Dictionary<string, AvatarModifier>() |
| | 34 | | { |
| | 35 | | { "HIDE_AVATARS", new HideAvatarsModifier() }, |
| | 36 | | { "DISABLE_PASSPORTS", new DisablePassportModifier() } |
| | 37 | | }; |
| 6 | 38 | | model = new Model(); |
| 6 | 39 | | } |
| | 40 | |
|
| | 41 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 42 | | { |
| | 43 | |
|
| | 44 | | // Clean up |
| 10 | 45 | | RemoveAllModifiers(); |
| 10 | 46 | | OnAvatarEnter = null; |
| 10 | 47 | | OnAvatarExit = null; |
| | 48 | |
|
| 10 | 49 | | ApplyCurrentModel(); |
| | 50 | |
|
| 10 | 51 | | return null; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | private void OnDestroy() |
| | 55 | | { |
| 5 | 56 | | var toRemove = new HashSet<GameObject>(); |
| 5 | 57 | | if (avatarsInArea != null) |
| 5 | 58 | | toRemove.UnionWith(avatarsInArea); |
| | 59 | |
|
| 5 | 60 | | var currentInArea = DetectAllAvatarsInArea(); |
| 5 | 61 | | if (currentInArea != null) |
| 0 | 62 | | toRemove.UnionWith(currentInArea); |
| | 63 | |
|
| 5 | 64 | | RemoveAllModifiers(toRemove); |
| 5 | 65 | | } |
| | 66 | |
|
| | 67 | | private void Update() |
| | 68 | | { |
| 31 | 69 | | if (cachedModel?.area == null) |
| | 70 | | { |
| 0 | 71 | | return; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | // Find avatars currently on the area |
| 31 | 75 | | HashSet<GameObject> newAvatarsInArea = DetectAllAvatarsInArea(); |
| 31 | 76 | | if (AreSetEquals(avatarsInArea, newAvatarsInArea)) |
| 1 | 77 | | return; |
| | 78 | |
|
| 30 | 79 | | if (avatarsInArea == null) |
| 0 | 80 | | avatarsInArea = new HashSet<GameObject>(); |
| | 81 | |
|
| 30 | 82 | | if (newAvatarsInArea == null) |
| 26 | 83 | | newAvatarsInArea = new HashSet<GameObject>(); |
| | 84 | |
|
| | 85 | | // Call event for avatars that just entered the area |
| 68 | 86 | | foreach (GameObject avatarThatEntered in newAvatarsInArea.Except(avatarsInArea)) |
| | 87 | | { |
| 4 | 88 | | OnAvatarEnter?.Invoke(avatarThatEntered); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | // Call events for avatars that just exited the area |
| 64 | 92 | | foreach (GameObject avatarThatExited in avatarsInArea.Except(newAvatarsInArea)) |
| | 93 | | { |
| 2 | 94 | | OnAvatarExit?.Invoke(avatarThatExited); |
| | 95 | | } |
| | 96 | |
|
| 30 | 97 | | avatarsInArea = newAvatarsInArea; |
| 30 | 98 | | } |
| | 99 | |
|
| | 100 | | private bool AreSetEquals(HashSet<GameObject> set1, HashSet<GameObject> set2) |
| | 101 | | { |
| 31 | 102 | | if (set1 == null && set2 == null) |
| 0 | 103 | | return true; |
| | 104 | |
|
| 31 | 105 | | if (set1 == null || set2 == null) |
| 26 | 106 | | return false; |
| | 107 | |
|
| 5 | 108 | | return set1.SetEquals(set2); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | private HashSet<GameObject> DetectAllAvatarsInArea() |
| | 112 | | { |
| 46 | 113 | | if (entity?.gameObject == null) |
| | 114 | | { |
| 4 | 115 | | return null; |
| | 116 | | } |
| | 117 | |
|
| 42 | 118 | | Vector3 center = entity.gameObject.transform.position; |
| 42 | 119 | | Quaternion rotation = entity.gameObject.transform.rotation; |
| 42 | 120 | | return cachedModel.area?.DetectAvatars(center, rotation); |
| | 121 | | } |
| | 122 | |
|
| 20 | 123 | | private void RemoveAllModifiers() { RemoveAllModifiers(DetectAllAvatarsInArea()); } |
| | 124 | |
|
| | 125 | | private void RemoveAllModifiers(HashSet<GameObject> avatars) |
| | 126 | | { |
| 15 | 127 | | if (cachedModel?.area == null) |
| | 128 | | { |
| 5 | 129 | | return; |
| | 130 | | } |
| | 131 | |
|
| 10 | 132 | | if (avatars != null) |
| | 133 | | { |
| 14 | 134 | | foreach (GameObject avatar in avatars) |
| | 135 | | { |
| 2 | 136 | | OnAvatarExit?.Invoke(avatar); |
| | 137 | | } |
| | 138 | | } |
| 10 | 139 | | } |
| | 140 | |
|
| | 141 | | private void ApplyCurrentModel() |
| | 142 | | { |
| 10 | 143 | | cachedModel = (Model)this.model; |
| 10 | 144 | | if (cachedModel.modifiers != null) |
| | 145 | | { |
| | 146 | | // Add all listeners |
| 30 | 147 | | foreach (string modifierKey in cachedModel.modifiers) |
| | 148 | | { |
| 5 | 149 | | if (!modifiers.TryGetValue(modifierKey, out AvatarModifier modifier)) |
| | 150 | | continue; |
| | 151 | |
|
| 5 | 152 | | OnAvatarEnter += modifier.ApplyModifier; |
| 5 | 153 | | OnAvatarExit += modifier.RemoveModifier; |
| | 154 | | } |
| | 155 | | } |
| 10 | 156 | | } |
| | 157 | |
|
| 0 | 158 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_MODIFIER_AREA; } |
| | 159 | | } |