| | 1 | | using DCL; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.ECSComponents.Utils; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace ECSSystems.AvatarModifierAreaSystem |
| | 9 | | { |
| | 10 | | public class ECSAvatarModifierAreaSystem |
| | 11 | | { |
| | 12 | | private IInternalECSComponent<InternalAvatarModifierArea> internalAvatarModifierArea; |
| | 13 | | private readonly DataStore_Player dataStore; |
| 4 | 14 | | private readonly HashSet<Collider> excludedColliders = new HashSet<Collider>(); |
| | 15 | |
|
| 4 | 16 | | public ECSAvatarModifierAreaSystem( |
| | 17 | | IInternalECSComponent<InternalAvatarModifierArea> internalAvatarModifierArea, |
| | 18 | | DataStore_Player dataStore) |
| | 19 | | { |
| 4 | 20 | | this.internalAvatarModifierArea = internalAvatarModifierArea; |
| 4 | 21 | | this.dataStore = dataStore; |
| 4 | 22 | | } |
| | 23 | |
|
| | 24 | | public void Update() |
| | 25 | | { |
| 8 | 26 | | var componentGroup = internalAvatarModifierArea.GetForAll(); |
| | 27 | |
|
| 8 | 28 | | int entitiesCount = componentGroup.Count; |
| 32 | 29 | | for (int i = 0; i < entitiesCount; i++) |
| | 30 | | { |
| 8 | 31 | | var scene = componentGroup[i].key1; |
| 8 | 32 | | var entityId = componentGroup[i].key2; |
| 8 | 33 | | var model = componentGroup[i].value.model; |
| 8 | 34 | | Transform entityTransform = scene.entities[entityId].gameObject.transform; |
| | 35 | |
|
| 8 | 36 | | UpdateExcludedCollidersCollection(model.excludedIds); |
| | 37 | |
|
| 8 | 38 | | HashSet<GameObject> currentAvatarsInArea = ECSAvatarUtils.DetectAvatars(model.area, entityTransform.posi |
| | 39 | | entityTransform.rotation, excludedColliders); |
| | 40 | |
|
| 8 | 41 | | if (model.removed) |
| | 42 | | { |
| 0 | 43 | | foreach (GameObject avatarGO in currentAvatarsInArea) |
| | 44 | | { |
| 0 | 45 | | model.OnAvatarExit?.Invoke(avatarGO); |
| | 46 | | } |
| | 47 | |
|
| | 48 | | continue; |
| | 49 | | } |
| | 50 | |
|
| 8 | 51 | | if (AreSetsEqual(model.avatarsInArea, currentAvatarsInArea)) |
| | 52 | | continue; |
| | 53 | |
|
| | 54 | | // Apply modifier for avatars that just entered the area |
| 6 | 55 | | if (currentAvatarsInArea.Count > 0) |
| | 56 | | { |
| 18 | 57 | | foreach (GameObject avatarThatEntered in currentAvatarsInArea.Except(model.avatarsInArea)) |
| | 58 | | { |
| 5 | 59 | | model.OnAvatarEnter?.Invoke(avatarThatEntered); |
| | 60 | | } |
| | 61 | | } |
| | 62 | |
|
| | 63 | | // Reset modifier for avatars that just exited the area |
| 6 | 64 | | if (model.avatarsInArea?.Count > 0) |
| | 65 | | { |
| 8 | 66 | | foreach (GameObject avatarThatExited in model.avatarsInArea.Except(currentAvatarsInArea)) |
| | 67 | | { |
| 2 | 68 | | model.OnAvatarExit?.Invoke(avatarThatExited); |
| | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| 6 | 72 | | model.avatarsInArea = currentAvatarsInArea; |
| 6 | 73 | | internalAvatarModifierArea.PutFor(scene, entityId, model); |
| | 74 | | } |
| 8 | 75 | | } |
| | 76 | |
|
| | 77 | | private bool AreSetsEqual(HashSet<GameObject> set1, HashSet<GameObject> set2) |
| | 78 | | { |
| 8 | 79 | | if (set1 == null && set2 == null) |
| 0 | 80 | | return true; |
| | 81 | |
|
| 8 | 82 | | if (set1 == null || set2 == null) |
| 0 | 83 | | return false; |
| | 84 | |
|
| 8 | 85 | | if (set1.Count != set2.Count) |
| 6 | 86 | | return false; |
| | 87 | |
|
| 2 | 88 | | return set1.SetEquals(set2); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | private void UpdateExcludedCollidersCollection(HashSet<string> excludedIds) |
| | 92 | | { |
| 8 | 93 | | excludedColliders.Clear(); |
| | 94 | |
|
| 15 | 95 | | if (excludedIds == null || excludedIds.Count == 0) return; |
| | 96 | |
|
| 1 | 97 | | var ownPlayer = dataStore.ownPlayer.Get(); |
| 4 | 98 | | foreach (string excludedId in excludedIds) |
| | 99 | | { |
| 1 | 100 | | if (dataStore.otherPlayers.TryGetValue(excludedId, out Player player)) |
| 1 | 101 | | excludedColliders.Add(player.collider); |
| 0 | 102 | | else if (ownPlayer != null && excludedId == ownPlayer.id) |
| 0 | 103 | | excludedColliders.Add(ownPlayer.collider); |
| | 104 | | } |
| 1 | 105 | | } |
| | 106 | | } |
| | 107 | | } |