diff --git a/S1API/GameTime/GameDateTime.cs b/S1API/GameTime/GameDateTime.cs index bf72ea81..9aa462d7 100644 --- a/S1API/GameTime/GameDateTime.cs +++ b/S1API/GameTime/GameDateTime.cs @@ -1,22 +1,150 @@ -// TODO: Implement GameDateTime wrapper -// #if (IL2CPP) -// using S1GameTime = Il2CppScheduleOne.GameTime; -// #elif (MONO) -// using S1GameTime = ScheduleOne.GameTime; -// #endif -// -// namespace S1API.API.GameTime -// { -// struct GameDateTime -// { -// public int elapsedDays; -// public int time; -// -// public GameDateTime(S1GameTime.GameDateTime gameDateTime) -// { -// -// } -// -// public void Ad -// } -// } \ No newline at end of file +#if (IL2CPP) +using S1GameDateTime = Il2CppScheduleOne.GameTime.GameDateTime; +using S1TimeManager = Il2CppScheduleOne.GameTime.TimeManager; +using S1GameDateTimeData = Il2CppScheduleOne.Persistence.Datas.GameDateTimeData; +#elif (MONO) +using S1GameDateTime = ScheduleOne.GameTime.GameDateTime; +using S1TimeManager = ScheduleOne.GameTime.TimeManager; +using S1GameDateTimeData = ScheduleOne.Persistence.Datas.GameDateTimeData; +#endif + +using System; + +namespace S1API.GameTime +{ + /// + /// Represents an in-game datetime (elapsed days and 24-hour time). + /// + public struct GameDateTime + { + public int ElapsedDays; + public int Time; + + /// + /// Constructs a GameDateTime from elapsed days and 24-hour time. + /// + public GameDateTime(int elapsedDays, int time) + { + ElapsedDays = elapsedDays; + Time = time; + } + + /// + /// Constructs a GameDateTime from total minutes. + /// + public GameDateTime(int minSum) + { + ElapsedDays = minSum / 1440; + int minutesInDay = minSum % 1440; + if (minSum < 0) + { + minutesInDay = -minSum % 1440; + } + Time = S1TimeManager.Get24HourTimeFromMinSum(minutesInDay); + } + + /// + /// Constructs a GameDateTime from an internal GameDateTimeData. + /// + public GameDateTime(S1GameDateTimeData data) + { + ElapsedDays = data.ElapsedDays; + Time = data.Time; + } + + /// + /// Constructs a GameDateTime from the internal GameDateTime struct. + /// + public GameDateTime(S1GameDateTime gameDateTime) + { + ElapsedDays = gameDateTime.elapsedDays; + Time = gameDateTime.time; + } + + /// + /// Returns the total minute sum (days * 1440 + minutes of day). + /// + public int GetMinSum() + { + return ElapsedDays * 1440 + S1TimeManager.GetMinSumFrom24HourTime(Time); + } + + /// + /// Returns a new GameDateTime with additional minutes added. + /// + public GameDateTime AddMinutes(int minutes) + { + return new GameDateTime(GetMinSum() + minutes); + } + + /// + /// Converts this wrapper to the internal GameDateTime struct. + /// + public S1GameDateTime ToS1() + { + return new S1GameDateTime(ElapsedDays, Time); + } + + /// + /// Returns the current time formatted as a 12-hour AM/PM string. + /// Example: "12:30 PM" + /// + public string GetFormattedTime() + { + return S1TimeManager.Get12HourTime(Time, true); + } + + /// + /// Returns true if the time is considered nighttime. + /// (Before 6AM or after 6PM) + /// + public bool IsNightTime() + { + return Time < 600 || Time >= 1800; + } + + /// + /// Returns true if the two GameDateTimes are on the same day (ignores time). + /// + public bool IsSameDay(GameDateTime other) + { + return ElapsedDays == other.ElapsedDays; + } + + /// + /// Returns true if the two GameDateTimes are at the same day and time. + /// + public bool IsSameTime(GameDateTime other) + { + return ElapsedDays == other.ElapsedDays && Time == other.Time; + } + + /// + /// String representation: "Day 3, 2:30 PM" + /// + public override string ToString() + { + return $"Day {ElapsedDays}, {GetFormattedTime()}"; + } + + public static GameDateTime operator +(GameDateTime a, GameDateTime b) + { + return new GameDateTime(a.GetMinSum() + b.GetMinSum()); + } + + public static GameDateTime operator -(GameDateTime a, GameDateTime b) + { + return new GameDateTime(a.GetMinSum() - b.GetMinSum()); + } + + public static bool operator >(GameDateTime a, GameDateTime b) + { + return a.GetMinSum() > b.GetMinSum(); + } + + public static bool operator <(GameDateTime a, GameDateTime b) + { + return a.GetMinSum() < b.GetMinSum(); + } + } +} diff --git a/S1API/GameTime/TimeManager.cs b/S1API/GameTime/TimeManager.cs index 45f78f9c..6e181260 100644 --- a/S1API/GameTime/TimeManager.cs +++ b/S1API/GameTime/TimeManager.cs @@ -1,7 +1,7 @@ #if (IL2CPP) -using S1GameTime = Il2CppScheduleOne.GameTime; +using S1GameTime = Il2CppScheduleOne.GameTime.TimeManager; #elif (MONO) -using S1GameTime = ScheduleOne.GameTime; +using S1GameTime = ScheduleOne.GameTime.TimeManager; #endif using System; @@ -14,14 +14,129 @@ namespace S1API.GameTime public static class TimeManager { /// - /// Action called when the day passes in-game. + /// Called when a new in-game day starts. /// public static Action OnDayPass = delegate { }; - + + /// + /// Called when a new in-game week starts. + /// + public static Action OnWeekPass = delegate { }; + + /// + /// Called when the player starts sleeping. + /// + public static Action OnSleepStart = delegate { }; + + /// + /// Called when the player finishes sleeping. + /// Parameter: total minutes skipped during sleep. + /// + public static Action OnSleepEnd = delegate { }; + + static TimeManager() + { + if (S1GameTime.Instance != null) + { + S1GameTime.Instance.onDayPass += (Action)(() => OnDayPass()); + S1GameTime.Instance.onWeekPass += (Action)(() => OnWeekPass()); + } + + S1GameTime.onSleepStart += (Action)(() => OnSleepStart()); + S1GameTime.onSleepEnd += (Action)(minutes => OnSleepEnd(minutes)); + } + + + /// + /// The current in-game day (Monday, Tuesday, etc.). + /// + public static Day CurrentDay => (Day)S1GameTime.Instance.CurrentDay; + + /// + /// The number of in-game days elapsed. + /// + public static int ElapsedDays => S1GameTime.Instance.ElapsedDays; + + /// + /// The current 24-hour time (e.g., 1330 for 1:30 PM). + /// + public static int CurrentTime => S1GameTime.Instance.CurrentTime; + + /// + /// Whether it is currently nighttime in-game. + /// + public static bool IsNight => S1GameTime.Instance.IsNight; + + /// + /// Whether the game is currently at the end of the day (4:00 AM). + /// + public static bool IsEndOfDay => S1GameTime.Instance.IsEndOfDay; + + /// + /// Whether the player is currently sleeping. + /// + public static bool SleepInProgress => S1GameTime.Instance.SleepInProgress; + + /// + /// Whether the time is currently overridden (frozen or custom). + /// + public static bool TimeOverridden => S1GameTime.Instance.TimeOverridden; + + /// + /// The current normalized time of day (0.0 = start, 1.0 = end). + /// + public static float NormalizedTime => S1GameTime.Instance.NormalizedTime; + + /// + /// Total playtime (in seconds). + /// + public static float Playtime => S1GameTime.Instance.Playtime; + + /// + /// Fast-forwards time to morning wake time (7:00 AM). + /// + public static void FastForwardToWakeTime() => S1GameTime.Instance.FastForwardToWakeTime(); + + /// + /// Sets the current time manually. + /// + public static void SetTime(int time24h, bool local = false) => S1GameTime.Instance.SetTime(time24h, local); + + /// + /// Sets the number of elapsed in-game days. + /// + public static void SetElapsedDays(int days) => S1GameTime.Instance.SetElapsedDays(days); + + /// + /// Gets the current time formatted in 12-hour AM/PM format. + /// + public static string GetFormatted12HourTime() + { + return S1GameTime.Get12HourTime(CurrentTime, true); + } + + /// + /// Returns true if the current time is within the specified 24-hour range. + /// + public static bool IsCurrentTimeWithinRange(int startTime24h, int endTime24h) + { + return S1GameTime.Instance.IsCurrentTimeWithinRange(startTime24h, endTime24h); + } + + /// + /// Converts 24-hour time to total minutes. + /// + public static int GetMinutesFrom24HourTime(int time24h) + { + return S1GameTime.GetMinSumFrom24HourTime(time24h); + } + /// - /// The current in-game day. + /// Converts total minutes into 24-hour time format. /// - public static Day CurrentDay => - (Day)S1GameTime.TimeManager.Instance.CurrentDay; + public static int Get24HourTimeFromMinutes(int minutes) + { + return S1GameTime.Get24HourTimeFromMinSum(minutes); + } } -} \ No newline at end of file +}