| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Models; |
| | 4 | | using UnityEngine.Assertions; |
| | 5 | |
|
| | 6 | | namespace DCL.ECSRuntime |
| | 7 | | { |
| | 8 | | public class ECSComponentsGroup<T1, T2> : IECSComponentsGroup, IECSReadOnlyComponentsGroup<T1, T2> |
| | 9 | | { |
| | 10 | | private readonly IECSComponent component1; |
| | 11 | | private readonly IECSComponent component2; |
| 26 | 12 | | private readonly List<ECSComponentsGroupData<T1, T2>> list = new List<ECSComponentsGroupData<T1, T2>>(); |
| | 13 | |
|
| 36 | 14 | | IReadOnlyList<ECSComponentsGroupData<T1, T2>> IECSReadOnlyComponentsGroup<T1, T2>.group => list; |
| | 15 | |
|
| 26 | 16 | | public ECSComponentsGroup(IECSComponent component1, IECSComponent component2) |
| | 17 | | { |
| 26 | 18 | | Assert.IsNotNull(component1, $"component1 must not be null"); |
| 26 | 19 | | Assert.IsNotNull(component2, $"component2 must not be null"); |
| 26 | 20 | | this.component1 = component1; |
| 26 | 21 | | this.component2 = component2; |
| 26 | 22 | | } |
| | 23 | |
|
| | 24 | | bool IECSComponentsGroup.Match(IParcelScene scene, IDCLEntity entity) |
| | 25 | | { |
| 60 | 26 | | return component1.HasComponent(scene, entity) && component2.HasComponent(scene, entity); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | bool IECSComponentsGroup.Match(IECSComponent component) |
| | 30 | | { |
| 82 | 31 | | return component == component1 || component == component2; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | void IECSComponentsGroup.Add(IParcelScene scene, IDCLEntity entity) |
| | 35 | | { |
| 19 | 36 | | ECSComponentsGroupData<T1, T2> data = new ECSComponentsGroupData<T1, T2> |
| | 37 | | ( |
| | 38 | | scene: scene, |
| | 39 | | entity: entity, |
| | 40 | | componentData1: ((ECSComponent<T1>)component1).Get(scene, entity), |
| | 41 | | componentData2: ((ECSComponent<T2>)component2).Get(scene, entity) |
| | 42 | | ); |
| 19 | 43 | | list.Add(data); |
| 19 | 44 | | } |
| | 45 | |
|
| | 46 | | bool IECSComponentsGroup.Remove(IDCLEntity entity) |
| | 47 | | { |
| 6 | 48 | | for (int i = 0; i < list.Count; i++) |
| | 49 | | { |
| 3 | 50 | | ECSComponentsGroupData<T1, T2> data = list[i]; |
| 3 | 51 | | if (data.entity != entity) |
| | 52 | | continue; |
| | 53 | |
|
| 3 | 54 | | list.RemoveAt(i); |
| 3 | 55 | | return true; |
| | 56 | | } |
| 0 | 57 | | return false; |
| | 58 | | } |
| | 59 | | } |
| | 60 | | } |