| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.ECS7; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | | using DCL.Models; |
| | 8 | | using ECSSystems.Helpers; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace ECSSystems.CameraSystem |
| | 12 | | { |
| | 13 | | public static class ECSCameraTransformSystem |
| | 14 | | { |
| | 15 | | private class State |
| | 16 | | { |
| | 17 | | public BaseVariable<Transform> cameraTransform; |
| | 18 | | public RendererState rendererState; |
| | 19 | | public Vector3Variable worldOffset; |
| | 20 | | public IReadOnlyList<IParcelScene> loadedScenes; |
| | 21 | | public IECSComponentWriter componentsWriter; |
| 2 | 22 | | public UnityEngine.Vector3 lastCameraPosition = UnityEngine.Vector3.zero; |
| 2 | 23 | | public Quaternion lastCameraRotation = Quaternion.identity; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public static Action CreateSystem(IECSComponentWriter componentsWriter) |
| | 27 | | { |
| 2 | 28 | | var state = new State() |
| | 29 | | { |
| | 30 | | cameraTransform = DataStore.i.camera.transform, |
| | 31 | | rendererState = CommonScriptableObjects.rendererState, |
| | 32 | | worldOffset = CommonScriptableObjects.worldOffset, |
| | 33 | | loadedScenes = DataStore.i.ecs7.scenes, |
| | 34 | | componentsWriter = componentsWriter |
| | 35 | | }; |
| 6 | 36 | | return () => Update(state); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | private static void Update(State state) |
| | 40 | | { |
| 4 | 41 | | if (!state.rendererState.Get()) |
| 0 | 42 | | return; |
| | 43 | |
|
| 4 | 44 | | Transform cameraT = state.cameraTransform.Get(); |
| | 45 | |
|
| 4 | 46 | | UnityEngine.Vector3 cameraPosition = cameraT.position; |
| 4 | 47 | | Quaternion cameraRotation = cameraT.rotation; |
| | 48 | |
|
| 4 | 49 | | if (state.lastCameraPosition == cameraPosition && state.lastCameraRotation == cameraRotation) |
| | 50 | | { |
| 1 | 51 | | return; |
| | 52 | | } |
| | 53 | |
|
| 3 | 54 | | state.lastCameraPosition = cameraPosition; |
| 3 | 55 | | state.lastCameraRotation = cameraRotation; |
| | 56 | |
|
| 3 | 57 | | UnityEngine.Vector3 worldOffset = state.worldOffset.Get(); |
| | 58 | |
|
| 3 | 59 | | var loadedScenes = state.loadedScenes; |
| 3 | 60 | | var componentsWriter = state.componentsWriter; |
| | 61 | |
|
| | 62 | | IParcelScene scene; |
| 12 | 63 | | for (int i = 0; i < loadedScenes.Count; i++) |
| | 64 | | { |
| 3 | 65 | | scene = loadedScenes[i]; |
| | 66 | |
|
| 3 | 67 | | var transform = TransformHelper.SetTransform(scene, ref cameraPosition, ref cameraRotation, ref worldOff |
| 3 | 68 | | componentsWriter.PutComponent(scene.sceneData.id, SpecialEntityId.CAMERA_ENTITY, ComponentID.TRANSFORM, |
| | 69 | | transform); |
| | 70 | | } |
| 3 | 71 | | } |
| | 72 | | } |
| | 73 | | } |