@starktma/minecraft-utils 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ export declare const NAMESPACE = "inventory-manager";
2
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1,2 @@
1
+ export const NAMESPACE = 'inventory-manager';
2
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/database/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG,mBAAmB,CAAC"}
@@ -0,0 +1,87 @@
1
+ import { Entity } from "@minecraft/server";
2
+ import { SimpleObject } from "./interfaces";
3
+ /**
4
+ * SimpleDatabase is a base class for databases that store custom objects with an id property.
5
+ * It provides methods for adding, updating, removing and retrieving objects from the database.
6
+ * A singleton pattern is used to ensure that only one instance of the database exists.
7
+ *
8
+ * @example
9
+ * class MyDatabase extends SimpleDatabase<PlayerObject> {
10
+ * protected static instance: MyDatabase;
11
+ * constructor() {
12
+ * super("myDatabase", undefined);
13
+ * }
14
+ *
15
+ * static getInstance(): MyDatabase {
16
+ * if (!MyDatabase.instance) {
17
+ * MyDatabase.instance = new MyDatabase();
18
+ * }
19
+ * return MyDatabase.instance;
20
+ * }
21
+ */
22
+ export declare class SimpleDatabase<T extends SimpleObject> {
23
+ private mainDB;
24
+ private localDB;
25
+ protected databaseName: string;
26
+ /**
27
+ * The constructor initializes the local database and syncs it with the main database.
28
+ * @param databaseName The name of the database.
29
+ * @param target The target entity to store the database in. If undefined, the database is stored in the world.
30
+ */
31
+ protected constructor(databaseName: string, target?: Entity | undefined);
32
+ /**
33
+ * Updates the main database with the local database.
34
+ * This method is called automatically when an object is added, updated or removed.
35
+ * @private
36
+ */
37
+ private updateMainDB;
38
+ /**
39
+ * Retrieves the main database.
40
+ * @private
41
+ * @returns The main database.
42
+ */
43
+ private getMainDB;
44
+ /**
45
+ * Adds an object to the local database and updates the main database.
46
+ * @param object The object to be added.
47
+ */
48
+ addObject(object: T): void;
49
+ /**
50
+ * Updates an object in the local database and the main database.
51
+ * If the object does not exist, it is added.
52
+ * @param object The object to be updated.
53
+ */
54
+ updateObject(object: T): void;
55
+ /**
56
+ * Checks if an object with the given id exists in the local database.
57
+ * @param id The id of the object.
58
+ * @returns True if the object exists, false otherwise.
59
+ */
60
+ hasObject(id: string): boolean;
61
+ /**
62
+ * Retrieves an object with the given id from the local database.
63
+ * @param id The id of the object.
64
+ * @returns The object if it exists, undefined otherwise.
65
+ */
66
+ getObject(id: string): T | undefined;
67
+ /**
68
+ * Removes an object with the given id from the local database and updates the main database.
69
+ * @param id The id of the object.
70
+ */
71
+ removeObject(id: string): void;
72
+ /**
73
+ * Retrieves all objects from the local database.
74
+ * @returns An array of all objects in the local database.
75
+ */
76
+ getAllObjects(): T[];
77
+ /**
78
+ * Removes all objects from the local database and updates the main database.
79
+ */
80
+ eraseAllObjects(): void;
81
+ /**
82
+ * Iterates over all objects in the local database.
83
+ * @param callback The function to be called for each object.
84
+ */
85
+ forEach(callback: (object: SimpleObject, index: number) => void): void;
86
+ }
87
+ //# sourceMappingURL=database.d.ts.map
@@ -0,0 +1,256 @@
1
+ import { Entity, world } from "@minecraft/server";
2
+ //@ts-ignore
3
+ import { NAMESPACE } from "./constants";
4
+ /**
5
+ * DatabaseManager is a class that manages databases stored in Minecraft's world properties.
6
+ * Currently only supports JSON databases.
7
+ */
8
+ class DatabaseManager {
9
+ static DYNAMIC_PROP_MAX_LENGTH = 32767;
10
+ static CHUNK_KEY = "__SPLIT__";
11
+ target;
12
+ constructor(target) {
13
+ if (target instanceof Entity) {
14
+ this.target = target;
15
+ }
16
+ else {
17
+ this.target = world;
18
+ }
19
+ }
20
+ /**
21
+ * Checks if a JSON database with the given name exists.
22
+ * @param databaseName The name of the database.
23
+ * @returns True if the database exists, false otherwise.
24
+ */
25
+ hasJSONDatabase(databaseName) {
26
+ return this.target.getDynamicProperty(NAMESPACE + ":" + databaseName) !== undefined;
27
+ }
28
+ /**
29
+ * Adds a new JSON database with the given name and data.
30
+ * @param databaseName The name of the database.
31
+ * @param database The data to be stored in the database.
32
+ */
33
+ addJSONDatabase(databaseName, database) {
34
+ const propertyName = `${NAMESPACE}:${databaseName}`;
35
+ const jsonString = JSON.stringify(database);
36
+ const existingProp = this.target.getDynamicProperty(propertyName);
37
+ let existingChunks = 0;
38
+ if (existingProp) {
39
+ try {
40
+ const propObj = JSON.parse(existingProp);
41
+ if (propObj && typeof propObj === "object" && DatabaseManager.CHUNK_KEY in propObj) {
42
+ existingChunks = propObj[DatabaseManager.CHUNK_KEY];
43
+ }
44
+ }
45
+ catch { }
46
+ }
47
+ if (jsonString.length <= DatabaseManager.DYNAMIC_PROP_MAX_LENGTH || jsonString.length === 0) {
48
+ if (existingChunks > 0) {
49
+ for (let i = 0; i < existingChunks; i++) {
50
+ const partName = `${propertyName}_${i}`;
51
+ this.target.setDynamicProperty(partName, undefined);
52
+ }
53
+ }
54
+ this.target.setDynamicProperty(propertyName, jsonString);
55
+ }
56
+ else {
57
+ const chunkSize = DatabaseManager.DYNAMIC_PROP_MAX_LENGTH;
58
+ const chunkCount = Math.ceil(jsonString.length / chunkSize);
59
+ if (existingChunks > 0) {
60
+ for (let i = 0; i < existingChunks; i++) {
61
+ const oldPartName = `${propertyName}_${i}`;
62
+ this.target.setDynamicProperty(oldPartName, undefined);
63
+ }
64
+ }
65
+ for (let i = 0; i < chunkCount; i++) {
66
+ const start = i * chunkSize;
67
+ const end = start + chunkSize;
68
+ const chunk = jsonString.slice(start, end);
69
+ const partName = `${propertyName}_${i}`;
70
+ this.target.setDynamicProperty(partName, chunk);
71
+ }
72
+ const meta = { [DatabaseManager.CHUNK_KEY]: chunkCount };
73
+ this.target.setDynamicProperty(propertyName, JSON.stringify(meta));
74
+ }
75
+ }
76
+ /**
77
+ * Removes a JSON database with the given name.
78
+ * @param databaseName The name of the database.
79
+ */
80
+ removeJSONDatabase(databaseName) {
81
+ const propertyName = `${NAMESPACE}:${databaseName}`;
82
+ const propString = this.target.getDynamicProperty(propertyName);
83
+ if (propString !== undefined) {
84
+ try {
85
+ const propObj = JSON.parse(propString);
86
+ if (propObj && typeof propObj === "object" && DatabaseManager.CHUNK_KEY in propObj) {
87
+ const chunkCount = propObj[DatabaseManager.CHUNK_KEY];
88
+ for (let i = 0; i < chunkCount; i++) {
89
+ const partName = `${propertyName}_${i}`;
90
+ this.target.setDynamicProperty(partName, undefined);
91
+ }
92
+ }
93
+ }
94
+ catch { }
95
+ this.target.setDynamicProperty(propertyName, undefined);
96
+ }
97
+ }
98
+ /**
99
+ * Retrieves a JSON database with the given name.
100
+ * @param databaseName The name of the database.
101
+ * @returns The data stored in the database.
102
+ * @throws An error if the database does not exist.
103
+ */
104
+ getJSONDatabase(databaseName) {
105
+ const propertyName = `${NAMESPACE}:${databaseName}`;
106
+ const propString = this.target.getDynamicProperty(propertyName);
107
+ if (propString === undefined) {
108
+ throw new Error("Database does not exist");
109
+ }
110
+ try {
111
+ const propObj = JSON.parse(propString);
112
+ if (propObj && typeof propObj === "object" && DatabaseManager.CHUNK_KEY in propObj) {
113
+ const chunkCount = propObj[DatabaseManager.CHUNK_KEY];
114
+ let combined = "";
115
+ for (let i = 0; i < chunkCount; i++) {
116
+ const partName = `${propertyName}_${i}`;
117
+ const part = this.target.getDynamicProperty(partName);
118
+ if (typeof part === "string") {
119
+ combined += part;
120
+ }
121
+ else {
122
+ combined += "";
123
+ }
124
+ }
125
+ return JSON.parse(combined);
126
+ }
127
+ else {
128
+ return JSON.parse(propString);
129
+ }
130
+ }
131
+ catch {
132
+ throw new Error("Failed to parse database JSON");
133
+ }
134
+ }
135
+ }
136
+ /**
137
+ * SimpleDatabase is a base class for databases that store custom objects with an id property.
138
+ * It provides methods for adding, updating, removing and retrieving objects from the database.
139
+ * A singleton pattern is used to ensure that only one instance of the database exists.
140
+ *
141
+ * @example
142
+ * class MyDatabase extends SimpleDatabase<PlayerObject> {
143
+ * protected static instance: MyDatabase;
144
+ * constructor() {
145
+ * super("myDatabase", undefined);
146
+ * }
147
+ *
148
+ * static getInstance(): MyDatabase {
149
+ * if (!MyDatabase.instance) {
150
+ * MyDatabase.instance = new MyDatabase();
151
+ * }
152
+ * return MyDatabase.instance;
153
+ * }
154
+ */
155
+ export class SimpleDatabase {
156
+ mainDB;
157
+ localDB;
158
+ databaseName;
159
+ /**
160
+ * The constructor initializes the local database and syncs it with the main database.
161
+ * @param databaseName The name of the database.
162
+ * @param target The target entity to store the database in. If undefined, the database is stored in the world.
163
+ */
164
+ constructor(databaseName, target) {
165
+ this.mainDB = new DatabaseManager(target);
166
+ this.databaseName = databaseName;
167
+ if (this.mainDB.hasJSONDatabase(this.databaseName)) {
168
+ this.localDB = this.getMainDB();
169
+ }
170
+ else {
171
+ this.localDB = [];
172
+ this.updateMainDB();
173
+ }
174
+ }
175
+ /**
176
+ * Updates the main database with the local database.
177
+ * This method is called automatically when an object is added, updated or removed.
178
+ * @private
179
+ */
180
+ updateMainDB() {
181
+ this.mainDB.addJSONDatabase(this.databaseName, this.localDB);
182
+ }
183
+ /**
184
+ * Retrieves the main database.
185
+ * @private
186
+ * @returns The main database.
187
+ */
188
+ getMainDB() {
189
+ return this.mainDB.getJSONDatabase(this.databaseName);
190
+ }
191
+ /**
192
+ * Adds an object to the local database and updates the main database.
193
+ * @param object The object to be added.
194
+ */
195
+ addObject(object) {
196
+ this.localDB.push(object);
197
+ this.updateMainDB();
198
+ }
199
+ /**
200
+ * Updates an object in the local database and the main database.
201
+ * If the object does not exist, it is added.
202
+ * @param object The object to be updated.
203
+ */
204
+ updateObject(object) {
205
+ if (this.hasObject(object.id)) {
206
+ this.removeObject(object.id);
207
+ }
208
+ this.addObject(object);
209
+ }
210
+ /**
211
+ * Checks if an object with the given id exists in the local database.
212
+ * @param id The id of the object.
213
+ * @returns True if the object exists, false otherwise.
214
+ */
215
+ hasObject(id) {
216
+ return this.localDB.map((object) => object.id).includes(id);
217
+ }
218
+ /**
219
+ * Retrieves an object with the given id from the local database.
220
+ * @param id The id of the object.
221
+ * @returns The object if it exists, undefined otherwise.
222
+ */
223
+ getObject(id) {
224
+ return this.localDB.filter((object) => object.id === id)[0];
225
+ }
226
+ /**
227
+ * Removes an object with the given id from the local database and updates the main database.
228
+ * @param id The id of the object.
229
+ */
230
+ removeObject(id) {
231
+ this.localDB = this.localDB.filter((object) => object.id !== id);
232
+ this.updateMainDB();
233
+ }
234
+ /**
235
+ * Retrieves all objects from the local database.
236
+ * @returns An array of all objects in the local database.
237
+ */
238
+ getAllObjects() {
239
+ return this.localDB;
240
+ }
241
+ /**
242
+ * Removes all objects from the local database and updates the main database.
243
+ */
244
+ eraseAllObjects() {
245
+ this.localDB = [];
246
+ this.updateMainDB();
247
+ }
248
+ /**
249
+ * Iterates over all objects in the local database.
250
+ * @param callback The function to be called for each object.
251
+ */
252
+ forEach(callback) {
253
+ this.localDB.forEach((object, index) => callback(object, index));
254
+ }
255
+ }
256
+ //# sourceMappingURL=database.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.js","sourceRoot":"","sources":["../../src/database/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAS,MAAM,mBAAmB,CAAC;AAGzD,YAAY;AACZ,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC;;;GAGG;AACH,MAAM,eAAe;IACZ,MAAM,CAAU,uBAAuB,GAAG,KAAK,CAAC;IAChD,MAAM,CAAU,SAAS,GAAG,WAAW,CAAC;IAExC,MAAM,CAAiB;IAE/B,YAAY,MAA0B;QACrC,IAAI,MAAM,YAAY,MAAM,EAAE,CAAC;YAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACtB,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACrB,CAAC;IACF,CAAC;IAED;;;;OAIG;IACH,eAAe,CAAC,YA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AC,CAAC;QACjE,IAAI,CAAC,YAAY,EAAE,CAAC;IACrB,CAAC;IAED;;;OAGG;IACH,aAAa;QACZ,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,eAAe;QACd,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,YAAY,EAAE,CAAC;IACrB,CAAC;IAED;;;OAGG;IACH,OAAO,CAAC,QAAuD;QAC9D,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;IAClE,CAAC;CACD"}
@@ -1,4 +1,4 @@
1
1
  import { SimpleDatabase } from "./database";
