From 98acb6621c0362f430aa067f7a20bc9a7bf4e0d4 Mon Sep 17 00:00:00 2001
From: Palnit <petranyibalint@gmail.com>
Date: Thu, 25 May 2023 10:58:07 +0200
Subject: [PATCH] Camera Zoom

---
 ChaoticCrates/Assets/Scenes/gameScene.unity   | 12 ++-
 .../GameEventSystem/GameEventSystem.cs        |  7 ++
 .../Scripts/Logic/MultiPlayer/SceneLoader.cs  |  1 +
 .../Scripts/UI/CameraMovements/CameraDrag.cs  | 88 +++++++------------
 .../UI/CameraMovements/UI.CameraDrag.asmdef   |  3 +-
 5 files changed, 49 insertions(+), 62 deletions(-)

diff --git a/ChaoticCrates/Assets/Scenes/gameScene.unity b/ChaoticCrates/Assets/Scenes/gameScene.unity
index 0ca84de..fccda35 100644
--- a/ChaoticCrates/Assets/Scenes/gameScene.unity
+++ b/ChaoticCrates/Assets/Scenes/gameScene.unity
@@ -748,9 +748,7 @@ MonoBehaviour:
   m_OnCullStateChanged:
     m_PersistentCalls:
       m_Calls: []
-  m_text: 'Input is valid:\n\t Sending: wait\nwait <color=green>Successful</color>\n
-
-'
+  m_text: 
   m_isRightToLeft: 0
   m_fontAsset: {fileID: 11400000, guid: c0376ea05027a2f47a34d11cd830abb4, type: 2}
   m_sharedMaterial: {fileID: -1435834765465814540, guid: c0376ea05027a2f47a34d11cd830abb4, type: 2}
@@ -78804,11 +78802,11 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 50b66d1379fa44e1855e47e5ca0e3eb4, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
+  zoomChange: 50
+  smoothChange: 0.7
+  minSize: 4
+  maxSize: 17
   cameraDragging: 0
-  outerLeftx: -10
-  outerRightx: 10
-  outerLefty: -10
-  outerRighty: 10
 --- !u!114 &875563103
 MonoBehaviour:
   m_ObjectHideFlags: 0
diff --git a/ChaoticCrates/Assets/Scripts/GameEventSystem/GameEventSystem.cs b/ChaoticCrates/Assets/Scripts/GameEventSystem/GameEventSystem.cs
index c853fb5..9469843 100644
--- a/ChaoticCrates/Assets/Scripts/GameEventSystem/GameEventSystem.cs
+++ b/ChaoticCrates/Assets/Scripts/GameEventSystem/GameEventSystem.cs
@@ -41,4 +41,11 @@ public static class GameEventSystem
     {
         SentAction?.Invoke(obj,valid);
     }
+
+    public static event Action CameraPosition;
+
+    public static void OnCameraPosition()
+    {
+        CameraPosition?.Invoke();
+    }
 }
diff --git a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/SceneLoader.cs b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/SceneLoader.cs
index 7d802a5..5eeffa5 100644
--- a/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/SceneLoader.cs
+++ b/ChaoticCrates/Assets/Scripts/Logic/MultiPlayer/SceneLoader.cs
@@ -76,6 +76,7 @@ namespace Logic.MultiPlayer
         {
             yield return StartCoroutine(GetMapProgeress());
             GameEventSystem.OnIsLoading(false);
+            GameEventSystem.OnCameraPosition();
             GameEventSystem.OnStartCountDown();
         }
         
