| | 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 ECSComponent<ModelType> : IECSComponent |
| | 10 | | { |
| 4782 | 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 | |
|
| 4782 | 17 | | public ECSComponent(Func<object, ModelType> deserializer, Func<IECSComponentHandler<ModelType>> handlerBuilder) |
| | 18 | | { |
| 4782 | 19 | | this.deserializer = deserializer; |
| 4782 | 20 | | this.handlerBuilder = handlerBuilder; |
| 4782 | 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 | | { |
| 803 | 30 | | var entityId = entity.entityId; |
| | 31 | |
|
| 803 | 32 | | if (componentData.ContainsKey(scene, entityId)) |
| | 33 | | { |
| 0 | 34 | | Debug.LogError($"entity {entityId.ToString()} already contains component {typeof(ModelType)}", entity.ga |
| 0 | 35 | | return; |
| | 36 | | } |
| | 37 | |
|
| 803 | 38 | | var handler = handlerBuilder?.Invoke(); |
| | 39 | |
|
| 803 | 40 | | componentData.Add(scene, entityId, new ECSComponentData<ModelType> |
| | 41 | | ( |
| | 42 | | entity: entity, |
| | 43 | | model: default, |
| | 44 | | scene: scene, |
| | 45 | | handler: handler |
| | 46 | | )); |
| | 47 | |
|
| 803 | 48 | | handler?.OnComponentCreated(scene, entity); |
| 803 | 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 | | { |
| 9791 | 59 | | if (!componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data)) |
| 9036 | 60 | | return false; |
| | 61 | |
|
| 755 | 62 | | data.handler?.OnComponentRemoved(scene, entity); |
| 755 | 63 | | componentData.Remove(scene, entity.entityId); |
| 755 | 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 | | { |
| 1249 | 75 | | SetModel(scene, entity.entityId, model); |
| 1249 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// set component model for entity |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="scene">target scene</param> |
| | 82 | | /// <param name="entityId">target entity</param> |
| | 83 | | /// <param name="model">new model</param> |
| | 84 | | public void SetModel(IParcelScene scene, long entityId, ModelType model) |
| | 85 | | { |
| 1249 | 86 | | if (!componentData.TryGetValue(scene, entityId, out ECSComponentData<ModelType> data)) |
| | 87 | | { |
| 0 | 88 | | Debug.LogError($"trying to update model but entity {entityId.ToString()} does not contains component {ty |
| 0 | 89 | | return; |
| | 90 | | } |
| | 91 | |
|
| 1249 | 92 | | componentData[scene, entityId] = data.With(model); |
| 1249 | 93 | | data.handler?.OnComponentModelUpdated(scene, data.entity, model); |
| 677 | 94 | | } |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// deserialize message and apply a new model for an entity |
| | 98 | | /// </summary> |
| | 99 | | /// <param name="scene">target scene</param> |
| | 100 | | /// <param name="entity">target entity</param> |
| | 101 | | /// <param name="message">message</param> |
| | 102 | | public void Deserialize(IParcelScene scene, IDCLEntity entity, object message) |
| | 103 | | { |
| 20 | 104 | | SetModel(scene, entity, deserializer.Invoke(message)); |
| 20 | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// check if entity contains component |
| | 109 | | /// </summary> |
| | 110 | | /// <param name="scene">target scene</param> |
| | 111 | | /// <param name="entity">target entity</param> |
| | 112 | | /// <returns>true if entity contains this component</returns> |
| | 113 | | public bool HasComponent(IParcelScene scene, IDCLEntity entity) |
| | 114 | | { |
| 4421 | 115 | | return componentData.ContainsKey(scene, entity.entityId); |
| | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// get component data for an entity |
| | 120 | | /// </summary> |
| | 121 | | /// <param name="scene">target scene</param> |
| | 122 | | /// <param name="entityId">target entity id</param> |
| | 123 | | /// <param name="data">entity's component data</param> |
| | 124 | | /// <returns>`true` if data is exists</returns>/// |
| | 125 | | public bool TryGet(IParcelScene scene, long entityId, out ECSComponentData<ModelType> data) |
| | 126 | | { |
| 1099 | 127 | | return componentData.TryGetValue(scene, entityId, out data); |
| | 128 | | } |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// get component data for an entity |
| | 132 | | /// </summary> |
| | 133 | | /// <param name="scene">target scene</param> |
| | 134 | | /// <param name="entityId">target entity id</param> |
| | 135 | | /// <returns>entity's component data</returns>/// |
| | 136 | | public ECSComponentData<ModelType>? Get(IParcelScene scene, long entityId) |
| | 137 | | { |
| 3 | 138 | | if (componentData.TryGetValue(scene, entityId, out var data)) |
| 3 | 139 | | return data; |
| | 140 | |
|
| 0 | 141 | | return null; |
| | 142 | | } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// get component data for all entities |
| | 146 | | /// </summary> |
| | 147 | | /// <returns>list of component's data</returns> |
| | 148 | | public IReadOnlyList<KeyValueSetTriplet<IParcelScene, long, ECSComponentData<ModelType>>> Get() |
| | 149 | | { |
| 179 | 150 | | return componentData.Pairs; |
| | 151 | | } |
| | 152 | | } |
| | 153 | | } |