2
2
  import { SimpleObject } from "./interfaces";
3
-
4
3
  export { SimpleDatabase, SimpleObject };
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,3 @@
1
+ import { SimpleDatabase } from "./database";
2
+ export { SimpleDatabase };
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAG5C,OAAO,EAAE,cAAc,EAAgB,CAAC"}
@@ -2,5 +2,6 @@
2
2
  * SimpleObject is an interface for objects with an id property.
3
3
  */
4
4
  export interface SimpleObject {
5
- id: string;
5
+ id: string;
6
6
  }
7
+ //# sourceMappingURL=interfaces.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../src/database/interfaces.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
@@ -1,37 +1,25 @@
1
1
  import { RGB, RGBA, Vector3 } from "@minecraft/server";
2
-
3
2
  /**
4
3
  * Converts an angle in degrees to radians.
5
4
  *
6
5
  * @param degrees - The angle in degrees to be converted.
7
6
  * @returns The angle in radians.
8
7
  */
9
- export function toRadians(degrees: number): number {
10
- return degrees * (Math.PI / 180);
11
- }
12
-
8
+ export declare function toRadians(degrees: number): number;
13
9
  /**
14
10
  * Converts an angle in radians to degrees.
15
11
  *
16
12
  * @param radians - The angle in radians to be converted.
17
13
  * @returns The angle in degrees.
18
14
  */