diff --git a/ChaoticCrates/Assets/Scripts/UI/CameraMovements/CameraDrag.cs b/ChaoticCrates/Assets/Scripts/UI/CameraMovements/CameraDrag.cs
index 9d30faf..b61a91d 100644
--- a/ChaoticCrates/Assets/Scripts/UI/CameraMovements/CameraDrag.cs
+++ b/ChaoticCrates/Assets/Scripts/UI/CameraMovements/CameraDrag.cs
@@ -14,17 +14,32 @@ namespace UI.CameraMovements
     public class CameraDrag : MonoBehaviour
     {
         private Vector3 _dragOrigin;
+        public float zoomChange;
+        public float smoothChange;
+        public float minSize;
+        public float maxSize;
+
+        private Camera cam;
 
         [HideInInspector]
         public bool cameraDragging = false;
-       
-        public float outerLeftx = -10f;
-        public float outerRightx = 10f;
-     
-     
-        public float outerLefty = -10f;
-        public float outerRighty = 10f;
 
+        private void Start()
+        {
+            GameEventSystem.CameraPosition += CenterCamera;
+            cam = GetComponent<Camera>();
+        }
+        
+
+
+        private void CenterCamera()
+        {
+            var playerTransform = NetworkManager.Singleton.LocalClient.PlayerObject.transform;
+            transform.position =
+                new Vector3(playerTransform.position.x,
+                    playerTransform.position.y,
+                    transform.position.z);
+        }
 
         public void OnDrag(InputAction.CallbackContext callbackContext)
         {
@@ -39,24 +54,25 @@ namespace UI.CameraMovements
 
         void Update()
         {
-            float wLeft = Screen.width * 0.2f;
-            float wRight = Screen.width - (Screen.width * 0.2f);
-            
-            float hUp = Screen.height * 0.2f;
-            float hDown = Screen.height - (Screen.height * 0.2f);
 
             if (Input.GetKeyDown(KeyCode.Space))
             {
 
                 if (GameNetworkManager.Instance.isSpec) { return;}
 
-                var playerTransform = NetworkManager.Singleton.LocalClient.PlayerObject.transform;
-                transform.position =
-                    new Vector3(playerTransform.position.x,
-                        playerTransform.position.y,
-                        transform.position.z);
+                CenterCamera();
             }
-            
+
+            if (Input.mouseScrollDelta.y > 0)
+            {
+                cam.orthographicSize -= zoomChange * Time.deltaTime * smoothChange;
+            }
+            if (Input.mouseScrollDelta.y < 0)
+            {
+                cam.orthographicSize += zoomChange * Time.deltaTime * smoothChange;
+            }
+
+            cam.orthographicSize = Mathf.Clamp(cam.orthographicSize, minSize, maxSize);
         }
 
         private void LateUpdate()
@@ -67,42 +83,6 @@ namespace UI.CameraMovements
             Vector3 move = _dragOrigin - difference;
             move.z = -10;
 
-            /*if (move.x > 0f && move.y > 0f)
-            {
-                if (this.transform.position.x < outerRightx && this.transform.position.y < outerRighty)
-                {
-                    transform.position = move;
-                }
-                     
-            }
- 
-            if (move.x > 0f && move.y <= 0f)
-            {
-                if (this.transform.position.x < outerRightx && this.transform.position.y > outerLefty)
-                {
-                    transform.position = move;
- 
-                }
-            }
-                 
-            if (move.x <= 0f && move.y > 0f)
-            {
-                if (this.transform.position.x > outerLeftx && this.transform.position.y < outerRighty)
-                {
-                    transform.position = move;
- 
-                }
-            }
-                 
-            if (move.x <= 0f && move.y <= 0f)
-            {
-                if (this.transform.position.x > outerLeftx && this.transform.position.y > outerLefty)
-                {
-                    transform.position = move;
- 
-                }
-            }*/
-            
             transform.position = move;
         }
     }
diff --git a/ChaoticCrates/Assets/Scripts/UI/CameraMovements/UI.CameraDrag.asmdef b/ChaoticCrates/Assets/Scripts/UI/CameraMovements/UI.CameraDrag.asmdef
index 0c81c50..979e2b3 100644
--- a/ChaoticCrates/Assets/Scripts/UI/CameraMovements/UI.CameraDrag.asmdef
+++ b/ChaoticCrates/Assets/Scripts/UI/CameraMovements/UI.CameraDrag.asmdef
@@ -6,7 +6,8 @@
         "GUID:5a1586fb7f87e0c40b04ae28ae921b2a",
         "GUID:3b8ed52f1b5c64994af4c4e0aa4b6c4b",
         "GUID:1491147abca9d7d4bb7105af628b223e",
-        "GUID:75469ad4d38634e559750d17036d5f7c"
+        "GUID:75469ad4d38634e559750d17036d5f7c",
+        "GUID:cdc6002414c29684683b7acf4e11ca65"
     ],
     "includePlatforms": [],
     "excludePlatforms": [],
-- 
GitLab