TeamMarkerCameraController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using UnityEngine;
  2. /// <summary>
  3. /// Universal camera controller for focusing on team marker and managing map overview.
  4. /// Can be attached to any camera to provide team marker centering functionality.
  5. /// </summary>
  6. public class TeamMarkerCameraController : MonoBehaviour
  7. {
  8. [Header("Team Marker Focus Settings")]
  9. [Tooltip("Key to center camera on team marker")]
  10. public KeyCode centerOnTeamKey = KeyCode.C;
  11. [Tooltip("Key to zoom out to show full map")]
  12. public KeyCode zoomToFullMapKey = KeyCode.F;
  13. [Tooltip("Key to show help information")]
  14. public KeyCode showHelpKey = KeyCode.H;
  15. [Header("Movement Settings")]
  16. [Tooltip("Speed of camera movement when centering")]
  17. [Range(0.5f, 5f)]
  18. public float centeringSpeed = 2f;
  19. [Tooltip("Zoom level when focusing on team marker")]
  20. [Range(5f, 50f)]
  21. public float focusZoomLevel = 15f;
  22. [Tooltip("Maximum zoom out level for full map view")]
  23. [Range(50f, 300f)]
  24. public float maxZoomOut = 200f;
  25. [Header("Auto-Center Settings")]
  26. [Tooltip("Automatically center on team marker when it's first placed")]
  27. public bool autoCenterOnTeamPlacement = true;
  28. [Tooltip("Delay before auto-centering (allows map to generate first)")]
  29. [Range(0f, 5f)]
  30. public float autoCenterDelay = 2f;
  31. // Private variables
  32. private Camera cam;
  33. private Vector3 targetPosition;
  34. private float targetZoom;
  35. private bool isMovingToTarget = false;
  36. private SimpleTeamPlacement teamPlacement;
  37. private bool hasAutocentered = false;
  38. void Start()
  39. {
  40. cam = GetComponent<Camera>();
  41. if (cam == null)
  42. cam = Camera.main;
  43. // Initialize target values
  44. targetPosition = transform.position;
  45. targetZoom = cam.orthographicSize;
  46. // Find team placement component
  47. teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  48. // Auto-center on team marker after delay if enabled
  49. if (autoCenterOnTeamPlacement)
  50. {
  51. Invoke(nameof(AutoCenterOnTeamMarker), autoCenterDelay);
  52. }
  53. }
  54. void Update()
  55. {
  56. // Handle input
  57. HandleInput();
  58. // Handle smooth movement to target
  59. if (isMovingToTarget)
  60. {
  61. HandleSmoothMovement();
  62. }
  63. }
  64. private void HandleInput()
  65. {
  66. // Center on team marker
  67. if (Input.GetKeyDown(centerOnTeamKey))
  68. {
  69. CenterOnTeamMarker();
  70. }
  71. // Zoom to full map
  72. if (Input.GetKeyDown(zoomToFullMapKey))
  73. {
  74. ZoomToFullMap();
  75. }
  76. // Show help
  77. if (Input.GetKeyDown(showHelpKey))
  78. {
  79. ShowHelp();
  80. }
  81. }
  82. private void HandleSmoothMovement()
  83. {
  84. // Smoothly move camera to target position and zoom
  85. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * centeringSpeed);
  86. cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetZoom, Time.deltaTime * centeringSpeed);
  87. // Check if we're close enough to stop moving
  88. float positionDistance = Vector3.Distance(transform.position, targetPosition);
  89. float zoomDistance = Mathf.Abs(cam.orthographicSize - targetZoom);
  90. if (positionDistance < 0.5f && zoomDistance < 0.5f)
  91. {
  92. isMovingToTarget = false;
  93. Debug.Log("🎯 Camera movement complete");
  94. }
  95. }
  96. /// <summary>
  97. /// Center the camera on the team marker position
  98. /// </summary>
  99. public void CenterOnTeamMarker()
  100. {
  101. if (teamPlacement == null)
  102. {
  103. teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  104. }
  105. if (teamPlacement != null && teamPlacement.IsTeamPlaced())
  106. {
  107. GameObject teamMarker = teamPlacement.GetTeamMarker();
  108. if (teamMarker != null)
  109. {
  110. Vector3 teamPos = teamMarker.transform.position;
  111. // Set target position to center on team marker (preserve camera height)
  112. targetPosition = new Vector3(teamPos.x, transform.position.y, teamPos.z);
  113. targetZoom = focusZoomLevel;
  114. isMovingToTarget = true;
  115. Debug.Log($"🎯 Centering camera on team marker at position: {teamPos}");
  116. }
  117. else
  118. {
  119. Debug.LogWarning("⚠️ Team marker object not found!");
  120. }
  121. }
  122. else
  123. {
  124. Debug.LogWarning("⚠️ Team placement not found or team not placed!");
  125. }
  126. }
  127. /// <summary>
  128. /// Zoom out to show the entire map
  129. /// </summary>
  130. public void ZoomToFullMap()
  131. {
  132. // Try to find MapMaker2 first
  133. MapMaker2 mapMaker = FindFirstObjectByType<MapMaker2>();
  134. if (mapMaker != null)
  135. {
  136. var mapData = mapMaker.GetMapData();
  137. if (mapData != null)
  138. {
  139. SetFullMapView(mapData.Width, mapData.Height);
  140. return;
  141. }
  142. }
  143. // Last resort: Use a reasonable default size
  144. Debug.LogWarning("⚠️ Could not find map data, using default map size for full view");
  145. SetFullMapView(100, 100);
  146. }
  147. private void SetFullMapView(int mapWidth, int mapHeight)
  148. {
  149. // Calculate map center
  150. targetPosition = new Vector3(
  151. mapWidth / 2f,
  152. transform.position.y,
  153. mapHeight / 2f
  154. );
  155. // Calculate required zoom to fit entire map with some padding
  156. float mapSize = Mathf.Max(mapWidth, mapHeight);
  157. targetZoom = Mathf.Min(mapSize * 0.8f, maxZoomOut);
  158. isMovingToTarget = true;
  159. Debug.Log($"🗺️ Zooming to full map: Size {mapWidth}x{mapHeight}, Zoom: {targetZoom}");
  160. }
  161. /// <summary>
  162. /// Auto-center on team marker (called after delay)
  163. /// </summary>
  164. private void AutoCenterOnTeamMarker()
  165. {
  166. if (!hasAutocentered && autoCenterOnTeamPlacement)
  167. {
  168. hasAutocentered = true;
  169. CenterOnTeamMarker();
  170. }
  171. }
  172. /// <summary>
  173. /// Show help information in console
  174. /// </summary>
  175. private void ShowHelp()
  176. {
  177. Debug.Log("🎮 Team Marker Camera Controls:\n" +
  178. $"- {centerOnTeamKey}: Center camera on team marker\n" +
  179. $"- {zoomToFullMapKey}: Zoom out to show full map\n" +
  180. $"- {showHelpKey}: Show this help\n" +
  181. "- Mouse wheel: Zoom in/out\n" +
  182. "- WASD/Arrow keys: Move camera (if supported by main controller)\n" +
  183. "- Middle mouse: Drag camera (if supported by main controller)");
  184. }
  185. /// <summary>
  186. /// Stop any automatic camera movement (useful when user wants manual control)
  187. /// </summary>
  188. public void StopAutoMovement()
  189. {
  190. isMovingToTarget = false;
  191. targetPosition = transform.position;
  192. targetZoom = cam.orthographicSize;
  193. }
  194. /// <summary>
  195. /// Force update team placement reference (useful if team placement changes)
  196. /// </summary>
  197. public void RefreshTeamPlacement()
  198. {
  199. teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  200. }
  201. void OnEnable()
  202. {
  203. // Show brief help when component is enabled
  204. Invoke(nameof(ShowBriefHelp), 1f);
  205. }
  206. private void ShowBriefHelp()
  207. {
  208. Debug.Log($"🎯 Team Marker Camera Controller enabled! Press {showHelpKey} for help.");
  209. }
  210. }