TeamMarkerCameraController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. }
  77. private void HandleSmoothMovement()
  78. {
  79. // Smoothly move camera to target position and zoom
  80. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * centeringSpeed);
  81. cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetZoom, Time.deltaTime * centeringSpeed);
  82. // Check if we're close enough to stop moving
  83. float positionDistance = Vector3.Distance(transform.position, targetPosition);
  84. float zoomDistance = Mathf.Abs(cam.orthographicSize - targetZoom);
  85. if (positionDistance < 0.5f && zoomDistance < 0.5f)
  86. {
  87. isMovingToTarget = false;
  88. }
  89. }
  90. /// <summary>
  91. /// Center the camera on the team marker position
  92. /// </summary>
  93. public void CenterOnTeamMarker()
  94. {
  95. if (teamPlacement == null)
  96. {
  97. teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  98. }
  99. if (teamPlacement != null && teamPlacement.IsTeamPlaced())
  100. {
  101. GameObject teamMarker = teamPlacement.GetTeamMarker();
  102. if (teamMarker != null)
  103. {
  104. Vector3 teamPos = teamMarker.transform.position;
  105. // Set target position to center on team marker (preserve camera height)
  106. targetPosition = new Vector3(teamPos.x, transform.position.y, teamPos.z);
  107. targetZoom = focusZoomLevel;
  108. isMovingToTarget = true;
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// Zoom out to show the entire map
  114. /// </summary>
  115. public void ZoomToFullMap()
  116. {
  117. // Try to find MapMaker2 first
  118. MapMaker2 mapMaker = FindFirstObjectByType<MapMaker2>();
  119. if (mapMaker != null)
  120. {
  121. var mapData = mapMaker.GetMapData();
  122. if (mapData != null)
  123. {
  124. SetFullMapView(mapData.Width, mapData.Height);
  125. return;
  126. }
  127. }
  128. // Last resort: Use a reasonable default size
  129. SetFullMapView(100, 100);
  130. }
  131. private void SetFullMapView(int mapWidth, int mapHeight)
  132. {
  133. // Calculate map center
  134. targetPosition = new Vector3(
  135. mapWidth / 2f,
  136. transform.position.y,
  137. mapHeight / 2f
  138. );
  139. // Calculate required zoom to fit entire map with some padding
  140. float mapSize = Mathf.Max(mapWidth, mapHeight);
  141. targetZoom = Mathf.Min(mapSize * 0.8f, maxZoomOut);
  142. isMovingToTarget = true;
  143. }
  144. /// <summary>
  145. /// Auto-center on team marker (called after delay)
  146. /// </summary>
  147. private void AutoCenterOnTeamMarker()
  148. {
  149. if (!hasAutocentered && autoCenterOnTeamPlacement)
  150. {
  151. hasAutocentered = true;
  152. CenterOnTeamMarker();
  153. }
  154. }
  155. /// <summary>
  156. /// Stop any automatic camera movement (useful when user wants manual control)
  157. /// </summary>
  158. public void StopAutoMovement()
  159. {
  160. isMovingToTarget = false;
  161. targetPosition = transform.position;
  162. targetZoom = cam.orthographicSize;
  163. }
  164. /// <summary>
  165. /// Force update team placement reference (useful if team placement changes)
  166. /// </summary>
  167. public void RefreshTeamPlacement()
  168. {
  169. teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  170. }
  171. }