| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Models; |
| | 4 | |
|
| | 5 | | namespace DCL.ECSRuntime |
| | 6 | | { |
| | 7 | | public class ECSComponentsManager |
| | 8 | | { |
| | 9 | | private readonly IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders; |
| 99 | 10 | | internal readonly KeyValueSet<int, IECSComponent> loadedComponents = new KeyValueSet<int, IECSComponent>(); |
| 99 | 11 | | internal readonly IList<IECSComponentsGroup> componentsGroups = new List<IECSComponentsGroup>(); |
| | 12 | |
|
| 99 | 13 | | public ECSComponentsManager(IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders |
| | 14 | | { |
| 99 | 15 | | this.componentBuilders = componentBuilders; |
| 99 | 16 | | } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// get a component instance using it id |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="componentId"></param> |
| | 22 | | /// <returns>component instance of null if it does not exist</returns> |
| | 23 | | public IECSComponent GetComponent(int componentId) |
| | 24 | | { |
| 1 | 25 | | loadedComponents.TryGetValue(componentId, out IECSComponent component); |
| 1 | 26 | | return component; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// get or create a component |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="componentId"></param> |
| | 33 | | /// <returns>the instance of existing or newly created component</returns> |
| | 34 | | public IECSComponent GetOrCreateComponent(int componentId) |
| | 35 | | { |
| 716 | 36 | | if (loadedComponents.TryGetValue(componentId, out IECSComponent component)) |
| 284 | 37 | | return component; |
| | 38 | |
|
| 432 | 39 | | if (!componentBuilders.TryGetValue(componentId, out ECSComponentsFactory.ECSComponentBuilder componentBuilde |
| 0 | 40 | | return null; |
| | 41 | |
|
| 432 | 42 | | component = componentBuilder.Invoke(); |
| 432 | 43 | | loadedComponents.Add(componentId, component); |
| | 44 | |
|
| 432 | 45 | | return component; |
| | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// get or create a component for an entity |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="componentId"></param> |
| | 52 | | /// <param name="scene"></param> |
| | 53 | | /// <param name="entity"></param> |
| | 54 | | /// <returns>the instance of existing or newly created component</returns> |
| | 55 | | public IECSComponent GetOrCreateComponent(int componentId, IParcelScene scene, IDCLEntity entity) |
| | 56 | | { |
| 286 | 57 | | IECSComponent component = GetOrCreateComponent(componentId); |
| 286 | 58 | | if (component == null || component.HasComponent(scene, entity)) |
| 81 | 59 | | return component; |
| | 60 | |
|
| 205 | 61 | | component.Create(scene, entity); |
| | 62 | |
|
| 506 | 63 | | for (int i = 0; i < componentsGroups.Count; i++) |
| | 64 | | { |
| 48 | 65 | | IECSComponentsGroup compGroup = componentsGroups[i]; |
| 48 | 66 | | if (compGroup.Match(component) && compGroup.Match(scene, entity)) |
| | 67 | | { |
| 15 | 68 | | compGroup.Add(scene, entity); |
| | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| 205 | 72 | | return component; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// deserialize data for a component. it will create the component if it does not exists |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="componentId"></param> |
| | 79 | | /// <param name="scene"></param> |
| | 80 | | /// <param name="entity"></param> |
| | 81 | | /// <param name="message"></param> |
| | 82 | | public void DeserializeComponent(int componentId, IParcelScene scene, IDCLEntity entity, object message) |
| | 83 | | { |
| 268 | 84 | | var component = GetOrCreateComponent(componentId, scene, entity); |
| 268 | 85 | | component?.Deserialize(scene, entity, message); |
| 268 | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// remove a component from an entity |
| | 90 | | /// </summary> |
| | 91 | | /// <param name="componentId"></param> |
| | 92 | | /// <param name="scene"></param> |
| | 93 | | /// <param name="entity"></param> |
| | 94 | | /// <returns>true if component removed successfully, false if entity didn't contain component</returns> |
| | 95 | | public bool RemoveComponent(int componentId, IParcelScene scene, IDCLEntity entity) |
| | 96 | | { |
| 71 | 97 | | if (!loadedComponents.TryGetValue(componentId, out IECSComponent component)) |
| 0 | 98 | | return false; |
| | 99 | |
|
| 71 | 100 | | if (!component.Remove(scene, entity)) |
| 0 | 101 | | return false; |
| | 102 | |
|
| 146 | 103 | | for (int i = 0; i < componentsGroups.Count; i++) |
| | 104 | | { |
| 2 | 105 | | IECSComponentsGroup compGroup = componentsGroups[i]; |
| 2 | 106 | | if (compGroup.Match(component)) |
| | 107 | | { |
| 2 | 108 | | compGroup.Remove(entity); |
| | 109 | | } |
| | 110 | | } |
| | 111 | |
|
| 71 | 112 | | return true; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// remove all components of a given entity |
| | 117 | | /// </summary> |
| | 118 | | /// <param name="scene"></param> |
| | 119 | | /// <param name="entity"></param> |
| | 120 | | public void RemoveAllComponents(IParcelScene scene, IDCLEntity entity) |
| | 121 | | { |
| 2 | 122 | | int count = loadedComponents.Count; |
| 12 | 123 | | for (int i = 0; i < count; i++) |
| | 124 | | { |
| 4 | 125 | | loadedComponents.Pairs[i].value.Remove(scene, entity); |
| | 126 | | } |
| | 127 | |
|
| 6 | 128 | | for (int i = 0; i < componentsGroups.Count; i++) |
| | 129 | | { |
| 1 | 130 | | componentsGroups[i].Remove(entity); |
| | 131 | | } |
| 2 | 132 | | } |
| | 133 | |
|
| | 134 | | /// <summary> |
| | 135 | | /// get if entity has any component |
| | 136 | | /// </summary> |
| | 137 | | /// <param name="scene"></param> |
| | 138 | | /// <param name="entity"></param> |
| | 139 | | /// <returns></returns> |
| | 140 | | public bool HasAnyComponent(IParcelScene scene, IDCLEntity entity) |
| | 141 | | { |
| 68 | 142 | | int count = loadedComponents.Count; |
| 644 | 143 | | for (int i = 0; i < count; i++) |
| | 144 | | { |
| 317 | 145 | | if (loadedComponents.Pairs[i].value.HasComponent(scene, entity)) |
| | 146 | | { |
| 63 | 147 | | return true; |
| | 148 | | } |
| | 149 | | } |
| 5 | 150 | | return false; |
| | 151 | | } |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// creates a components group |
| | 155 | | /// </summary> |
| | 156 | | /// <param name="componentId1">id of one of the components</param> |
| | 157 | | /// <param name="componentId2">id of the other component</param> |
| | 158 | | /// <typeparam name="T1">model type of `componentId1`</typeparam> |
| | 159 | | /// <typeparam name="T2">model type of `componentId2`</typeparam> |
| | 160 | | /// <returns></returns> |
| | 161 | | public IECSReadOnlyComponentsGroup<T1, T2> CreateComponentGroup<T1, T2>(int componentId1, int componentId2) |
| | 162 | | { |
| 21 | 163 | | var compGroup = new ECSComponentsGroup<T1, T2>(GetOrCreateComponent(componentId1), GetOrCreateComponent(comp |
| 21 | 164 | | componentsGroups.Add(compGroup); |
| 21 | 165 | | return compGroup; |
| | 166 | | } |
| | 167 | | } |
| | 168 | | } |