| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.ECS7.InternalComponents; |
| | 4 | | using DCL.ECSComponents; |
| | 5 | | using DCL.ECSRuntime; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Rendering; |
| | 8 | |
|
| | 9 | | namespace ECSSystems.VisibilitySystem |
| | 10 | | { |
| | 11 | | public static class ECSVisibilitySystem |
| | 12 | | { |
| | 13 | | private class State |
| | 14 | | { |
| | 15 | | public IECSReadOnlyComponentsGroup<InternalRenderers, InternalVisibility> componentsGroup; |
| | 16 | | public IInternalECSComponent<InternalRenderers> renderersComponent; |
| | 17 | | public IInternalECSComponent<InternalVisibility> visibilityComponent; |
| | 18 | |
|
| | 19 | | } |
| | 20 | |
|
| | 21 | | public static Action CreateSystem(IECSReadOnlyComponentsGroup<InternalRenderers, InternalVisibility> componentsG |
| | 22 | | IInternalECSComponent<InternalRenderers> renderersComponent, |
| | 23 | | IInternalECSComponent<InternalVisibility> visibilityComponent) |
| | 24 | | { |
| 1 | 25 | | var state = new State() |
| | 26 | | { |
| | 27 | | componentsGroup = componentsGroup, |
| | 28 | | renderersComponent = renderersComponent, |
| | 29 | | visibilityComponent = visibilityComponent |
| | 30 | | }; |
| 5 | 31 | | return () => Update(state); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | private static void Update(State state) |
| | 35 | | { |
| 4 | 36 | | var componentGroup = state.componentsGroup.group; |
| | 37 | |
|
| 24 | 38 | | for (int i = 0; i < componentGroup.Count; i++) |
| | 39 | | { |
| 8 | 40 | | var entityData = componentGroup[i]; |
| 8 | 41 | | InternalRenderers renderersModel = entityData.componentData1.model; |
| 8 | 42 | | InternalVisibility visibilityModel = entityData.componentData2.model; |
| | 43 | |
|
| | 44 | |
|
| | 45 | | // if neither component has changed then we skip this entity |
| 8 | 46 | | if (!renderersModel.dirty && !visibilityModel.dirty) |
| | 47 | | continue; |
| | 48 | |
|
| 4 | 49 | | IList<Renderer> renderers = renderersModel.renderers; |
| | 50 | |
|
| 16 | 51 | | for (int j = 0; j < renderers.Count; j++) |
| | 52 | | { |
| 4 | 53 | | Renderer renderer = renderers[j]; |
| 4 | 54 | | renderer.enabled = visibilityModel.visible; |
| | 55 | | } |
| | 56 | |
|
| 4 | 57 | | renderersModel.dirty = false; |
| 4 | 58 | | visibilityModel.dirty = false; |
| | 59 | |
|
| 4 | 60 | | state.renderersComponent.PutFor(entityData.scene, entityData.entity, renderersModel); |
| 4 | 61 | | state.visibilityComponent.PutFor(entityData.scene, entityData.entity, visibilityModel); |
| | 62 | | } |
| 4 | 63 | | } |
| | 64 | | } |
| | 65 | | } |