GameVsJam/3d Prototyp/Assets/Scripts/TimeManager.cs

67 lines
1.5 KiB
C#
Raw Normal View History

using System;
using UnityEngine;
using Utility;
public class TimeManager : MonoBehaviourSingleton<TimeManager>
{
[SerializeField]
private int _daysUntilRelease = 30;
[SerializeField]
private double _secondsPerDay = 48;
[SerializeField]
private double _dayTime = 0.0;
[SerializeField] private Light _sun;
[SerializeField]
private Weekday _currentWeekday;
public int DaysLeft => _daysUntilRelease;
public double DayTime => _dayTime;
public TimeSpan TimeOfDay
{
get
{
double hour = _dayTime * 24.0;
double minute = hour * 60;
double seconds = minute * 60;
return new TimeSpan((int)Math.Floor(hour), (int)Math.Floor(minute % 60), (int)Math.Floor(seconds % 60));
}
}
[SerializeField, ShowOnly]
private string stringgy;
void Update()
{
if (GameManager.Instance.IsGameRunning)
{
UpdateTime();
UpdateSun();
}
}
void UpdateTime()
{
_dayTime += Time.deltaTime / _secondsPerDay;
if (_dayTime >= 1.0)
{
_dayTime -= 1.0;
_daysUntilRelease--;
_currentWeekday = _currentWeekday.GetNext();
}
stringgy = TimeOfDay.ToString();
}
private void UpdateSun()
{
_sun.transform.rotation = Quaternion.Euler(new Vector3(Mathf.LerpAngle(-90, 60, (float)Math.Sin(_dayTime * Math.PI)), (float)(_dayTime * 360.0), 0));
}
}