19
- export function calculateDistance(position1: Vector3, position2: Vector3): number {
20
- return Math.sqrt(
21
- Math.pow(position1.x - position2.x, 2) + Math.pow(position1.y - position2.y, 2) + Math.pow(position1.z - position2.z, 2)
22
- );
23
- }
24
-
15
+ export declare function calculateDistance(position1: Vector3, position2: Vector3): number;
25
16
  /**
26
17
  * Converts an angle in radians to degrees.
27
18
  *
28
19
  * @param radians - The angle in radians to be converted.
29
20
  * @returns The angle in degrees.
30
21
  */
31
- export function toDegrees(radians: number): number {
32
- return radians * (180 / Math.PI);
33
- }
34
-
22
+ export declare function toDegrees(radians: number): number;
35
23
  /**
36
24
  * Converts a hex color string to an RGBA object.
37
25
  *
@@ -39,30 +27,7 @@ export function toDegrees(radians: number): number {
39
27
  * @returns An object containing the red, green, blue, and alpha components as numbers between 0 and 1.
40
28
  * @throws Error if the hex string is invalid.
41
29
  */
42
- export function hexToRgba(hex: string, stripAlpha: boolean = false): RGB | RGBA {
43
- if (!/^#([a-fA-F0-9]{4}|[a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(hex)) {
44
- throw new Error("Invalid hex color");
45
- }
46
-
47
- let normalized = hex.slice(1);
48
- if (normalized.length === 3 || normalized.length === 4) {
49
- normalized = normalized
50
- .split("")
51
- .map((c) => c + c)
52
- .join("");
53
- }
54
-
55
- const red = parseInt(normalized.substring(0, 2), 16) / 255;
56
- const green = parseInt(normalized.substring(2, 4), 16) / 255;
57
- const blue = parseInt(normalized.substring(4, 6), 16) / 255;
58
- const alpha = (normalized.length === 8 ? parseInt(normalized.substring(6, 8), 16) : 255) / 255;
59
-
60
- if (stripAlpha) {
61
- return { red, green, blue };
62
- }
63
- return { red, green, blue, alpha };
64
- }
65
-
30
+ export declare function hexToRgba(hex: string, stripAlpha?: boolean): RGB | RGBA;
66
31
  /**
67
32
  * Clamps a value between a minimum and maximum range.
68
33
  *
@@ -71,31 +36,21 @@ export function hexToRgba(hex: string, stripAlpha: boolean = false): RGB | RGBA
71
36
  * @param max - The maximum value of the range.
72
37
  * @returns The clamped value, which will be between `min` and `max`.
73
38
  */
