refactor
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class TimeManager : MonoBehaviour
|
||||
{
|
||||
public static TimeManager Instance { get; private set; }
|
||||
|
||||
private const float MINUTE_TIME = 1f;
|
||||
private const float FF_TIME = 0.003f;
|
||||
|
||||
public Action OnMinuteChanged;
|
||||
public Action OnFastForwardEnd;
|
||||
|
||||
[SerializeField]
|
||||
private TimeSpan _startTime = new TimeSpan(1, 08, 00, 00);
|
||||
|
||||
//[SerializeField]
|
||||
//private Light _sunLight;
|
||||
|
||||
[SerializeField]
|
||||
private float _sunriseHour;
|
||||
[SerializeField]
|
||||
private float _sunsetHour;
|
||||
|
||||
private TimeSpan _sunriseTime;
|
||||
private TimeSpan _sunsetTime;
|
||||
|
||||
private float _timer;
|
||||
private float _sunInitialIntensity;
|
||||
|
||||
private float _minuteToRealTime;
|
||||
|
||||
private static TimeSpan _currentTime;
|
||||
public static TimeSpan CurrentTime => _currentTime;
|
||||
|
||||
|
||||
private TimeSpan _timeToStop;
|
||||
|
||||
private bool _isPause = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
else
|
||||
Destroy(gameObject);
|
||||
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
// _sunInitialIntensity = _sunLight.intensity;
|
||||
_timer = _minuteToRealTime;
|
||||
_currentTime = TimeSpan.Zero + _startTime;
|
||||
_timeToStop = _currentTime;
|
||||
_sunriseTime = TimeSpan.FromHours(_sunriseHour);
|
||||
_sunsetTime = TimeSpan.FromHours(_sunsetHour);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (!_isPause)
|
||||
{
|
||||
UpdateTime();
|
||||
RotateSun();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTime()
|
||||
{
|
||||
_timer -= Time.deltaTime;
|
||||
if (_timer <= 0)
|
||||
{
|
||||
_currentTime = _currentTime.Add(TimeSpan.FromMinutes(1));
|
||||
OnMinuteChanged?.Invoke();
|
||||
|
||||
if (_currentTime.TotalMinutes >= _timeToStop.TotalMinutes)
|
||||
{
|
||||
_minuteToRealTime = MINUTE_TIME;
|
||||
_timeToStop = TimeSpan.MaxValue;
|
||||
OnFastForwardEnd?.Invoke();
|
||||
}
|
||||
_timer = _minuteToRealTime;
|
||||
}
|
||||
}
|
||||
|
||||
public void FastForward(TimeSpan timeToStop)
|
||||
{
|
||||
_isPause = false;
|
||||
_timeToStop = _currentTime.Add(timeToStop);
|
||||
_minuteToRealTime = FF_TIME;
|
||||
}
|
||||
|
||||
private void RotateSun()
|
||||
{
|
||||
float intensityMultiplier = 1;
|
||||
float timeofDay = (float)(CurrentTime.TotalDays - CurrentTime.Days);
|
||||
//_sunLight.transform.localRotation = Quaternion.Euler((timeofDay * 360f) - 90, 170, 0);
|
||||
if (timeofDay > _sunriseTime.TotalDays && timeofDay < _sunsetTime.TotalDays)
|
||||
{
|
||||
if (timeofDay <= _sunsetTime.TotalDays)
|
||||
intensityMultiplier = Mathf.Clamp01((timeofDay - ((float)_sunsetTime.TotalDays - 0.02f)) * (1 / 0.02f));
|
||||
if (timeofDay >= _sunriseTime.TotalDays)
|
||||
intensityMultiplier = Mathf.Clamp01(1 - (timeofDay - ((float)_sunriseTime.TotalDays - 0.02f) * (1 / 0.02f)));
|
||||
}
|
||||
else
|
||||
{
|
||||
intensityMultiplier = 0;
|
||||
}
|
||||
// _sunLight.intensity = _sunInitialIntensity * intensityMultiplier;
|
||||
}
|
||||
|
||||
private TimeSpan CalculateTimeDifference(TimeSpan from, TimeSpan to)
|
||||
{
|
||||
TimeSpan diff = to - from;
|
||||
if (diff.TotalSeconds < 0)
|
||||
{
|
||||
diff += TimeSpan.FromHours(24);
|
||||
}
|
||||
|
||||
return diff;
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
_isPause = true;
|
||||
}
|
||||
|
||||
internal void Resume()
|
||||
{
|
||||
_isPause = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user