woofjs-engine 0.2.0 → 0.3.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +16 -106
  3. package/THIRD_PARTY_NOTICES.md +7 -0
  4. package/dist/canvas.js +4 -31
  5. package/dist/collision.js +8 -5
  6. package/dist/game.js +4 -84
  7. package/dist/index-1jykaz1m.js +142 -0
  8. package/dist/index-9mz0qg9d.js +39 -0
  9. package/dist/index-b9a70qgn.js +34 -0
  10. package/dist/index-dp75gf4w.js +57 -0
  11. package/dist/index-f63yb8zf.js +85 -0
  12. package/dist/index-gypg8d50.js +31 -0
  13. package/dist/index-pw0zq53c.js +2533 -0
  14. package/dist/index.js +134 -239
  15. package/dist/input.js +4 -33
  16. package/dist/play.js +296 -0
  17. package/dist/sound.js +4 -56
  18. package/dist/store.js +4 -30
  19. package/dist/types/canvas.d.ts +20 -2
  20. package/dist/types/collision.d.ts +62 -0
  21. package/dist/types/game.d.ts +21 -1
  22. package/dist/types/global.d.ts +4 -0
  23. package/dist/types/helpers.d.ts +92 -0
  24. package/dist/types/index.d.ts +23 -12
  25. package/dist/types/input.d.ts +4 -0
  26. package/dist/types/play.d.ts +159 -0
  27. package/dist/types/sound.d.ts +10 -3
  28. package/dist/types/sprites/custom.d.ts +9 -0
  29. package/dist/types/sprites/easing.d.ts +9 -0
  30. package/dist/types/sprites/geometry.d.ts +22 -0
  31. package/dist/types/sprites/options.d.ts +98 -0
  32. package/dist/types/sprites/render.d.ts +2 -0
  33. package/dist/types/sprites/sprite.d.ts +283 -0
  34. package/dist/types/store.d.ts +4 -0
  35. package/dist/types/ui.d.ts +65 -0
  36. package/dist/types/woof.d.ts +386 -0
  37. package/dist/types/world/audio.d.ts +118 -0
  38. package/dist/types/world/globals.d.ts +3 -0
  39. package/dist/types/world/keys.d.ts +2 -0
  40. package/dist/types/world/options.d.ts +44 -0
  41. package/dist/types/world/render.d.ts +13 -0
  42. package/dist/types/world/scheduler.d.ts +65 -0
  43. package/dist/ui.js +235 -0
  44. package/dist/woof.global.js +3577 -0
  45. package/docs/api.json +5359 -0
  46. package/docs/woof-parity.md +231 -0
  47. package/package.json +25 -8
  48. package/docs/architecture.md +0 -37
  49. package/docs/planning.md +0 -33
