| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using UnityEngine;
- /// <summary>
- /// Universal camera controller for focusing on team marker and managing map overview.
- /// Can be attached to any camera to provide team marker centering functionality.
- /// </summary>
- public class TeamMarkerCameraController : MonoBehaviour
- {
- [Header("Team Marker Focus Settings")]
- [Tooltip("Key to center camera on team marker")]
- public KeyCode centerOnTeamKey = KeyCode.C;
- [Tooltip("Key to zoom out to show full map")]
- public KeyCode zoomToFullMapKey = KeyCode.F;
- [Tooltip("Key to show help information")]
- public KeyCode showHelpKey = KeyCode.H;
- [Header("Movement Settings")]
- [Tooltip("Speed of camera movement when centering")]
- [Range(0.5f, 5f)]
- public float centeringSpeed = 2f;
- [Tooltip("Zoom level when focusing on team marker")]
- [Range(5f, 50f)]
- public float focusZoomLevel = 15f;
- [Tooltip("Maximum zoom out level for full map view")]
- [Range(50f, 300f)]
- public float maxZoomOut = 200f;
- [Header("Auto-Center Settings")]
- [Tooltip("Automatically center on team marker when it's first placed")]
- public bool autoCenterOnTeamPlacement = true;
- [Tooltip("Delay before auto-centering (allows map to generate first)")]
- [Range(0f, 5f)]
- public float autoCenterDelay = 2f;
- // Private variables
- private Camera cam;
- private Vector3 targetPosition;
- private float targetZoom;
- private bool isMovingToTarget = false;
- private SimpleTeamPlacement teamPlacement;
- private bool hasAutocentered = false;
- void Start()
- {
- cam = GetComponent<Camera>();
- if (cam == null)
- cam = Camera.main;
- // Initialize target values
- targetPosition = transform.position;
- targetZoom = cam.orthographicSize;
- // Find team placement component
- teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
- // Auto-center on team marker after delay if enabled
- if (autoCenterOnTeamPlacement)
- {
- Invoke(nameof(AutoCenterOnTeamMarker), autoCenterDelay);
- }
- }
- void Update()
- {
- // Handle input
- HandleInput();
- // Handle smooth movement to target
- if (isMovingToTarget)
- {
- HandleSmoothMovement();
- }
- }
- private void HandleInput()
- {
- // Center on team marker
- if (Input.GetKeyDown(centerOnTeamKey))
- {
- CenterOnTeamMarker();
- }
- // Zoom to full map
- if (Input.GetKeyDown(zoomToFullMapKey))
- {
- ZoomToFullMap();
- }
- }
- private void HandleSmoothMovement()
- {
- // Smoothly move camera to target position and zoom
- transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * centeringSpeed);
- cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetZoom, Time.deltaTime * centeringSpeed);
- // Check if we're close enough to stop moving
- float positionDistance = Vector3.Distance(transform.position, targetPosition);
- float zoomDistance = Mathf.Abs(cam.orthographicSize - targetZoom);
- if (positionDistance < 0.5f && zoomDistance < 0.5f)
- {
- isMovingToTarget = false;
- }
- }
- /// <summary>
- /// Center the camera on the team marker position
- /// </summary>
- public void CenterOnTeamMarker()
- {
- if (teamPlacement == null)
- {
- teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
- }
- if (teamPlacement != null && teamPlacement.IsTeamPlaced())
- {
- GameObject teamMarker = teamPlacement.GetTeamMarker();
- if (teamMarker != null)
- {
- Vector3 teamPos = teamMarker.transform.position;
- // Set target position to center on team marker (preserve camera height)
- targetPosition = new Vector3(teamPos.x, transform.position.y, teamPos.z);
- targetZoom = focusZoomLevel;
- isMovingToTarget = true;
- }
- }
- }
- /// <summary>
- /// Zoom out to show the entire map
- /// </summary>
- public void ZoomToFullMap()
- {
- // Try to find MapMaker2 first
- MapMaker2 mapMaker = FindFirstObjectByType<MapMaker2>();
- if (mapMaker != null)
- {
- var mapData = mapMaker.GetMapData();
- if (mapData != null)
- {
- SetFullMapView(mapData.Width, mapData.Height);
- return;
- }
- }
- // Last resort: Use a reasonable default size
- SetFullMapView(100, 100);
- }
- private void SetFullMapView(int mapWidth, int mapHeight)
- {
- // Calculate map center
- targetPosition = new Vector3(
- mapWidth / 2f,
- transform.position.y,
- mapHeight / 2f
- );
- // Calculate required zoom to fit entire map with some padding
- float mapSize = Mathf.Max(mapWidth, mapHeight);
- targetZoom = Mathf.Min(mapSize * 0.8f, maxZoomOut);
- isMovingToTarget = true;
- }
- /// <summary>
- /// Auto-center on team marker (called after delay)
- /// </summary>
- private void AutoCenterOnTeamMarker()
- {
- if (!hasAutocentered && autoCenterOnTeamPlacement)
- {
- hasAutocentered = true;
- CenterOnTeamMarker();
- }
- }
- /// <summary>
- /// Stop any automatic camera movement (useful when user wants manual control)
- /// </summary>
- public void StopAutoMovement()
- {
- isMovingToTarget = false;
- targetPosition = transform.position;
- targetZoom = cam.orthographicSize;
- }
- /// <summary>
- /// Force update team placement reference (useful if team placement changes)
- /// </summary>
- public void RefreshTeamPlacement()
- {
- teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
- }
- }
|