@defold-typescript/types 0.56.0 → 0.58.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/ecs/debug.d.ts ADDED
@@ -0,0 +1,57 @@
1
+ // Generated from packages/ecs/src/debug.ts by `bun run --cwd packages/transpiler declarations`; do not edit.
2
+ import type { World } from "./index";
3
+ /**
4
+ * Returned by `inspect`. Keep it on `self` and call its `draw` from `update`.
5
+ * @noSelf
6
+ */
7
+ export interface WorldInspector {
8
+ /**
9
+ * Draws the label and each system's entity count as debug text, 40 units above `target`'s
10
+ * world position, `-` for a `nocache` system. Each call also advances the frame number the
11
+ * console lines show. In a release build it does nothing.
12
+ *
13
+ * @param target The game object to draw above, such as `"."` for the script's own.
14
+ */
15
+ readonly draw: (target: Hash | Url | string) => void;
16
+ }
17
+ /**
18
+ * Prints one line when it is called, then one line each time a system of `target` gains or loses
19
+ * an entity or runs `onModify`, each starting with `ecs`, the label and the frame number. Systems
20
+ * are named by their index and entities by the order this inspector first sees them. A keyed
21
+ * reload keeps the logging, and a later `inspect` of the same world replaces it. A label another
22
+ * `inspect` call already used gets an ordinal, such as `level#2`. In a release build it registers
23
+ * nothing and returns an inspector whose `draw` does nothing, so the calls can stay in shipped
24
+ * code.
25
+ *
26
+ * @param target The world to log.
27
+ * @param label The name the console lines and the debug text show.
28
+ * @returns The inspector, whose `draw` shows each system's entity count above a game object.
29
+ * @example
30
+ * ```ts
31
+ * import { defineScript } from "@defold-typescript/types";
32
+ * import { processingSystem, requireAll, world } from "@defold-typescript/types/ecs";
33
+ * import { inspect } from "@defold-typescript/types/ecs/debug";
34
+ *
35
+ * interface Ent {
36
+ * position?: Vector3;
37
+ * }
38
+ *
39
+ * export default defineScript({
40
+ * init() {
41
+ * const w = world<Ent>(
42
+ * processingSystem<Ent>()(requireAll("position"), {
43
+ * process(e, dt) {
44
+ * e.position.x += dt;
45
+ * },
46
+ * }),
47
+ * );
48
+ * return { w, inspector: inspect(w, "level") };
49
+ * },
50
+ * update(self, dt) {
51
+ * self.w.update(dt);
52
+ * self.inspector.draw(".");
53
+ * },
54
+ * });
55
+ * ```
56
+ */
57
+ export declare function inspect<E>(target: World<E>, label: string): WorldInspector;
package/ecs/hsm.d.ts ADDED
@@ -0,0 +1,192 @@
1
+ // Generated from packages/ecs/src/hsm.ts by `bun run --cwd packages/transpiler declarations`; do not edit.
2
+ import { type System, type World } from "./index";
3
+ type Hook<E> = (this: void, ctx: E) => void;
4
+ /**
5
+ * The `enter` and `exit` hooks `member` returns, to spread into a state's config.
6
+ *
7
+ * @see member
8
+ */
9
+ export interface MembershipHooks<E> {
10
+ /** Sets the component when the machine enters the state. */
11
+ readonly enter: Hook<E>;
12
+ /** Clears the component when the machine leaves the state. */
13
+ readonly exit: Hook<E>;
14
+ }
15
+ interface Ticking {
16
+ readonly update: (this: void, dt: number) => void;
17
+ }
18
+ type OptionalKey<E> = {
19
+ [K in keyof E]-?: Record<never, never> extends Pick<E, K> ? K : never;
20
+ }[keyof E] & string;
21
+ type FlagKey<E> = {
22
+ [K in OptionalKey<E>]: true extends E[K] ? K : never;
23
+ }[OptionalKey<E>];
24
+ type MachineKey<E> = {
25
+ [K in keyof E]-?: NonNullable<E[K]> extends Ticking ? K : never;
26
+ }[keyof E] & string;
27
+ /**
28
+ * Hooks that set the optional `true` component `key` while the machine is in the state.
29
+ *
30
+ * @remarks
31
+ * Entering the state sets `key` to `true` and leaving it clears `key`, so the entity joins and
32
+ * leaves the systems that require it at the world's next `refresh` or `update`. The hooks
33
+ * re-check the entity only while `world` holds it: a pending add reads the components when it
34
+ * applies, and `changed` on an entity the world no longer holds would add it back.
35
+ *
36
+ * @param world The world the entity belongs to.
37
+ * @param key An optional component typed `true`.
38
+ * @returns The `enter` and `exit` hooks, to spread into the state's config.
39
+ * @see https://defold-typescript.github.io/toolchain/ecs-and-hsm#join-systems-from-a-state-with-member
40
+ * @example
41
+ * ```ts
42
+ * import { world } from "@defold-typescript/types/ecs";
43
+ * import { member } from "@defold-typescript/types/ecs/hsm";
44
+ * import { defineMachine } from "@defold-typescript/types/hsm";
45
+ *
46
+ * interface Mole {
47
+ * readonly name: string;
48
+ * up?: true;
49
+ * }
50
+ *
51
+ * const moles = world<Mole>();
52
+ *
53
+ * export const moleMachine = defineMachine<Mole, { type: "HIT" }>()({
54
+ * initial: "/up",
55
+ * states: {
56
+ * up: { ...member(moles, "up"), on: { HIT: "/down" } },
57
+ * down: {},
58
+ * },
59
+ * });
60
+ * ```
61
+ */
62
+ export declare function member<E, K extends FlagKey<E>>(world: World<E>, key: K): MembershipHooks<E>;
63
+ /**
64
+ * Hooks that set the optional component `key` to `value` while the machine is in the state.
65
+ *
66
+ * @remarks
67
+ * Entering the state sets `key` to `value` and leaving it clears `key`, so the entity joins and
68
+ * leaves the systems that require it at the world's next `refresh` or `update`. The hooks
69
+ * re-check the entity only while `world` holds it.
70
+ *
71
+ * @param world The world the entity belongs to.
72
+ * @param key An optional component.
73
+ * @param value What the component holds while the machine is in the state.
74
+ * @returns The `enter` and `exit` hooks, to spread into the state's config.
75
+ * @example
76
+ * ```ts
77
+ * import { world } from "@defold-typescript/types/ecs";
78
+ * import { member } from "@defold-typescript/types/ecs/hsm";
79
+ * import { defineMachine } from "@defold-typescript/types/hsm";
80
+ *
81
+ * interface Hunter {
82
+ * readonly name: string;
83
+ * target?: Url;
84
+ * }
85
+ *
86
+ * const hunters = world<Hunter>();
87
+ *
88
+ * export function hunt(target: Url) {
89
+ * return defineMachine<Hunter, { type: "SPOT" } | { type: "LOSE" }>()({
90
+ * initial: "/idle",
91
+ * states: {
92
+ * idle: { on: { SPOT: "/chase" } },
93
+ * chase: { ...member(hunters, "target", target), on: { LOSE: "/idle" } },
94
+ * },
95
+ * });
96
+ * }
97
+ * ```
98
+ */
99
+ export declare function member<E, K extends OptionalKey<E>>(world: World<E>, key: K, value: NonNullable<E[K]>): MembershipHooks<E>;
100
+ /**
101
+ * A hook that adds the machine's `ctx` to `world` as an entity.
102
+ *
103
+ * @remarks
104
+ * The add applies at the world's next `refresh` or `update`. Set it as the root's `enter` to add
105
+ * the entity when the machine starts.
106
+ *
107
+ * @param world The world to add the entity to.
108
+ * @returns The hook.
109
+ * @see leave
110
+ * @example
111
+ * ```ts
112
+ * import { world } from "@defold-typescript/types/ecs";
113
+ * import { join, leave } from "@defold-typescript/types/ecs/hsm";
114
+ * import { defineMachine } from "@defold-typescript/types/hsm";
115
+ *
116
+ * interface Mole {
117
+ * readonly name: string;
118
+ * }
119
+ *
120
+ * const moles = world<Mole>();
121
+ *
122
+ * export const moleMachine = defineMachine<Mole, { type: "HIT" }>()({
123
+ * initial: "/up",
124
+ * enter: join(moles),
125
+ * states: {
126
+ * up: { on: { HIT: "/gone" } },
127
+ * gone: { enter: leave(moles) },
128
+ * },
129
+ * });
130
+ * ```
131
+ */
132
+ export declare function join<E>(world: World<E>): Hook<E>;
133
+ /**
134
+ * A hook that removes the machine's `ctx` from `world`.
135
+ *
136
+ * @remarks
137
+ * The removal applies at the world's next `refresh` or `update`. Set it as the `enter` of a state
138
+ * the entity leaves the world in.
139
+ *
140
+ * @param world The world to remove the entity from.
141
+ * @returns The hook.
142
+ * @see join
143
+ * @example
144
+ * ```ts
145
+ * import { world } from "@defold-typescript/types/ecs";
146
+ * import { join, leave } from "@defold-typescript/types/ecs/hsm";
147
+ * import { defineMachine } from "@defold-typescript/types/hsm";
148
+ *
149
+ * interface Coin {
150
+ * readonly id: Hash;
151
+ * }
152
+ *
153
+ * const coins = world<Coin>();
154
+ *
155
+ * export const coinMachine = defineMachine<Coin, { type: "PICKED" }>()({
156
+ * initial: "/lying",
157
+ * enter: join(coins),
158
+ * states: {
159
+ * lying: { on: { PICKED: "/picked" } },
160
+ * picked: { enter: leave(coins) },
161
+ * },
162
+ * });
163
+ * ```
164
+ */
165
+ export declare function leave<E>(world: World<E>): Hook<E>;
166
+ /**
167
+ * A processing system that calls `update(dt)` on the machine each entity holds in `key`.
168
+ *
169
+ * @remarks
170
+ * It passes the world update's `dt`, and runs where it stands in the world's system order.
171
+ * Membership a hook changes during the update shows at the next one; update the machines before
172
+ * `update` for same-frame membership.
173
+ *
174
+ * @param key The component that holds the entity's machine.
175
+ * @returns The system, to add to a world.
176
+ * @see https://defold-typescript.github.io/toolchain/ecs-and-hsm#tick-machines-from-the-world-with-tickmachines
177
+ * @example
178
+ * ```ts
179
+ * import { world } from "@defold-typescript/types/ecs";
180
+ * import { tickMachines } from "@defold-typescript/types/ecs/hsm";
181
+ * import type { MachineInstance } from "@defold-typescript/types/hsm";
182
+ *
183
+ * interface Critter {
184
+ * readonly id: Hash;
185
+ * fsm?: MachineInstance<Critter, { type: "SCARE" }>;
186
+ * }
187
+ *
188
+ * export const herd = world<Critter>(tickMachines<Critter>("fsm"));
189
+ * ```
190
+ */
191
+ export declare function tickMachines<E>(key: MachineKey<E>): System<E>;
192
+ export {};