MapCameraController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // Only handle camera input if mouse is within the map area
  49. if (IsMouseInMapArea())
  50. {
  51. HandleMovement();
  52. HandleZoom();
  53. }
  54. // Always smooth camera movement (even if input is disabled)
  55. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * centeringSpeed);
  56. cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetSize, Time.deltaTime * centeringSpeed);
  57. }
  58. /// <summary>
  59. /// Center the camera on the team marker position
  60. /// </summary>
  61. public void CenterOnTeamMarker()
  62. {
  63. if (teamPlacement == null)
  64. {
  65. teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  66. }
  67. if (teamPlacement != null && teamPlacement.IsTeamPlaced())
  68. {
  69. GameObject teamMarker = teamPlacement.GetTeamMarker();
  70. if (teamMarker != null)
  71. {
  72. Vector3 teamPos = teamMarker.transform.position;
  73. // Set target position to center on team marker
  74. targetPosition = new Vector3(teamPos.x, transform.position.y, teamPos.z);
  75. // Optionally adjust zoom level for better focus
  76. targetSize = focusZoomLevel;
  77. }
  78. }
  79. else
  80. {
  81. Debug.LogWarning("⚠️ Team placement not found or team not placed!");
  82. }
  83. }
  84. /// <summary>
  85. /// Zoom out to show the entire map
  86. /// </summary>
  87. public void ZoomToFullMap()
  88. {
  89. // Find the MapMaker2 component to get map dimensions
  90. MapMaker2 mapMaker = FindFirstObjectByType<MapMaker2>();
  91. if (mapMaker != null)
  92. {
  93. var mapData = mapMaker.GetMapData();
  94. if (mapData != null)
  95. {
  96. // Calculate map center
  97. Vector3 mapCenter = new Vector3(
  98. mapData.Width / 2f,
  99. transform.position.y,
  100. mapData.Height / 2f
  101. );
  102. // Calculate required zoom to fit entire map
  103. float mapSize = Mathf.Max(mapData.Width, mapData.Height);
  104. float requiredZoom = mapSize * 0.6f; // Add some padding
  105. targetPosition = mapCenter;
  106. targetSize = Mathf.Clamp(requiredZoom, minZoom, maxZoom);
  107. }
  108. }
  109. }
  110. void HandleMovement()
  111. {
  112. Vector3 moveDirection = Vector3.zero;
  113. // WASD or Arrow Keys movement
  114. if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
  115. moveDirection.z += 1f;
  116. if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
  117. moveDirection.z -= 1f;
  118. if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
  119. moveDirection.x -= 1f;
  120. if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
  121. moveDirection.x += 1f;
  122. // Apply movement
  123. targetPosition += moveDirection.normalized * moveSpeed * Time.deltaTime;
  124. // Keep camera within reasonable bounds (adjust based on your map size)
  125. targetPosition.x = Mathf.Clamp(targetPosition.x, -20f, 120f);
  126. targetPosition.z = Mathf.Clamp(targetPosition.z, -20f, 120f);
  127. }
  128. void HandleZoom()
  129. {
  130. float scroll = Input.GetAxis("Mouse ScrollWheel");
  131. if (scroll != 0f)
  132. {
  133. targetSize -= scroll * zoomSpeed;
  134. targetSize = Mathf.Clamp(targetSize, minZoom, maxZoom);
  135. }
  136. }
  137. /// <summary>
  138. /// Check if the mouse is currently within the map area (not in UI panels)
  139. /// </summary>
  140. /// <returns>True if mouse is in the map area, false if in UI areas</returns>
  141. private bool IsMouseInMapArea()
  142. {
  143. Vector2 mousePosition = Input.mousePosition;
  144. // Define the map area bounds (adjust these values based on your UI layout)
  145. // These values represent the approximate red square area from your image
  146. float leftUIWidth = 150f; // Map Legend panel width
  147. float rightUIWidth = 300f; // Your Team panel width
  148. float topUIHeight = 0f; // No top UI currently
  149. float bottomUIHeight = 0f; // No bottom UI currently
  150. // Calculate the actual map area
  151. float mapLeft = leftUIWidth;
  152. float mapRight = Screen.width - rightUIWidth;
  153. float mapTop = Screen.height - topUIHeight;
  154. float mapBottom = bottomUIHeight;
  155. // Check if mouse is within the map area
  156. bool inMapArea = mousePosition.x >= mapLeft &&
  157. mousePosition.x <= mapRight &&
  158. mousePosition.y >= mapBottom &&
  159. mousePosition.y <= mapTop;
  160. return inMapArea;
  161. }
  162. }