MapCameraController.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. public class MapCameraController : MonoBehaviour
  4. {
  5. [Header("Camera Movement")]
  6. public float moveSpeed = 10f;
  7. public float zoomSpeed = 5f;
  8. public float minZoom = 5f;
  9. public float maxZoom = 100f; // Increased for better map overview
  10. public float initialZoom = 35f; // Starting zoom level to see more of the map
  11. [Header("Team Marker Focus")]
  12. public KeyCode centerOnTeamKey = KeyCode.C;
  13. public float focusZoomLevel = 15f; // Zoom level when focusing on team marker
  14. public float centeringSpeed = 2f; // Speed of camera movement when centering
  15. private Camera cam;
  16. private Vector3 targetPosition;
  17. private float targetSize;
  18. private SimpleTeamPlacement teamPlacement;
  19. void Start()
  20. {
  21. cam = GetComponent<Camera>();
  22. if (cam == null)
  23. cam = Camera.main;
  24. // Set camera to look straight down
  25. transform.rotation = Quaternion.Euler(90f, 0f, 0f);
  26. // Position camera above the center of the map (assuming 100x100 map)
  27. transform.position = new Vector3(50f, 30f, 50f);
  28. targetPosition = transform.position;
  29. targetSize = initialZoom;
  30. cam.orthographicSize = initialZoom;
  31. // Ensure camera is orthographic for top-down view
  32. cam.orthographic = true;
  33. // Find team placement component
  34. teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  35. }
  36. void Update()
  37. {
  38. // Handle team marker centering
  39. if (Input.GetKeyDown(centerOnTeamKey))
  40. {
  41. CenterOnTeamMarker();
  42. }
  43. // Handle zoom to full map
  44. if (Input.GetKeyDown(KeyCode.F))
  45. {
  46. ZoomToFullMap();
  47. }
  48. // Show help
  49. if (Input.GetKeyDown(KeyCode.H))
  50. {
  51. Debug.Log("💡 Camera Controls:\n" +
  52. $"- {centerOnTeamKey}: Center on team marker\n" +
  53. "- F: Zoom to full map\n" +
  54. "- WASD/Arrows: Move camera\n" +
  55. "- Mouse wheel: Zoom");
  56. }
  57. // Only handle camera input if mouse is within the map area
  58. if (IsMouseInMapArea())
  59. {
  60. HandleMovement();
  61. HandleZoom();
  62. }
  63. // Always smooth camera movement (even if input is disabled)
  64. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * centeringSpeed);
  65. cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetSize, Time.deltaTime * centeringSpeed);
  66. }
  67. /// <summary>
  68. /// Center the camera on the team marker position
  69. /// </summary>
  70. public void CenterOnTeamMarker()
  71. {
  72. if (teamPlacement == null)
  73. {
  74. teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  75. }
  76. if (teamPlacement != null && teamPlacement.IsTeamPlaced())
  77. {
  78. GameObject teamMarker = teamPlacement.GetTeamMarker();
  79. if (teamMarker != null)
  80. {
  81. Vector3 teamPos = teamMarker.transform.position;
  82. // Set target position to center on team marker
  83. targetPosition = new Vector3(teamPos.x, transform.position.y, teamPos.z);
  84. // Optionally adjust zoom level for better focus
  85. targetSize = focusZoomLevel;
  86. Debug.Log($"🎯 Centering camera on team marker at position: {teamPos}");
  87. }
  88. else
  89. {
  90. Debug.LogWarning("⚠️ Team marker object not found!");
  91. }
  92. }
  93. else
  94. {
  95. Debug.LogWarning("⚠️ Team placement not found or team not placed!");
  96. }
  97. }
  98. /// <summary>
  99. /// Zoom out to show the entire map
  100. /// </summary>
  101. public void ZoomToFullMap()
  102. {
  103. // Find the MapMaker2 component to get map dimensions
  104. MapMaker2 mapMaker = FindFirstObjectByType<MapMaker2>();
  105. if (mapMaker != null)
  106. {
  107. var mapData = mapMaker.GetMapData();
  108. if (mapData != null)
  109. {
  110. // Calculate map center
  111. Vector3 mapCenter = new Vector3(
  112. mapData.Width / 2f,
  113. transform.position.y,
  114. mapData.Height / 2f
  115. );
  116. // Calculate required zoom to fit entire map
  117. float mapSize = Mathf.Max(mapData.Width, mapData.Height);
  118. float requiredZoom = mapSize * 0.6f; // Add some padding
  119. targetPosition = mapCenter;
  120. targetSize = Mathf.Clamp(requiredZoom, minZoom, maxZoom);
  121. Debug.Log($"🗺️ Zooming to full map: Size {mapData.Width}x{mapData.Height}, Zoom: {targetSize}");
  122. }
  123. }
  124. }
  125. void HandleMovement()
  126. {
  127. Vector3 moveDirection = Vector3.zero;
  128. // WASD or Arrow Keys movement
  129. if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
  130. moveDirection.z += 1f;
  131. if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
  132. moveDirection.z -= 1f;
  133. if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
  134. moveDirection.x -= 1f;
  135. if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
  136. moveDirection.x += 1f;
  137. // Apply movement
  138. targetPosition += moveDirection.normalized * moveSpeed * Time.deltaTime;
  139. // Keep camera within reasonable bounds (adjust based on your map size)
  140. targetPosition.x = Mathf.Clamp(targetPosition.x, -20f, 120f);
  141. targetPosition.z = Mathf.Clamp(targetPosition.z, -20f, 120f);
  142. }
  143. void HandleZoom()
  144. {
  145. float scroll = Input.GetAxis("Mouse ScrollWheel");
  146. if (scroll != 0f)
  147. {
  148. targetSize -= scroll * zoomSpeed;
  149. targetSize = Mathf.Clamp(targetSize, minZoom, maxZoom);
  150. }
  151. }
  152. /// <summary>
  153. /// Check if the mouse is currently within the map area (not in UI panels)
  154. /// </summary>
  155. /// <returns>True if mouse is in the map area, false if in UI areas</returns>
  156. private bool IsMouseInMapArea()
  157. {
  158. Vector2 mousePosition = Input.mousePosition;
  159. // Define the map area bounds (adjust these values based on your UI layout)
  160. // These values represent the approximate red square area from your image
  161. float leftUIWidth = 150f; // Map Legend panel width
  162. float rightUIWidth = 300f; // Your Team panel width
  163. float topUIHeight = 0f; // No top UI currently
  164. float bottomUIHeight = 0f; // No bottom UI currently
  165. // Calculate the actual map area
  166. float mapLeft = leftUIWidth;
  167. float mapRight = Screen.width - rightUIWidth;
  168. float mapTop = Screen.height - topUIHeight;
  169. float mapBottom = bottomUIHeight;
  170. // Check if mouse is within the map area
  171. bool inMapArea = mousePosition.x >= mapLeft &&
  172. mousePosition.x <= mapRight &&
  173. mousePosition.y >= mapBottom &&
  174. mousePosition.y <= mapTop;
  175. return inMapArea;
  176. }
  177. }