-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimeManager.cs
60 lines (47 loc) · 1.43 KB
/
TimeManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeManager : MonoBehaviour
{
public const int hoursInDay = 24, minutesInHour = 60;
public float dayDuration = 30f;
float totalTime = 0;
float currentTime = 0;
public float nightDuration = .4f;
public float sunriseHour = 6;
// Update is called once per frame
void Update()
{
totalTime += Time.deltaTime;
currentTime = totalTime % dayDuration;
}
public float GetHour()
{
return currentTime * hoursInDay / dayDuration;
}
public float GetMinutes()
{
return (currentTime * hoursInDay * minutesInHour / dayDuration)%minutesInHour;
}
public string Clock24Hour()
{
//00:00
return Mathf.FloorToInt(GetHour()).ToString("00") + ":" + Mathf.FloorToInt(GetMinutes()).ToString("00");
}
public string Clock12Hour()
{
int hour = Mathf.FloorToInt(GetHour());
string abbreviation = "AM";
if (hour >= 12)
{
abbreviation = "PM";
hour -= 12;
}
if (hour == 0) hour = 12;
return hour.ToString("00") + ":" + Mathf.FloorToInt(GetMinutes()).ToString("00") + " " + abbreviation;
}
public float GetSunsetHour()
{
return (sunriseHour + (1 - nightDuration) * hoursInDay) % hoursInDay;
}
}