thrunium 1.2.0-rc.2 → 1.2.0-rc.4
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/README.md +14 -3
- package/dist/cli.mjs +373 -94
- package/dist/thrunium-global.mjs +1 -0
- package/dist/thrunium.js +1320 -67
- package/dist/thrunium.min.js +3 -2
- package/dist/thrunium.min.js.map +4 -4
- package/dist/types/button.d.ts +60 -0
- package/dist/types/collider.d.ts +4 -4
- package/dist/types/debug.d.ts +80 -0
- package/dist/types/font-string.d.ts +12 -0
- package/dist/types/fonts.d.ts +35 -0
- package/dist/types/game.d.ts +336 -15
- package/dist/types/input.d.ts +127 -0
- package/dist/types/internals.d.ts +14 -0
- package/dist/types/namespace.d.ts +2 -0
- package/dist/types/reference.d.ts +7 -1
- package/dist/types/sprite.d.ts +22 -0
- package/dist/types/timers.d.ts +18 -0
- package/package.json +1 -1
- package/templates/js/src/game.js +35 -0
- package/templates/ts/src/game.ts +35 -0
- package/templates/js/game.js +0 -17
- package/templates/ts/game.ts +0 -17
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { DrawTextOptions } from './game';
|
|
2
|
+
/** Options for `Game.button()`: everything `drawText()` takes, plus the button's own. */
|
|
3
|
+
export interface ButtonOptions extends DrawTextOptions {
|
|
4
|
+
/** Text color while the pointer is over the button. Leave out to keep `color`. */
|
|
5
|
+
hoverColor?: string;
|
|
6
|
+
/** Space in pixels between the text and the edge of the clickable box, on every side. Default `8`. */
|
|
7
|
+
padding?: number;
|
|
8
|
+
/** Called every frame the button is drawn while the pointer is over it, with the button. */
|
|
9
|
+
onHover?: (b: Button) => void;
|
|
10
|
+
}
|
|
11
|
+
/** The set of buttons drawn in one frame, and in the frame before it. Owned by the Game. */
|
|
12
|
+
export interface DrawnButtons {
|
|
13
|
+
current: Set<Button>;
|
|
14
|
+
previous: Set<Button>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Text you can click. Create it once with `game.button()` and call `draw()` every frame: a button counts as hovered or clicked only if it was also drawn in the frame before, so a button made anew each frame is never clickable. The clickable box is the text's width plus `padding` on each side, by `size` plus `padding` on each side, placed around (x, y) by `align` and `baseline` like the text.
|
|
18
|
+
*
|
|
19
|
+
* @category Drawing
|
|
20
|
+
* @example
|
|
21
|
+
* ```js
|
|
22
|
+
* var game = new THRUNIUM.Game(400, 400);
|
|
23
|
+
* var play = game.button('Play', 200, 200, { size: 28, align: 'center', baseline: 'middle', color: '#fff', hoverColor: '#ff0' });
|
|
24
|
+
* game.states.menu = {
|
|
25
|
+
* draw: function() {
|
|
26
|
+
* play.draw();
|
|
27
|
+
* if (play.clicked) game.state = 'level1';
|
|
28
|
+
* }
|
|
29
|
+
* };
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare class Button {
|
|
33
|
+
/** The label. Change it any time; the box follows on the next `draw()`. */
|
|
34
|
+
text: string;
|
|
35
|
+
/** X coordinate of the label, placed by `options.align`. */
|
|
36
|
+
x: number;
|
|
37
|
+
/** Y coordinate of the label, placed by `options.baseline`. */
|
|
38
|
+
y: number;
|
|
39
|
+
/** How the button looks and where it sits. Read by every `draw()`. */
|
|
40
|
+
options: ButtonOptions;
|
|
41
|
+
private game;
|
|
42
|
+
private drawn;
|
|
43
|
+
/** The clickable box as of the last `draw()`, in game coordinates. */
|
|
44
|
+
private box;
|
|
45
|
+
/** `true` while the pointer is over a button that was drawn in the previous frame too. */
|
|
46
|
+
get hovered(): boolean;
|
|
47
|
+
/** `true` for the one frame in which the left button (or a touch) came up over a button that was drawn in the previous frame too. */
|
|
48
|
+
get clicked(): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Draws the button, and keeps it clickable: call it every frame, even while the game is paused. While the pointer is over it, the text uses `hoverColor`, the canvas cursor becomes a pointer and `onHover` is called.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```js
|
|
54
|
+
* play.draw();
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
draw(): void;
|
|
58
|
+
/** Works out the clickable box from the text's measured size, placed the way `drawText` places the text. */
|
|
59
|
+
private measure;
|
|
60
|
+
}
|
package/dist/types/collider.d.ts
CHANGED
|
@@ -51,13 +51,13 @@ export declare const Collider: {
|
|
|
51
51
|
*/
|
|
52
52
|
isRect(rect: RectShape): boolean;
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
55
|
-
* @param object The shape to
|
|
54
|
+
* Returns a copy of the shape with `x` and `y` filled in from `pos.x` and `pos.y`. The object you pass in is not changed (before 1.2.0 `x` and `y` were written onto it), so shapes whose `x` and `y` are getters work. A shape without a `pos` is returned as it is, not copied. The result has the same shape you passed in, so a circle stays a circle and a rect stays a rect. The collision checks call it for you.
|
|
55
|
+
* @param object The shape to read.
|
|
56
56
|
* @example
|
|
57
57
|
* ```js
|
|
58
58
|
* var rect = { pos: new THRUNIUM.Vector(100, 100), width: 50, height: 50 };
|
|
59
|
-
* THRUNIUM.Collider.fixPos(rect);
|
|
60
|
-
* //
|
|
59
|
+
* var fixed = THRUNIUM.Collider.fixPos(rect);
|
|
60
|
+
* // fixed.x is 100 and fixed.y is 100; rect itself has no x or y
|
|
61
61
|
* ```
|
|
62
62
|
*/
|
|
63
63
|
fixPos<S extends CircleShape | RectShape>(object: S): S;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An in-page panel with live numbers about the running game: fps and a frame-time graph, state, timers, assets, fonts, input and your own `watch` lines. It is off unless you turn it on, except under `thrunium dev` where it starts on. Press the backquote key (`Backquote`) to show or hide it. It sits over the page (not on the canvas), so it never changes what the game draws or the canvas size; only its own buttons take mouse and touch input.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```js
|
|
6
|
+
* var game = new THRUNIUM.Game(400, 400);
|
|
7
|
+
* game.debug.enabled = true;
|
|
8
|
+
* game.debug.watch('meteors', function() { return meteors.length; });
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare class Debug {
|
|
12
|
+
/**
|
|
13
|
+
* `true` shows the panel (and lets the backquote key hide it); `false` removes it. Until you set it, it is `true` under `thrunium dev` and `false` otherwise. Setting it in your code always wins.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```js
|
|
17
|
+
* var game = new THRUNIUM.Game(400, 400);
|
|
18
|
+
* game.debug.enabled = true;
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
get enabled(): boolean;
|
|
22
|
+
set enabled(value: boolean);
|
|
23
|
+
/**
|
|
24
|
+
* Whether the panel is shown while `enabled`. Starts `true`; the backquote key toggles it.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```js
|
|
28
|
+
* var game = new THRUNIUM.Game(400, 400);
|
|
29
|
+
* game.debug.visible = false; // starts hidden; press ` to show it
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
visible: boolean;
|
|
33
|
+
/** What game code set `enabled` to; `undefined` while it has not, so the default applies. */
|
|
34
|
+
private override;
|
|
35
|
+
private readonly game;
|
|
36
|
+
private readonly watches;
|
|
37
|
+
private frameTimes;
|
|
38
|
+
private lastFrame;
|
|
39
|
+
private lastText;
|
|
40
|
+
private root;
|
|
41
|
+
private info;
|
|
42
|
+
private graph;
|
|
43
|
+
private pauseButton;
|
|
44
|
+
private stepButton;
|
|
45
|
+
/**
|
|
46
|
+
* Adds a line `label: value` to the panel; `read` is called to get the value. Watching a label again replaces its function.
|
|
47
|
+
*
|
|
48
|
+
* @param label The text before the value.
|
|
49
|
+
* @param read Returns the value to show.
|
|
50
|
+
* @example
|
|
51
|
+
* ```js
|
|
52
|
+
* game.debug.watch('meteors', function() { return meteors.length; });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
watch(label: string, read: () => unknown): void;
|
|
56
|
+
/**
|
|
57
|
+
* Removes the line added by `watch(label, ...)`.
|
|
58
|
+
*
|
|
59
|
+
* @param label The label passed to `watch`.
|
|
60
|
+
* @example
|
|
61
|
+
* ```js
|
|
62
|
+
* game.debug.unwatch('meteors');
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
unwatch(label: string): void;
|
|
66
|
+
/** Removes the panel; the next enabled frame builds a fresh one. */
|
|
67
|
+
private remove;
|
|
68
|
+
/** Creates the panel elements and appends them to the page. */
|
|
69
|
+
private build;
|
|
70
|
+
/**
|
|
71
|
+
* A small button that accepts pointer events while the rest of the panel does not. It never
|
|
72
|
+
* keeps keyboard focus: a focused button would be clicked again by Space or Enter while the
|
|
73
|
+
* developer plays, toggling pause.
|
|
74
|
+
*/
|
|
75
|
+
private makeButton;
|
|
76
|
+
/** Sets one bar per recorded frame time; bars are created on demand, never more than the graph length. */
|
|
77
|
+
private drawGraph;
|
|
78
|
+
/** The text lines for the current frame. */
|
|
79
|
+
private lines;
|
|
80
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns `family` ready for the CSS font shorthand. A plain name is quoted, since canvas
|
|
3
|
+
* rejects an unquoted name with a word that starts with a digit (`Press Start 2P`) and then
|
|
4
|
+
* keeps its old font. Left as written: a name already in quotes, a generic family such as
|
|
5
|
+
* `monospace`, and a comma-separated list, which is the author's own CSS.
|
|
6
|
+
*/
|
|
7
|
+
export declare function quoteFamily(family: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* The `ctx.font` value for a weight, size and family. A number size is pixels; a string is
|
|
10
|
+
* any CSS size such as `'2em'`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function fontString(weight: string | undefined, size: number | string, family: string): string;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** A font to load before the game starts: a Google Fonts URL, `{ url, cdn? }`, or a font file `{ family, src, weight?, style? }`. */
|
|
2
|
+
export type FontSpec = string | {
|
|
3
|
+
url: string;
|
|
4
|
+
cdn?: boolean;
|
|
5
|
+
} | {
|
|
6
|
+
family: string;
|
|
7
|
+
src: string;
|
|
8
|
+
weight?: string;
|
|
9
|
+
style?: string;
|
|
10
|
+
};
|
|
11
|
+
/** One font file of a Google Fonts URL that `thrunium build` or `thrunium dev` shipped with the game. */
|
|
12
|
+
export interface FontManifestEntry {
|
|
13
|
+
family: string;
|
|
14
|
+
/** URL of the font file, relative to the page. */
|
|
15
|
+
src: string;
|
|
16
|
+
weight?: string;
|
|
17
|
+
style?: string;
|
|
18
|
+
unicodeRange?: string;
|
|
19
|
+
}
|
|
20
|
+
/** The manifest the CLI puts in `window.THRUNIUM_FONTS`: Google Fonts URL to its font files. */
|
|
21
|
+
export type FontManifest = Record<string, FontManifestEntry[]>;
|
|
22
|
+
/** Progress of `loadFonts()`. */
|
|
23
|
+
export interface FontStatus {
|
|
24
|
+
/** Number of declared fonts. */
|
|
25
|
+
total: number;
|
|
26
|
+
/** Number of declared fonts that have loaded or failed so far. */
|
|
27
|
+
loaded(): number;
|
|
28
|
+
/** Resolves once every font has loaded or failed; never rejects. */
|
|
29
|
+
done: Promise<void>;
|
|
30
|
+
/** Where each font comes from: shipped with the game, loaded from Google, or a font file. */
|
|
31
|
+
sources: {
|
|
32
|
+
family: string;
|
|
33
|
+
source: 'bundled' | 'google' | 'file';
|
|
34
|
+
}[];
|
|
35
|
+
}
|