using System.Collections;
using UnityEngine;
using Game;
using Music;
using Player;
namespace Game
{
///
/// Manages the day-night cycle by transitioning between different sky and cloud sprites.
///
public class DayNightCycle : MonoBehaviour
{
///
/// Sprite representing the sky during the day.
///
public SpriteRenderer daySky;
///
/// Sprite representing the sky during the evening.
///
public SpriteRenderer eveningSky;
///
/// Sprite representing the sky during the night.
///
public SpriteRenderer nightSky;
///
/// Sprite representing the back clouds during the day.
///
public SpriteRenderer dayBackClouds;
///
/// Sprite representing the back clouds during the evening.
///
public SpriteRenderer eveningBackClouds;
///
/// Sprite representing the back clouds during the night.
///
public SpriteRenderer nightBackClouds;
///
/// Sprite representing the front clouds during the day.
///
public SpriteRenderer dayFrontClouds;
///
/// Sprite representing the front clouds during the evening.
///
public SpriteRenderer eveningFrontClouds;
///
/// Sprite representing the front clouds during the night.
///
public SpriteRenderer nightFrontClouds;
///
/// Duration of the transition between different phases of the day-night cycle.
///
public float transitionDuration = 5f;
///
/// Duration of each phase (day, evening, night) before transitioning to the next phase.
///
public float cycleDuration = 60f;
///
/// Initializes the day-night cycle by setting the initial alpha values and starting the cycle routine.
///
private void Start()
{
// Set initial alpha (only day visible)
SetAlpha(daySky, 1);
SetAlpha(eveningSky, 0);
SetAlpha(nightSky, 0);
SetAlpha(dayBackClouds, 1);
SetAlpha(eveningBackClouds, 0);
SetAlpha(nightBackClouds, 0);
SetAlpha(dayFrontClouds, 1);
SetAlpha(eveningFrontClouds, 0);
SetAlpha(nightFrontClouds, 0);
// Start the cycle
StartCoroutine(DayNightCycleRoutine());
}
///
/// Coroutine that manages the day-night cycle by transitioning between phases in a loop.
///
private IEnumerator DayNightCycleRoutine()
{
while (true)
{
yield return new WaitForSeconds(cycleDuration);
yield return StartCoroutine(FadeTransition(
new SpriteRenderer[] { daySky, dayBackClouds, dayFrontClouds },
new SpriteRenderer[] { eveningSky, eveningBackClouds, eveningFrontClouds },
transitionDuration));
yield return new WaitForSeconds(cycleDuration);
yield return StartCoroutine(FadeTransition(
new SpriteRenderer[] { eveningSky, eveningBackClouds, eveningFrontClouds },
new SpriteRenderer[] { nightSky, nightBackClouds, nightFrontClouds },
transitionDuration));
yield return new WaitForSeconds(cycleDuration);
yield return StartCoroutine(FadeTransition(
new SpriteRenderer[] { nightSky, nightBackClouds, nightFrontClouds },
new SpriteRenderer[] { daySky, dayBackClouds, dayFrontClouds },
transitionDuration));
}
}
///
/// Coroutine that handles the fade transition between two sets of sprites over a specified duration.
///
/// Array of sprites to fade out.
/// Array of sprites to fade in.
/// Duration of the fade transition.
private IEnumerator FadeTransition(SpriteRenderer[] from, SpriteRenderer[] to, float duration)
{
float elapsedTime = 0;
while (elapsedTime < duration)
{
elapsedTime += Time.deltaTime;
float alpha = elapsedTime / duration;
// Apply fading to all elements simultaneously
for (int i = 0; i < from.Length; i++)
{
SetAlpha(from[i], 1 - alpha);
SetAlpha(to[i], alpha);
}
yield return null;
}
// Ensure final alpha values are set correctly
for (int i = 0; i < from.Length; i++)
{
SetAlpha(from[i], 0);
SetAlpha(to[i], 1);
}
}
///
/// Sets the alpha value of a sprite and its child sprites (if any).
///
/// The sprite renderer to modify.
/// The alpha value to set (0 to 1).
private void SetAlpha(SpriteRenderer sprite, float alpha)
{
if (sprite)
{
Color color = sprite.color;
color.a = alpha;
sprite.color = color;
foreach (Transform child in sprite.transform)
{
if (child.TryGetComponent(out SpriteRenderer childSprite))
{
childSprite.color = color;
}
}
}
}
}
}