74
- export function clamp(value: number, min: number, max: number): number {
75
- return Math.max(min, Math.min(max, value));
76
- }
77
-
39
+ export declare function clamp(value: number, min: number, max: number): number;
78
40
  /**
79
41
  * Converts an angle in degrees to its signed equivalent in the range [-180, 180].
80
42
  *
81
43
  * @param angle - The angle in degrees to be converted.
82
44
  * @returns The signed angle in degrees, normalized to the range [-180, 180].
83
45
  */
84
- export function toSigned(angle: number): number {
85
- const wrapped = ((angle % 360) + 360) % 360;
86
- return wrapped > 180 ? wrapped - 360 : wrapped;
87
- }
88
-
46
+ export declare function toSigned(angle: number): number;
89
47
  /**
90
48
  * Converts an angle in degrees to its unsigned equivalent in the range [0, 360].
91
49
  *
92
50
  * @param angle - The angle in degrees to be converted.
93
51
  * @returns The unsigned angle in degrees, normalized to the range [0, 360].
94
52
  */
95
- export function toUnsigned(angle: number): number {
96
- return ((angle % 360) + 360) % 360;
97
- }
98
-
53
+ export declare function toUnsigned(angle: number): number;
99
54
  /**
100
55
  * Calculates the shortest signed arc between two angles in degrees.
101
56
  *
@@ -103,10 +58,7 @@ export function toUnsigned(angle: number): number {
103
58
  * @param to - The target angle in degrees.
104
59
  * @returns The shortest arc in degrees from `from` to `to`.
105
60
  */
