| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.ECSRuntime |
| | 8 | | { |
| | 9 | | public class ECSComponentsManager |
| | 10 | | { |
| 331 | 11 | | internal readonly KeyValueSet<int, IECSComponent> loadedComponents = new KeyValueSet<int, IECSComponent>(); |
| 331 | 12 | | internal readonly IList<IECSComponentsGroup> componentsGroups = new List<IECSComponentsGroup>(); |
| 331 | 13 | | internal readonly Dictionary<IDCLEntity, List<IECSComponentsGroup>> entitiesGroups = |
| | 14 | | new Dictionary<IDCLEntity, List<IECSComponentsGroup>>(); |
| | 15 | | private readonly IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders; |
| | 16 | |
|
| 331 | 17 | | public ECSComponentsManager(IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders |
| | 18 | | { |
| 331 | 19 | | this.componentBuilders = componentBuilders; |
| 331 | 20 | | } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// get a component instance using it id |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="componentId"></param> |
| | 26 | | /// <returns>component instance of null if it does not exist</returns> |
| | 27 | | public IECSComponent GetComponent(int componentId) |
| | 28 | | { |
| 1 | 29 | | loadedComponents.TryGetValue(componentId, out IECSComponent component); |
| 1 | 30 | | return component; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// get or create a component |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="componentId"></param> |
| | 37 | | /// <returns>the instance of existing or newly created component</returns> |
| | 38 | | public IECSComponent GetOrCreateComponent(int componentId) |
| | 39 | | { |
| 5034 | 40 | | if (loadedComponents.TryGetValue(componentId, out IECSComponent component)) |
| 251 | 41 | | return component; |
| | 42 | |
|
| 4783 | 43 | | if (!componentBuilders.TryGetValue(componentId, out ECSComponentsFactory.ECSComponentBuilder componentBuilde |
| 13 | 44 | | return null; |
| | 45 | |
|
| 4770 | 46 | | component = componentBuilder.Invoke(); |
| 4770 | 47 | | loadedComponents.Add(componentId, component); |
| | 48 | |
|
| 4770 | 49 | | return component; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// get or create a component for an entity |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="componentId"></param> |
| | 56 | | /// <param name="scene"></param> |
| | 57 | | /// <param name="entity"></param> |
| | 58 | | /// <returns>the instance of existing or newly created component</returns> |
| | 59 | | public IECSComponent GetOrCreateComponent(int componentId, IParcelScene scene, IDCLEntity entity) |
| | 60 | | { |
| 85 | 61 | | IECSComponent component = GetOrCreateComponent(componentId); |
| | 62 | |
|
| 85 | 63 | | if (component == null || component.HasComponent(scene, entity)) |
| 20 | 64 | | return component; |
| | 65 | |
|
| 65 | 66 | | component.Create(scene, entity); |
| 65 | 67 | | SetGroupsOnComponentCreated(component, scene, entity, componentsGroups, entitiesGroups); |
| 65 | 68 | | return component; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// deserialize data for a component. it will create the component if it does not exists |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="componentId"></param> |
| | 75 | | /// <param name="scene"></param> |
| | 76 | | /// <param name="entity"></param> |
| | 77 | | /// <param name="message"></param> |
| | 78 | | public void DeserializeComponent(int componentId, IParcelScene scene, IDCLEntity entity, object message) |
| | 79 | | { |
| 24 | 80 | | var component = GetOrCreateComponent(componentId, scene, entity); |
| | 81 | |
|
| 24 | 82 | | if (component != null) |
| | 83 | | { |
| 19 | 84 | | component.Deserialize(scene, entity, message); |
| 19 | 85 | | UpdateComponentGroups(component, scene, entity, entitiesGroups); |
| | 86 | | } |
| 24 | 87 | | } |
| | 88 | |
|
| | 89 | | public void SignalComponentCreated(IParcelScene scene, IDCLEntity entity, IECSComponent component) |
| | 90 | | { |
| 700 | 91 | | SetGroupsOnComponentCreated(component, scene, entity, componentsGroups, entitiesGroups); |
| 700 | 92 | | } |
| | 93 | |
|
| | 94 | | public void SignalComponentUpdated(IParcelScene scene, IDCLEntity entity, IECSComponent component) |
| | 95 | | { |
| 1193 | 96 | | UpdateComponentGroups(component, scene, entity, entitiesGroups); |
| 1193 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// remove a component from an entity |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="componentId"></param> |
| | 103 | | /// <param name="scene"></param> |
| | 104 | | /// <param name="entity"></param> |
| | 105 | | /// <returns>true if component removed successfully, false if entity didn't contain component</returns> |
| | 106 | | public bool RemoveComponent(int componentId, IParcelScene scene, IDCLEntity entity) |
| | 107 | | { |
| 191 | 108 | | if (!loadedComponents.TryGetValue(componentId, out IECSComponent component)) |
| 0 | 109 | | return false; |
| | 110 | |
|
| 191 | 111 | | if (!component.Remove(scene, entity)) |
| 12 | 112 | | return false; |
| | 113 | |
|
| 179 | 114 | | SetGroupsOnComponentRemoved(component, scene, entity, componentsGroups, entitiesGroups); |
| | 115 | |
|
| 179 | 116 | | return true; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// remove all components of a given entity |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="scene"></param> |
| | 123 | | /// <param name="entity"></param> |
| | 124 | | public void RemoveAllComponents(IParcelScene scene, IDCLEntity entity) |
| | 125 | | { |
| 516 | 126 | | int count = loadedComponents.Count; |
| | 127 | |
|
| 20176 | 128 | | for (int i = 0; i < count; i++) |
| | 129 | | { |
| 9572 | 130 | | loadedComponents.Pairs[i].value.Remove(scene, entity); |
| | 131 | | } |
| | 132 | |
|
| 516 | 133 | | if (entitiesGroups.TryGetValue(entity, out List<IECSComponentsGroup> entityInGroups)) |
| | 134 | | { |
| 60 | 135 | | for (int i = 0; i < entityInGroups.Count; i++) |
| | 136 | | { |
| 15 | 137 | | entityInGroups[i].Remove(entity); |
| | 138 | | } |
| | 139 | |
|
| 15 | 140 | | entitiesGroups.Remove(entity); |
| | 141 | | } |
| 516 | 142 | | } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// get if entity has any component |
| | 146 | | /// </summary> |
| | 147 | | /// <param name="scene"></param> |
| | 148 | | /// <param name="entity"></param> |
| | 149 | | /// <returns></returns> |
| | 150 | | public bool HasAnyComponent(IParcelScene scene, IDCLEntity entity) |
| | 151 | | { |
| 180 | 152 | | int count = loadedComponents.Count; |
| | 153 | |
|
| 5882 | 154 | | for (int i = 0; i < count; i++) |
| | 155 | | { |
| 2903 | 156 | | if (loadedComponents.Pairs[i].value.HasComponent(scene, entity)) |
| | 157 | | { |
| 142 | 158 | | return true; |
| | 159 | | } |
| | 160 | | } |
| | 161 | |
|
| 38 | 162 | | return false; |
| | 163 | | } |
| | 164 | |
|
| | 165 | | /// <summary> |
| | 166 | | /// creates a components group |
| | 167 | | /// </summary> |
| | 168 | | /// <param name="componentId1">id of one of the components</param> |
| | 169 | | /// <param name="componentId2">id of the other component</param> |
| | 170 | | /// <typeparam name="T1">model type of `componentId1`</typeparam> |
| | 171 | | /// <typeparam name="T2">model type of `componentId2`</typeparam> |
| | 172 | | /// <returns></returns> |
| | 173 | | public IECSReadOnlyComponentsGroup<T1, T2> CreateComponentGroup<T1, T2>(int componentId1, int componentId2) |
| | 174 | | { |
| 41 | 175 | | var compGroup = new ECSComponentsGroup<T1, T2>(GetOrCreateComponent(componentId1), GetOrCreateComponent(comp |
| 41 | 176 | | componentsGroups.Add(compGroup); |
| 41 | 177 | | return compGroup; |
| | 178 | | } |
| | 179 | |
|
| | 180 | | /// <summary> |
| | 181 | | /// creates a components group |
| | 182 | | /// </summary> |
| | 183 | | /// <param name="componentId1">id of one of the components</param> |
| | 184 | | /// <param name="componentId2">id of the other component</param> |
| | 185 | | /// <param name="componentId3">id of the other component</param> |
| | 186 | | /// <typeparam name="T1">model type of `componentId1`</typeparam> |
| | 187 | | /// <typeparam name="T2">model type of `componentId2`</typeparam> |
| | 188 | | /// <typeparam name="T3">model type of `componentId3`</typeparam> |
| | 189 | | /// <returns></returns> |
| | 190 | | public IECSReadOnlyComponentsGroup<T1, T2, T3> CreateComponentGroup<T1, T2, T3>(int componentId1, int componentI |
| | 191 | | { |
| 13 | 192 | | var compGroup = new ECSComponentsGroup<T1, T2, T3>( |
| | 193 | | GetOrCreateComponent(componentId1), |
| | 194 | | GetOrCreateComponent(componentId2), |
| | 195 | | GetOrCreateComponent(componentId3)); |
| | 196 | |
|
| 13 | 197 | | componentsGroups.Add(compGroup); |
| 13 | 198 | | return compGroup; |
| | 199 | | } |
| | 200 | |
|
| | 201 | | /// <summary> |
| | 202 | | /// creates a components group of components without `excludeComponentId` |
| | 203 | | /// </summary> |
| | 204 | | /// <param name="componentId">id of component that must be present</param> |
| | 205 | | /// <param name="excludeComponentId">id of component that must not be present</param> |
| | 206 | | /// <typeparam name="T1">type of component that must be present</typeparam> |
| | 207 | | /// <returns></returns> |
| | 208 | | public IECSReadOnlyComponentsGroup<T1> CreateComponentGroupWithoutComponent<T1>(int componentId, int excludeComp |
| | 209 | | { |
| 16 | 210 | | var compGroup = new ECSComponentsGroupWithout<T1>(GetOrCreateComponent(componentId), |
| | 211 | | GetOrCreateComponent(excludeComponentId)); |
| | 212 | |
|
| 16 | 213 | | componentsGroups.Add(compGroup); |
| 16 | 214 | | return compGroup; |
| | 215 | | } |
| | 216 | |
|
| | 217 | | /// <summary> |
| | 218 | | /// creates a components group of components without `excludeComponentId` |
| | 219 | | /// </summary> |
| | 220 | | /// <param name="componentId1">id of component that must be present</param> |
| | 221 | | /// <param name="componentId2">id of component that must be present</param> |
| | 222 | | /// <param name="excludeComponentId">id of component that must not be present</param> |
| | 223 | | /// <typeparam name="T1">model type of `componentId1`</typeparam> |
| | 224 | | /// <typeparam name="T2">model type of `componentId2`</typeparam> |
| | 225 | | /// <returns></returns> |
| | 226 | | public IECSReadOnlyComponentsGroup<T1, T2> CreateComponentGroupWithoutComponent<T1, T2>(int componentId1, int co |
| | 227 | | { |
| 26 | 228 | | var compGroup = new ECSComponentsGroupWithout<T1, T2>( |
| | 229 | | GetOrCreateComponent(componentId1), |
| | 230 | | GetOrCreateComponent(componentId2), |
| | 231 | | GetOrCreateComponent(excludeComponentId)); |
| | 232 | |
|
| 26 | 233 | | componentsGroups.Add(compGroup); |
| 26 | 234 | | return compGroup; |
| | 235 | | } |
| | 236 | |
|
| | 237 | | private static void SetGroupsOnComponentCreated(IECSComponent component, IParcelScene scene, IDCLEntity entity, |
| | 238 | | IList<IECSComponentsGroup> componentsGroups, IDictionary<IDCLEntity, List<IECSComponentsGroup>> entitiesGrou |
| | 239 | | { |
| 765 | 240 | | List<IECSComponentsGroup> entityInGroups = null; |
| | 241 | |
|
| 765 | 242 | | if (entitiesGroups.TryGetValue(entity, out entityInGroups)) |
| | 243 | | { |
| 48 | 244 | | for (int i = entityInGroups.Count - 1; i >= 0; i--) |
| | 245 | | { |
| 12 | 246 | | IECSComponentsGroup compGroup = entityInGroups[i]; |
| | 247 | |
|
| 12 | 248 | | if (compGroup.ShouldRemoveOnComponentAdd(component)) |
| | 249 | | { |
| 10 | 250 | | compGroup.Remove(entity); |
| 10 | 251 | | entityInGroups.RemoveAt(i); |
| | 252 | | } |
| | 253 | | } |
| | 254 | |
|
| 12 | 255 | | if (entityInGroups.Count == 0) |
| | 256 | | { |
| 10 | 257 | | entitiesGroups.Remove(entity); |
| 10 | 258 | | entityInGroups = null; |
| | 259 | | } |
| | 260 | | } |
| | 261 | |
|
| 1994 | 262 | | for (int i = 0; i < componentsGroups.Count; i++) |
| | 263 | | { |
| 232 | 264 | | IECSComponentsGroup compGroup = componentsGroups[i]; |
| | 265 | |
|
| 232 | 266 | | if (compGroup.ShouldAddOnComponentAdd(component) && compGroup.MatchEntity(scene, entity)) |
| | 267 | | { |
| 43 | 268 | | compGroup.Add(scene, entity); |
| | 269 | |
|
| 43 | 270 | | if (entityInGroups == null) |
| | 271 | | { |
| 43 | 272 | | entityInGroups = new List<IECSComponentsGroup>(); |
| 43 | 273 | | entitiesGroups[entity] = entityInGroups; |
| | 274 | | } |
| | 275 | |
|
| 43 | 276 | | entityInGroups.Add(compGroup); |
| | 277 | | } |
| | 278 | | } |
| 765 | 279 | | } |
| | 280 | |
|
| | 281 | | private static void SetGroupsOnComponentRemoved(IECSComponent component, IParcelScene scene, IDCLEntity entity, |
| | 282 | | IList<IECSComponentsGroup> componentsGroups, Dictionary<IDCLEntity, List<IECSComponentsGroup>> entitiesGroup |
| | 283 | | { |
| 179 | 284 | | List<IECSComponentsGroup> entityInGroups = null; |
| | 285 | |
|
| 179 | 286 | | if (entitiesGroups.TryGetValue(entity, out entityInGroups)) |
| | 287 | | { |
| 40 | 288 | | for (int i = entityInGroups.Count - 1; i >= 0; i--) |
| | 289 | | { |
| 10 | 290 | | IECSComponentsGroup compGroup = entityInGroups[i]; |
| | 291 | |
|
| 10 | 292 | | if (compGroup.ShouldRemoveOnComponentRemove(component)) |
| | 293 | | { |
| 10 | 294 | | compGroup.Remove(entity); |
| 10 | 295 | | entityInGroups.RemoveAt(i); |
| | 296 | | } |
| | 297 | | } |
| | 298 | |
|
| 10 | 299 | | if (entityInGroups.Count == 0) |
| | 300 | | { |
| 10 | 301 | | entitiesGroups.Remove(entity); |
| 10 | 302 | | entityInGroups = null; |
| | 303 | | } |
| | 304 | | } |
| | 305 | |
|
| 410 | 306 | | for (int i = 0; i < componentsGroups.Count; i++) |
| | 307 | | { |
| 26 | 308 | | IECSComponentsGroup compGroup = componentsGroups[i]; |
| | 309 | |
|
| 26 | 310 | | if (compGroup.ShouldAddOnComponentRemove(component) && compGroup.MatchEntity(scene, entity)) |
| | 311 | | { |
| 3 | 312 | | compGroup.Add(scene, entity); |
| | 313 | |
|
| 3 | 314 | | if (entityInGroups == null) |
| | 315 | | { |
| 3 | 316 | | entityInGroups = new List<IECSComponentsGroup>(); |
| 3 | 317 | | entitiesGroups[entity] = entityInGroups; |
| | 318 | | } |
| | 319 | |
|
| 3 | 320 | | entityInGroups.Add(compGroup); |
| | 321 | | } |
| | 322 | | } |
| 179 | 323 | | } |
| | 324 | |
|
| | 325 | | private static void UpdateComponentGroups(IECSComponent component, IParcelScene scene, IDCLEntity entity, |
| | 326 | | IDictionary<IDCLEntity, List<IECSComponentsGroup>> entitiesGroups) |
| | 327 | | { |
| 1212 | 328 | | if (entitiesGroups.TryGetValue(entity, out var entityInGroups)) |
| | 329 | | { |
| 348 | 330 | | for (int i = 0; i < entityInGroups.Count; i++) |
| | 331 | | { |
| 87 | 332 | | IECSComponentsGroup compGroup = entityInGroups[i]; |
| 87 | 333 | | compGroup.Update(scene, entity, component); |
| | 334 | | } |
| | 335 | | } |
| 1212 | 336 | | } |
| | 337 | | } |
| | 338 | | } |