tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
712 lines (437 loc) β’ 20.9 kB
Markdown
# π TinyDayNightCycle Documentation
**TinyDayNightCycle** is a lightweight and flexible JavaScript library designed to simulate day and night cycles along with seasonal changes, moons, weather patterns, and in-game time tracking. It allows you to manage and customize time progression, including hours, minutes, and seconds, as well as date transitions with support for variable month lengths and years.
This system also supports dynamic weather changes, moon phases, and seasonal adjustments, making it ideal for game development, simulations, or any interactive experience that requires realistic or custom time and environmental cycles.
With easy-to-use methods and configurable settings, TinyDayNightCycle provides you with precise control over the flow of time and weather in your application.
### π Type Definitions
#### `SelectedWeather`
Represents a mapping of weather type names to their selected values.
Each key is a weather type name, and the value is either:
- A string containing the selected weather configuration name, or
- `null` if no selection has been made.
```js
typedef {Record<string, string|null>} SelectedWeather
```
#### `WeatherCallback`
A callback used to dynamically calculate weather probabilities.
**Parameters:**
* `configs` (Object): Contextual info about the current time and weather state
* `hour` (number): Current in-game hour (0β23)
* `minute` (number): Current in-game minute (0β59)
* `currentMinutes` (number): Minutes since midnight (0β1439)
* `isDay` (boolean): Whether it's currently daytime
* `season` (string): Current season
* `weather` (SelectedWeather): Current active weather types or null if none
**Returns:**
* (number): Probability weight for the weather type
```js
@callback WeatherCallback
```
#### `WeatherCfgs`
Represents the complete set of weather configurations.
| Property | Description |
| --------- | ---------------------------------------------------------------- |
| `default` | Default weather configuration when no specific condition matches |
| `day` | Weather configuration used during daytime hours |
| `night` | Weather configuration used during nighttime hours |
| `hours` | Specific weather configurations mapped by hour (e.g., `"06"`) |
| `seasons` | Specific weather configurations mapped by season name |
```js
typedef {Object} WeatherCfgs
```
#### `WeatherCfg`
A weather configuration object mapping weather types to either:
* Numeric probability weights, or
* Dynamic `WeatherCallback` functions.
```js
typedef {Record<string, number | WeatherCallback>} WeatherCfg
```
#### `WeatherData`
Resolved weather data with numeric probability values only.
```js
typedef {Record<string, number>} WeatherData
```
#### `MoonData`
Data for a moon's current phase.
| Property | Description |
| ------------- | ------------------------------------------ |
| `name` | Name of the moon |
| `phaseIndex` | Current phase index (zero-based) |
| `phaseName` | Descriptive name of the current phase |
| `cycleLength` | Total phases/days in the moon's full cycle |
```js
typedef {Object} MoonData
```
#### `MoonRaw`
Raw data structure for a moon's cycle.
| Property | Description |
| -------------- | ---------------------------------------------- |
| `name` | Name of the moon |
| `cycleLength` | Total number of phases in the moon's cycle |
| `currentPhase` | Current phase index (0-based) |
| `phaseNames` | Optional list of names for each phase in cycle |
```js
typedef {Object} MoonRaw
```
### βοΈ TinyDayNightCycle Class
`TinyDayNightCycle` is a simulation system that handles:
* Customizable day/night cycle with variable sunrise and sunset
* Calendar system with adjustable month lengths
* Dynamic weather with multiple configurable probability layers
* Multi-moon phase tracking
All time and date calculations are independent from real-world time and can run at any speed.
```js
class TinyDayNightCycle {
// implementation...
}
```
### π Properties, Getters, Setters & Constructor
#### ποΈ Private Fields
| Property | Type | Description |
|--------------------|---------------------|-----------------------------------------------------------------------------------------------|
| `#seasons` | `Map<string, number[]>` | Season configurations mapping each season name to an array of months. |
| `#moons` | `MoonRaw[]` | Array of tracked moons with independent phases. |
| `#dayStart` | `number` | Hour at which the day starts (0β23). |
| `#nightStart` | `number` | Hour at which the night starts (0β23). |
| `#weather` | `SelectedWeather` | Currently active weather types (strings or nulls). |
| `#currentSeconds` | `number` | Current time in seconds since midnight (0β86399). |
| `#currentMinutes` | `number` | Current time in minutes since midnight (0β1439). |
| `#currentHours` | `number` | Current time in hours since midnight (0β24). |
| `#currentSeason` | `string` | Current season name. |
| `#currentDay` | `number` | Current day of the month. |
| `#currentMonth` | `number` | Current month number. |
| `#currentYear` | `number` | Current year count. |
| `#isDestroyed` | `boolean` | Flag indicating whether the instance has been destroyed. |
| `#monthDays` | `number[]` | Number of days in each month (default all 31). |
| `#weatherConfig` | `WeatherCfgs` | Weather configuration layers, including defaults, day/night, hours, and seasons. |
| `#weatherDuration` | `{min: number, max: number}` | Range for duration of weather types in minutes. |
| `#weatherTimeLeft` | `number` | Minutes remaining until current weather expires. |
#### βοΈ Getters
- **`isDestroyed`** β Returns whether the instance is destroyed.
_Returns_: `boolean`
- **`currentSeconds`** β Current time in seconds since midnight.
_Returns_: `number` (0 to 86399)
- **`currentMinutes`** β Current time in minutes since midnight.
_Returns_: `number` (0 to 1439)
- **`currentHours`** β Current time in hours since midnight, may be decimal (e.g., 14.5 = 14:30).
_Returns_: `number` (0 to <24)
- **`moons`** β Array of moons with current phase details (`MoonData[]`).
_Returns_: `MoonData[]`
- **`seasons`** β List of all configured season names.
_Returns_: `string[]`
- **`dayStart`** β Hour the day starts (0β23).
_Returns_: `number`
- **`nightStart`** β Hour the night starts (0β23).
_Returns_: `number`
- **`weather`** β Current weather type(s) as a shallow copy.
_Returns_: `SelectedWeather`
- **`currentSeason`** β Current active season name.
_Returns_: `string`
- **`currentDay`** β Current day of the month.
_Returns_: `number`
- **`currentMonth`** β Current month number.
_Returns_: `number`
- **`currentYear`** β Current year count.
_Returns_: `number`
- **`monthDays`** β Copy of month-to-days mapping for calendar.
_Returns_: `number[]`
- **`weatherDuration`** β Range of weather durations (min/max minutes).
_Returns_: `{min: number, max: number}`
- **`weatherTimeLeft`** β Minutes left until weather changes.
_Returns_: `number`
- **`weatherConfig`** β Deep copy of complete weather configuration object.
_Returns_: `WeatherCfgs`
#### βοΈ Setters
- **`currentSeconds`** β Sets current time in seconds (0β86399). Updates minutes and hours accordingly.
_Throws_: `TypeError | RangeError`
- **`currentMinutes`** β Sets current time in minutes (0β1439). Updates seconds and hours accordingly.
_Throws_: `TypeError | RangeError`
- **`currentHours`** β Sets current time in hours (0β<24), decimals allowed. Updates minutes and seconds accordingly.
_Throws_: `TypeError | RangeError`
- **`dayStart`** β Sets hour day starts (must be positive number).
_Throws_: `TypeError`
- **`nightStart`** β Sets hour night starts (must be positive number).
_Throws_: `TypeError`
- **`weather`** β Sets current weather object with string values or nulls.
_Throws_: `TypeError`
- **`currentSeason`** β Sets current season, must be configured season name.
_Throws_: `TypeError`
- **`currentDay`** β Sets current day of month (valid day according to month).
_Throws_: `TypeError`
- **`currentMonth`** β Sets current month number (must be valid month).
_Throws_: `TypeError`
- **`currentYear`** β Sets current year (positive non-zero number).
_Throws_: `TypeError`
- **`monthDays`** β Sets custom days per month array.
_Throws_: `TypeError`
- **`weatherDuration`** β Sets min/max range for weather durations in minutes.
_Throws_: `TypeError`
- **`weatherTimeLeft`** β Sets remaining time for current weather in minutes.
_Throws_: `TypeError`
- **`weatherConfig`** β Sets full weather configuration object with validation.
_Throws_: `TypeError`
#### βοΈ Constructor
```js
/**
* Creates a new TinyDayNightCycle instance.
* @param {number} [dayStart=6] - Hour when the day starts (default 6).
* @param {number} [nightStart=18] - Hour when the night starts (default 18).
*/
constructor(dayStart = 6, nightStart = 18)
```
Initializes the cycle with provided start times for day and night.
### π± SEASON SYSTEM
#### `addSeason(name, values)`
β Adds or updates a season with the given name and associated months.
* **Parameters:**
* `name` (string) β Name of the season.
* `values` (number\[]) β Array of month numbers linked to this season.
* **Throws:**
* `TypeError` if `name` is not a string or `values` is not an array of numbers.
#### `removeSeason(name)`
β Removes a season by name. If the removed season is the current season, resets current season to empty.
* **Parameters:**
* `name` (string) β Name of the season to remove.
* **Throws:**
* `TypeError` if `name` is not a string.
#### `hasSeason(name)`
π Checks if a season exists.
* **Parameters:**
* `name` (string) β Season name to check.
* **Returns:**
* `boolean` β `true` if the season exists, otherwise `false`.
* **Throws:**
* `TypeError` if `name` is not a string.
#### `getSeason(name)`
π
Retrieves the months linked to a given season.
* **Parameters:**
* `name` (string) β Season name.
* **Returns:**
* `number[]` β Array of month numbers for the season.
* **Throws:**
* `TypeError` if `name` is not a string.
* `Error` if the season does not exist.
### β° TIME SYSTEM
#### `setTime({hour = 0, minute = 0, second = 0})`
π°οΈ Sets the internal time directly.
* **Parameters:**
* `hour` (number, 0β23) β Hour to set.
* `minute` (number, 0β59) β Minute to set.
* `second` (number, 0β59) β Second to set.
#### `addTime({hours = 0, minutes = 0, seconds = 0})`
β Adds time to the current clock, automatically rolling days if needed.
* **Parameters:**
* `hours` (number) β Hours to add.
* `minutes` (number) β Minutes to add.
* `seconds` (number) β Seconds to add.
#### `getTime(settings)`
β³ Gets the current time as an object and formatted string, using the in-game time scale.
* **Parameters:**
* `settings` (object, optional) β Configuration object with the following optional properties:
* `hourSize` (`number`) β Number of in-game seconds representing an hour. Default: current internal `hourSize`.
* `minuteSize` (`number`) β Number of in-game seconds representing a minute. Default: current internal `minuteSize`.
* `withSeconds` (`boolean`) β Whether to include seconds in the formatted string. Default: `false`.
* **Returns:**
An object with the following properties:
```ts
{
hour: number; // current hour (integer)
minute: number; // current minute (integer)
second: number; // current second (integer)
formatted: string; // formatted time string, e.g. "14:05" or "14:05:30"
}
```
#### `isDay()`
βοΈ Determines if current time is considered day based on configured day/night start hours.
* **Returns:**
* `boolean` β `true` if it is day, `false` if night.
#### Time Until Day/Night Methods
Calculates time left until the next day or night period starts.
* `minutesUntilDay()` β Minutes until day start.
* `secondsUntilDay()` β Seconds until day start.
* `hoursUntilDay()` β Hours until day start.
* `minutesUntilNight()` β Minutes until night start.
* `secondsUntilNight()` β Seconds until night start.
* `hoursUntilNight()` β Hours until night start.
#### `timeUntil(targetHour, unit)`
β Helper calculating time difference until a specific hour.
* **Parameters:**
* `targetHour` (number) β Target hour (0β23).
* `unit` ('minutes' | 'seconds' | 'hours') β Unit of returned value.
* **Returns:**
* Number of units until target hour.
#### `setTo(phase)`
β‘ Instantly sets time to start of a phase ("day" or "night").
* **Parameters:**
* `phase` ("day" | "night") β Phase to jump to.
### π
DAY/MONTH/YEAR SYSTEM
#### `nextDay(amount = 1)`
βΆοΈ Advances the current date by given days, wraps months and years, updates season and moons.
* **Parameters:**
* `amount` (number) β Number of days to advance.
#### `prevDay(amount = 1)`
βοΈ Moves the current date backward by given days, wraps months and years, updates season and moons.
* **Parameters:**
* `amount` (number) β Number of days to rewind.
#### `updateSeason()`
π Updates the current season based on the current month.
### π¦οΈ WEATHER SYSTEM
#### `setWeatherConfig(config)`
βοΈ Sets the weather configuration object.
* **Parameters:**
* `config` (WeatherCfgs) β Object containing default, time-based, day/night, and seasonal weather probabilities.
#### `setWeatherDuration(minMinutes, maxMinutes)`
β²οΈ Sets minimum and maximum duration for weather in minutes.
* **Parameters:**
* `minMinutes` (number) β Minimum duration in minutes.
* `maxMinutes` (number) β Maximum duration in minutes.
#### `updateWeatherTimer(minutesPassed)`
β³ Updates the weather timer and triggers new weather if time expires.
* **Parameters:**
* `minutesPassed` (number) β Minutes passed since last update.
#### `forceWeather({ where = 'main', type, duration = null })`
π― Forces weather to a specific type optionally for a given duration.
* **Parameters:**
* `where` (string) β Weather zone key (default `"main"`).
* `type` (string | null) β Weather type to force.
* `duration` (number | null) β Duration in minutes; if `null`, random duration is used.
#### `chooseNewWeather({ customWeather, where = 'main' } = {})`
π² Chooses new weather based on weighted probabilities from config, current time, season, and custom overrides.
* **Parameters:**
* `customWeather` (WeatherCfg) β Optional overrides for probabilities.
* `where` (string) β Weather zone key (default `"main"`).
* **Returns:**
* `string | null` β Selected weather type or null if none chosen.
#### `_randomInRange(min, max)`
π² Returns a random integer between min and max inclusive.
* **Parameters:**
* `min` (number) β Minimum integer.
* `max` (number) β Maximum integer.
* **Returns:**
* Random integer in the range \[min, max].
### π MOON SYSTEM
#### `addMoon(name, cycleLength, phaseNames, startingPhase = 0)`
π Adds a new moon to the system with cycle length and optional phase names.
* **Parameters:**
* `name` (string) β Moon name.
* `cycleLength` (number) β Number of days in the moon cycle (minimum 1).
* `phaseNames` (string\[]) β Optional array of phase names.
* `startingPhase` (number, optional) β Initial phase index (default is 0).
* **Returns:**
* `number` β Index of the newly added moon.
#### `removeMoon(name)`
π Removes a moon by its name.
* **Parameters:**
* `name` (string) β Name of the moon to remove.
#### `advanceMoons(days = 1)`
β‘οΈ Advances all moons forward by the specified number of days.
* **Parameters:**
* `days` (number) β Days to advance (default 1).
#### `rewindMoons(days = 1)`
β¬
οΈ Moves all moons backward by the specified number of days.
* **Parameters:**
* `days` (number) β Days to rewind (default 1).
#### `advanceMoon(moonIndex, days = 1)`
β© Advances a single moonβs phase by given days.
* **Parameters:**
* `moonIndex` (number) β Index of the moon to advance.
* `days` (number) β Number of days to advance (default 1).
* **Throws:**
* `RangeError` if moonIndex is invalid.
#### `rewindMoon(moonIndex, days = 1)`
βͺ Rewinds a single moonβs phase by given days.
* **Parameters:**
* `moonIndex` (number) β Index of the moon to rewind.
* `days` (number) β Number of days to rewind (default 1).
* **Throws:**
* `RangeError` if moonIndex is invalid.
#### `moonExists(index)`
π Checks if a moon exists at the given index.
* **Parameters:**
* `index` (number) β Moon index to check.
* **Returns:**
* `boolean` β `true` if moon exists, else `false`.
#### `getMoon(index)`
π Retrieves moon data including name, current phase, phase name, and cycle length.
* **Parameters:**
* `index` (number | MoonRaw) β Moon index or a MoonRaw object.
* **Returns:**
* `MoonData` β Object with moon details:
* `name` (string)
* `phaseIndex` (number)
* `phaseName` (string)
* `cycleLength` (number)
* **Throws:**
* `TypeError` if argument is not a valid index or MoonRaw object.
* `RangeError` if index is invalid.
* `Error` if MoonRaw is missing required properties.
### β³ Day, Hour & Minute Sizes
* **`autoSizeAdjuste`** β Stores whether proportional recalculation of `daySize`, `hourSize`, and `minuteSize` should occur automatically when one of them is updated.
* **Getter:** Returns whether automatic proportional size adjustment is enabled.
* **Setter:** Sets whether automatic proportional size adjustment is enabled.
* **`daySize`** β Gets or sets the number of in-game seconds representing a full day.
Keeps the proportion for `hourSize` and `minuteSize` if `autoSizeAdjuste` is enabled.
* **Getter:** Returns the current value.
* **Setter:** Accepts a positive finite number; throws an error otherwise.
* **`hourSize`** β Gets or sets the number of in-game seconds representing a full hour.
Keeps the proportion for `daySize` and `minuteSize` if `autoSizeAdjuste` is enabled.
* **Getter:** Returns the current value.
* **Setter:** Accepts a positive finite number; throws an error otherwise.
* **`minuteSize`** β Gets or sets the number of in-game seconds representing a full minute.
Keeps the proportion for `hourSize` and `daySize` if `autoSizeAdjuste` is enabled.
* **Getter:** Returns the current value.
* **Setter:** Accepts a positive finite number; throws an error otherwise.
> β οΈ Changing these values dynamically will affect how in-game time progresses.
### β οΈ INTERNAL UTILITIES
#### `_checkDestroyed()`
π Checks if the instance has been destroyed and throws if so.
* **Throws:**
* `Error` if instance is already destroyed.
#### `destroy()`
π₯ Destroys the instance, clearing all data and marking it unusable.
* Clears all internal collections and resets primitive values.
* After calling, any further method calls should throw or be ignored.