Fix up eagle eye's weirdness, add option to auto clear saves
This commit is contained in:
@@ -1,84 +1,131 @@
|
||||
using System.Collections;
|
||||
using Unity.Cinemachine;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class EagleEyeBehaviour : MonoBehaviour
|
||||
namespace DamianExperiments
|
||||
{
|
||||
[SerializeField] private CinemachineCamera virtualCamera;
|
||||
[SerializeField] private CinemachineConfiner2D confiner2D;
|
||||
[SerializeField] private float zoomOutOrthoSize = 30f;
|
||||
[SerializeField] private float normalOrthoSize = 15f;
|
||||
private float currentOrthoSize;
|
||||
[SerializeField] private float transitionDuration = 0.5f; // Duration of the transition
|
||||
[SerializeField] private float eagleEyeDuration = 3f; // Duration to stay zoomed out
|
||||
[SerializeField] private bool disablePlayerInputDuringEagleEye = true; // Gate input disable
|
||||
[SerializeField] private UnityEngine.UI.Button eagleEyeButton; // Reference to the UI button
|
||||
|
||||
private Coroutine zoomCoroutine;
|
||||
|
||||
public void ActivateEagleEye()
|
||||
public class EagleEyeBehaviour : MonoBehaviour
|
||||
{
|
||||
//Assigns the Virtual Camera and Confiner if not already assigned
|
||||
if (virtualCamera == null)
|
||||
{
|
||||
virtualCamera = FindAnyObjectByType<CinemachineCamera>();
|
||||
}
|
||||
if (confiner2D == null)
|
||||
{
|
||||
confiner2D = virtualCamera.GetComponent<CinemachineConfiner2D>();
|
||||
}
|
||||
// Serialized backing fields allow manual assignment in the inspector
|
||||
private GameObject _cinecameraObject;
|
||||
private CinemachineCamera _virtualCamera;
|
||||
private CinemachineConfiner2D _confiner2D;
|
||||
|
||||
// Implementation for activating eagle eye behaviour
|
||||
if (disablePlayerInputDuringEagleEye)
|
||||
// Lazy-fetched properties: if null, try to find the GameObject tagged "MainCinemachineCamera"
|
||||
private CinemachineCamera VirtualCamera
|
||||
{
|
||||
currentOrthoSize = virtualCamera.Lens.OrthographicSize;
|
||||
}
|
||||
if (eagleEyeButton != null)
|
||||
{
|
||||
eagleEyeButton.interactable = false;
|
||||
}
|
||||
if (zoomCoroutine != null) StopCoroutine(zoomCoroutine);
|
||||
zoomCoroutine = StartCoroutine(EagleEyeSequence());
|
||||
}
|
||||
|
||||
private IEnumerator EagleEyeSequence()
|
||||
{
|
||||
if (disablePlayerInputDuringEagleEye)
|
||||
{
|
||||
Core.GameManager.Instance.RequestPause(this); // Disable player input
|
||||
}
|
||||
yield return StartCoroutine(SmoothOrthoSize(virtualCamera, zoomOutOrthoSize, transitionDuration));
|
||||
yield return new WaitForSeconds(eagleEyeDuration);
|
||||
float zoomInTarget = disablePlayerInputDuringEagleEye ? currentOrthoSize : normalOrthoSize;
|
||||
yield return StartCoroutine(SmoothOrthoSize(virtualCamera, zoomInTarget, transitionDuration));
|
||||
if (disablePlayerInputDuringEagleEye)
|
||||
{
|
||||
Core.GameManager.Instance.ReleasePause(this); // Re-enable player input
|
||||
}
|
||||
if (eagleEyeButton != null)
|
||||
{
|
||||
eagleEyeButton.interactable = true;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator SmoothOrthoSize(CinemachineCamera cam, float targetSize, float duration)
|
||||
{
|
||||
float startSize = cam.Lens.OrthographicSize;
|
||||
float elapsed = 0f;
|
||||
while (elapsed < duration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
cam.Lens.OrthographicSize = Mathf.Lerp(startSize, targetSize, elapsed / duration);
|
||||
if (confiner2D != null)
|
||||
get
|
||||
{
|
||||
confiner2D.InvalidateBoundingShapeCache();
|
||||
if (_virtualCamera == null)
|
||||
{
|
||||
if (_cinecameraObject == null)
|
||||
_cinecameraObject = GameObject.FindWithTag("MainCinemachineCamera");
|
||||
if (_cinecameraObject != null)
|
||||
{
|
||||
_virtualCamera = _cinecameraObject.GetComponent<CinemachineCamera>();
|
||||
|
||||
if (_virtualCamera == null)
|
||||
Debug.LogWarning("EagleEyeBehaviour: Found object with tag 'MainCinemachineCamera' " +
|
||||
"but couldn't find a CinemachineCamera component.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("EagleEyeBehaviour: No GameObject found with tag 'MainCinemachineCamera'.");
|
||||
}
|
||||
}
|
||||
|
||||
return _virtualCamera;
|
||||
}
|
||||
yield return null;
|
||||
set => _virtualCamera = value;
|
||||
}
|
||||
cam.Lens.OrthographicSize = targetSize;
|
||||
if (confiner2D != null)
|
||||
|
||||
private CinemachineConfiner2D Confiner2D
|
||||
{
|
||||
confiner2D.InvalidateBoundingShapeCache();
|
||||
get
|
||||
{
|
||||
if (_confiner2D == null)
|
||||
{
|
||||
// If a virtual camera exists, try to pull the confiner from it
|
||||
if (VirtualCamera != null)
|
||||
{
|
||||
_confiner2D = VirtualCamera.GetComponent<CinemachineConfiner2D>();
|
||||
}
|
||||
|
||||
if (_confiner2D == null)
|
||||
{
|
||||
Debug.LogWarning("EagleEyeBehaviour: CinemachineConfiner2D not found on the MainCinemachineCamera object.");
|
||||
}
|
||||
}
|
||||
|
||||
return _confiner2D;
|
||||
}
|
||||
set => _confiner2D = value;
|
||||
}
|
||||
|
||||
[SerializeField] private float zoomOutOrthoSize = 30f;
|
||||
[SerializeField] private float normalOrthoSize = 15f;
|
||||
[SerializeField] private float transitionDuration = 0.5f; // Duration of the transition
|
||||
[SerializeField] private float eagleEyeDuration = 3f; // Duration to stay zoomed out
|
||||
[SerializeField] private UnityEngine.UI.Button eagleEyeButton; // Reference to the UI button
|
||||
|
||||
private Coroutine _zoomCoroutine;
|
||||
private Coroutine _smoothOrthoCoroutine;
|
||||
private float _currentOrthoSize;
|
||||
|
||||
public void ResetEagleEye()
|
||||
{
|
||||
if (_zoomCoroutine != null)
|
||||
StopCoroutine(_zoomCoroutine);
|
||||
if (_smoothOrthoCoroutine != null)
|
||||
StopCoroutine(_smoothOrthoCoroutine);
|
||||
if (eagleEyeButton != null)
|
||||
eagleEyeButton.interactable = true;
|
||||
}
|
||||
|
||||
|
||||
public void ActivateEagleEye()
|
||||
{
|
||||
if (eagleEyeButton != null)
|
||||
{
|
||||
eagleEyeButton.interactable = false;
|
||||
}
|
||||
if (_zoomCoroutine != null) StopCoroutine(_zoomCoroutine);
|
||||
_zoomCoroutine = StartCoroutine(EagleEyeSequence());
|
||||
}
|
||||
|
||||
private IEnumerator EagleEyeSequence()
|
||||
{
|
||||
_smoothOrthoCoroutine = StartCoroutine(SmoothOrthoSize(VirtualCamera, zoomOutOrthoSize, transitionDuration));
|
||||
yield return _smoothOrthoCoroutine;
|
||||
yield return new WaitForSeconds(eagleEyeDuration);
|
||||
float zoomInTarget = normalOrthoSize;
|
||||
_smoothOrthoCoroutine = StartCoroutine(SmoothOrthoSize(VirtualCamera, zoomInTarget, transitionDuration));;
|
||||
yield return _smoothOrthoCoroutine;
|
||||
if (eagleEyeButton != null)
|
||||
{
|
||||
eagleEyeButton.interactable = true;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator SmoothOrthoSize(CinemachineCamera cam, float targetSize, float duration)
|
||||
{
|
||||
float startSize = cam.Lens.OrthographicSize;
|
||||
float elapsed = 0f;
|
||||
while (elapsed < duration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
cam.Lens.OrthographicSize = Mathf.Lerp(startSize, targetSize, elapsed / duration);
|
||||
if (Confiner2D != null)
|
||||
{
|
||||
Confiner2D.InvalidateBoundingShapeCache();
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
cam.Lens.OrthographicSize = targetSize;
|
||||
if (Confiner2D != null)
|
||||
{
|
||||
Confiner2D.InvalidateBoundingShapeCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user