samengine 1.9.1 → 1.10.0
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.
- package/LICENSE +201 -0
- package/README.md +168 -0
- package/dist/config/buildconfig.d.ts +146 -0
- package/dist/config/buildconfig.js +115 -0
- package/dist/config/index.d.ts +9 -0
- package/dist/config/index.js +1 -0
- package/dist/core.d.ts +17 -0
- package/dist/core.js +24 -0
- package/dist/html.d.ts +29 -0
- package/dist/html.js +20 -0
- package/dist/input.d.ts +51 -0
- package/dist/input.js +44 -3
- package/dist/keys.d.ts +6 -0
- package/dist/keys.js +6 -2
- package/dist/logger.d.ts +8 -0
- package/dist/logger.js +8 -1
- package/dist/nonbrowser/getversion.d.ts +13 -0
- package/dist/nonbrowser/getversion.js +35 -0
- package/dist/nonbrowser/ghresolver.d.ts +1 -0
- package/dist/nonbrowser/ghresolver.js +7 -0
- package/dist/nonbrowser/index.d.ts +9 -0
- package/dist/nonbrowser/index.js +9 -0
- package/dist/nonbrowser/internal/buildhelper.d.ts +42 -0
- package/dist/nonbrowser/internal/buildhelper.js +144 -0
- package/dist/nonbrowser/internal/cli/argparser.d.ts +20 -0
- package/dist/nonbrowser/internal/cli/argparser.js +36 -0
- package/dist/nonbrowser/internal/cli/main.d.ts +13 -0
- package/dist/nonbrowser/internal/cli/main.js +262 -0
- package/dist/nonbrowser/internal/config.d.ts +9 -0
- package/dist/nonbrowser/internal/config.js +40 -0
- package/dist/nonbrowser/internal/exporthtml.d.ts +37 -0
- package/dist/nonbrowser/internal/exporthtml.js +622 -0
- package/dist/nonbrowser/internal/projcreator/downloadZip.d.ts +4 -0
- package/dist/nonbrowser/internal/projcreator/downloadZip.js +83 -0
- package/dist/nonbrowser/internal/projcreator/main.d.ts +1 -0
- package/dist/nonbrowser/internal/projcreator/main.js +81 -0
- package/dist/nonbrowser/utils.d.ts +8 -0
- package/dist/nonbrowser/utils.js +18 -0
- package/dist/physics/collision.d.ts +33 -0
- package/dist/physics/collision.js +27 -0
- package/dist/physics/physicsEngine.d.ts +18 -0
- package/dist/physics/physicsEngine.js +18 -0
- package/dist/physics/physicsObject.d.ts +20 -0
- package/dist/physics/physicsObject.js +20 -0
- package/dist/renderer.d.ts +78 -0
- package/dist/renderer.js +72 -9
- package/dist/samegui/index.d.ts +29 -0
- package/dist/samegui/index.js +26 -0
- package/dist/save.d.ts +12 -0
- package/dist/save.js +10 -0
- package/dist/sound/audioplayer.d.ts +39 -0
- package/dist/sound/audioplayer.js +39 -5
- package/dist/storage/index.d.ts +40 -2
- package/dist/storage/index.js +34 -3
- package/dist/text/index.d.ts +14 -0
- package/dist/text/index.js +58 -0
- package/dist/texture.d.ts +100 -0
- package/dist/texture.js +75 -41
- package/dist/types/button.d.ts +25 -0
- package/dist/types/button.js +22 -0
- package/dist/types/circle.d.ts +26 -0
- package/dist/types/circle.js +21 -7
- package/dist/types/color.d.ts +17 -0
- package/dist/types/color.js +11 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.js +1 -1
- package/dist/types/rectangle.d.ts +29 -0
- package/dist/types/rectangle.js +23 -7
- package/dist/types/triangle.d.ts +23 -0
- package/dist/types/triangle.js +20 -6
- package/dist/types/vector2d.d.ts +42 -0
- package/dist/types/vector2d.js +39 -11
- package/dist/types/vector3d.d.ts +38 -0
- package/dist/types/vector3d.js +35 -11
- package/dist/utils/index.d.ts +11 -4
- package/dist/utils/index.js +11 -4
- package/dist/utils/logger/index.d.ts +24 -0
- package/dist/utils/logger/index.js +44 -0
- package/dist/utils/math.d.ts +18 -0
- package/dist/utils/math.js +18 -4
- package/package.json +36 -11
- package/dist/utils/csv/index.d.ts +0 -3
- package/dist/utils/csv/index.js +0 -2
- package/dist/utils/csv/parser.d.ts +0 -25
- package/dist/utils/csv/parser.js +0 -212
- package/dist/utils/csv/stringifier.d.ts +0 -30
- package/dist/utils/csv/stringifier.js +0 -130
- package/dist/utils/csv/types.d.ts +0 -63
- package/dist/utils/csv/types.js +0 -1
- package/dist/utils/jsonc-parser.d.ts +0 -4
- package/dist/utils/jsonc-parser.js +0 -166
- package/dist/utils/markdown.d.ts +0 -41
- package/dist/utils/markdown.js +0 -699
package/dist/texture.js
CHANGED
|
@@ -3,7 +3,17 @@ const textures = {};
|
|
|
3
3
|
function getresourcepath(path) {
|
|
4
4
|
return "/resources/" + path;
|
|
5
5
|
}
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Loads an image and stores it in the texture cache.
|
|
8
|
+
*
|
|
9
|
+
* Normal builds load from `/resources/${src}`. Single-file exports can provide
|
|
10
|
+
* embedded resources through `window.__resources`; if a matching key exists,
|
|
11
|
+
* that data URL is used instead. The cache key always remains the original
|
|
12
|
+
* `src` string, so `getTexture(src)` works the same in both modes.
|
|
13
|
+
*
|
|
14
|
+
* @param src Resource path relative to the resources folder.
|
|
15
|
+
* @returns Promise resolving to the loaded `HTMLImageElement`.
|
|
16
|
+
*/
|
|
7
17
|
export function loadTextureAsync(src) {
|
|
8
18
|
return new Promise((resolve, reject) => {
|
|
9
19
|
// Wenn schon geladen
|
|
@@ -32,11 +42,22 @@ export function loadTextureAsync(src) {
|
|
|
32
42
|
img.src = finalSrc;
|
|
33
43
|
});
|
|
34
44
|
}
|
|
35
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Reads a texture from the cache.
|
|
47
|
+
*
|
|
48
|
+
* This does not start loading by itself. Call `loadTextureAsync` first or handle
|
|
49
|
+
* the `undefined` result.
|
|
50
|
+
*/
|
|
36
51
|
export function getTexture(src) {
|
|
37
52
|
return textures[src];
|
|
38
53
|
}
|
|
39
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Draws a texture to the canvas.
|
|
56
|
+
*
|
|
57
|
+
* The image is drawn around its center for rotation and flipping, but `x` and
|
|
58
|
+
* `y` still describe the top-left target position. If the texture is missing,
|
|
59
|
+
* a magenta fallback rectangle is drawn to make missing assets visible.
|
|
60
|
+
*/
|
|
40
61
|
export function drawTexture(ctx, texture, x, y, options = {}) {
|
|
41
62
|
const { width, height, rotation = 0, flipX = false, flipY = false, scale = 1 } = options;
|
|
42
63
|
if (!texture) {
|
|
@@ -56,17 +77,20 @@ export function drawTexture(ctx, texture, x, y, options = {}) {
|
|
|
56
77
|
ctx.drawImage(texture, -w / 2, -h / 2, w, h);
|
|
57
78
|
ctx.restore();
|
|
58
79
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Loads a texture atlas image and its JSON frame data.
|
|
82
|
+
*
|
|
83
|
+
* The JSON file is fetched from the resources folder and must contain a
|
|
84
|
+
* `frames` object with frame names mapped to rectangles:
|
|
85
|
+
*
|
|
86
|
+
* ```json
|
|
87
|
+
* {
|
|
88
|
+
* "frames": {
|
|
89
|
+
* "player_idle_0": { "x": 0, "y": 0, "width": 32, "height": 32 }
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
70
94
|
export async function loadAtlas(imageSrc, dataSrc) {
|
|
71
95
|
const [image, data] = await Promise.all([
|
|
72
96
|
loadTextureAsync(imageSrc),
|
|
@@ -77,6 +101,11 @@ export async function loadAtlas(imageSrc, dataSrc) {
|
|
|
77
101
|
frames: data.frames
|
|
78
102
|
};
|
|
79
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Draws one named frame from a texture atlas.
|
|
106
|
+
*
|
|
107
|
+
* Missing frame names are logged through `dlog` and skipped.
|
|
108
|
+
*/
|
|
80
109
|
export function drawAtlasFrame(ctx, atlas, frameName, x, y, options = {}) {
|
|
81
110
|
const { width, height, rotation = 0, flipX = false, flipY = false, scale = 1 } = options;
|
|
82
111
|
const frame = atlas.frames[frameName];
|
|
@@ -95,40 +124,28 @@ export function drawAtlasFrame(ctx, atlas, frameName, x, y, options = {}) {
|
|
|
95
124
|
ctx.drawImage(atlas.image, frame.x, frame.y, frame.width, frame.height, -w / 2, -h / 2, w, h);
|
|
96
125
|
ctx.restore();
|
|
97
126
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
// // Define Animation
|
|
105
|
-
// const walkAnimation: Animation = {
|
|
106
|
-
// frames: ["walk_0", "walk_1", "walk_2", "walk_3"],
|
|
107
|
-
// fps: 8,
|
|
108
|
-
// loop: true
|
|
109
|
-
// };
|
|
110
|
-
//
|
|
111
|
-
// const player = new AnimationPlayer(walkAnimation);
|
|
112
|
-
//
|
|
113
|
-
// // Game Loop
|
|
114
|
-
// function update(dt: number) {
|
|
115
|
-
// player.update(dt);
|
|
116
|
-
// }
|
|
117
|
-
//
|
|
118
|
-
// function render(ctx: CanvasRenderingContext2D) {
|
|
119
|
-
// drawAnimation(ctx, atlas, player, 100, 100, 64, 64);
|
|
120
|
-
// }
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
// Animation Player Class
|
|
127
|
+
/**
|
|
128
|
+
* Keeps track of time and current frame for a named-frame animation.
|
|
129
|
+
*
|
|
130
|
+
* The player only stores animation state. It does not draw anything by itself;
|
|
131
|
+
* call `drawAnimation` after `update(dt)` to render the current atlas frame.
|
|
132
|
+
*/
|
|
124
133
|
export class AnimationPlayer {
|
|
125
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Creates a player for the given animation definition.
|
|
136
|
+
*/
|
|
126
137
|
constructor(animation) {
|
|
127
138
|
this.animation = animation;
|
|
128
139
|
this.time = 0;
|
|
129
140
|
this.currentFrameIndex = 0;
|
|
130
141
|
this.finished = false;
|
|
131
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Advances the animation by `deltaTime` seconds.
|
|
145
|
+
*
|
|
146
|
+
* Looping animations wrap back to the first frame. Non-looping animations
|
|
147
|
+
* stop on their final frame and then report `isFinished() === true`.
|
|
148
|
+
*/
|
|
132
149
|
update(deltaTime) {
|
|
133
150
|
if (this.finished)
|
|
134
151
|
return;
|
|
@@ -148,22 +165,39 @@ export class AnimationPlayer {
|
|
|
148
165
|
}
|
|
149
166
|
}
|
|
150
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Returns the frame name that should currently be rendered from the atlas.
|
|
170
|
+
*/
|
|
151
171
|
getCurrentFrame() {
|
|
152
172
|
return this.animation.frames[this.currentFrameIndex];
|
|
153
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Restarts the animation from the first frame.
|
|
176
|
+
*/
|
|
154
177
|
reset() {
|
|
155
178
|
this.time = 0;
|
|
156
179
|
this.currentFrameIndex = 0;
|
|
157
180
|
this.finished = false;
|
|
158
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Returns whether a non-looping animation reached its final frame.
|
|
184
|
+
*/
|
|
159
185
|
isFinished() {
|
|
160
186
|
return this.finished;
|
|
161
187
|
}
|
|
162
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* Draws the current frame of an `AnimationPlayer`.
|
|
191
|
+
*/
|
|
163
192
|
export function drawAnimation(ctx, atlas, player, x, y, options = {}) {
|
|
164
193
|
const frame = player.getCurrentFrame();
|
|
165
194
|
drawAtlasFrame(ctx, atlas, frame, x, y, options);
|
|
166
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* Helper for side-view sprites.
|
|
198
|
+
*
|
|
199
|
+
* Returns `true` when a negative direction should flip a sprite horizontally.
|
|
200
|
+
*/
|
|
167
201
|
export function getFlipFromDirection(dir) {
|
|
168
202
|
return dir < 0; // links = true
|
|
169
203
|
}
|
package/dist/types/button.d.ts
CHANGED
|
@@ -1,9 +1,34 @@
|
|
|
1
1
|
import { Rect } from "./rectangle.js";
|
|
2
2
|
import { Mouse } from "../input.js";
|
|
3
|
+
/**
|
|
4
|
+
* Simple canvas button model.
|
|
5
|
+
*
|
|
6
|
+
* The button is not a DOM element. It is drawn on the canvas and checked with
|
|
7
|
+
* mouse hit testing.
|
|
8
|
+
*/
|
|
3
9
|
export type Button = {
|
|
10
|
+
/** Rectangle that defines the button position and size. */
|
|
4
11
|
form: Rect;
|
|
12
|
+
/** Text label drawn in the button center. */
|
|
5
13
|
text: string;
|
|
6
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* Creates a canvas button description.
|
|
17
|
+
*/
|
|
7
18
|
export declare function makeButton(form: Rect, text: string): Button;
|
|
19
|
+
/**
|
|
20
|
+
* Returns true when the button rectangle was clicked during the current frame.
|
|
21
|
+
*/
|
|
8
22
|
export declare function clickedButton(btn: Button, mouse: Mouse): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Draws a simple filled rectangle button with centered text.
|
|
25
|
+
*/
|
|
9
26
|
export declare function drawButton(btn: Button, ctx: CanvasRenderingContext2D, color?: string, textColor?: string, font?: string): void;
|
|
27
|
+
/**
|
|
28
|
+
* Check if the Button is hovered
|
|
29
|
+
*/
|
|
30
|
+
export declare function isButtonhovered(btn: Button, mouse: Mouse): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Check if the Button is pressed!
|
|
33
|
+
*/
|
|
34
|
+
export declare function isButtonpressed(btn: Button, mouse: Mouse): boolean;
|
package/dist/types/button.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
// Button
|
|
2
|
+
import { isMouseInRect } from "./rectangle.js";
|
|
2
3
|
import { isRectClicked } from "./rectangle.js";
|
|
3
4
|
import { drawRect } from "../renderer.js";
|
|
4
5
|
import { renderText } from "../renderer.js";
|
|
5
6
|
// Function to create a new Button Type
|
|
7
|
+
/**
|
|
8
|
+
* Creates a canvas button description.
|
|
9
|
+
*/
|
|
6
10
|
export function makeButton(form, text) {
|
|
7
11
|
return {
|
|
8
12
|
form: form,
|
|
@@ -10,10 +14,16 @@ export function makeButton(form, text) {
|
|
|
10
14
|
};
|
|
11
15
|
}
|
|
12
16
|
// Prüft, ob der Button geklickt wurde
|
|
17
|
+
/**
|
|
18
|
+
* Returns true when the button rectangle was clicked during the current frame.
|
|
19
|
+
*/
|
|
13
20
|
export function clickedButton(btn, mouse) {
|
|
14
21
|
return isRectClicked(mouse, btn.form);
|
|
15
22
|
}
|
|
16
23
|
// Zeichnet den Button (Rechteck + Text)
|
|
24
|
+
/**
|
|
25
|
+
* Draws a simple filled rectangle button with centered text.
|
|
26
|
+
*/
|
|
17
27
|
export function drawButton(btn, ctx, color = "#444", textColor = "white", font = "20px Arial") {
|
|
18
28
|
drawRect(ctx, btn.form, color);
|
|
19
29
|
// Text mittig im Button platzieren
|
|
@@ -23,3 +33,15 @@ export function drawButton(btn, ctx, color = "#444", textColor = "white", font =
|
|
|
23
33
|
ctx.textBaseline = "middle";
|
|
24
34
|
renderText(ctx, btn.text, textX, textY, textColor, font);
|
|
25
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Check if the Button is hovered
|
|
38
|
+
*/
|
|
39
|
+
export function isButtonhovered(btn, mouse) {
|
|
40
|
+
return isMouseInRect(mouse, btn.form);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Check if the Button is pressed!
|
|
44
|
+
*/
|
|
45
|
+
export function isButtonpressed(btn, mouse) {
|
|
46
|
+
return isRectClicked(mouse, btn.form);
|
|
47
|
+
}
|
package/dist/types/circle.d.ts
CHANGED
|
@@ -1,14 +1,40 @@
|
|
|
1
1
|
import { Mouse } from "../input.js";
|
|
2
2
|
import { type Vector2d } from "./vector2d.js";
|
|
3
|
+
/**
|
|
4
|
+
* Circle shape used for drawing, hit tests, and simple collision checks.
|
|
5
|
+
*
|
|
6
|
+
* `x` and `y` are the center of the circle.
|
|
7
|
+
*/
|
|
3
8
|
export type Circle = {
|
|
4
9
|
x: number;
|
|
5
10
|
y: number;
|
|
6
11
|
radius: number;
|
|
7
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Creates a circle object.
|
|
15
|
+
*/
|
|
8
16
|
export declare function makeCircle(x: number, y: number, radius: number): Circle;
|
|
17
|
+
/**
|
|
18
|
+
* Returns the center of the circle as a `Vector2d`.
|
|
19
|
+
*/
|
|
9
20
|
export declare function centerCircle(circle: Circle): Vector2d;
|
|
21
|
+
/**
|
|
22
|
+
* Checks whether a point is inside or on the border of a circle.
|
|
23
|
+
*/
|
|
10
24
|
export declare function isPointInCircle(x: number, y: number, circle: Circle): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Checks whether the current mouse position is inside a circle.
|
|
27
|
+
*/
|
|
11
28
|
export declare function isMouseInCircle(mouse: Mouse, circle: Circle): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Checks whether a circle was clicked during the current frame.
|
|
31
|
+
*/
|
|
12
32
|
export declare function isCircleClicked(mouse: Mouse, circle: Circle): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Checks whether two circles overlap or touch.
|
|
35
|
+
*/
|
|
13
36
|
export declare function isCircleColliding(circle1: Circle, circle2: Circle): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Returns the center-to-center distance between two circles.
|
|
39
|
+
*/
|
|
14
40
|
export declare function getCircleDistance(circle1: Circle, circle2: Circle): number;
|
package/dist/types/circle.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Circle Type for Hitboxes
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Creates a circle object.
|
|
4
|
+
*/
|
|
3
5
|
export function makeCircle(x, y, radius) {
|
|
4
6
|
return {
|
|
5
7
|
x: x,
|
|
@@ -7,31 +9,43 @@ export function makeCircle(x, y, radius) {
|
|
|
7
9
|
radius: radius,
|
|
8
10
|
};
|
|
9
11
|
}
|
|
10
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Returns the center of the circle as a `Vector2d`.
|
|
14
|
+
*/
|
|
11
15
|
export function centerCircle(circle) {
|
|
12
16
|
return { x: circle.x, y: circle.y };
|
|
13
17
|
}
|
|
14
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Checks whether a point is inside or on the border of a circle.
|
|
20
|
+
*/
|
|
15
21
|
export function isPointInCircle(x, y, circle) {
|
|
16
22
|
const distance = Math.sqrt((x - circle.x) * (x - circle.x) +
|
|
17
23
|
(y - circle.y) * (y - circle.y));
|
|
18
24
|
return distance <= circle.radius;
|
|
19
25
|
}
|
|
20
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Checks whether the current mouse position is inside a circle.
|
|
28
|
+
*/
|
|
21
29
|
export function isMouseInCircle(mouse, circle) {
|
|
22
30
|
return isPointInCircle(mouse.x, mouse.y, circle);
|
|
23
31
|
}
|
|
24
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether a circle was clicked during the current frame.
|
|
34
|
+
*/
|
|
25
35
|
export function isCircleClicked(mouse, circle) {
|
|
26
36
|
return isMouseInCircle(mouse, circle) && mouse.justPressed;
|
|
27
37
|
}
|
|
28
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Checks whether two circles overlap or touch.
|
|
40
|
+
*/
|
|
29
41
|
export function isCircleColliding(circle1, circle2) {
|
|
30
42
|
const distance = Math.sqrt((circle1.x - circle2.x) * (circle1.x - circle2.x) +
|
|
31
43
|
(circle1.y - circle2.y) * (circle1.y - circle2.y));
|
|
32
44
|
return distance <= (circle1.radius + circle2.radius);
|
|
33
45
|
}
|
|
34
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Returns the center-to-center distance between two circles.
|
|
48
|
+
*/
|
|
35
49
|
export function getCircleDistance(circle1, circle2) {
|
|
36
50
|
return Math.sqrt((circle1.x - circle2.x) * (circle1.x - circle2.x) +
|
|
37
51
|
(circle1.y - circle2.y) * (circle1.y - circle2.y));
|
package/dist/types/color.d.ts
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RGBA color object.
|
|
3
|
+
*
|
|
4
|
+
* `r`, `g`, and `b` are expected in the 0-255 range. `a` is optional and can be
|
|
5
|
+
* used by callers as an alpha channel value.
|
|
6
|
+
*/
|
|
1
7
|
export type Color = {
|
|
2
8
|
r: number;
|
|
3
9
|
g: number;
|
|
4
10
|
b: number;
|
|
5
11
|
a?: number;
|
|
6
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Creates a color object.
|
|
15
|
+
*/
|
|
7
16
|
export declare function makeColor(r: number, g: number, b: number, a?: number): Color;
|
|
17
|
+
/**
|
|
18
|
+
* Returns the RGB inverse of a color while preserving alpha if it exists.
|
|
19
|
+
*/
|
|
8
20
|
export declare function invertcolor(color: Color): Color;
|
|
21
|
+
/**
|
|
22
|
+
* Inverts a hex color in `#rrggbb` or `rrggbb` format.
|
|
23
|
+
*
|
|
24
|
+
* The returned value always starts with `#`.
|
|
25
|
+
*/
|
|
9
26
|
export declare function invertHexColor(hex: string): string;
|
package/dist/types/color.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Creates a color object.
|
|
3
|
+
*/
|
|
2
4
|
export function makeColor(r, g, b, a) {
|
|
3
5
|
return {
|
|
4
6
|
r: r,
|
|
@@ -7,6 +9,9 @@ export function makeColor(r, g, b, a) {
|
|
|
7
9
|
a: a
|
|
8
10
|
};
|
|
9
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Returns the RGB inverse of a color while preserving alpha if it exists.
|
|
14
|
+
*/
|
|
10
15
|
export function invertcolor(color) {
|
|
11
16
|
return {
|
|
12
17
|
r: 255 - color.r,
|
|
@@ -15,6 +20,11 @@ export function invertcolor(color) {
|
|
|
15
20
|
...(color.a !== undefined ? { a: color.a } : {}) // optional alpha behalten
|
|
16
21
|
};
|
|
17
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Inverts a hex color in `#rrggbb` or `rrggbb` format.
|
|
25
|
+
*
|
|
26
|
+
* The returned value always starts with `#`.
|
|
27
|
+
*/
|
|
18
28
|
export function invertHexColor(hex) {
|
|
19
29
|
// Entferne das führende #
|
|
20
30
|
const cleanHex = hex.replace('#', '');
|
package/dist/types/index.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export { type Color, makeColor, invertcolor, invertHexColor } from "./color.js";
|
|
|
4
4
|
export { type Rect, makeRect, centerRectX, centerRectY, centerRect, isPointInRect, isMouseInRect, isRectClicked } from "./rectangle.js";
|
|
5
5
|
export { type Circle, makeCircle, centerCircle, isPointInCircle, isMouseInCircle, isCircleClicked, isCircleColliding, getCircleDistance } from "./circle.js";
|
|
6
6
|
export { type Triangle, makeTriangle, centerTriangle, isPointInTriangle, isMouseInTriangle, isTriangleClicked, getTrianglePerimeter, } from "./triangle.js";
|
|
7
|
-
export { type Button, makeButton, clickedButton, drawButton } from "./button.js";
|
|
7
|
+
export { type Button, makeButton, clickedButton, drawButton, isButtonhovered, isButtonpressed } from "./button.js";
|
package/dist/types/index.js
CHANGED
|
@@ -12,4 +12,4 @@ export { makeCircle, centerCircle, isPointInCircle, isMouseInCircle, isCircleCli
|
|
|
12
12
|
// Triangle Type
|
|
13
13
|
export { makeTriangle, centerTriangle, isPointInTriangle, isMouseInTriangle, isTriangleClicked, getTrianglePerimeter, } from "./triangle.js";
|
|
14
14
|
// Button Type
|
|
15
|
-
export { makeButton, clickedButton, drawButton } from "./button.js";
|
|
15
|
+
export { makeButton, clickedButton, drawButton, isButtonhovered, isButtonpressed } from "./button.js";
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { type Mouse } from "../input.js";
|
|
2
2
|
import { type Vector2d } from "./vector2d.js";
|
|
3
|
+
/**
|
|
4
|
+
* Axis-aligned rectangle used for drawing, UI, and simple hit tests.
|
|
5
|
+
*
|
|
6
|
+
* `x` and `y` describe the top-left corner. `width` and `height` extend to the
|
|
7
|
+
* right and downward in normal canvas coordinates.
|
|
8
|
+
*/
|
|
3
9
|
export type Rect = {
|
|
4
10
|
x: number;
|
|
5
11
|
y: number;
|
|
@@ -7,10 +13,33 @@ export type Rect = {
|
|
|
7
13
|
height: number;
|
|
8
14
|
borderRadius?: number;
|
|
9
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* Creates a rectangle object.
|
|
18
|
+
*/
|
|
10
19
|
export declare function makeRect(x: number, y: number, width: number, height: number, borderRadius?: number): Rect;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the horizontal center coordinate of a rectangle.
|
|
22
|
+
*/
|
|
11
23
|
export declare function centerRectX(rect: Rect): number;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the vertical center coordinate of a rectangle.
|
|
26
|
+
*/
|
|
12
27
|
export declare function centerRectY(rect: Rect): number;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the rectangle center as a `Vector2d`.
|
|
30
|
+
*/
|
|
13
31
|
export declare function centerRect(rect: Rect): Vector2d;
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether a point is inside or on the border of a rectangle.
|
|
34
|
+
*/
|
|
14
35
|
export declare function isPointInRect(x: number, y: number, rect: Rect): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Checks whether the current mouse position is inside a rectangle.
|
|
38
|
+
*/
|
|
15
39
|
export declare function isMouseInRect(mouse: Mouse, rect: Rect): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Checks whether a rectangle was clicked during the current frame.
|
|
42
|
+
*
|
|
43
|
+
* This depends on `mouse.justPressed`, so call it before `resetInput()`.
|
|
44
|
+
*/
|
|
16
45
|
export declare function isRectClicked(mouse: Mouse, rect: Rect): boolean;
|
package/dist/types/rectangle.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Rectangle Type for Hitboxes
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Creates a rectangle object.
|
|
4
|
+
*/
|
|
3
5
|
export function makeRect(x, y, width, height, borderRadius = 0) {
|
|
4
6
|
return {
|
|
5
7
|
x: x,
|
|
@@ -9,34 +11,48 @@ export function makeRect(x, y, width, height, borderRadius = 0) {
|
|
|
9
11
|
borderRadius: borderRadius
|
|
10
12
|
};
|
|
11
13
|
}
|
|
12
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Returns the horizontal center coordinate of a rectangle.
|
|
16
|
+
*/
|
|
13
17
|
export function centerRectX(rect) {
|
|
14
18
|
return (rect.x + (rect.width / 2));
|
|
15
19
|
}
|
|
16
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Returns the vertical center coordinate of a rectangle.
|
|
22
|
+
*/
|
|
17
23
|
export function centerRectY(rect) {
|
|
18
24
|
return (rect.y + (rect.height / 2));
|
|
19
25
|
}
|
|
20
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Returns the rectangle center as a `Vector2d`.
|
|
28
|
+
*/
|
|
21
29
|
export function centerRect(rect) {
|
|
22
30
|
let vector = { x: centerRectX(rect), y: centerRectY(rect) };
|
|
23
31
|
return vector;
|
|
24
32
|
}
|
|
25
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Checks whether a point is inside or on the border of a rectangle.
|
|
35
|
+
*/
|
|
26
36
|
export function isPointInRect(x, y, rect) {
|
|
27
37
|
return (x >= rect.x &&
|
|
28
38
|
x <= rect.x + rect.width &&
|
|
29
39
|
y >= rect.y &&
|
|
30
40
|
y <= rect.y + rect.height);
|
|
31
41
|
}
|
|
32
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Checks whether the current mouse position is inside a rectangle.
|
|
44
|
+
*/
|
|
33
45
|
export function isMouseInRect(mouse, rect) {
|
|
34
46
|
return (mouse.x >= rect.x &&
|
|
35
47
|
mouse.x <= rect.x + rect.width &&
|
|
36
48
|
mouse.y >= rect.y &&
|
|
37
49
|
mouse.y <= rect.y + rect.height);
|
|
38
50
|
}
|
|
39
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Checks whether a rectangle was clicked during the current frame.
|
|
53
|
+
*
|
|
54
|
+
* This depends on `mouse.justPressed`, so call it before `resetInput()`.
|
|
55
|
+
*/
|
|
40
56
|
export function isRectClicked(mouse, rect) {
|
|
41
57
|
return isMouseInRect(mouse, rect) && mouse.justPressed;
|
|
42
58
|
}
|
package/dist/types/triangle.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Mouse } from "../input.js";
|
|
2
2
|
import { type Vector2d } from "./vector2d.js";
|
|
3
|
+
/**
|
|
4
|
+
* Triangle shape represented by three points.
|
|
5
|
+
*/
|
|
3
6
|
export type Triangle = {
|
|
4
7
|
x1: number;
|
|
5
8
|
y1: number;
|
|
@@ -8,9 +11,29 @@ export type Triangle = {
|
|
|
8
11
|
x3: number;
|
|
9
12
|
y3: number;
|
|
10
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Creates a triangle from three points.
|
|
16
|
+
*/
|
|
11
17
|
export declare function makeTriangle(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number): Triangle;
|
|
18
|
+
/**
|
|
19
|
+
* Returns the centroid of the triangle.
|
|
20
|
+
*/
|
|
12
21
|
export declare function centerTriangle(triangle: Triangle): Vector2d;
|
|
22
|
+
/**
|
|
23
|
+
* Checks whether a point lies inside the triangle.
|
|
24
|
+
*
|
|
25
|
+
* Uses an area comparison with a small tolerance to avoid floating point noise.
|
|
26
|
+
*/
|
|
13
27
|
export declare function isPointInTriangle(x: number, y: number, triangle: Triangle): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Checks whether the current mouse position is inside the triangle.
|
|
30
|
+
*/
|
|
14
31
|
export declare function isMouseInTriangle(mouse: Mouse, triangle: Triangle): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether the triangle was clicked during the current frame.
|
|
34
|
+
*/
|
|
15
35
|
export declare function isTriangleClicked(mouse: Mouse, triangle: Triangle): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the perimeter length of the triangle.
|
|
38
|
+
*/
|
|
16
39
|
export declare function getTrianglePerimeter(triangle: Triangle): number;
|
package/dist/types/triangle.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Triangle Type for Hitboxes
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Creates a triangle from three points.
|
|
4
|
+
*/
|
|
3
5
|
export function makeTriangle(x1, y1, x2, y2, x3, y3) {
|
|
4
6
|
return {
|
|
5
7
|
x1: x1,
|
|
@@ -14,14 +16,20 @@ export function makeTriangle(x1, y1, x2, y2, x3, y3) {
|
|
|
14
16
|
function getTriangleArea(x1, y1, x2, y2, x3, y3) {
|
|
15
17
|
return Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);
|
|
16
18
|
}
|
|
17
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Returns the centroid of the triangle.
|
|
21
|
+
*/
|
|
18
22
|
export function centerTriangle(triangle) {
|
|
19
23
|
return {
|
|
20
24
|
x: (triangle.x1 + triangle.x2 + triangle.x3) / 3,
|
|
21
25
|
y: (triangle.y1 + triangle.y2 + triangle.y3) / 3
|
|
22
26
|
};
|
|
23
27
|
}
|
|
24
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Checks whether a point lies inside the triangle.
|
|
30
|
+
*
|
|
31
|
+
* Uses an area comparison with a small tolerance to avoid floating point noise.
|
|
32
|
+
*/
|
|
25
33
|
export function isPointInTriangle(x, y, triangle) {
|
|
26
34
|
const areaTriangle = getTriangleArea(triangle.x1, triangle.y1, triangle.x2, triangle.y2, triangle.x3, triangle.y3);
|
|
27
35
|
const area1 = getTriangleArea(x, y, triangle.x2, triangle.y2, triangle.x3, triangle.y3);
|
|
@@ -29,15 +37,21 @@ export function isPointInTriangle(x, y, triangle) {
|
|
|
29
37
|
const area3 = getTriangleArea(triangle.x1, triangle.y1, triangle.x2, triangle.y2, x, y);
|
|
30
38
|
return Math.abs(areaTriangle - (area1 + area2 + area3)) < 0.01;
|
|
31
39
|
}
|
|
32
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Checks whether the current mouse position is inside the triangle.
|
|
42
|
+
*/
|
|
33
43
|
export function isMouseInTriangle(mouse, triangle) {
|
|
34
44
|
return isPointInTriangle(mouse.x, mouse.y, triangle);
|
|
35
45
|
}
|
|
36
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Checks whether the triangle was clicked during the current frame.
|
|
48
|
+
*/
|
|
37
49
|
export function isTriangleClicked(mouse, triangle) {
|
|
38
50
|
return isMouseInTriangle(mouse, triangle) && mouse.justPressed;
|
|
39
51
|
}
|
|
40
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Returns the perimeter length of the triangle.
|
|
54
|
+
*/
|
|
41
55
|
export function getTrianglePerimeter(triangle) {
|
|
42
56
|
const side1 = Math.sqrt((triangle.x2 - triangle.x1) * (triangle.x2 - triangle.x1) +
|
|
43
57
|
(triangle.y2 - triangle.y1) * (triangle.y2 - triangle.y1));
|