1
1
# Game Time API
2
2
3
- The Game Time API provides tools for working with and manipulating in-game time, including day/night cycles, seasons, and time-based events .
3
+ The Game Time API provides tools for working with and manipulating in-game time.
4
4
5
5
## Namespace
6
6
@@ -10,249 +10,138 @@ using S1API.GameTime;
10
10
11
11
## Key Classes
12
12
13
- ### GameClock
13
+ ### Day
14
14
15
- The main class for accessing and controlling the game's time system .
15
+ Enum representing days of the week .
16
16
17
17
``` csharp
18
- public static class GameClock
18
+ public enum Day
19
19
{
20
- // Current time properties
21
- public static int Hour { get ; }
22
- public static int Minute { get ; }
23
- public static int Day { get ; }
24
- public static int Month { get ; }
25
- public static int Year { get ; }
26
- public static Season CurrentSeason { get ; }
27
- public static WeatherType CurrentWeather { get ; }
28
- public static DayPhase CurrentDayPhase { get ; }
29
- public static bool IsNight { get ; }
30
-
31
- // Time flow control
32
- public static float TimeScale { get ; set ; }
33
- public static bool IsPaused { get ; }
34
-
35
- // Methods
36
- public static void AdvanceTime (int hours = 0 , int minutes = 0 , int days = 0 );
37
- public static void PauseTime ();
38
- public static void ResumeTime ();
39
- public static float GetDayProgress (); // 0.0 to 1.0 representing progress through the day
40
- public static TimeSpan GetTimeUntil (int hour , int minute );
41
- public static void SetTime (int hour , int minute );
42
- public static void SetDate (int day , int month , int year );
43
-
44
- // Formatted time strings
45
- public static string GetTimeString (bool use24HourFormat = false );
46
- public static string GetDateString (string format = " d MMM yyyy" );
20
+ Monday ,
21
+ Tuesday ,
22
+ Wednesday ,
23
+ Thursday ,
24
+ Friday ,
25
+ Saturday ,
26
+ Sunday
47
27
}
48
28
```
49
29
50
- ### Season
30
+ ### GameDateTime
51
31
52
- Enum representing seasons in the game .
32
+ Represents an in-game datetime (elapsed days and 24-hour time) .
53
33
54
34
``` csharp
55
- public enum Season
35
+ public struct GameDateTime
56
36
{
57
- Spring ,
58
- Summer ,
59
- Fall ,
60
- Winter
61
- }
62
- ```
63
-
64
- ### DayPhase
65
-
66
- Enum representing different phases of the day.
67
-
68
- ``` csharp
69
- public enum DayPhase
70
- {
71
- Dawn ,
72
- Morning ,
73
- Noon ,
74
- Afternoon ,
75
- Dusk ,
76
- Evening ,
77
- Midnight
78
- }
79
- ```
80
-
81
- ### WeatherType
82
-
83
- Enum representing different weather conditions.
84
-
85
- ``` csharp
86
- public enum WeatherType
87
- {
88
- Clear ,
89
- Cloudy ,
90
- Rainy ,
91
- Stormy ,
92
- Foggy ,
93
- Snowy
94
- }
95
- ```
96
-
97
- ### TimeEvent
98
-
99
- A class for scheduling events at specific times.
100
-
101
- ``` csharp
102
- public class TimeEvent
103
- {
104
- public string EventID { get ; }
105
- public string Description { get ; set ; }
106
- public bool IsRecurring { get ; set ; }
107
- public TimeSpan RecurrenceInterval { get ; set ; }
37
+ public int ElapsedDays ;
38
+ public int Time ;
108
39
109
- public TimeEvent (string eventID , string description , DateTime triggerTime );
110
- public TimeEvent (string eventID , string description , int hour , int minute , bool triggerToday = true );
40
+ // Constructors
41
+ public GameDateTime (int elapsedDays , int time );
42
+ public GameDateTime (int minSum );
111
43
112
- public void SetRecurring (TimeSpan interval );
113
- public void SetRecurringDaily (int hour , int minute );
114
- public void SetRecurringWeekly (DayOfWeek day , int hour , int minute );
115
- public void Cancel ();
44
+ // Methods
45
+ public int GetMinSum ();
46
+ public GameDateTime AddMinutes (int minutes );
47
+ public string GetFormattedTime ();
48
+ public bool IsNightTime ();
49
+ public bool IsSameDay (GameDateTime other );
50
+ public bool IsSameTime (GameDateTime other );
51
+ public override string ToString ();
52
+
53
+ // Operators
54
+ public static GameDateTime operator + (GameDateTime a , GameDateTime b );
55
+ public static GameDateTime operator - (GameDateTime a , GameDateTime b );
56
+ public static bool operator > (GameDateTime a , GameDateTime b );
57
+ public static bool operator < (GameDateTime a , GameDateTime b );
116
58
}
117
59
```
118
60
119
- ### TimeEventManager
61
+ ### TimeManager
120
62
121
- Static class for managing time-based events .
63
+ Provides access to various time management functions in the game .
122
64
123
65
``` csharp
124
- public static class TimeEventManager
66
+ public static class TimeManager
125
67
{
126
- public static event Action <TimeEvent > OnEventTriggered ;
68
+ // Events
69
+ public static Action OnDayPass ;
70
+ public static Action OnWeekPass ;
71
+ public static Action OnSleepStart ;
72
+ public static Action <int > OnSleepEnd ;
127
73
128
- public static void RegisterEvent (TimeEvent timeEvent );
129
- public static void UnregisterEvent (string eventID );
130
- public static TimeEvent GetEvent (string eventID );
131
- public static List <TimeEvent > GetUpcomingEvents (int maxCount = 10 );
74
+ // Properties
75
+ public static Day CurrentDay { get ; }
76
+ public static int ElapsedDays { get ; }
77
+ public static int CurrentTime { get ; }
78
+ public static bool IsNight { get ; }
79
+ public static bool IsEndOfDay { get ; }
80
+ public static bool SleepInProgress { get ; }
81
+ public static bool TimeOverridden { get ; }
82
+ public static float NormalizedTime { get ; }
83
+ public static float Playtime { get ; }
84
+
85
+ // Methods
86
+ public static void FastForwardToWakeTime ();
87
+ public static void SetTime (int time24h , bool local = false );
88
+ public static void SetElapsedDays (int days );
89
+ public static string GetFormatted12HourTime ();
90
+ public static bool IsCurrentTimeWithinRange (int startTime24h , int endTime24h );
91
+ public static int GetMinutesFrom24HourTime (int time24h );
92
+ public static int Get24HourTimeFromMinutes (int minutes );
132
93
}
133
94
```
134
95
135
96
## Usage Examples
136
97
137
- ### Basic Time Manipulation
98
+ ### Working with GameDateTime
138
99
139
100
``` csharp
140
- // Get current time information
141
- int currentHour = GameClock .Hour ;
142
- int currentMinute = GameClock .Minute ;
143
- string timeString = GameClock .GetTimeString (); // e.g. "3:45 PM"
144
- string dateString = GameClock .GetDateString (); // e.g. "15 Jun 2025"
145
-
146
- // Check time conditions
147
- bool isNighttime = GameClock .IsNight ;
148
- bool isWinter = GameClock .CurrentSeason == Season .Winter ;
149
- bool isStormy = GameClock .CurrentWeather == WeatherType .Stormy ;
150
-
151
- Console .WriteLine ($" It's {timeString } on {dateString }" );
152
- Console .WriteLine ($" Current day phase: {GameClock .CurrentDayPhase }" );
153
-
154
- // Manipulate time
155
- GameClock .AdvanceTime (hours : 2 , minutes : 30 ); // Advance 2.5 hours
156
- GameClock .SetTime (18 , 0 ); // Set time to 6:00 PM
157
- ```
158
-
159
- ### Time Flow Control
101
+ // Create a new game date time (day 3, 2:30 PM)
102
+ GameDateTime dateTime = new GameDateTime (3 , 1430 );
160
103
161
- ``` csharp
162
- // Speed up time (2x normal speed)
163
- GameClock .TimeScale = 2 . 0 f ;
104
+ // Add 60 minutes
105
+ GameDateTime later = dateTime .AddMinutes (60 );
164
106
165
- // Pause time for a cutscene
166
- GameClock . PauseTime ();
107
+ // Check if it's night time
108
+ bool isNight = dateTime . IsNightTime ();
167
109
168
- // Resume normal time flow
169
- GameClock .ResumeTime ();
170
- GameClock .TimeScale = 1 . 0 f ;
110
+ // Format the time
111
+ string timeString = dateTime .GetFormattedTime (); // e.g., "2:30 PM"
171
112
172
- // Skip to morning
173
- if (GameClock .IsNight )
174
- {
175
- // Calculate hours until 7:00 AM
176
- var hoursUntilMorning = GameClock .GetTimeUntil (7 , 0 ).TotalHours ;
177
- GameClock .AdvanceTime (hours : (int )hoursUntilMorning );
178
- }
113
+ // Compare game date times
114
+ bool sameDay = dateTime .IsSameDay (later );
115
+ bool laterInTime = later > dateTime ;
179
116
```
180
117
181
- ### Scheduling Time Events
118
+ ### Using TimeManager
182
119
183
120
``` csharp
184
- // Create a one-time event for shop closing
185
- var shopClosingEvent = new TimeEvent (
186
- " mymod.shop_closing" ,
187
- " Local shop closes for the night" ,
188
- hour : 20 ,
189
- minute : 0 ,
190
- triggerToday : true
191
- );
192
-
193
- // Register the event
194
- TimeEventManager .RegisterEvent (shopClosingEvent );
121
+ // Get current day and time information
122
+ Day today = TimeManager .CurrentDay ;
123
+ int currentTime = TimeManager .CurrentTime ;
124
+ bool isNightTime = TimeManager .IsNight ;
125
+ float dayProgress = TimeManager .NormalizedTime ; // 0.0 to 1.0
195
126
196
- // Create a recurring daily event for shop opening
197
- var shopOpeningEvent = new TimeEvent (
198
- " mymod.shop_opening" ,
199
- " Local shop opens for the day" ,
200
- hour : 8 ,
201
- minute : 0
202
- );
203
- shopOpeningEvent .SetRecurringDaily (8 , 0 );
127
+ // Format current time
128
+ string timeString = TimeManager .GetFormatted12HourTime (); // e.g., "2:30 PM"
204
129
205
- // Register the event
206
- TimeEventManager . RegisterEvent ( shopOpeningEvent );
130
+ // Check time conditions
131
+ bool isOpeningHours = TimeManager . IsCurrentTimeWithinRange ( 900 , 1700 ); // 9 AM to 5 PM
207
132
208
- // Listen for events
209
- TimeEventManager .OnEventTriggered += (timeEvent ) => {
210
- if (timeEvent .EventID == " mymod.shop_closing" )
211
- {
212
- // Handle shop closing
213
- CloseShop ();
214
- DisplayNotification (" The shop has closed for the night!" );
215
- }
216
- else if (timeEvent .EventID == " mymod.shop_opening" )
217
- {
218
- // Handle shop opening
219
- OpenShop ();
220
- DisplayNotification (" The shop is now open!" );
221
- }
133
+ // Manipulate time
134
+ TimeManager .SetTime (1800 ); // Set to 6:00 PM
135
+ TimeManager .SetElapsedDays (10 ); // Set to day 10
136
+ TimeManager .FastForwardToWakeTime (); // Skip to 7 AM
137
+
138
+ // Subscribe to time events
139
+ TimeManager .OnDayPass += () => {
140
+ // Handle new day starting
141
+ Console .WriteLine (" A new day has begun!" );
222
142
};
223
143
224
- // Get upcoming events
225
- var upcomingEvents = TimeEventManager .GetUpcomingEvents (5 );
226
- foreach (var evt in upcomingEvents )
227
- {
228
- Console .WriteLine ($" Upcoming: {evt .Description }" );
229
- }
230
- ```
231
-
232
- ## Events
233
-
234
- The Game Time API provides events to hook into time changes:
235
-
236
- ``` csharp
237
- // Time changes
238
- GameClock .OnHourChanged += (oldHour , newHour ) => { /* ... */ };
239
- GameClock .OnDayChanged += (oldDay , newDay ) => { /* ... */ };
240
- GameClock .OnSeasonChanged += (oldSeason , newSeason ) => { /* ... */ };
241
-
242
- // Day/night cycle
243
- GameClock .OnDayPhaseChanged += (oldPhase , newPhase ) => { /* ... */ };
244
- GameClock .OnNightBegin += () => { /* ... */ };
245
- GameClock .OnDayBegin += () => { /* ... */ };
246
-
247
- // Weather changes
248
- GameClock .OnWeatherChanged += (oldWeather , newWeather ) => { /* ... */ };
249
- ```
250
-
251
- ## Best Practices
252
-
253
- 1 . Use the GameClock for all time-related operations rather than implementing your own time system
254
- 2 . Consider performance when scheduling many recurring events
255
- 3 . Be mindful of time scale changes and how they affect gameplay
256
- 4 . Use time events for scheduling rather than continuously checking time conditions
257
- 5 . When advancing time, consider the impact on scheduled events and NPC schedules
258
- 6 . Use the day/night and season events to adjust your mod's behavior appropriately
144
+ TimeManager .OnSleepEnd += (minutesSlept ) => {
145
+ Console .WriteLine ($" Good morning! You slept for {minutesSlept / 60 } hours." );
146
+ };
147
+ ```
0 commit comments