Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
133 lines (121 loc) · 4.64 KB
/
Copy pathProgram.cs
File metadata and controls
133 lines (121 loc) · 4.64 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CustomInGameTime
{
class Program
{
public class InGameDate{
public int Days {get; set;}
public int Hour {get; set;}
public int Min {get; set;}
public string ToString(bool ampm = false)
{
if (!ampm)
return string.Format("{0:00} {1:00}:{2:00}", this.Days, this.Hour, this.Min);
string lblAmPm = "AM";
if (this.Hour >= 12)
lblAmPm = "PM";
int hr12 = this.Hour;
if (this.Hour == 0)
hr12 = 12;
if (this.Hour >= 13)
hr12 = this.Hour - 12;
return string.Format("{0:00} {1:00}:{2:00} {3}", this.Days, hr12, this.Min, lblAmPm);
}
#if DEBUG
public double CorrespondingTimeStamp { get; set; }
#endif
}
public class InGameTime
{
/// <summary>
/// Total of real seconds that represent a InGame Day
/// </summary>
private const float InGameDay_RealTimeSpan = 7200;
private double TimeStamp;
public InGameDate CurrentInGameDate
{
get
{
InGameDate gameDate = new InGameDate();
gameDate.Days = (int)Math.Floor(this.TimeStamp / InGameDay_RealTimeSpan);
double secondsRemaining = this.TimeStamp % InGameDay_RealTimeSpan;
float lerp = MathS.SuperLerp(0.0f, 24.0f, 0.0f, InGameDay_RealTimeSpan, (float)secondsRemaining);
gameDate.Hour = (int)Math.Floor(lerp);
float hrFraction = lerp - gameDate.Hour;
gameDate.Min = (int)Math.Floor(hrFraction * 60.0f);
gameDate.CorrespondingTimeStamp = this.TimeStamp;
return gameDate;
}
}
public InGameTime()
{
this.TimeStamp = CalculatesTimeStamp(DateTime.Now);
}
#if DEBUG
public InGameTime(DateTime customDate)
{
this.TimeStamp = CalculatesTimeStamp(customDate);
}
#endif
public void Update(float TimeDeltaTime)
{
this.TimeStamp += TimeDeltaTime;
}
//private static DateTime EPOCH = new DateTime(1970, 1, 1, 0, 0, 0);
private static DateTime EPOCH = new DateTime(2015, 3, 21, 0, 0, 0); //lets use a more recent date for test purposes (might be useful to change it to product launch)
public static double CalculatesTimeStamp(DateTime realTime)
{
TimeSpan span = realTime - EPOCH;
return span.TotalSeconds;
}
}
static void Main(string[] args)
{
Console.WriteLine("CustomInGameTime Test");
InGameTime inGameTimeEngine = new InGameTime(DateTime.Today);
Console.Write("Using Today ({0}) inGame Time is ", DateTime.Today);
Print(inGameTimeEngine.CurrentInGameDate);
Console.WriteLine();
Console.WriteLine("stamp\t 24hr 12hr ");
for (int i = 0; i < 1440; i++)
{
inGameTimeEngine.Update(60.0f);//adding 60 seconds to the timestamp
Print(inGameTimeEngine.CurrentInGameDate);
}
Console.WriteLine("CustomInGameTime Test Finished");
Console.ReadLine();
}
private static void Print(InGameDate gameDate)
{
Console.WriteLine("{0:00000}\t{1} --> {2}", gameDate.CorrespondingTimeStamp, gameDate.ToString(false), gameDate.ToString(true));
}
/// <summary>
/// Source http://wiki.unity3d.com/index.php?title=SpeedLerp
/// </summary>
public static class MathS
{
public static float SuperLerp(float from, float to, float from2, float to2, float value)
{
if (from2 < to2)
{
if (value < from2)
value = from2;
else if (value > to2)
value = to2;
}
else
{
if (value < to2)
value = to2;
else if (value > from2)
value = from2;
}
return (to - from) * ((value - from2) / (to2 - from2)) + from;
}
}
}
}