| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Models; |
| | 5 | |
|
| | 6 | | namespace DCL.ECSRuntime |
| | 7 | | { |
| | 8 | | public class ECSComponentsManager |
| | 9 | | { |
| | 10 | | private readonly IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders; |
| 9 | 11 | | internal readonly Dictionary<int, IECSComponent> sceneComponents = new Dictionary<int, IECSComponent>(); |
| | 12 | | private readonly IParcelScene scene; |
| | 13 | |
|
| 9 | 14 | | public ECSComponentsManager(IParcelScene scene, IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilde |
| | 15 | | { |
| 9 | 16 | | this.componentBuilders = componentBuilders; |
| 9 | 17 | | this.scene = scene; |
| 9 | 18 | | } |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// get a component instance using it id |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="componentId"></param> |
| | 24 | | /// <returns>component instance of null if it does not exist</returns> |
| | 25 | | public IECSComponent GetComponent(int componentId) |
| | 26 | | { |
| 1 | 27 | | sceneComponents.TryGetValue(componentId, out IECSComponent component); |
| 1 | 28 | | return component; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// get or create a component for an entity |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="componentId"></param> |
| | 35 | | /// <param name="entity"></param> |
| | 36 | | /// <returns>the instance of existing or newly created component</returns> |
| | 37 | | public IECSComponent GetOrCreateComponent(int componentId, IDCLEntity entity) |
| | 38 | | { |
| 14 | 39 | | if (sceneComponents.TryGetValue(componentId, out IECSComponent component)) |
| | 40 | | { |
| 2 | 41 | | if (!component.HasComponent(entity)) |
| | 42 | | { |
| 0 | 43 | | component.Create(entity); |
| | 44 | | } |
| 0 | 45 | | } |
| 12 | 46 | | else if (componentBuilders.TryGetValue(componentId, out ECSComponentsFactory.ECSComponentBuilder componentBu |
| | 47 | | { |
| 12 | 48 | | component = componentBuilder.Invoke(scene); |
| 12 | 49 | | sceneComponents.Add(componentId, component); |
| 12 | 50 | | component.Create(entity); |
| | 51 | | } |
| 14 | 52 | | return component; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// deserialize data for a component. it will create the component if it does not exists |
| | 57 | | /// </summary> |
| | 58 | | /// <param name="componentId"></param> |
| | 59 | | /// <param name="entity"></param> |
| | 60 | | /// <param name="message"></param> |
| | 61 | | public void DeserializeComponent(int componentId, IDCLEntity entity, object message) |
| | 62 | | { |
| 7 | 63 | | var component = GetOrCreateComponent(componentId, entity); |
| 7 | 64 | | component?.Deserialize(entity, message); |
| 7 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// remove a component from an entity |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="componentId"></param> |
| | 71 | | /// <param name="entity"></param> |
| | 72 | | /// <returns>true if component removed successfully, false if entity didn't contain component</returns> |
| | 73 | | public bool RemoveComponent(int componentId, IDCLEntity entity) |
| | 74 | | { |
| 5 | 75 | | if (sceneComponents.TryGetValue(componentId, out IECSComponent component)) |
| | 76 | | { |
| 5 | 77 | | return component.Remove(entity); |
| | 78 | | } |
| 0 | 79 | | return false; |
| | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// remove all components of a given entity |
| | 84 | | /// </summary> |
| | 85 | | /// <param name="entity"></param> |
| | 86 | | public void RemoveAllComponents(IDCLEntity entity) |
| | 87 | | { |
| 1 | 88 | | using (var iterator = sceneComponents.GetEnumerator()) |
| | 89 | | { |
| 3 | 90 | | while (iterator.MoveNext()) |
| | 91 | | { |
| 2 | 92 | | iterator.Current.Value.Remove(entity); |
| | 93 | | } |
| 1 | 94 | | } |
| 1 | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// get if entity has any component |
| | 99 | | /// </summary> |
| | 100 | | /// <param name="entity"></param> |
| | 101 | | /// <returns></returns> |
| | 102 | | public bool HasAnyComponent(IDCLEntity entity) |
| | 103 | | { |
| 3 | 104 | | using (var iterator = sceneComponents.GetEnumerator()) |
| | 105 | | { |
| 6 | 106 | | while (iterator.MoveNext()) |
| | 107 | | { |
| 4 | 108 | | if (iterator.Current.Value.HasComponent(entity)) |
| | 109 | | { |
| 1 | 110 | | return true; |
| | 111 | | } |
| | 112 | | } |
| 2 | 113 | | } |
| 2 | 114 | | return false; |
| 1 | 115 | | } |
| | 116 | | } |
| | 117 | | } |