package/dist/ui.js ADDED
@@ -0,0 +1,235 @@
1
+ import"./index-1jykaz1m.js";
2
+ import {
3
+ woof2
4
+ } from "./index-pw0zq53c.js";
5
+
6
+ // src/ui.ts
7
+ var tokens = {
8
+ background: ["--background", "--p-content-background", "Canvas"],
9
+ foreground: ["--foreground", "--p-text-color", "CanvasText"],
10
+ primary: ["--primary", "--p-primary-color", "LinkText"],
11
+ primaryForeground: [
12
+ "--primary-foreground",
13
+ "--p-primary-contrast-color",
14
+ "Canvas"
15
+ ],
16
+ muted: ["--muted", "--p-content-hover-background", "ButtonFace"],
17
+ mutedForeground: ["--muted-foreground", "--p-text-muted-color", "GrayText"],
18
+ destructive: ["--destructive", "--p-red-500", "red"],
19
+ border: ["--border", "--p-content-border-color", "GrayText"]
20
+ };
21
+ function readTheme(element) {
22
+ const doc = element.ownerDocument;
23
+ const win = doc.defaultView;
24
+ const style = win.getComputedStyle(element);
25
+ const probe = doc.createElement("span");
26
+ probe.style.display = "none";
27
+ element.appendChild(probe);
28
+ const theme = {};
29
+ try {
30
+ for (const name of Object.keys(tokens)) {
31
+ const [shadcn, optimus, system] = tokens[name];
32
+ const value = style.getPropertyValue(shadcn).trim() || style.getPropertyValue(optimus).trim() || system;
33
+ probe.style.color = system;
34
+ probe.style.color = /^[\d.]+(deg)?\s+[\d.]+%\s+[\d.]+%/.test(value) ? `hsl(${value})` : value;
35
+ theme[name] = win.getComputedStyle(probe).color || value;
36
+ }
37
+ } finally {
38
+ probe.remove();
39
+ }
40
+ return theme;
41
+ }
42
+ function watchTheme(element, listener) {
43
+ const doc = element.ownerDocument;
44
+ const win = doc.defaultView;
45
+ let previous = JSON.stringify(readTheme(element));
46
+ const check = () => {
47
+ const theme = readTheme(element);
48
+ const next = JSON.stringify(theme);
49
+ if (next === previous)
50
+ return;
51
+ previous = next;
52
+ listener(theme);
53
+ };
54
+ const observer = new win.MutationObserver(check);
55
+ for (let node = element;node; node = node.parentElement)
56
+ observer.observe(node, {
57
+ attributes: true,
58
+ attributeFilter: ["class", "style", "data-theme", "data-mode", "data-palette"]
59
+ });
60
+ observer.observe(doc.head, {
61
+ childList: true,
62
+ subtree: true,
63
+ characterData: true
64
+ });
65
+ const preference = win.matchMedia("(prefers-color-scheme: dark)");
66
+ preference.addEventListener("change", check);
67
+ return () => {
68
+ observer.disconnect();
69
+ preference.removeEventListener("change", check);
70
+ };
71
+ }
72
+ var scrollKeys = new Set([
73
+ " ",
74
+ "ArrowUp",
75
+ "ArrowDown",
76
+ "ArrowLeft",
77
+ "ArrowRight"
78
+ ]);
79
+ function mountGame(element, setup, options = {}) {
80
+ const {
81
+ label = "Game",
82
+ muted = false,
83
+ autoStart = true,
84
+ onError,
85
+ audioContext,
86
+ backdrop,
87
+ ...projectOptions
88
+ } = options;
89
+ const win = element.ownerDocument.defaultView;
90
+ const theme = readTheme(element);
91
+ const listeners = new Set;
92
+ let state = { running: false, muted, error: undefined };
93
+ let game;
94
+ let cleanup;
95
+ let audio;
96
+ let destroyed = false;
97
+ const set = (change) => {
98
+ const next = { ...state, ...change };
99
+ if (next.running === state.running && next.muted === state.muted && next.error === state.error)
100
+ return;
101
+ state = next;
102
+ for (const listener of [...listeners])
103
+ listener(state);
104
+ };
105
+ const fail = (error) => {
106
+ onError?.(error);
107
+ if (state.error === undefined)
108
+ set({ error });
109
+ };
110
+ const close = () => {
111
+ try {
112
+ cleanup?.();
113
+ } catch (error) {
114
+ fail(error);
115
+ }
116
+ cleanup = undefined;
117
+ game?.destroy();
118
+ game = undefined;
119
+ audio = undefined;
120
+ };
121
+ const build = () => {
122
+ const project = woof2({
123
+ document: element.ownerDocument,
124
+ window: win,
125
+ ...projectOptions,
126
+ backdrop,
127
+ parent: element,
128
+ autoStart: false,
129
+ onError: fail,
130
+ audioContext: audioContext ?? (() => audio ??= new win.AudioContext)
131
+ });
132
+ game = project;
133
+ const canvas = project.canvas;
134
+ canvas.style.maxWidth = "100%";
135
+ canvas.style.height = "auto";
136
+ canvas.tabIndex = 0;
137
+ canvas.setAttribute("role", "application");
138
+ canvas.setAttribute("aria-label", label);
139
+ canvas.addEventListener("keydown", (event) => {
140
+ if (scrollKeys.has(event.key))
141
+ event.preventDefault();
142
+ });
143
+ if (backdrop === undefined)
144
+ project.setBackdropColor(theme.background);
145
+ if (state.muted)
146
+ project.mute();
147
+ try {
148
+ cleanup = setup(project, theme);
149
+ } catch (error) {
150
+ fail(error);
151
+ }
152
+ project.step(0);
153
+ };
154
+ const run = () => {
155
+ game?.start();
156
+ if (audio?.state === "suspended")
157
+ audio.resume();
158
+ set({ running: true });
159
+ };
160
+ const stopTheme = watchTheme(element, (next) => {
161
+ Object.assign(theme, next);
162
+ if (backdrop === undefined)
163
+ game?.setBackdropColor(theme.background);
164
+ if (!state.running)
165
+ game?.step(0);
166
+ });
167
+ const controller = {
168
+ get game() {
169
+ return game;
170
+ },
171
+ getState: () => state,
172
+ subscribe(listener) {
173
+ listeners.add(listener);
174
+ listener(state);
175
+ return () => {
176
+ listeners.delete(listener);
177
+ };
178
+ },
179
+ play() {
180
+ if (destroyed)
181
+ return;
182
+ run();
183
+ game?.canvas.focus({ preventScroll: true });
184
+ },
185
+ pause() {
186
+ if (destroyed)
187
+ return;
188
+ game?.stop();
189
+ if (audio?.state === "running")
190
+ audio.suspend();
191
+ set({ running: false });
192
+ },
193
+ toggle() {
194
+ if (state.running)
195
+ controller.pause();
196
+ else
197
+ controller.play();
198
+ },
199
+ restart() {
200
+ if (destroyed)
201
+ return;
202
+ close();
203
+ set({ running: false, error: undefined });
204
+ build();
205
+ controller.play();
206
+ },
207
+ setMuted(value) {
208
+ if (destroyed)
209
+ return;
210
+ if (value)
211
+ game?.mute();
212
+ else
213
+ game?.unmute();
214
+ set({ muted: value });
215
+ },
216
+ destroy() {
217
+ if (destroyed)
218
+ return;
219
+ destroyed = true;
220
+ stopTheme();
221
+ close();
222
+ set({ running: false });
223
+ listeners.clear();
224
+ }
225
+ };
226
+ build();
227
+ if (autoStart)
228
+ run();
229
+ return controller;
230
+ }
231
+ export {
232
+ mountGame,
233
+ readTheme,
234
+ watchTheme
235
+ };