minimojs 1.0.0-alpha.37 → 1.0.0-alpha.39

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,181 @@
1
+ import { DrawSprite, Game } from "./minimo.js";
2
+ /** The kinds of die a {@link Dice} set makes: a d4, d6, d8, d10, d12, or d20. */
3
+ export type DieSides = 4 | 6 | 8 | 10 | 12 | 20;
4
+ /**
5
+ * How a die turns, in degrees per second about the screen's three axes.
6
+ * The sign is the direction and the size is the speed.
7
+ *
8
+ * - `x`: positive turns the die's top toward the player.
9
+ * - `y`: positive turns the die's front to the right.
10
+ * - `z`: positive turns the die clockwise, as `Sprite.rotation` does.
11
+ */
12
+ export interface DieSpin {
13
+ x?: number;
14
+ y?: number;
15
+ z?: number;
16
+ }
17
+ /**
18
+ * The look of a die. A {@link Dice} set holds one for all its dice, and each
19
+ * die can override any field.
20
+ */
21
+ export interface DieLook {
22
+ /** Color of the die's body. Default `"#f4f1ea"`. */
23
+ color?: string;
24
+ /** Color of the die's edges. Default `"rgba(0, 0, 0, 0.35)"`. */
25
+ edgeColor?: string;
26
+ /** Color of the pips and numbers. Default `"#1b1f1d"`. */
27
+ markColor?: string;
28
+ /**
29
+ * How faces are marked: `"pips"` or `"numbers"`. Pips exist only on a d6,
30
+ * and are its default; every other die shows numbers. On a die with
31
+ * numbers, `6` and `9` are underlined, as on real dice.
32
+ */
33
+ marks?: "pips" | "numbers";
34
+ /**
35
+ * CSS font family of the numbers, such as `'"Lilita One"'`. Use a web font
36
+ * the page declares and the game registers with `game.requireFont`, so the
37
+ * numbers look the same on every device. Default: the engine's default
38
+ * font, `monospace`. Pips are shapes and need no font.
39
+ */
40
+ font?: string;
41
+ /** Font weight of the numbers. Default `"700"`. */
42
+ fontWeight?: string;
43
+ /** Body color of single faces, by value, over {@link DieLook.color}. */
44
+ faceColors?: Record<number, string>;
45
+ /**
46
+ * An image key for single faces, by value, drawn in place of that face's
47
+ * pips or number. On a d6 the image fills the face. On a d4, whose result is
48
+ * read at its top corner, it is drawn at the corner of that value on each
49
+ * face.
50
+ */
51
+ faces?: Record<number, string>;
52
+ }
53
+ /** Where a new die is, how big, and how it looks. */
54
+ export interface DieOptions extends DieLook {
55
+ /** X position in world space (center), in pixels. Default `0`. */
56
+ x?: number;
57
+ /** Y position in world space (center), in pixels. Default `0`. */
58
+ y?: number;
59
+ /**
60
+ * About how wide the die is at rest, in pixels. Default `64`. It is also the
61
+ * die's collision body; its drawing surface is larger, to hold the die at
62
+ * every angle.
63
+ */
64
+ size?: number;
65
+ /** The face shown at first. Default `1`. */
66
+ value?: number;
67
+ }
68
+ /** How {@link Die.roll} throws a die. */
69
+ export interface DieRollOptions {
70
+ /**
71
+ * The turning at the throw, which slows down to a stop. Default: the die's
72
+ * current spin while it turns, otherwise `{ x: 600, y: 420, z: 90 }`.
73
+ */
74
+ spin?: DieSpin;
75
+ /** How long the roll takes, in milliseconds. Default `1000`. */
76
+ ms?: number;
77
+ }
78
+ /**
79
+ * A set of dice that share a look. It makes each die and advances all of them
80
+ * with one {@link Dice.update} call per frame.
81
+ *
82
+ * The set never decides a result: the game chooses each value, and the dice
83
+ * turn and land on it. The same value, spin, and duration turn a die the same
84
+ * way on every device, so a multiplayer room can put them in its shared state
85
+ * and every player sees the same roll.
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * const dice = new Dice(game, { color: "#f4f1ea", font: '"Lilita One"' });
90
+ * const d20 = dice.create(20, { x: 200, y: 300, size: 72 });
91
+ * game.onUpdate = (dt) => dice.update(dt);
92
+ *
93
+ * const value = 1 + Math.floor(game.random() * 20);
94
+ * await d20.roll(value, { spin: { x: 540, y: -360, z: 90 }, ms: 1200 });
95
+ * ```
96
+ */
97
+ export declare class Dice {
98
+ /** The game the dice are added to. */
99
+ readonly game: Game;
100
+ /** The look every die of the set starts from. */
101
+ readonly look: DieLook;
102
+ constructor(game: Game, look?: DieLook);
103
+ /** The dice of this set, in the order they were made. */
104
+ get dice(): readonly Die[];
105
+ /**
106
+ * Makes a die, adds it to the game, and returns it. The die is a sprite: the
107
+ * game can move it, give it gravity, and test its collisions while it turns.
108
+ * @param sides - `4`, `6`, `8`, `10`, `12`, or `20`.
109
+ * @param options - Position, size, first face, and any look over the set's.
110
+ */
111
+ create(sides: DieSides, options?: DieOptions): Die;
112
+ /** Removes a die from the game and from this set. */
113
+ remove(die: Die): void;
114
+ /** Advances every die of the set. Call it once per frame from `onUpdate`. */
115
+ update(dt: number): void;
116
+ }
117
+ /**
118
+ * One die, drawn in simulated 3D: a real solid, turned, shaded, and projected
119
+ * onto the 2D canvas. It is a `DrawSprite`, so it has everything a sprite has.
120
+ * The module turns it in place; moving it is the game's, with the same
121
+ * physics as any other sprite.
122
+ *
123
+ * Make dice with {@link Dice.create}, which also advances them. A die made
124
+ * with `new Die` must be added with `game.add` and advanced with
125
+ * {@link Die.update} by the game.
126
+ */
127
+ export declare class Die extends DrawSprite {
128
+ /** The kind of die: `4`, `6`, `8`, `10`, `12`, or `20`. */
129
+ readonly sides: DieSides;
130
+ /** About how wide the die is at rest, in pixels. */
131
+ readonly size: number;
132
+ /** Called with the value whenever the die comes to rest after turning. */
133
+ onLand: ((value: number) => void) | null;
134
+ /**
135
+ * @param sides - `4`, `6`, `8`, `10`, `12`, or `20`.
136
+ * @param options - Position, size, first face, and look.
137
+ */
138
+ constructor(sides: DieSides, options?: DieOptions);
139
+ /** The look of this die. Change it with {@link Die.setLook}. */
140
+ get look(): Readonly<DieLook>;
141
+ /** Changes some of the die's look, keeping the rest. */
142
+ setLook(look: DieLook): void;
143
+ /**
144
+ * The value facing the player: the result once the die is at rest, and
145
+ * whatever face is in front while it turns. A d4 is read at its top corner,
146
+ * as a real one is: its value is the corner pointing up.
147
+ */
148
+ get value(): number;
149
+ /** `true` while the die turns, from a spin or a roll until it lands. */
150
+ get rolling(): boolean;
151
+ /**
152
+ * Starts the die turning, or changes how it turns, and keeps it turning
153
+ * until {@link Die.land} or {@link Die.show}. A landing in progress stops,
154
+ * and the die turns freely again.
155
+ */
156
+ spin(spin: DieSpin): void;
157
+ /**
158
+ * Slows the die down and settles it on `value`, upright, in `ms`. The die
159
+ * keeps the direction and speed it was turning at, and slows to a stop
160
+ * exactly on its face.
161
+ * @returns A promise that resolves with the value when the die is at rest.
162
+ */
163
+ land(value: number, ms?: number): Promise<number>;
164
+ /**
165
+ * Throws the die: it starts turning with `spin` and lands on `value` after
166
+ * `ms`, slowing down all the way. This is the call most games need.
167
+ * @returns A promise that resolves with the value when the die is at rest.
168
+ */
169
+ roll(value: number, options?: DieRollOptions): Promise<number>;
170
+ /**
171
+ * Turns the die to `value` at once, with no animation, such as when a game
172
+ * restores its state. A die that was turning stops.
173
+ */
174
+ show(value: number): void;
175
+ /**
176
+ * Advances the die's turning. {@link Dice.update} calls it for every die of
177
+ * its set; call it yourself only for a die made with `new Die`.
178
+ */
179
+ update(dt: number): void;
180
+ redraw(ctx: CanvasRenderingContext2D): void;
181
+ }