| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.ECSRuntime |
| | 8 | | { |
| | 9 | | public class ECSComponent<ModelType> : IECSComponent |
| | 10 | | { |
| 42 | 11 | | internal readonly Dictionary<IParcelScene, Dictionary<long, ECSComponentData<ModelType>>> componentData = |
| | 12 | | new Dictionary<IParcelScene, Dictionary<long, ECSComponentData<ModelType>>>(); |
| | 13 | |
|
| | 14 | | private readonly Func<IECSComponentHandler<ModelType>> handlerBuilder; |
| | 15 | | private readonly Func<object, ModelType> deserializer; |
| | 16 | |
|
| 42 | 17 | | public ECSComponent(Func<object, ModelType> deserializer, Func<IECSComponentHandler<ModelType>> handlerBuilder) |
| | 18 | | { |
| 42 | 19 | | this.deserializer = deserializer; |
| 42 | 20 | | this.handlerBuilder = handlerBuilder; |
| 42 | 21 | | } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// creates and add component to an entity |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="scene">target scene</param> |
| | 27 | | /// <param name="entity">target entity</param> |
| | 28 | | public void Create(IParcelScene scene, IDCLEntity entity) |
| | 29 | | { |
| 73 | 30 | | var entityId = entity.entityId; |
| | 31 | |
|
| 73 | 32 | | if (ComponentDataContainsEntity(componentData, scene, entity)) |
| | 33 | | { |
| 0 | 34 | | Debug.LogError($"entity {entityId} already contains component {typeof(ModelType)}", entity.gameObject); |
| 0 | 35 | | return; |
| | 36 | | } |
| | 37 | |
|
| 73 | 38 | | var handler = handlerBuilder?.Invoke(); |
| | 39 | |
|
| 73 | 40 | | ComponentDataAdd(componentData, scene, entity, |
| | 41 | | new ECSComponentData<ModelType>() |
| | 42 | | { |
| | 43 | | entity = entity, |
| | 44 | | model = default, |
| | 45 | | scene = scene, |
| | 46 | | handler = handler |
| | 47 | | } |
| | 48 | | ); |
| | 49 | |
|
| 73 | 50 | | handler?.OnComponentCreated(scene, entity); |
| 73 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// remove component from entity |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="scene">target scene</param> |
| | 57 | | /// <param name="entity">target entity</param> |
| | 58 | | /// <returns>true if component removed successfully, false if entity didn't contain component</returns> |
| | 59 | | public bool Remove(IParcelScene scene, IDCLEntity entity) |
| | 60 | | { |
| 39 | 61 | | if (!ComponentDataRemove(componentData, scene, entity, out ECSComponentData<ModelType> data)) |
| 0 | 62 | | return false; |
| | 63 | |
|
| 39 | 64 | | data.handler?.OnComponentRemoved(scene, entity); |
| 39 | 65 | | return true; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// set component model for entity |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="scene">target scene</param> |
| | 72 | | /// <param name="entity">target entity</param> |
| | 73 | | /// <param name="model">new model</param> |
| | 74 | | public void SetModel(IParcelScene scene, IDCLEntity entity, ModelType model) |
| | 75 | | { |
| 70 | 76 | | if (ComponentDataTryGet(componentData, scene, entity, out ECSComponentData<ModelType> data)) |
| | 77 | | { |
| 70 | 78 | | data.model = model; |
| 70 | 79 | | data.handler?.OnComponentModelUpdated(scene, entity, model); |
| 70 | 80 | | } |
| | 81 | | else |
| | 82 | | { |
| 0 | 83 | | Debug.LogError($"trying to update model but entity {entity.entityId} does not contains component {typeof |
| | 84 | | entity.gameObject); |
| | 85 | | } |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// deserialize message and apply a new model for an entity |
| | 90 | | /// </summary> |
| | 91 | | /// <param name="scene">target scene</param> |
| | 92 | | /// <param name="entity">target entity</param> |
| | 93 | | /// <param name="message">message</param> |
| | 94 | | public void Deserialize(IParcelScene scene, IDCLEntity entity, object message) |
| | 95 | | { |
| 39 | 96 | | SetModel(scene, entity, deserializer.Invoke(message)); |
| 39 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// check if entity contains component |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="scene">target scene</param> |
| | 103 | | /// <param name="entity">target entity</param> |
| | 104 | | /// <returns>true if entity contains this component</returns> |
| | 105 | | public bool HasComponent(IParcelScene scene, IDCLEntity entity) |
| | 106 | | { |
| 112 | 107 | | return ComponentDataContainsEntity(componentData, scene, entity); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// get component data for an entity |
| | 112 | | /// </summary> |
| | 113 | | /// <param name="scene">target scene</param> |
| | 114 | | /// <param name="entity">target entity</param> |
| | 115 | | /// <returns>component data, including model</returns> |
| | 116 | | public IECSReadOnlyComponentData<ModelType> Get(IParcelScene scene, IDCLEntity entity) |
| | 117 | | { |
| 33 | 118 | | if (ComponentDataTryGet(componentData, scene, entity, out ECSComponentData<ModelType> data)) |
| | 119 | | { |
| 33 | 120 | | return data; |
| | 121 | | } |
| 0 | 122 | | return null; |
| | 123 | | } |
| | 124 | |
|
| | 125 | | private static bool ComponentDataContainsEntity(IReadOnlyDictionary<IParcelScene, Dictionary<long, ECSComponentD |
| | 126 | | IParcelScene scene, IDCLEntity entity) |
| | 127 | | { |
| 185 | 128 | | if (!componentData.TryGetValue(scene, out Dictionary<long, ECSComponentData<ModelType>> entitiesData)) |
| 79 | 129 | | return false; |
| | 130 | |
|
| 106 | 131 | | return entitiesData.ContainsKey(entity.entityId); |
| | 132 | | } |
| | 133 | |
|
| | 134 | | private static void ComponentDataAdd(IDictionary<IParcelScene, Dictionary<long, ECSComponentData<ModelType>>> co |
| | 135 | | IParcelScene scene, IDCLEntity entity, ECSComponentData<ModelType> newData) |
| | 136 | | { |
| 73 | 137 | | if (!componentData.TryGetValue(scene, out Dictionary<long, ECSComponentData<ModelType>> entitiesData)) |
| | 138 | | { |
| 34 | 139 | | entitiesData = new Dictionary<long, ECSComponentData<ModelType>>(); |
| 34 | 140 | | componentData.Add(scene, entitiesData); |
| | 141 | | } |
| 73 | 142 | | entitiesData.Add(entity.entityId, newData); |
| 73 | 143 | | } |
| | 144 | |
|
| | 145 | | private static bool ComponentDataRemove(IDictionary<IParcelScene, Dictionary<long, ECSComponentData<ModelType>>> |
| | 146 | | IParcelScene scene, IDCLEntity entity, out ECSComponentData<ModelType> data) |
| | 147 | | { |
| 39 | 148 | | if (!componentData.TryGetValue(scene, out Dictionary<long, ECSComponentData<ModelType>> entitiesData)) |
| | 149 | | { |
| 0 | 150 | | data = null; |
| 0 | 151 | | return false; |
| | 152 | | } |
| | 153 | |
|
| 39 | 154 | | if (!entitiesData.TryGetValue(entity.entityId, out data)) |
| 0 | 155 | | return false; |
| | 156 | |
|
| 39 | 157 | | entitiesData.Remove(entity.entityId); |
| | 158 | |
|
| 39 | 159 | | if (entitiesData.Count == 0) |
| | 160 | | { |
| 10 | 161 | | componentData.Remove(scene); |
| | 162 | | } |
| 39 | 163 | | return true; |
| | 164 | | } |
| | 165 | |
|
| | 166 | | private static bool ComponentDataTryGet(IReadOnlyDictionary<IParcelScene, Dictionary<long, ECSComponentData<Mode |
| | 167 | | IParcelScene scene, IDCLEntity entity, out ECSComponentData<ModelType> data) |
| | 168 | | { |
| 103 | 169 | | if (componentData.TryGetValue(scene, out Dictionary<long, ECSComponentData<ModelType>> entitiesData)) |
| 103 | 170 | | return entitiesData.TryGetValue(entity.entityId, out data); |
| | 171 | |
|
| 0 | 172 | | data = null; |
| 0 | 173 | | return false; |
| | 174 | | } |
| | 175 | | } |
| | 176 | | } |