| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine.Assertions; |
| | 5 | |
|
| | 6 | | namespace DCL.ECSRuntime |
| | 7 | | { |
| | 8 | | public class ECSComponentsGroupWithout<T1> : IECSComponentsGroup, IECSReadOnlyComponentsGroup<T1> |
| | 9 | | { |
| | 10 | | private readonly IECSComponent component1; |
| | 11 | | private readonly IECSComponent excludedComponent; |
| 16 | 12 | | private readonly List<ECSComponentsGroupData<T1>> list = new List<ECSComponentsGroupData<T1>>(); |
| | 13 | |
|
| 9 | 14 | | IReadOnlyList<ECSComponentsGroupData<T1>> IECSReadOnlyComponentsGroup<T1>.group => list; |
| | 15 | |
|
| 16 | 16 | | public ECSComponentsGroupWithout(IECSComponent component1, IECSComponent excludedComponent) |
| | 17 | | { |
| 16 | 18 | | Assert.IsNotNull(component1, $"component must not be null"); |
| 16 | 19 | | Assert.IsNotNull(excludedComponent, $"excludedComponent must not be null"); |
| 16 | 20 | | this.component1 = component1; |
| 16 | 21 | | this.excludedComponent = excludedComponent; |
| 16 | 22 | | } |
| | 23 | |
|
| | 24 | | bool IECSComponentsGroup.MatchEntity(IParcelScene scene, IDCLEntity entity) |
| | 25 | | { |
| 14 | 26 | | return component1.HasComponent(scene, entity) && !excludedComponent.HasComponent(scene, entity); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | bool IECSComponentsGroup.ShouldAddOnComponentAdd(IECSComponent component) |
| | 30 | | { |
| 34 | 31 | | return component == this.component1; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | bool IECSComponentsGroup.ShouldRemoveOnComponentRemove(IECSComponent component) |
| | 35 | | { |
| 3 | 36 | | return component == this.component1; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | bool IECSComponentsGroup.ShouldAddOnComponentRemove(IECSComponent component) |
| | 40 | | { |
| 6 | 41 | | return component == excludedComponent; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | bool IECSComponentsGroup.ShouldRemoveOnComponentAdd(IECSComponent component) |
| | 45 | | { |
| 4 | 46 | | return component == excludedComponent; |
| | 47 | | } |
| | 48 | |
|
| | 49 | | void IECSComponentsGroup.Add(IParcelScene scene, IDCLEntity entity) |
| | 50 | | { |
| 9 | 51 | | ((ECSComponent<T1>)component1).TryGet(scene, entity.entityId, out var componentData); |
| | 52 | |
|
| 9 | 53 | | ECSComponentsGroupData<T1> data = new ECSComponentsGroupData<T1> |
| | 54 | | ( |
| | 55 | | scene: scene, |
| | 56 | | entity: entity, |
| | 57 | | componentData: componentData |
| | 58 | | ); |
| | 59 | |
|
| 9 | 60 | | list.Add(data); |
| 9 | 61 | | } |
| | 62 | |
|
| | 63 | | bool IECSComponentsGroup.Remove(IDCLEntity entity) |
| | 64 | | { |
| 14 | 65 | | for (int i = 0; i < list.Count; i++) |
| | 66 | | { |
| 7 | 67 | | ECSComponentsGroupData<T1> data = list[i]; |
| | 68 | |
|
| 7 | 69 | | if (data.entity != entity) |
| | 70 | | continue; |
| | 71 | |
|
| 7 | 72 | | list.RemoveAt(i); |
| 7 | 73 | | return true; |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | return false; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | void IECSComponentsGroup.Update(IParcelScene scene, IDCLEntity entity, IECSComponent component) |
| | 80 | | { |
| 1 | 81 | | long entityId = entity.entityId; |
| 1 | 82 | | if (!Utils.TryGetDataIndex(list, entity, out int index)) |
| 0 | 83 | | return; |
| | 84 | |
|
| 1 | 85 | | if (Utils.TryGetComponentData<T1>(scene, entityId, component1, component, out var d1)) |
| 1 | 86 | | list[index] = new ECSComponentsGroupData<T1>(scene, entity, d1); |
| 1 | 87 | | } |
| | 88 | | } |
| | 89 | | } |