188 lines
6.0 KiB
C#
188 lines
6.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using FishNet;
|
|
using FishNet.Transporting;
|
|
using Managers;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
// possible future additions
|
|
// weather sys
|
|
// skybox changes based on time of day
|
|
// moon and stars
|
|
// clouds
|
|
// rain
|
|
// Sound effects, bird chrirping, crickets, wind, etc
|
|
// Temperture?
|
|
// Lighting effects! So that the transition is more visually appealing
|
|
// Moonlight phases based on day of month ect
|
|
// Seasons - impact the length of day, temp, weather, etc
|
|
|
|
namespace UI
|
|
{
|
|
public class GameTimeUIManager : MonoBehaviour
|
|
{
|
|
// public Light2D globalLight;
|
|
|
|
public Gradient lightColorGradient;
|
|
public Gradient lightIntensityGradient;
|
|
|
|
public TMP_Text dayText;
|
|
public TMP_Text seasonText;
|
|
public TMP_Text timeText;
|
|
|
|
private readonly int _daysPerMonth = 28;
|
|
private readonly int _monthsPerYear = 4;
|
|
|
|
// private Season _currentSeason;
|
|
// private float _currentTime;
|
|
// private int _daysPassed;
|
|
|
|
private bool _isFirstDay = true;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
// Find the globeLight on the root object
|
|
// FindLights();
|
|
}
|
|
|
|
|
|
private void Start()
|
|
{
|
|
/*if (IsServerInitialized)
|
|
{
|
|
dayText.text = GetCurrentDayOfMonth().ToString();
|
|
seasonText.text = GetCurrentSeason().ToString();
|
|
|
|
StartCoroutine(ServerDayNightCycleCoroutine());
|
|
_isFirstDay = false;
|
|
}
|
|
*/
|
|
var networkManager = GameInit.Instance.GetNetworkManager();
|
|
if (networkManager == null)
|
|
{
|
|
Debug.LogError("NetworkManager not found");
|
|
return;
|
|
}
|
|
|
|
networkManager.ServerManager.OnServerConnectionState += OnServerConnectionStateChange;
|
|
networkManager.ClientManager.OnClientConnectionState += OnClientConnectionStateChange;
|
|
}
|
|
|
|
|
|
private void Update()
|
|
{
|
|
if (!InstanceFinder.IsClientStarted)
|
|
{
|
|
Debug.LogWarning("Client not initialized");
|
|
return;
|
|
}
|
|
|
|
// need to make sure the _networkManager is initialized first
|
|
timeText.text = GameTimeManager.Instance.GetCurrentTimeFormatted();
|
|
|
|
//todo: optimize to only update on day change
|
|
dayText.text = GameTimeManager.Instance.GetCurrentDayOfMonth().ToString();
|
|
seasonText.text = GameTimeManager.Instance.GetCurrentSeason().ToString().ToUpper();
|
|
}
|
|
|
|
private void OnClientConnectionStateChange(ClientConnectionStateArgs obj)
|
|
{
|
|
Debug.Log("OnClientConnectionStateChange Triggered");
|
|
}
|
|
|
|
private void OnServerConnectionStateChange(ServerConnectionStateArgs obj)
|
|
{
|
|
Debug.Log("OnServerConnectionStateChange Triggered");
|
|
}
|
|
|
|
private void FindLights()
|
|
{
|
|
// Find the globeLight on the root object
|
|
try
|
|
{
|
|
// globalLight = GameObject.Find("SunLight").GetComponent<Light2D>();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError("Error finding lights: " + e.Message);
|
|
}
|
|
}
|
|
|
|
|
|
private void UpdateUIDisplay()
|
|
{
|
|
timeText.text = GameTimeManager.Instance.GetCurrentTimeFormatted();
|
|
//Todo: optimize to only update on day change
|
|
dayText.text = GameTimeManager.Instance.GetCurrentDayOfMonth().ToString();
|
|
seasonText.text = GameTimeManager.Instance.GetCurrentSeason().ToString();
|
|
}
|
|
|
|
/*public override void OnStartServer()
|
|
{
|
|
base.OnStartServer();
|
|
StartCoroutine(ServerDayNightCycleCoroutine());
|
|
}*/
|
|
|
|
public static event Action OnDayChange;
|
|
public static event Action OnSeasonChange;
|
|
|
|
|
|
// [Server]
|
|
private IEnumerator ServerDayNightCycleCoroutine()
|
|
{
|
|
// var previousSeason = GetCurrentSeason();
|
|
while (true)
|
|
{
|
|
var timeSettings = GameManager.Instance.GetTimeSettings();
|
|
// Calculate the current time of day between 0 (midnight) and 1 (next midnight)
|
|
var timeOfDay = timeSettings.CurrentTime / (timeSettings.DayLengthInMinutes * 60);
|
|
|
|
/*// Adjust the intensity of the light source based on the time of day
|
|
globalLight.color = lightColorGradient.Evaluate(timeOfDay);
|
|
globalLight.intensity = lightIntensityGradient.Evaluate(timeOfDay).a;*/
|
|
|
|
// increment current time
|
|
timeSettings.CurrentTime += Time.deltaTime;
|
|
|
|
|
|
// check if a day has passed
|
|
if (timeSettings.CurrentTime >= timeSettings.DayLengthInMinutes * 60)
|
|
{
|
|
timeSettings.DaysPassed++;
|
|
Debug.Log("Day " + timeSettings.DaysPassed + " has passed");
|
|
timeSettings.CurrentTime = 0;
|
|
_isFirstDay = false;
|
|
|
|
OnDayChange?.Invoke();
|
|
|
|
var newSeason = GameTimeManager.Instance.GetCurrentSeason(timeSettings.DaysPassed);
|
|
if (newSeason != timeSettings.Season)
|
|
{
|
|
Debug.Log("Season changed from " + timeSettings.Season + " to " + newSeason);
|
|
timeSettings.Season = newSeason;
|
|
OnSeasonChange?.Invoke();
|
|
}
|
|
}
|
|
|
|
// _gameManager.RequestUpdateTimeSettingsServerRpc(timeSettings);
|
|
|
|
UpdateClientsRpc(timeOfDay);
|
|
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
// [ObserversRpc]
|
|
private void UpdateClientsRpc(float timeOfDay)
|
|
{
|
|
/*if (globalLight == null) return;
|
|
|
|
globalLight.color = lightColorGradient.Evaluate(timeOfDay);
|
|
globalLight.intensity = lightIntensityGradient.Evaluate(timeOfDay).a;*/
|
|
|
|
UpdateUIDisplay();
|
|
}
|
|
}
|
|
} |