106
- export function shortestArc(from: number, to: number): number {
107
- return -toSigned((to - from + 360) % 360);
108
- }
109
-
61
+ export declare function shortestArc(from: number, to: number): number;
110
62
  /**
111
63
  * Checks if two positions are equal within a specified tolerance.
112
64
  *
@@ -115,10 +67,7 @@ export function shortestArc(from: number, to: number): number {
115
67
  * @param tolerance - The tolerance for comparison (default is 0.5).
116
68
  * @returns `true` if the positions are equal within the tolerance, otherwise `false`.
117
69
  */
118
- export function positionsEqual(a: Vector3, b: Vector3, tolerance: number = 0.5): boolean {
119
- return Math.abs(a.x - b.x) < tolerance && Math.abs(a.y - b.y) < tolerance && Math.abs(a.z - b.z) < tolerance;
120
- }
121
-
70
+ export declare function positionsEqual(a: Vector3, b: Vector3, tolerance?: number): boolean;
122
71
  /**
123
72
  * Checks if a value is within a specified range.
124
73
  *
@@ -127,6 +76,5 @@ export function positionsEqual(a: Vector3, b: Vector3, tolerance: number = 0.5):
127
76
  * @param max - The maximum value of the range.
128
77
  * @returns `true` if the value is within the range [min, max], otherwise `false`.
129
78
  */
