2025-09-26 00:23:27 +02:00
|
|
|
using UnityEngine;
|
|
|
|
|
using Unity.Cinemachine;
|
2025-09-26 15:12:18 +02:00
|
|
|
using System.Collections;
|
2025-11-05 13:48:25 +01:00
|
|
|
using Core.SaveLoad;
|
2025-09-26 15:12:18 +02:00
|
|
|
using Pixelplacement;
|
2025-09-26 00:23:27 +02:00
|
|
|
|
|
|
|
|
public class cameraSwitcher : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private CinemachineCamera virtualCamera;
|
2025-09-26 15:12:18 +02:00
|
|
|
[SerializeField] private CinemachineConfiner2D confiner2D;
|
|
|
|
|
[SerializeField] private float zoomOutOrthoSize = 27f;
|
|
|
|
|
[SerializeField] private float normalOrthoSize = 20f;
|
|
|
|
|
[SerializeField] private float transitionDuration = 0.5f; // Duration of the transition
|
|
|
|
|
[SerializeField] private soundBird_FlyingBehaviour flyingBehaviour;
|
2025-11-04 16:26:33 +01:00
|
|
|
[SerializeField] private soundBird_TakeOffBehaviour takeOffBehaviour; // New reference
|
2025-11-05 13:48:25 +01:00
|
|
|
[SerializeField] private AppleMachine birdStateMachine;
|
2025-09-26 00:23:27 +02:00
|
|
|
|
|
|
|
|
private int playerInsideCount = 0;
|
2025-09-26 15:12:18 +02:00
|
|
|
private Coroutine zoomCoroutine;
|
2025-09-26 00:23:27 +02:00
|
|
|
|
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
|
|
|
{
|
|
|
|
|
if (other.CompareTag("Player"))
|
|
|
|
|
{
|
|
|
|
|
playerInsideCount++;
|
2025-09-26 15:12:18 +02:00
|
|
|
if (playerInsideCount == 1 && virtualCamera != null)
|
2025-09-26 00:23:27 +02:00
|
|
|
{
|
2025-09-26 15:12:18 +02:00
|
|
|
if (zoomCoroutine != null) StopCoroutine(zoomCoroutine);
|
|
|
|
|
zoomCoroutine = StartCoroutine(SmoothOrthoSize(virtualCamera, zoomOutOrthoSize, transitionDuration));
|
|
|
|
|
}
|
2025-09-26 00:23:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTriggerExit2D(Collider2D other)
|
|
|
|
|
{
|
2025-11-05 13:48:25 +01:00
|
|
|
if (!gameObject.activeInHierarchy)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-09-26 00:23:27 +02:00
|
|
|
if (other.CompareTag("Player"))
|
|
|
|
|
{
|
|
|
|
|
playerInsideCount--;
|
2025-09-26 15:12:18 +02:00
|
|
|
if (playerInsideCount == 0 && virtualCamera != null)
|
2025-09-26 00:23:27 +02:00
|
|
|
{
|
2025-09-26 15:12:18 +02:00
|
|
|
if (zoomCoroutine != null) StopCoroutine(zoomCoroutine);
|
|
|
|
|
zoomCoroutine = StartCoroutine(SmoothOrthoSize(virtualCamera, normalOrthoSize, transitionDuration));
|
2025-11-04 16:26:33 +01:00
|
|
|
if (birdStateMachine.currentState != null)
|
2025-09-26 15:12:18 +02:00
|
|
|
{
|
2025-11-04 16:26:33 +01:00
|
|
|
if (birdStateMachine.currentState.name == "SoundBirdFlyAround" && flyingBehaviour != null)
|
|
|
|
|
{
|
|
|
|
|
flyingBehaviour.StartLanding();
|
|
|
|
|
}
|
|
|
|
|
else if (birdStateMachine.currentState.name == "SoundBirdTakeoff" && takeOffBehaviour != null)
|
|
|
|
|
{
|
|
|
|
|
takeOffBehaviour.StartLanding();
|
|
|
|
|
}
|
2025-09-26 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2025-11-06 17:11:30 +01:00
|
|
|
if (confiner2D != null)
|
|
|
|
|
{
|
|
|
|
|
confiner2D.InvalidateBoundingShapeCache();
|
|
|
|
|
}
|
2025-09-26 15:12:18 +02:00
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
cam.Lens.OrthographicSize = targetSize;
|
|
|
|
|
if (confiner2D != null)
|
|
|
|
|
{
|
|
|
|
|
confiner2D.InvalidateBoundingShapeCache();
|
2025-09-26 00:23:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|