forked from Wacky-Mole/WackysDatabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValheimTime.cs
74 lines (59 loc) · 2.13 KB
/
ValheimTime.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using UnityEngine;
namespace wackydatabase.Datas
{
[Serializable]
public class ValheimTime
{
private const int DAY_LENGTH = 86400;
public int Hour { get; }
public int Minute { get; }
public int Second { get; }
public int Day { get; }
public ValheimTime(int hour = 0, int minute = 0, int second = 0, int day = -1)
{
Day = day;
Hour = hour;
Minute = minute;
Second = second;
}
public string ToDataString()
{
return Hour + ":" + Minute + ":" + Second + " (" + Day + ")";
}
public int ToSeconds()
{
return (int)new TimeSpan(Hour, Minute, Second).TotalSeconds;
}
public static float RatioUntilTime(ValheimTime a, ValheimTime b, ValheimTime span)
{
if (Mathf.Abs(a.ToSeconds() - b.ToSeconds()) < span.ToSeconds())
{
float diff = Mathf.Abs(a.ToSeconds() - b.ToSeconds());
return 1.0f - (diff / span.ToSeconds());
}
else if (Mathf.Abs(a.ToSeconds() + DAY_LENGTH - b.ToSeconds()) < span.ToSeconds())
{
float diff = Mathf.Abs(a.ToSeconds() + DAY_LENGTH - b.ToSeconds());
return 1.0f - (diff / span.ToSeconds());
}
else if (Mathf.Abs(a.ToSeconds() - (b.ToSeconds() + DAY_LENGTH)) < span.ToSeconds())
{
float diff = Mathf.Abs(a.ToSeconds() - (b.ToSeconds() + DAY_LENGTH));
return 1.0f - (diff / span.ToSeconds());
}
return 0;
}
public static ValheimTime Get()
{
if (!EnvMan.instance)
return new ValheimTime(-1);
float time = EnvMan.instance.m_smoothDayFraction;
int day = EnvMan.instance.GetCurrentDay();
int hour = (int)(time * 24);
int minute = (int)((time * 24 - hour) * 60);
int second = (int)((((time * 24 - hour) * 60) - minute) * 60);
return new ValheimTime(hour, minute, second, day);
}
}
}