130
- export function inRange(value: number, min: number, max: number): boolean {
131
- return value >= min && value <= max;
132
- }
79
+ export declare function inRange(value: number, min: number, max: number): boolean;
80
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Converts an angle in degrees to radians.
3
+ *
4
+ * @param degrees - The angle in degrees to be converted.
5
+ * @returns The angle in radians.
6
+ */
7
+ export function toRadians(degrees) {
8
+ return degrees * (Math.PI / 180);
9
+ }
10
+ /**
11
+ * Converts an angle in radians to degrees.
12
+ *
13
+ * @param radians - The angle in radians to be converted.
14
+ * @returns The angle in degrees.
15
+ */
16
+ export function calculateDistance(position1, position2) {
17
+ return Math.sqrt(Math.pow(position1.x - position2.x, 2) + Math.pow(position1.y - position2.y, 2) + Math.pow(position1.z - position2.z, 2));
18
+ }
19
+ /**
20
+ * Converts an angle in radians to degrees.
21
+ *
22
+ * @param radians - The angle in radians to be converted.
23
+ * @returns The angle in degrees.
24
+ */
25
+ export function toDegrees(radians) {
26
+ return radians * (180 / Math.PI);
27
+ }
28
+ /**
29
+ * Converts a hex color string to an RGBA object.
30
+ *
31
+ * @param hex - The hex color string, which can be in the formats #RGB, #RGBA, #RRGGBB, or #RRGGBBAA.
32
+ * @returns An object containing the red, green, blue, and alpha components as numbers between 0 and 1.
33
+ * @throws Error if the hex string is invalid.
34
+ */
35
+ export function hexToRgba(hex, stripAlpha = false) {
36
+ if (!/^#([a-fA-F0-9]{4}|[a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(hex)) {
37
+ throw new Error("Invalid hex color");
38
+ }
39
+ let normalized = hex.slice(1);
40
+ if (normalized.length === 3 || normalized.length === 4) {
41
+ normalized = normalized
42
+ .split("")
43
+ .map((c) => c + c)
44
+ .join("");
45
+ }
46
+ const red = parseInt(normalized.substring(0, 2), 16) / 255;
47
+ const green = parseInt(normalized.substring(2, 4), 16) / 255;
48
+ const blue = parseInt(normalized.substring(4, 6), 16) / 255;
49
+ const alpha = (normalized.length === 8 ? parseInt(normalized.substring(6, 8), 16) : 255) / 255;
50
+ if (stripAlpha) {
51
+ return { red, green, blue };
52
+ }
53
+ return { red, green, blue, alpha };
54
+ }
55
+ /**
56
+ * Clamps a value between a minimum and maximum range.
57
+ *
58
+ * @param value - The value to be clamped.
59
+ * @param min - The minimum value of the range.
60
+ * @param max - The maximum value of the range.
61
+ * @returns The clamped value, which will be between `min` and `max`.
62
+ */
63
+ export function clamp(value, min, max) {
64
+ return Math.max(min, Math.min(max, value));
65
+ }
66
+ /**
67
+ * Converts an angle in degrees to its signed equivalent in the range [-180, 180].
68
+ *
69
+ * @param angle - The angle in degrees to be converted.
70
+ * @returns The signed angle in degrees, normalized to the range [-180, 180].
71
+ */
72
+ export function toSigned(angle) {
73
+ const wrapped = ((angle % 360) + 360) % 360;
74
+ return wrapped > 180 ? wrapped - 360 : wrapped;
75
+ }
76
+ /**
77
+ * Converts an angle in degrees to its unsigned equivalent in the range [0, 360].
78
+ *
79
+ * @param angle - The angle in degrees to be converted.
80
+ * @returns The unsigned angle in degrees, normalized to the range [0, 360].
81
+ */
82
+ export function toUnsigned(angle) {
83
+ return ((angle % 360) + 360) % 360;
84
+ }
85
+ /**
86
+ * Calculates the shortest signed arc between two angles in degrees.
87
+ *
88
+ * @param from - The starting angle in degrees.
89
+ * @param to - The target angle in degrees.
90
+ * @returns The shortest arc in degrees from `from` to `to`.
91
+ */
92
+ export function shortestArc(from, to) {
93
+ return -toSigned((to - from + 360) % 360);
94
+ }
95
+ /**
96
+ * Checks if two positions are equal within a specified tolerance.
97
+ *
98
+ * @param a - The first position.
99
+ * @param b - The second position.
100
+ * @param tolerance - The tolerance for comparison (default is 0.5).
101
+ * @returns `true` if the positions are equal within the tolerance, otherwise `false`.
102
+ */
103
+ export function positionsEqual(a, b, tolerance = 0.5) {
104
+ return Math.abs(a.x - b.x) < tolerance && Math.abs(a.y - b.y) < tolerance && Math.abs(a.z - b.z) < tolerance;
105
+ }
106
+ /**
107
+ * Checks if a value is within a specified range.
108
+ *
109
+ * @param value - The value to check.
110
+ * @param min - The minimum value of the range.
111
+ * @param max - The maximum value of the range.
112
+ * @returns `true` if the value is within the range [min, max], otherwise `false`.
113
+ */
114
+ export function inRange(value, min, max) {
115
+ return value >= min && value <= max;
116
+ }
117
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/math/index.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,OAAe;IACxC,OAAO,OAAO,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC;AAClC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,SAAkB,EAAE,SAAkB;IACvE,OAAO,IAAI,CAAC,IAAI,CACf,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CA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