| | 1 | | using DCL; |
| | 2 | | using ECSSystems.AnimationSystem; |
| | 3 | | using ECSSystems.AvatarModifierAreaSystem; |
| | 4 | | using ECSSystems.BillboardSystem; |
| | 5 | | using ECSSystems.CameraSystem; |
| | 6 | | using ECSSystems.ECSEngineInfoSystem; |
| | 7 | | using ECSSystems.ECSRaycastSystem; |
| | 8 | | using ECSSystems.ECSSceneBoundsCheckerSystem; |
| | 9 | | using ECSSystems.ECSUiPointerEventsSystem; |
| | 10 | | using ECSSystems.GltfContainerLoadingStateSystem; |
| | 11 | | using ECSSystems.InputSenderSystem; |
| | 12 | | using ECSSystems.MaterialSystem; |
| | 13 | | using ECSSystems.PlayerSystem; |
| | 14 | | using ECSSystems.PointerInputSystem; |
| | 15 | | using ECSSystems.ScenesUiSystem; |
| | 16 | | using ECSSystems.TweenSystem; |
| | 17 | | using ECSSystems.UiCanvasInformationSystem; |
| | 18 | | using ECSSystems.UIInputSenderSystem; |
| | 19 | | using ECSSystems.VideoPlayerSystem; |
| | 20 | | using ECSSystems.VisibilitySystem; |
| | 21 | | using System; |
| | 22 | | using System.Collections.Generic; |
| | 23 | | using UnityEngine; |
| | 24 | | using UnityEngine.UIElements; |
| | 25 | | using ECS7System = System.Action; |
| | 26 | | using Environment = DCL.Environment; |
| | 27 | | using Object = UnityEngine.Object; |
| | 28 | |
|
| | 29 | | public class ECSSystemsController : IDisposable |
| | 30 | | { |
| | 31 | | private readonly IList<ECS7System> updateSystems; |
| | 32 | | private readonly IList<ECS7System> lateUpdateSystems; |
| | 33 | | private readonly IUpdateEventHandler updateEventHandler; |
| | 34 | | private readonly ECS7System internalComponentMarkDirtySystem; |
| | 35 | | private readonly ECS7System internalComponentRemoveDirtySystem; |
| | 36 | | private readonly ECSScenesUiSystem uiSystem; |
| | 37 | | private readonly ECSCameraEntitySystem cameraEntitySystem; |
| | 38 | | private readonly ECSPlayerTransformSystem playerTransformSystem; |
| | 39 | | private readonly ECSSceneBoundsCheckerSystem sceneBoundsCheckerSystem; |
| | 40 | | private readonly ECSUiCanvasInformationSystem uiCanvasInformationSystem; |
| | 41 | | private readonly GameObject hoverCanvas; |
| | 42 | | private readonly GameObject scenesUi; |
| | 43 | | private readonly IWorldState worldState; |
| | 44 | |
|
| 1 | 45 | | public ECSSystemsController(SystemsContext context) |
| | 46 | | { |
| 1 | 47 | | this.worldState = Environment.i.world.state; |
| 1 | 48 | | this.updateEventHandler = Environment.i.platform.updateEventHandler; |
| 1 | 49 | | this.internalComponentMarkDirtySystem = context.internalEcsComponents.MarkDirtyComponentsUpdate; |
| 1 | 50 | | this.internalComponentRemoveDirtySystem = context.internalEcsComponents.ResetDirtyComponentsUpdate; |
| | 51 | |
|
| 1 | 52 | | var canvas = Resources.Load<GameObject>("ECSInteractionHoverCanvas"); |
| 1 | 53 | | hoverCanvas = Object.Instantiate(canvas); |
| 1 | 54 | | hoverCanvas.name = "_ECSInteractionHoverCanvas"; |
| 1 | 55 | | IECSInteractionHoverCanvas interactionHoverCanvas = hoverCanvas.GetComponent<IECSInteractionHoverCanvas>(); |
| | 56 | |
|
| 1 | 57 | | var scenesUiResource = Resources.Load<UIDocument>("ScenesUI"); |
| 1 | 58 | | var scenesUiDocument = Object.Instantiate(scenesUiResource); |
| 1 | 59 | | scenesUiDocument.name = "_ECSScenesUI"; |
| 1 | 60 | | scenesUi = scenesUiDocument.gameObject; |
| | 61 | |
|
| 1 | 62 | | uiSystem = new ECSScenesUiSystem( |
| | 63 | | scenesUiDocument, |
| | 64 | | context.internalEcsComponents.uiContainerComponent, |
| | 65 | | DataStore.i.ecs7.scenes, |
| | 66 | | Environment.i.world.state, |
| | 67 | | CommonScriptableObjects.allUIHidden, |
| | 68 | | DataStore.i.HUDs.isCurrentSceneUiEnabled, |
| | 69 | | DataStore.i.HUDs.isSceneUiEnabled); |
| | 70 | |
|
| 1 | 71 | | ECSBillboardSystem billboardSystem = new ECSBillboardSystem(context.billboards, DataStore.i.camera); |
| | 72 | |
|
| 1 | 73 | | ECSVideoPlayerSystem videoPlayerSystem = new ECSVideoPlayerSystem( |
| | 74 | | context.internalEcsComponents.videoPlayerComponent, |
| | 75 | | context.internalEcsComponents.videoMaterialComponent, |
| | 76 | | context.internalEcsComponents.EngineInfo, |
| | 77 | | context.ComponentWriters, |
| | 78 | | context.VideoEventPool); |
| | 79 | |
|
| 1 | 80 | | cameraEntitySystem = new ECSCameraEntitySystem(context.ComponentWriters, |
| | 81 | | context.CameraModePool, context.PointerLockPool, context.TransformPool, context.TransformComponent, |
| | 82 | | DataStore.i.ecs7.scenes, DataStore.i.camera.transform, CommonScriptableObjects.worldOffset, CommonScriptable |
| | 83 | |
|
| 1 | 84 | | playerTransformSystem = new ECSPlayerTransformSystem(context.ComponentWriters, context.TransformPool, |
| | 85 | | context.TransformComponent, DataStore.i.ecs7.scenes, |
| | 86 | | DataStore.i.world.avatarTransform, CommonScriptableObjects.worldOffset); |
| | 87 | |
|
| 1 | 88 | | ECSUIInputSenderSystem uiInputSenderSystem = new ECSUIInputSenderSystem( |
| | 89 | | context.internalEcsComponents.uiInputResultsComponent, |
| | 90 | | context.ComponentWriters); |
| | 91 | |
|
| 1 | 92 | | ECSRaycastSystem raycastSystem = new ECSRaycastSystem( |
| | 93 | | context.internalEcsComponents.raycastComponent, |
| | 94 | | context.internalEcsComponents.physicColliderComponent, |
| | 95 | | context.internalEcsComponents.onPointerColliderComponent, |
| | 96 | | context.internalEcsComponents.customLayerColliderComponent, |
| | 97 | | context.internalEcsComponents.EngineInfo, |
| | 98 | | context.ComponentWriters, |
| | 99 | | context.RaycastResultPool); |
| | 100 | |
|
| 1 | 101 | | sceneBoundsCheckerSystem = new ECSSceneBoundsCheckerSystem( |
| | 102 | | DataStore.i.ecs7.scenes, |
| | 103 | | context.internalEcsComponents.sceneBoundsCheckComponent, |
| | 104 | | context.internalEcsComponents.visibilityComponent, |
| | 105 | | context.internalEcsComponents.renderersComponent, |
| | 106 | | context.internalEcsComponents.onPointerColliderComponent, |
| | 107 | | context.internalEcsComponents.physicColliderComponent, |
| | 108 | | DataStore.i.debugConfig.isDebugMode.Get()); |
| | 109 | |
|
| 1 | 110 | | ECSUiPointerEventsSystem uiPointerEventsSystem = new ECSUiPointerEventsSystem( |
| | 111 | | context.internalEcsComponents.RegisteredUiPointerEventsComponent, |
| | 112 | | context.internalEcsComponents.inputEventResultsComponent, |
| | 113 | | context.componentGroups.UnregisteredUiPointerEvents, |
| | 114 | | context.componentGroups.RegisteredUiPointerEvents, |
| | 115 | | context.componentGroups.RegisteredUiPointerEventsWithUiRemoved, |
| | 116 | | context.componentGroups.RegisteredUiPointerEventsWithPointerEventsRemoved); |
| | 117 | |
|
| 1 | 118 | | ECSPointerInputSystem pointerInputSystem = new ECSPointerInputSystem( |
| | 119 | | context.internalEcsComponents.onPointerColliderComponent, |
| | 120 | | context.internalEcsComponents.inputEventResultsComponent, |
| | 121 | | context.internalEcsComponents.PointerEventsComponent, |
| | 122 | | interactionHoverCanvas, |
| | 123 | | Environment.i.world.state, |
| | 124 | | DataStore.i.ecs7); |
| | 125 | |
|
| 1 | 126 | | GltfContainerLoadingStateSystem gltfContainerLoadingStateSystem = new GltfContainerLoadingStateSystem( |
| | 127 | | context.ComponentWriters, |
| | 128 | | context.GltfContainerLoadingStatePool, |
| | 129 | | context.internalEcsComponents.GltfContainerLoadingStateComponent); |
| | 130 | |
|
| 1 | 131 | | ECSEngineInfoSystem engineInfoSystem = new ECSEngineInfoSystem( |
| | 132 | | context.ComponentWriters, |
| | 133 | | context.EngineInfoPool, |
| | 134 | | context.internalEcsComponents.EngineInfo); |
| | 135 | |
|
| 1 | 136 | | uiCanvasInformationSystem = new ECSUiCanvasInformationSystem( |
| | 137 | | context.ComponentWriters, |
| | 138 | | context.UiCanvasInformationPool, |
| | 139 | | DataStore.i.ecs7.scenes); |
| | 140 | |
|
| 1 | 141 | | ECSInputSenderSystem inputSenderSystem = new ECSInputSenderSystem( |
| | 142 | | context.internalEcsComponents.inputEventResultsComponent, |
| | 143 | | context.internalEcsComponents.EngineInfo, |
| | 144 | | context.ComponentWriters, |
| | 145 | | context.PointerEventsResultPool, |
| 0 | 146 | | () => worldState.GetCurrentSceneNumber()); |
| | 147 | |
|
| 1 | 148 | | AnimationSystem animationSystem = new AnimationSystem( |
| | 149 | | context.componentGroups.AnimationGroup, |
| | 150 | | context.internalEcsComponents.Animation); |
| | 151 | |
|
| 1 | 152 | | ECSTweenSystem tweenSystem = new ECSTweenSystem( |
| | 153 | | context.internalEcsComponents.TweenComponent, |
| | 154 | | context.ComponentWriters, |
| | 155 | | context.TweenStatePool, |
| | 156 | | context.TransformPool, |
| | 157 | | CommonScriptableObjects.worldOffset, |
| | 158 | | context.internalEcsComponents.sceneBoundsCheckComponent); |
| | 159 | |
|
| 1 | 160 | | ECSAvatarModifierAreaSystem avatarModifierAreaSystem = new ECSAvatarModifierAreaSystem( |
| | 161 | | context.internalEcsComponents.AvatarModifierAreaComponent, DataStore.i.player); |
| | 162 | |
|
| 1 | 163 | | updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, Update); |
| 1 | 164 | | updateEventHandler.AddListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate); |
| | 165 | |
|
| 1 | 166 | | updateSystems = new ECS7System[] |
| | 167 | | { |
| | 168 | | engineInfoSystem.Update, |
| | 169 | | ECSTransformParentingSystem.CreateSystem(context.internalEcsComponents.sceneBoundsCheckComponent), |
| | 170 | | tweenSystem.Update, |
| | 171 | | ECSMaterialSystem.CreateSystem(context.componentGroups.texturizableGroup, |
| | 172 | | context.internalEcsComponents.texturizableComponent, context.internalEcsComponents.materialComponent), |
| | 173 | | ECSVisibilitySystem.CreateSystem(context.componentGroups.visibilityGroup, |
| | 174 | | context.internalEcsComponents.renderersComponent, context.internalEcsComponents.visibilityComponent), |
| | 175 | | avatarModifierAreaSystem.Update, |
| | 176 | | uiSystem.Update, |
| | 177 | | pointerInputSystem.Update, |
| | 178 | | billboardSystem.Update, |
| | 179 | | videoPlayerSystem.Update, |
| | 180 | | uiCanvasInformationSystem.Update, |
| | 181 | | animationSystem.Update |
| | 182 | | }; |
| | 183 | |
|
| 1 | 184 | | lateUpdateSystems = new ECS7System[] |
| | 185 | | { |
| | 186 | | uiPointerEventsSystem.Update, |
| | 187 | | uiInputSenderSystem.Update, // Input detection happens during Update() so this system has to run in LateUpda |
| | 188 | | inputSenderSystem.Update, |
| | 189 | | cameraEntitySystem.Update, |
| | 190 | | playerTransformSystem.Update, |
| | 191 | | gltfContainerLoadingStateSystem.Update, |
| | 192 | | raycastSystem.Update, // Should always be after player/entity transformations update |
| | 193 | | sceneBoundsCheckerSystem.Update // Should always be the last system |
| | 194 | | }; |
| 1 | 195 | | } |
| | 196 | |
|
| | 197 | | public void Dispose() |
| | 198 | | { |
| 1 | 199 | | updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update); |
| 1 | 200 | | updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, LateUpdate); |
| 1 | 201 | | uiSystem.Dispose(); |
| 1 | 202 | | cameraEntitySystem.Dispose(); |
| 1 | 203 | | playerTransformSystem.Dispose(); |
| 1 | 204 | | sceneBoundsCheckerSystem.Dispose(); |
| 1 | 205 | | uiCanvasInformationSystem.Dispose(); |
| 1 | 206 | | Object.Destroy(hoverCanvas); |
| 1 | 207 | | Object.Destroy(scenesUi); |
| 1 | 208 | | } |
| | 209 | |
|
| | 210 | | private void Update() |
| | 211 | | { |
| | 212 | | try |
| | 213 | | { |
| 0 | 214 | | internalComponentMarkDirtySystem.Invoke(); |
| 0 | 215 | | } |
| | 216 | | catch (Exception e) |
| | 217 | | { |
| 0 | 218 | | Debug.LogException(e); |
| 0 | 219 | | } |
| | 220 | |
|
| 0 | 221 | | int count = updateSystems.Count; |
| | 222 | |
|
| 0 | 223 | | for (int i = 0; i < count; i++) |
| | 224 | | { |
| | 225 | | try |
| | 226 | | { |
| 0 | 227 | | updateSystems[i].Invoke(); |
| 0 | 228 | | } |
| | 229 | | catch (Exception e) |
| | 230 | | { |
| 0 | 231 | | Debug.LogException(e); |
| 0 | 232 | | } |
| | 233 | | } |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | private void LateUpdate() |
| | 237 | | { |
| 0 | 238 | | int count = lateUpdateSystems.Count; |
| | 239 | |
|
| 0 | 240 | | for (int i = 0; i < count; i++) |
| | 241 | | { |
| | 242 | | try |
| | 243 | | { |
| 0 | 244 | | lateUpdateSystems[i].Invoke(); |
| 0 | 245 | | } |
| | 246 | | catch (Exception e) |
| | 247 | | { |
| 0 | 248 | | Debug.LogException(e); |
| 0 | 249 | | } |
| | 250 | | } |
| | 251 | |
|
| | 252 | | try |
| | 253 | | { |
| 0 | 254 | | internalComponentRemoveDirtySystem.Invoke(); |
| 0 | 255 | | } |
| | 256 | | catch (Exception e) |
| | 257 | | { |
| 0 | 258 | | Debug.LogException(e); |
| 0 | 259 | | } |
| 0 | 260 | | } |
| | 261 | | } |