| | 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 | | { |
| 16 | 11 | | internal Dictionary<long, ECSComponentData<ModelType>> entities = new Dictionary<long, ECSComponentData<ModelTyp |
| 16 | 12 | | internal Dictionary<long, IECSComponentHandler<ModelType>> handlers = new Dictionary<long, IECSComponentHandler< |
| | 13 | |
|
| | 14 | | private readonly Func<IECSComponentHandler<ModelType>> handlerBuilder; |
| | 15 | | private readonly Func<object, ModelType> deserializer; |
| | 16 | | private readonly IParcelScene scene; |
| | 17 | |
|
| 16 | 18 | | public ECSComponent(IParcelScene scene, Func<object, ModelType> deserializer, Func<IECSComponentHandler<ModelTyp |
| | 19 | | { |
| 16 | 20 | | this.scene = scene; |
| 16 | 21 | | this.deserializer = deserializer; |
| 16 | 22 | | this.handlerBuilder = handlerBuilder; |
| 16 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// creates and add component to an entity |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="entity">target entity</param> |
| | 29 | | public void Create(IDCLEntity entity) |
| | 30 | | { |
| 46 | 31 | | var entityId = entity.entityId; |
| | 32 | |
|
| 46 | 33 | | if (entities.ContainsKey(entityId)) |
| | 34 | | { |
| 0 | 35 | | Debug.LogError($"entity {entityId} already contains component {typeof(ModelType)}", entity.gameObject); |
| 0 | 36 | | return; |
| | 37 | | } |
| | 38 | |
|
| 46 | 39 | | entities[entityId] = new ECSComponentData<ModelType>() |
| | 40 | | { |
| | 41 | | entity = entity, |
| | 42 | | model = default, |
| | 43 | | scene = scene |
| | 44 | | }; |
| | 45 | |
|
| 46 | 46 | | var handler = handlerBuilder?.Invoke(); |
| | 47 | |
|
| 46 | 48 | | if (handler != null) |
| | 49 | | { |
| 46 | 50 | | handlers[entityId] = handler; |
| 46 | 51 | | handler.OnComponentCreated(scene, entity); |
| | 52 | | } |
| 46 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// remove component from entity |
| | 57 | | /// </summary> |
| | 58 | | /// <param name="entity">target entity</param> |
| | 59 | | /// <returns>true if component removed successfully, false if entity didn't contain component</returns> |
| | 60 | | public bool Remove(IDCLEntity entity) |
| | 61 | | { |
| 34 | 62 | | var entityId = entity.entityId; |
| 34 | 63 | | if (handlers.TryGetValue(entityId, out IECSComponentHandler<ModelType> handler)) |
| | 64 | | { |
| 34 | 65 | | handler.OnComponentRemoved(scene, entity); |
| | 66 | | } |
| 34 | 67 | | handlers.Remove(entityId); |
| 34 | 68 | | return entities.Remove(entityId); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// set component model for entity |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="entity">target entity</param> |
| | 75 | | /// <param name="model">new model</param> |
| | 76 | | public void SetModel(IDCLEntity entity, ModelType model) |
| | 77 | | { |
| 39 | 78 | | var entityId = entity.entityId; |
| 39 | 79 | | if (entities.TryGetValue(entity.entityId, out ECSComponentData<ModelType> data)) |
| | 80 | | { |
| 39 | 81 | | data.model = model; |
| 39 | 82 | | } |
| | 83 | | else |
| | 84 | | { |
| 0 | 85 | | Debug.LogError($"trying to update model but entity {entityId} does not contains component {typeof(ModelT |
| | 86 | | entity.gameObject); |
| | 87 | | } |
| | 88 | |
|
| 39 | 89 | | if (handlers.TryGetValue(entityId, out IECSComponentHandler<ModelType> handler)) |
| | 90 | | { |
| 39 | 91 | | handler.OnComponentModelUpdated(scene, entity, model); |
| | 92 | | } |
| 39 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// deserialize message and apply a new model for an entity |
| | 97 | | /// </summary> |
| | 98 | | /// <param name="entity">target entity</param> |
| | 99 | | /// <param name="message">message</param> |
| | 100 | | public void Deserialize(IDCLEntity entity, object message) |
| | 101 | | { |
| 8 | 102 | | SetModel(entity, deserializer.Invoke(message)); |
| 8 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// check if entity contains component |
| | 107 | | /// </summary> |
| | 108 | | /// <param name="entity">target entity</param> |
| | 109 | | /// <returns>true if entity contains this component</returns> |
| | 110 | | public bool HasComponent(IDCLEntity entity) |
| | 111 | | { |
| 13 | 112 | | return entities.ContainsKey(entity.entityId); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// get component data for an entity |
| | 117 | | /// </summary> |
| | 118 | | /// <param name="entity">target entity</param> |
| | 119 | | /// <returns>component data, including model</returns> |
| | 120 | | public ECSComponentData<ModelType> Get(IDCLEntity entity) |
| | 121 | | { |
| 3 | 122 | | if (entities.TryGetValue(entity.entityId, out ECSComponentData<ModelType> data)) |
| | 123 | | { |
| 3 | 124 | | return data; |
| | 125 | | } |
| 0 | 126 | | return null; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// get every component data for every entity containing this component |
| | 131 | | /// </summary> |
| | 132 | | /// <returns>enumerator to iterate through the component data</returns> |
| | 133 | | public IEnumerator<ECSComponentData<ModelType>> Get() |
| | 134 | | { |
| 1 | 135 | | using (var iterator = entities.GetEnumerator()) |
| | 136 | | { |
| 6 | 137 | | while (iterator.MoveNext()) |
| | 138 | | { |
| 5 | 139 | | yield return iterator.Current.Value; |
| | 140 | | } |
| 1 | 141 | | } |
| 1 | 142 | | } |
| | 143 | | } |
| | 144 | | } |