Last active
April 25, 2020 15:43
-
-
Save Ratstail91/1bc5971da650ccc15ee15e8822e21670 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
//using UnityEngine; | |
public static class TimeManager { | |
//constants for reuse below | |
public const int FULL_DAY_LENGTH = 1440; | |
public const int HORDE_NIGHT_FREQUENCY = 7; | |
//these can be controlled by an external calendar script | |
public static int sunrise = 240; | |
public static int sunset = 1120; | |
//properties for rotating the sun | |
public static int DayLength { | |
get { | |
return sunset - sunrise; | |
} | |
} | |
public static float SunDayRotationPerMinute { | |
get { | |
return 180f / DayLength; | |
} | |
} | |
public static float SunNightRotationPerMinute { | |
get { | |
return 180f / (FULL_DAY_LENGTH - DayLength); | |
} | |
} | |
//the key to this whole thing | |
public static float SunRotationAmount { | |
get { | |
//early morning | |
if (TimeOfDay <= sunrise) { | |
return ((float)TimeOfDay / sunrise) * 90f; | |
} | |
//daytime | |
if (TimeOfDay <= sunset) { | |
return 90 + (TimeOfDay - sunrise) * SunDayRotationPerMinute; | |
} | |
//else late night | |
return 270f + ( (float)(TimeOfDay - sunset) / (FULL_DAY_LENGTH - sunset)) * 90f; | |
} | |
} | |
//check if tonight is horde night | |
public static bool IsHordeNight { | |
get { | |
return Day % HORDE_NIGHT_FREQUENCY == 6; //6 == day 7 (zero indexed) | |
} | |
} | |
//this increases over time | |
private static int _day = 0; | |
public static int Day { | |
private set { | |
_day = value; | |
} | |
get { | |
return _day; | |
} | |
} | |
private static int _timeOfDay = 0; | |
public static int TimeOfDay { | |
set { | |
_timeOfDay = value; | |
//use while, in case you increment by several days at once | |
while (_timeOfDay >= FULL_DAY_LENGTH) { | |
_timeOfDay -= FULL_DAY_LENGTH; | |
_day++; | |
} | |
//check for subtraction too | |
while (_timeOfDay < 0) { | |
_timeOfDay += FULL_DAY_LENGTH; | |
_day--; | |
} | |
} | |
get { | |
return _timeOfDay; | |
} | |
} | |
//for testing | |
public static void Main(String[] args) { | |
Console.WriteLine("Running TimeManager test"); | |
Console.WriteLine(TimeManager.sunrise); | |
Console.WriteLine(TimeManager.sunset); | |
Console.WriteLine(TimeManager.DayLength); | |
Console.WriteLine("---"); | |
Console.WriteLine(TimeManager.SunDayRotationPerMinute); | |
Console.WriteLine(TimeManager.SunNightRotationPerMinute); | |
Console.WriteLine("---"); | |
const int increment = 1; | |
for (int i = 0; i < TimeManager.FULL_DAY_LENGTH; i += increment) { | |
TimeManager.TimeOfDay += increment; | |
Console.WriteLine("" + i + ":" + TimeManager.SunRotationAmount); | |
// if (i > 250) break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment