| | 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 ECSComponentsGroup<T1, T2, T3> : IECSComponentsGroup, IECSReadOnlyComponentsGroup<T1, T2, T3> |
| | 9 | | { |
| | 10 | | private readonly IECSComponent component1; |
| | 11 | | private readonly IECSComponent component2; |
| | 12 | | private readonly IECSComponent component3; |
| 13 | 13 | | private readonly List<ECSComponentsGroupData<T1, T2, T3>> list = new List<ECSComponentsGroupData<T1, T2, T3>>(); |
| | 14 | |
|
| 9 | 15 | | IReadOnlyList<ECSComponentsGroupData<T1, T2, T3>> IECSReadOnlyComponentsGroup<T1, T2, T3>.group => list; |
| | 16 | |
|
| 13 | 17 | | public ECSComponentsGroup(IECSComponent component1, IECSComponent component2, IECSComponent component3) |
| | 18 | | { |
| 13 | 19 | | Assert.IsNotNull(component1, $"component1 must not be null"); |
| 13 | 20 | | Assert.IsNotNull(component2, $"component2 must not be null"); |
| 13 | 21 | | Assert.IsNotNull(component3, $"component3 must not be null"); |
| 13 | 22 | | this.component1 = component1; |
| 13 | 23 | | this.component2 = component2; |
| 13 | 24 | | this.component3 = component3; |
| 13 | 25 | | } |
| | 26 | |
|
| | 27 | | bool IECSComponentsGroup.MatchEntity(IParcelScene scene, IDCLEntity entity) |
| | 28 | | { |
| 28 | 29 | | return component1.HasComponent(scene, entity) |
| | 30 | | && component2.HasComponent(scene, entity) |
| | 31 | | && component3.HasComponent(scene, entity); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | bool IECSComponentsGroup.ShouldAddOnComponentAdd(IECSComponent component) |
| | 35 | | { |
| 35 | 36 | | return component == component1 || component == component2 || component == component3; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | bool IECSComponentsGroup.ShouldRemoveOnComponentRemove(IECSComponent component) |
| | 40 | | { |
| 2 | 41 | | return component == component1 || component == component2 || component == component3; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | bool IECSComponentsGroup.ShouldAddOnComponentRemove(IECSComponent component) |
| | 45 | | { |
| 4 | 46 | | return false; |
| | 47 | | } |
| | 48 | |
|
| | 49 | | bool IECSComponentsGroup.ShouldRemoveOnComponentAdd(IECSComponent component) |
| | 50 | | { |
| 1 | 51 | | return false; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | void IECSComponentsGroup.Add(IParcelScene scene, IDCLEntity entity) |
| | 55 | | { |
| 8 | 56 | | ((ECSComponent<T1>)component1).TryGet(scene, entity.entityId, out var componentData1); |
| 8 | 57 | | ((ECSComponent<T2>)component2).TryGet(scene, entity.entityId, out var componentData2); |
| 8 | 58 | | ((ECSComponent<T3>)component3).TryGet(scene, entity.entityId, out var componentData3); |
| | 59 | |
|
| 8 | 60 | | ECSComponentsGroupData<T1, T2, T3> data = new ECSComponentsGroupData<T1, T2, T3> |
| | 61 | | ( |
| | 62 | | scene: scene, |
| | 63 | | entity: entity, |
| | 64 | | componentData1: componentData1, |
| | 65 | | componentData2: componentData2, |
| | 66 | | componentData3: componentData3 |
| | 67 | | ); |
| | 68 | |
|
| 8 | 69 | | list.Add(data); |
| 8 | 70 | | } |
| | 71 | |
|
| | 72 | | bool IECSComponentsGroup.Remove(IDCLEntity entity) |
| | 73 | | { |
| 12 | 74 | | for (int i = 0; i < list.Count; i++) |
| | 75 | | { |
| 6 | 76 | | ECSComponentsGroupData<T1, T2, T3> data = list[i]; |
| | 77 | |
|
| 6 | 78 | | if (data.entity != entity) |
| | 79 | | continue; |
| | 80 | |
|
| 6 | 81 | | list.RemoveAt(i); |
| 6 | 82 | | return true; |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | return false; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | void IECSComponentsGroup.Update(IParcelScene scene, IDCLEntity entity, IECSComponent component) |
| | 89 | | { |
| 19 | 90 | | long entityId = entity.entityId; |
| 19 | 91 | | if (!Utils.TryGetDataIndex(list, entity, out int index)) |
| 0 | 92 | | return; |
| | 93 | |
|
| 19 | 94 | | if (Utils.TryGetComponentData<T1>(scene, entityId, component1, component, out var d1)) |
| 3 | 95 | | list[index] = list[index].With(d1); |
| 19 | 96 | | if (Utils.TryGetComponentData<T2>(scene, entityId, component2, component, out var d2)) |
| 3 | 97 | | list[index] = list[index].With(d2); |
| 19 | 98 | | if (Utils.TryGetComponentData<T3>(scene, entityId, component3, component, out var d3)) |
| 9 | 99 | | list[index] = list[index].With(d3); |
| | 100 | |
|
| 19 | 101 | | } |
| | 102 | | } |
| | 103 | | } |