| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.ECS7.InternalComponents; |
| | 5 | | using DCL.ECSRuntime; |
| | 6 | | using DCL.Models; |
| | 7 | |
|
| | 8 | | public readonly struct InternalComponentWriteData |
| | 9 | | { |
| | 10 | | public readonly IParcelScene scene; |
| | 11 | | public readonly long entityId; |
| | 12 | | public readonly int componentId; |
| | 13 | | public readonly InternalComponent data; |
| | 14 | |
|
| | 15 | | public InternalComponentWriteData(IParcelScene scene, long entityId, int componentId, InternalComponent data) |
| | 16 | | { |
| | 17 | | this.scene = scene; |
| | 18 | | this.entityId = entityId; |
| | 19 | | this.componentId = componentId; |
| | 20 | | this.data = data; |
| | 21 | | } |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public class InternalECSComponent<T> : IInternalECSComponent<T> where T : InternalComponent |
| | 25 | | { |
| | 26 | | private readonly ECSComponentsFactory componentsFactory; |
| | 27 | | private readonly int componentId; |
| | 28 | | private readonly ECSComponent<T> component; |
| | 29 | | private readonly IList<InternalComponentWriteData> scheduledWrite; |
| | 30 | |
|
| 374 | 31 | | public InternalECSComponent(InternalECSComponentsId id, |
| | 32 | | ECSComponentsManager componentsManager, |
| | 33 | | ECSComponentsFactory componentsFactory, |
| | 34 | | Func<IECSComponentHandler<T>> handlerBuilder, |
| | 35 | | IList<InternalComponentWriteData> scheduledWrite) |
| | 36 | | { |
| 374 | 37 | | this.componentId = (int)id; |
| 374 | 38 | | this.componentsFactory = componentsFactory; |
| 374 | 39 | | this.scheduledWrite = scheduledWrite; |
| | 40 | |
|
| 604 | 41 | | componentsFactory.AddOrReplaceComponent<T>(componentId, x => (T)x, handlerBuilder); |
| 374 | 42 | | component = (ECSComponent<T>)componentsManager.GetOrCreateComponent(componentId); |
| 374 | 43 | | } |
| | 44 | |
|
| | 45 | | public void PutFor(IParcelScene scene, IDCLEntity entity, T model) |
| | 46 | | { |
| 193 | 47 | | PutFor(scene, entity.entityId, model); |
| 193 | 48 | | } |
| | 49 | |
|
| | 50 | | public void PutFor(IParcelScene scene, long entityId, T model) |
| | 51 | | { |
| 228 | 52 | | model._dirty = true; |
| 228 | 53 | | scene.crdtExecutor.ExecuteWithoutStoringState(entityId, componentId, model); |
| 228 | 54 | | scheduledWrite.Add(new InternalComponentWriteData(scene, entityId, componentId, model)); |
| 228 | 55 | | } |
| | 56 | |
|
| | 57 | | public void RemoveFor(IParcelScene scene, IDCLEntity entity) |
| | 58 | | { |
| 63 | 59 | | RemoveFor(scene, entity, null); |
| 63 | 60 | | } |
| | 61 | |
|
| | 62 | | public void RemoveFor(IParcelScene scene, IDCLEntity entity, T defaultModel) |
| | 63 | | { |
| 65 | 64 | | if (defaultModel != null) |
| | 65 | | { |
| 2 | 66 | | defaultModel._dirty = true; |
| 2 | 67 | | scene.crdtExecutor.ExecuteWithoutStoringState(entity.entityId, componentId, defaultModel); |
| 2 | 68 | | scheduledWrite.Add(new InternalComponentWriteData(scene, entity.entityId, componentId, null)); |
| 2 | 69 | | } |
| | 70 | | else |
| | 71 | | { |
| 63 | 72 | | scene.crdtExecutor.ExecuteWithoutStoringState(entity.entityId, componentId, null); |
| | 73 | | } |
| 63 | 74 | | } |
| | 75 | |
|
| | 76 | | public IECSReadOnlyComponentData<T> GetFor(IParcelScene scene, IDCLEntity entity) |
| | 77 | | { |
| 286 | 78 | | return component.Get(scene, entity); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | public IECSReadOnlyComponentData<T> GetFor(IParcelScene scene, long entityId) |
| | 82 | | { |
| 43 | 83 | | return component.Get(scene, entityId); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public IReadOnlyList<KeyValueSetTriplet<IParcelScene, long, ECSComponentData<T>>> GetForAll() |
| | 87 | | { |
| 22 | 88 | | return component.Get(); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public void Dispose() |
| | 92 | | { |
| 6 | 93 | | componentsFactory.RemoveComponent(componentId); |
| 6 | 94 | | } |
| | 95 | | } |