Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 150 additions & 22 deletions S1API/GameTime/GameDateTime.cs
Original file line number Diff line number Diff line change
@@ -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
// }
// }
#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
{
/// <summary>
/// Represents an in-game datetime (elapsed days and 24-hour time).
/// </summary>
public struct GameDateTime
{
public int ElapsedDays;
public int Time;

/// <summary>
/// Constructs a GameDateTime from elapsed days and 24-hour time.
/// </summary>
public GameDateTime(int elapsedDays, int time)
{
ElapsedDays = elapsedDays;
Time = time;
}

/// <summary>
/// Constructs a GameDateTime from total minutes.
/// </summary>
public GameDateTime(int minSum)
{
ElapsedDays = minSum / 1440;
int minutesInDay = minSum % 1440;
if (minSum < 0)
{
minutesInDay = -minSum % 1440;
}
Time = S1TimeManager.Get24HourTimeFromMinSum(minutesInDay);
}

/// <summary>
/// Constructs a GameDateTime from an internal GameDateTimeData.
/// </summary>
public GameDateTime(S1GameDateTimeData data)
{
ElapsedDays = data.ElapsedDays;
Time = data.Time;
}

/// <summary>
/// Constructs a GameDateTime from the internal GameDateTime struct.
/// </summary>
public GameDateTime(S1GameDateTime gameDateTime)
{
ElapsedDays = gameDateTime.elapsedDays;
Time = gameDateTime.time;
}

/// <summary>
/// Returns the total minute sum (days * 1440 + minutes of day).
/// </summary>
public int GetMinSum()
{
return ElapsedDays * 1440 + S1TimeManager.GetMinSumFrom24HourTime(Time);
}

/// <summary>
/// Returns a new GameDateTime with additional minutes added.
/// </summary>
public GameDateTime AddMinutes(int minutes)
{
return new GameDateTime(GetMinSum() + minutes);
}

/// <summary>
/// Converts this wrapper to the internal GameDateTime struct.
/// </summary>
public S1GameDateTime ToS1()
{
return new S1GameDateTime(ElapsedDays, Time);
}

/// <summary>
/// Returns the current time formatted as a 12-hour AM/PM string.
/// Example: "12:30 PM"
/// </summary>
public string GetFormattedTime()
{
return S1TimeManager.Get12HourTime(Time, true);
}

/// <summary>
/// Returns true if the time is considered nighttime.
/// (Before 6AM or after 6PM)
/// </summary>
public bool IsNightTime()
{
return Time < 600 || Time >= 1800;
}

/// <summary>
/// Returns true if the two GameDateTimes are on the same day (ignores time).
/// </summary>
public bool IsSameDay(GameDateTime other)
{
return ElapsedDays == other.ElapsedDays;
}

/// <summary>
/// Returns true if the two GameDateTimes are at the same day and time.
/// </summary>
public bool IsSameTime(GameDateTime other)
{
return ElapsedDays == other.ElapsedDays && Time == other.Time;
}

/// <summary>
/// String representation: "Day 3, 2:30 PM"
/// </summary>
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();
}
}
}
131 changes: 123 additions & 8 deletions S1API/GameTime/TimeManager.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,14 +14,129 @@ namespace S1API.GameTime
public static class TimeManager
{
/// <summary>
/// Action called when the day passes in-game.
/// Called when a new in-game day starts.
/// </summary>
public static Action OnDayPass = delegate { };


/// <summary>
/// Called when a new in-game week starts.
/// </summary>
public static Action OnWeekPass = delegate { };

/// <summary>
/// Called when the player starts sleeping.
/// </summary>
public static Action OnSleepStart = delegate { };

/// <summary>
/// Called when the player finishes sleeping.
/// Parameter: total minutes skipped during sleep.
/// </summary>
public static Action<int> 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<int>)(minutes => OnSleepEnd(minutes));
}


/// <summary>
/// The current in-game day (Monday, Tuesday, etc.).
/// </summary>
public static Day CurrentDay => (Day)S1GameTime.Instance.CurrentDay;

/// <summary>
/// The number of in-game days elapsed.
/// </summary>
public static int ElapsedDays => S1GameTime.Instance.ElapsedDays;

/// <summary>
/// The current 24-hour time (e.g., 1330 for 1:30 PM).
/// </summary>
public static int CurrentTime => S1GameTime.Instance.CurrentTime;

/// <summary>
/// Whether it is currently nighttime in-game.
/// </summary>
public static bool IsNight => S1GameTime.Instance.IsNight;

/// <summary>
/// Whether the game is currently at the end of the day (4:00 AM).
/// </summary>
public static bool IsEndOfDay => S1GameTime.Instance.IsEndOfDay;

/// <summary>
/// Whether the player is currently sleeping.
/// </summary>
public static bool SleepInProgress => S1GameTime.Instance.SleepInProgress;

/// <summary>
/// Whether the time is currently overridden (frozen or custom).
/// </summary>
public static bool TimeOverridden => S1GameTime.Instance.TimeOverridden;

/// <summary>
/// The current normalized time of day (0.0 = start, 1.0 = end).
/// </summary>
public static float NormalizedTime => S1GameTime.Instance.NormalizedTime;

/// <summary>
/// Total playtime (in seconds).
/// </summary>
public static float Playtime => S1GameTime.Instance.Playtime;

/// <summary>
/// Fast-forwards time to morning wake time (7:00 AM).
/// </summary>
public static void FastForwardToWakeTime() => S1GameTime.Instance.FastForwardToWakeTime();

/// <summary>
/// Sets the current time manually.
/// </summary>
public static void SetTime(int time24h, bool local = false) => S1GameTime.Instance.SetTime(time24h, local);

/// <summary>
/// Sets the number of elapsed in-game days.
/// </summary>
public static void SetElapsedDays(int days) => S1GameTime.Instance.SetElapsedDays(days);

/// <summary>
/// Gets the current time formatted in 12-hour AM/PM format.
/// </summary>
public static string GetFormatted12HourTime()
{
return S1GameTime.Get12HourTime(CurrentTime, true);
}

/// <summary>
/// Returns true if the current time is within the specified 24-hour range.
/// </summary>
public static bool IsCurrentTimeWithinRange(int startTime24h, int endTime24h)
{
return S1GameTime.Instance.IsCurrentTimeWithinRange(startTime24h, endTime24h);
}

/// <summary>
/// Converts 24-hour time to total minutes.
/// </summary>
public static int GetMinutesFrom24HourTime(int time24h)
{
return S1GameTime.GetMinSumFrom24HourTime(time24h);
}

/// <summary>
/// The current in-game day.
/// Converts total minutes into 24-hour time format.
/// </summary>
public static Day CurrentDay =>
(Day)S1GameTime.TimeManager.Instance.CurrentDay;
public static int Get24HourTimeFromMinutes(int minutes)
{
return S1GameTime.Get24HourTimeFromMinSum(minutes);
}
}
}
}
Loading