| | 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 | | { |
| 152 | 11 | | internal readonly DualKeyValueSet<IParcelScene, long, ECSComponentData<ModelType>> componentData = |
| | 12 | | new DualKeyValueSet<IParcelScene, long, ECSComponentData<ModelType>>(50); |
| | 13 | |
|
| | 14 | | private readonly Func<IECSComponentHandler<ModelType>> handlerBuilder; |
| | 15 | | private readonly Func<object, ModelType> deserializer; |
| | 16 | |
|
| 152 | 17 | | public ECSComponent(Func<object, ModelType> deserializer, Func<IECSComponentHandler<ModelType>> handlerBuilder) |
| | 18 | | { |
| 152 | 19 | | this.deserializer = deserializer; |
| 152 | 20 | | this.handlerBuilder = handlerBuilder; |
| 152 | 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 | | { |
| 140 | 30 | | var entityId = entity.entityId; |
| | 31 | |
|
| 140 | 32 | | if (componentData.ContainsKey(scene, entityId)) |
| | 33 | | { |
| 0 | 34 | | Debug.LogError($"entity {entityId} already contains component {typeof(ModelType)}", entity.gameObject); |
| 0 | 35 | | return; |
| | 36 | | } |
| | 37 | |
|
| 140 | 38 | | var handler = handlerBuilder?.Invoke(); |
| | 39 | |
|
| 140 | 40 | | componentData.Add(scene, entityId, new ECSComponentData<ModelType>() |
| | 41 | | { |
| | 42 | | entity = entity, |
| | 43 | | model = default, |
| | 44 | | scene = scene, |
| | 45 | | handler = handler |
| | 46 | | }); |
| | 47 | |
|
| 140 | 48 | | handler?.OnComponentCreated(scene, entity); |
| 140 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// remove component from entity |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="scene">target scene</param> |
| | 55 | | /// <param name="entity">target entity</param> |
| | 56 | | /// <returns>true if component removed successfully, false if entity didn't contain component</returns> |
| | 57 | | public bool Remove(IParcelScene scene, IDCLEntity entity) |
| | 58 | | { |
| 59 | 59 | | if (!componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data)) |
| 0 | 60 | | return false; |
| | 61 | |
|
| 59 | 62 | | data.handler?.OnComponentRemoved(scene, entity); |
| 59 | 63 | | componentData.Remove(scene, entity.entityId); |
| 59 | 64 | | return true; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// set component model for entity |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="scene">target scene</param> |
| | 71 | | /// <param name="entity">target entity</param> |
| | 72 | | /// <param name="model">new model</param> |
| | 73 | | public void SetModel(IParcelScene scene, IDCLEntity entity, ModelType model) |
| | 74 | | { |
| 146 | 75 | | if (componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data)) |
| | 76 | | { |
| 146 | 77 | | data.model = model; |
| 146 | 78 | | data.handler?.OnComponentModelUpdated(scene, entity, model); |
| 95 | 79 | | } |
| | 80 | | else |
| | 81 | | { |
| 0 | 82 | | Debug.LogError($"trying to update model but entity {entity.entityId} does not contains component {typeof |
| | 83 | | entity.gameObject); |
| | 84 | | } |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// deserialize message and apply a new model for an entity |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="scene">target scene</param> |
| | 91 | | /// <param name="entity">target entity</param> |
| | 92 | | /// <param name="message">message</param> |
| | 93 | | public void Deserialize(IParcelScene scene, IDCLEntity entity, object message) |
| | 94 | | { |
| 115 | 95 | | SetModel(scene, entity, deserializer.Invoke(message)); |
| 115 | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// check if entity contains component |
| | 100 | | /// </summary> |
| | 101 | | /// <param name="scene">target scene</param> |
| | 102 | | /// <param name="entity">target entity</param> |
| | 103 | | /// <returns>true if entity contains this component</returns> |
| | 104 | | public bool HasComponent(IParcelScene scene, IDCLEntity entity) |
| | 105 | | { |
| 274 | 106 | | return componentData.ContainsKey(scene, entity.entityId); |
| | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// get component data for an entity |
| | 111 | | /// </summary> |
| | 112 | | /// <param name="scene">target scene</param> |
| | 113 | | /// <param name="entity">target entity</param> |
| | 114 | | /// <returns>component data, including model</returns> |
| | 115 | | public IECSReadOnlyComponentData<ModelType> Get(IParcelScene scene, IDCLEntity entity) |
| | 116 | | { |
| 141 | 117 | | if (componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data)) |
| | 118 | | { |
| 93 | 119 | | return data; |
| | 120 | | } |
| 48 | 121 | | return null; |
| | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// get component data for all entities |
| | 126 | | /// </summary> |
| | 127 | | /// <returns>list of component's data</returns> |
| | 128 | | public IReadOnlyList<KeyValueSetTriplet<IParcelScene, long, ECSComponentData<ModelType>>> Get() |
| | 129 | | { |
| 0 | 130 | | return componentData.Pairs; |
| | 131 | | } |
| | 132 | | } |
| | 133 | | } |