@cjcoding-git/gamelib 1.1.2 → 1.1.3

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 (136) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  2. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  3. package/.github/dependabot.yml +6 -0
  4. package/.github/workflows/build.yml +54 -0
  5. package/.github/workflows/release.yml +79 -0
  6. package/LICENSE +21 -0
  7. package/README.md +7 -0
  8. package/assets/test-frog.gltf +1921 -0
  9. package/assets/test-frog.gml +0 -0
  10. package/buildtime.txt +1 -0
  11. package/buildversion.txt +1 -0
  12. package/dist/GameLib.js +71599 -0
  13. package/dist/GameLib.min.js +8 -0
  14. package/docs/2D/API.md +1 -0
  15. package/docs/2D/GETTING_STARTED.md +1 -0
  16. package/docs/3D/API.md +1 -0
  17. package/docs/3D/GETTING_STARTED.md +23 -0
  18. package/docs/Core/API.md +1 -0
  19. package/docs/Core/GETTING_STARTED.md +1 -0
  20. package/docs/README.md +32 -0
  21. package/package.json +12 -13
  22. package/scripts/gltf2gml.html +312 -0
  23. package/src/2D/Box.js +42 -0
  24. package/src/2D/Coord.js +17 -0
  25. package/src/2D/GameLib.js +317 -0
  26. package/src/2D/Renderable.js +31 -0
  27. package/src/2D/Rotation.js +42 -0
  28. package/src/2D/Size.js +17 -0
  29. package/src/2D/Sprite.js +56 -0
  30. package/src/2D/UI/Button.js +52 -0
  31. package/src/2D/UI/TextInput.js +77 -0
  32. package/src/2D/UI/UIElement.js +10 -0
  33. package/src/2D/UI/index.js +15 -0
  34. package/src/2D/index.js +24 -0
  35. package/src/3D/Camera.js +25 -0
  36. package/src/3D/GameLib.js +202 -0
  37. package/src/3D/Geometry/Box.js +39 -0
  38. package/src/3D/Geometry/Cone.js +45 -0
  39. package/src/3D/Geometry/ModelLoaders/GLBLoader.js +12 -0
  40. package/src/3D/Geometry/ModelLoaders/GLTFLoader.js +12 -0
  41. package/src/3D/Geometry/ModelLoaders/GMLLoader.js +114 -0
  42. package/src/3D/Geometry/ModelLoaders/OBJLoader.js +12 -0
  43. package/src/3D/Geometry/ModelLoaders/index.js +17 -0
  44. package/src/3D/Geometry/Pyramid.js +55 -0
  45. package/src/3D/Geometry/Sphere.js +37 -0
  46. package/src/3D/Geometry/index.js +19 -0
  47. package/src/3D/Lighting/AmbientLight.js +17 -0
  48. package/src/3D/Lighting/DirectionalLight.js +17 -0
  49. package/src/3D/Lighting/PointLight.js +17 -0
  50. package/src/3D/Lighting/index.js +15 -0
  51. package/src/3D/Material.js +181 -0
  52. package/src/3D/Mesh.js +142 -0
  53. package/src/3D/Object.js +13 -0
  54. package/src/3D/Position.js +15 -0
  55. package/src/3D/Rotation.js +21 -0
  56. package/src/3D/Scale.js +10 -0
  57. package/src/3D/ShaderFactory/FunctionParam.js +14 -0
  58. package/src/3D/ShaderFactory/Modifiers/Variable.js +85 -0
  59. package/src/3D/ShaderFactory/Modifiers/index.js +11 -0
  60. package/src/3D/ShaderFactory/Node.js +25 -0
  61. package/src/3D/ShaderFactory/NodeTypes/A.js +9 -0
  62. package/src/3D/ShaderFactory/NodeTypes/Add.js +13 -0
  63. package/src/3D/ShaderFactory/NodeTypes/AssignVariable.js +11 -0
  64. package/src/3D/ShaderFactory/NodeTypes/Clamp.js +14 -0
  65. package/src/3D/ShaderFactory/NodeTypes/Constant.js +9 -0
  66. package/src/3D/ShaderFactory/NodeTypes/Define.js +11 -0
  67. package/src/3D/ShaderFactory/NodeTypes/Divide.js +13 -0
  68. package/src/3D/ShaderFactory/NodeTypes/Dot.js +14 -0
  69. package/src/3D/ShaderFactory/NodeTypes/Exp2.js +9 -0
  70. package/src/3D/ShaderFactory/NodeTypes/Function.js +36 -0
  71. package/src/3D/ShaderFactory/NodeTypes/Group.js +9 -0
  72. package/src/3D/ShaderFactory/NodeTypes/Header.js +12 -0
  73. package/src/3D/ShaderFactory/NodeTypes/Length.js +9 -0
  74. package/src/3D/ShaderFactory/NodeTypes/MainFunction.js +7 -0
  75. package/src/3D/ShaderFactory/NodeTypes/Max.js +14 -0
  76. package/src/3D/ShaderFactory/NodeTypes/Mix.js +14 -0
  77. package/src/3D/ShaderFactory/NodeTypes/Multiply.js +13 -0
  78. package/src/3D/ShaderFactory/NodeTypes/Negative.js +9 -0
  79. package/src/3D/ShaderFactory/NodeTypes/NodeTypes.js +9 -0
  80. package/src/3D/ShaderFactory/NodeTypes/Normalize.js +10 -0
  81. package/src/3D/ShaderFactory/NodeTypes/Precision.js +11 -0
  82. package/src/3D/ShaderFactory/NodeTypes/RGB.js +9 -0
  83. package/src/3D/ShaderFactory/NodeTypes/Subtract.js +13 -0
  84. package/src/3D/ShaderFactory/NodeTypes/Symbol.js +9 -0
  85. package/src/3D/ShaderFactory/NodeTypes/Texture2D.js +10 -0
  86. package/src/3D/ShaderFactory/NodeTypes/Variable.js +22 -0
  87. package/src/3D/ShaderFactory/NodeTypes/Vector.js +15 -0
  88. package/src/3D/ShaderFactory/NodeTypes/XYZ.js +9 -0
  89. package/src/3D/ShaderFactory/NodeTypes/index.js +32 -0
  90. package/src/3D/ShaderFactory/PrecisionTypes.js +8 -0
  91. package/src/3D/ShaderFactory/Program.js +41 -0
  92. package/src/3D/ShaderFactory/VariableTypes.js +49 -0
  93. package/src/3D/ShaderFactory/index.js +45 -0
  94. package/src/3D/Texture.js +59 -0
  95. package/src/3D/TextureFactory.js +22 -0
  96. package/src/3D/Transform.js +35 -0
  97. package/src/3D/UI/UIImage.js +24 -0
  98. package/src/3D/UI/UILoading.js +41 -0
  99. package/src/3D/UI/UIObject.js +16 -0
  100. package/src/3D/UI/UIPath.js +25 -0
  101. package/src/3D/UI/UIRenderer.js +207 -0
  102. package/src/3D/UI/UIText.js +35 -0
  103. package/src/3D/UI/index.js +19 -0
  104. package/src/3D/Utils.js +9 -0
  105. package/src/3D/index.js +43 -0
  106. package/src/Core/AssetManager.js +116 -0
  107. package/src/Core/AudioManager.js +151 -0
  108. package/src/Core/Error.js +9 -0
  109. package/src/Core/FullscreenManager.js +49 -0
  110. package/src/Core/GameLibBase.js +29 -0
  111. package/src/Core/InputManager.js +195 -0
  112. package/src/Core/Loading.js +111 -0
  113. package/src/Core/Matrix/index.js +11 -0
  114. package/src/Core/Matrix/mat4.js +178 -0
  115. package/src/Core/Multiplayer.js +54 -0
  116. package/src/Core/Network/PeerNetwork.js +410 -0
  117. package/src/Core/Network/index.js +11 -0
  118. package/src/Core/Storage.js +45 -0
  119. package/src/Core/Store.js +45 -0
  120. package/src/Core/Utils.js +34 -0
  121. package/src/Core/index.js +30 -0
  122. package/src/Lib/Ammo.js +72 -0
  123. package/src/Lib/Matter.js +1 -0
  124. package/src/Shaders/Material/Fragment/GL1.glsl +49 -0
  125. package/src/Shaders/Material/Fragment/GL2.glsl +51 -0
  126. package/src/Shaders/Material/Vertex/GL1.glsl +19 -0
  127. package/src/Shaders/Material/Vertex/GL2.glsl +20 -0
  128. package/src/Shaders/UI/Fragment/GL1.glsl +5 -0
  129. package/src/Shaders/UI/Fragment/GL2.glsl +7 -0
  130. package/src/Shaders/UI/Vertex/GL1.glsl +10 -0
  131. package/src/Shaders/UI/Vertex/GL2.glsl +11 -0
  132. package/src/Shaders/UITexture/Fragment/GL1.glsl +6 -0
  133. package/src/Shaders/UITexture/Fragment/GL2.glsl +8 -0
  134. package/src/Shaders/UITexture/Vertex/GL1.glsl +10 -0
  135. package/src/Shaders/UITexture/Vertex/GL2.glsl +11 -0
  136. package/src/index.js +19 -0
@@ -0,0 +1,410 @@
1
+ var DEFAULT_CONFIG = { iceServers: [{ urls: "stun:stun.l.google.com:19302" }] };
2
+ var SIGNALING_VERSION = "1.5.5";
3
+ var CLOSE_PACKET = JSON.stringify({ __peerData: { type: "close" } });
4
+ class PeerNetwork {
5
+ constructor(options = {}) {
6
+ this.options = options;
7
+ this.id = options.id || PeerNetwork._makeId();
8
+ this.token = options.token || PeerNetwork._makeToken();
9
+ this.socket = null;
10
+ this.peers = new Map();
11
+ this.links = new Map();
12
+ this.listeners = new Map();
13
+ this.candidates = new Map();
14
+ this.state = "idle";
15
+ this._ready = null;
16
+ this._rejectReady = null;
17
+ this._heartbeat = null;
18
+ this._closed = false;
19
+ }
20
+ on(event, listener) {
21
+ if (typeof listener !== "function") throw new TypeError("listener must be a function");
22
+ if (!this.listeners.has(event)) this.listeners.set(event, new Set());
23
+ this.listeners.get(event).add(listener);
24
+ return this;
25
+ }
26
+ off(event, listener) {
27
+ if (this.listeners.has(event)) this.listeners.get(event).delete(listener);
28
+ return this;
29
+ }
30
+ start() {
31
+ if (this._closed) return Promise.reject(new Error("Network has been closed"));
32
+ if (this.state === "ready") return Promise.resolve(this.id);
33
+ if (this._ready) return this._ready;
34
+ var WebSocketType = this.options.WebSocket || globalThis.WebSocket;
35
+ if (!WebSocketType) return Promise.reject(new Error("WebSocket is unavailable"));
36
+ this.state = "connecting";
37
+ var self = this;
38
+ this._ready = new Promise(function(resolve, reject) {
39
+ var settled = false;
40
+ try {
41
+ self.socket = new WebSocketType(self._brokerUrl());
42
+ } catch (error) {
43
+ self.state = "idle";
44
+ self._ready = null;
45
+ reject(error);
46
+ return;
47
+ }
48
+ var socket = self.socket;
49
+ self._rejectReady = function(error) {
50
+ if (settled) return;
51
+ settled = true;
52
+ self.state = "idle";
53
+ self._ready = null;
54
+ self._rejectReady = null;
55
+ reject(error);
56
+ socket.close();
57
+ };
58
+ socket.onopen = function() {
59
+ self._heartbeat = setInterval(function() { self._signal({ type: "HEARTBEAT" }); }, self.options.heartbeatInterval || 5000);
60
+ };
61
+ socket.onmessage = function(event) {
62
+ var message;
63
+ try {
64
+ message = JSON.parse(event.data);
65
+ } catch (error) {
66
+ self._emit("error", new Error("Invalid broker message"));
67
+ return;
68
+ }
69
+ if (message.type === "OPEN") {
70
+ if (settled) return;
71
+ settled = true;
72
+ self.state = "ready";
73
+ self._ready = null;
74
+ self._rejectReady = null;
75
+ self._emit("ready", self.id);
76
+ resolve(self.id);
77
+ return;
78
+ }
79
+ if (!settled && ["ERROR", "INVALID-KEY", "ID-TAKEN"].includes(message.type)) {
80
+ self._rejectReady(new Error(message.payload?.msg || message.type));
81
+ self._emit("error", new Error(message.payload?.msg || message.type));
82
+ return;
83
+ }
84
+ self._route(message);
85
+ };
86
+ socket.onerror = function(error) {
87
+ self._emit("error", error);
88
+ if (!settled) self._rejectReady(error instanceof Error ? error : new Error("Could not connect to signaling server"));
89
+ };
90
+ socket.onclose = function(event) {
91
+ clearInterval(self._heartbeat);
92
+ self._heartbeat = null;
93
+ if (self.socket === socket) self.socket = null;
94
+ var wasReady = self.state === "ready";
95
+ self.state = self._closed ? "closed" : "idle";
96
+ if (!settled) {
97
+ settled = true;
98
+ self._ready = null;
99
+ self._rejectReady = null;
100
+ reject(new Error("Signaling socket closed before registration (" + (event.code || "closed") + ")"));
101
+ }
102
+ if (wasReady && !self._closed) self._emit("serverclose", event);
103
+ };
104
+ });
105
+ return this._ready;
106
+ }
107
+ async connect(peerId, options = {}) {
108
+ if (!peerId) throw new TypeError("peerId is required");
109
+ if (this._closed) throw new Error("Network has been closed");
110
+ await this.start();
111
+ var id = options.connectionId || "dc_" + PeerNetwork._makeToken();
112
+ var link = this._newLink(peerId, id, options);
113
+ link.channel = link.pc.createDataChannel(options.label || id, {
114
+ ordered: options.reliable !== false,
115
+ ...(options.maxRetransmits == null ? {} : { maxRetransmits: options.maxRetransmits })
116
+ });
117
+ this._watchChannel(link);
118
+ var opened = new Promise(function(resolve, reject) {
119
+ link.once("open", function() { resolve(link); });
120
+ link.once("error", reject);
121
+ link.once("close", function() { reject(new Error("Connection to " + peerId + " closed before opening")); });
122
+ });
123
+ try {
124
+ var offer = await link.pc.createOffer();
125
+ await link.pc.setLocalDescription(offer);
126
+ this._signal({
127
+ type: "OFFER",
128
+ dst: peerId,
129
+ payload: {
130
+ type: "data",
131
+ connectionId: id,
132
+ sdp: link.pc.localDescription,
133
+ label: link.label,
134
+ reliable: link.reliable,
135
+ serialization: link.serialization,
136
+ metadata: link.metadata
137
+ }
138
+ });
139
+ } catch (error) {
140
+ this._fail(link, error);
141
+ throw error;
142
+ }
143
+ return opened;
144
+ }
145
+ getConnectedIds() {
146
+ return Array.from(this.peers.keys());
147
+ }
148
+ send(peerId, data) {
149
+ var link = this.peers.get(peerId);
150
+ if (!link || !link.open) return false;
151
+ link.send(data);
152
+ return true;
153
+ }
154
+ broadcast(data) {
155
+ var delivered = 0;
156
+ this.peers.forEach(function(link) {
157
+ if (link.open) {
158
+ link.send(data);
159
+ delivered++;
160
+ }
161
+ });
162
+ return delivered;
163
+ }
164
+ close(peerId) {
165
+ if (peerId == null) {
166
+ Array.from(this.links.values()).forEach((link) => this._end(link, true));
167
+ } else {
168
+ this.links.forEach((link) => {
169
+ if (link.peerId === peerId) this._end(link, true);
170
+ });
171
+ }
172
+ return this;
173
+ }
174
+ destroy() {
175
+ if (this._closed) return this;
176
+ this._closed = true;
177
+ if (this._rejectReady) this._rejectReady(new Error("Network has been closed"));
178
+ this.close();
179
+ clearInterval(this._heartbeat);
180
+ this._heartbeat = null;
181
+ var socket = this.socket;
182
+ this.socket = null;
183
+ if (socket) {
184
+ socket.onopen = socket.onmessage = socket.onerror = socket.onclose = null;
185
+ if (socket.readyState < 2) socket.close();
186
+ }
187
+ this.state = "closed";
188
+ this._emit("close");
189
+ this.listeners.clear();
190
+ return this;
191
+ }
192
+ _brokerUrl() {
193
+ var location = globalThis.location;
194
+ var host = this.options.host || location?.hostname || "0.peerjs.com";
195
+ var secure = this.options.secure ?? (host === "0.peerjs.com" || location?.protocol === "https:");
196
+ var port = this.options.port ?? (secure ? 443 : 9000);
197
+ var path = this.options.path || "/";
198
+ if (!path.startsWith("/")) path = "/" + path;
199
+ if (!path.endsWith("/")) path += "/";
200
+ var url = new URL((secure ? "wss:" : "ws:") + "//" + host + ":" + port + path + "peerjs");
201
+ url.searchParams.set("key", this.options.key || "peerjs");
202
+ url.searchParams.set("id", this.id);
203
+ url.searchParams.set("token", this.token);
204
+ url.searchParams.set("version", SIGNALING_VERSION);
205
+ return url.href;
206
+ }
207
+ _signal(message) {
208
+ if (this.socket && this.socket.readyState === 1) this.socket.send(JSON.stringify(message));
209
+ }
210
+ _route(message) {
211
+ var payload = message.payload || {};
212
+ switch (message.type) {
213
+ case "OFFER":
214
+ if (payload.type === "data") this._acceptOffer(message.src, payload);
215
+ break;
216
+ case "ANSWER":
217
+ this._acceptAnswer(payload);
218
+ break;
219
+ case "CANDIDATE":
220
+ this._acceptCandidate(payload);
221
+ break;
222
+ case "LEAVE":
223
+ Array.from(this.links.values()).forEach((link) => {
224
+ if (link.peerId === message.src) this._end(link, false);
225
+ });
226
+ break;
227
+ case "EXPIRE":
228
+ this._emit("error", new Error("Peer unavailable: " + (message.src || "unknown")));
229
+ break;
230
+ case "ID-TAKEN":
231
+ case "INVALID-KEY":
232
+ case "ERROR":
233
+ this._emit("error", new Error(payload.msg || message.type));
234
+ break;
235
+ default:
236
+ this._emit("signal", message);
237
+ }
238
+ }
239
+ _newLink(peerId, connectionId, options = {}) {
240
+ var RTCPeerConnectionType = this.options.RTCPeerConnection || globalThis.RTCPeerConnection;
241
+ if (!RTCPeerConnectionType) throw new Error("WebRTC is unavailable");
242
+ var pc = new RTCPeerConnectionType(options.config || this.options.config || DEFAULT_CONFIG);
243
+ var listeners = new Map();
244
+ var self = this;
245
+ var link = {
246
+ peerId: peerId,
247
+ connectionId: connectionId,
248
+ label: options.label || connectionId,
249
+ metadata: options.metadata,
250
+ serialization: options.serialization || "json",
251
+ reliable: options.reliable !== false,
252
+ pc: pc,
253
+ channel: null,
254
+ open: false,
255
+ on: function(name, callback) {
256
+ if (!listeners.has(name)) listeners.set(name, new Set());
257
+ listeners.get(name).add(callback);
258
+ return this;
259
+ },
260
+ once: function(name, callback) {
261
+ var onceCallback = function() {
262
+ this.off(name, onceCallback);
263
+ callback.apply(null, arguments);
264
+ }.bind(this);
265
+ return this.on(name, onceCallback);
266
+ },
267
+ off: function(name, callback) {
268
+ if (listeners.has(name)) listeners.get(name).delete(callback);
269
+ return this;
270
+ },
271
+ emit: function(name) {
272
+ var args = Array.prototype.slice.call(arguments, 1);
273
+ Array.from(listeners.get(name) || []).forEach(function(callback) { callback.apply(null, args); });
274
+ },
275
+ send: function(data) {
276
+ if (!this.open || !this.channel || this.channel.readyState !== "open") throw new Error("Connection is not open");
277
+ this.channel.send(this.serialization === "json" ? JSON.stringify(data) : data);
278
+ },
279
+ close: function() { self._end(link, true); }
280
+ };
281
+ this.links.set(connectionId, link);
282
+ pc.onicecandidate = function(event) {
283
+ if (event.candidate && event.candidate.candidate) {
284
+ self._signal({ type: "CANDIDATE", dst: peerId, payload: { type: "data", connectionId: connectionId, candidate: event.candidate } });
285
+ }
286
+ };
287
+ pc.ondatachannel = function(event) {
288
+ link.channel = event.channel;
289
+ link.label = event.channel.label;
290
+ self._watchChannel(link);
291
+ };
292
+ pc.onconnectionstatechange = function() {
293
+ if (pc.connectionState === "failed") self._fail(link, new Error("WebRTC connection failed"));
294
+ else if (pc.connectionState === "closed") self._end(link, false);
295
+ };
296
+ return link;
297
+ }
298
+ _watchChannel(link) {
299
+ var channel = link.channel;
300
+ channel.onopen = () => {
301
+ if (link.open || link._ended) return;
302
+ link.open = true;
303
+ this.peers.set(link.peerId, link);
304
+ link.emit("open", link);
305
+ this._emit("connection", link);
306
+ };
307
+ channel.onmessage = (event) => {
308
+ var packet = event.data;
309
+ if (link.serialization === "json" && typeof packet === "string") {
310
+ try { packet = JSON.parse(packet); } catch (error) {}
311
+ }
312
+ if (packet && packet.__peerData && packet.__peerData.type === "close") {
313
+ this._end(link, false);
314
+ return;
315
+ }
316
+ link.emit("data", packet);
317
+ this._emit("data", packet, link);
318
+ };
319
+ channel.onerror = (error) => this._fail(link, error);
320
+ channel.onclose = () => this._end(link, false);
321
+ }
322
+ async _acceptOffer(peerId, payload) {
323
+ if (!peerId || !payload.connectionId || !payload.sdp) return;
324
+ var previous = this.links.get(payload.connectionId);
325
+ if (previous) this._end(previous, false);
326
+ var link = this._newLink(peerId, payload.connectionId, payload);
327
+ link.label = payload.label || payload.connectionId;
328
+ link.metadata = payload.metadata;
329
+ link.serialization = payload.serialization || "json";
330
+ link.reliable = payload.reliable !== false;
331
+ try {
332
+ await link.pc.setRemoteDescription(payload.sdp);
333
+ await this._drainCandidates(link);
334
+ var answer = await link.pc.createAnswer();
335
+ await link.pc.setLocalDescription(answer);
336
+ this._signal({ type: "ANSWER", dst: peerId, payload: { type: "data", connectionId: link.connectionId, sdp: link.pc.localDescription } });
337
+ } catch (error) {
338
+ this._fail(link, error);
339
+ }
340
+ }
341
+ async _acceptAnswer(payload) {
342
+ var link = this.links.get(payload.connectionId);
343
+ if (!link || !payload.sdp) return;
344
+ try {
345
+ await link.pc.setRemoteDescription(payload.sdp);
346
+ await this._drainCandidates(link);
347
+ } catch (error) {
348
+ this._fail(link, error);
349
+ }
350
+ }
351
+ async _acceptCandidate(payload) {
352
+ if (!payload.connectionId || !payload.candidate) return;
353
+ var link = this.links.get(payload.connectionId);
354
+ if (!link || !link.pc.remoteDescription) {
355
+ if (!this.candidates.has(payload.connectionId)) this.candidates.set(payload.connectionId, []);
356
+ this.candidates.get(payload.connectionId).push(payload.candidate);
357
+ return;
358
+ }
359
+ try {
360
+ await link.pc.addIceCandidate(payload.candidate);
361
+ } catch (error) {
362
+ this._emit("error", error, link);
363
+ }
364
+ }
365
+ async _drainCandidates(link) {
366
+ var queued = this.candidates.get(link.connectionId) || [];
367
+ this.candidates.delete(link.connectionId);
368
+ for (var i = 0; i < queued.length; i++) await link.pc.addIceCandidate(queued[i]);
369
+ }
370
+ _fail(link, error) {
371
+ link.emit("error", error);
372
+ this._emit("error", error, link);
373
+ this._end(link, false);
374
+ }
375
+ _end(link, notifyPeer) {
376
+ if (!link || link._ended) return;
377
+ link._ended = true;
378
+ if (notifyPeer && link.channel && link.channel.readyState === "open") {
379
+ try { link.channel.send(CLOSE_PACKET); } catch (error) {}
380
+ }
381
+ link.open = false;
382
+ if (link.channel && link.channel.readyState !== "closed") {
383
+ link.channel.onopen = link.channel.onmessage = link.channel.onerror = link.channel.onclose = null;
384
+ try { link.channel.close(); } catch (error) {}
385
+ }
386
+ if (link.pc.signalingState !== "closed") {
387
+ link.pc.onicecandidate = link.pc.ondatachannel = link.pc.onconnectionstatechange = null;
388
+ try { link.pc.close(); } catch (error) {}
389
+ }
390
+ this.links.delete(link.connectionId);
391
+ if (this.peers.get(link.peerId) === link) this.peers.delete(link.peerId);
392
+ this.candidates.delete(link.connectionId);
393
+ link.emit("close");
394
+ this._emit("connectionclose", link);
395
+ }
396
+ _emit(event) {
397
+ var args = Array.prototype.slice.call(arguments, 1);
398
+ Array.from(this.listeners.get(event) || []).forEach(function(callback) {
399
+ try { callback.apply(null, args); } catch (error) { setTimeout(function() { throw error; }, 0); }
400
+ });
401
+ }
402
+ static _makeId() {
403
+ return String(Math.floor(100000 + Math.random() * 900000));
404
+ }
405
+ static _makeToken() {
406
+ if (globalThis.crypto && globalThis.crypto.randomUUID) return globalThis.crypto.randomUUID().replace(/-/g, "");
407
+ return Date.now().toString(36) + Math.random().toString(36).slice(2);
408
+ }
409
+ }
410
+ export { PeerNetwork };
@@ -0,0 +1,11 @@
1
+ import { GameLibError } from "../Error.js"
2
+ import { PeerNetwork } from "./PeerNetwork.js"
3
+ class Network {
4
+ constructor() {
5
+ throw new GameLibError("Class Network may not be constructed!");
6
+ }
7
+ static PeerNetwork = PeerNetwork;
8
+ }
9
+ export {
10
+ Network
11
+ }
@@ -0,0 +1,45 @@
1
+ class Storage {
2
+ constructor(name, ...stores) {
3
+ this._idb = indexedDB.open(name, 1);
4
+ this._name = name;
5
+ this._storeList = stores;
6
+ this._stores = {};
7
+ this._storeList.forEach((function(store) {
8
+ this._stores[store.name] = store;
9
+ store.setStorage(this);
10
+ }).bind(this));
11
+ this.onload = null;
12
+ this._idb.onupgradeneeded = (function(evt) {
13
+ this._storeList.forEach((function(store) {
14
+ var objStore = evt.currentTarget.result.createObjectStore(store.name, { 'keyPath': 'key', 'autoIncrement': false });
15
+ objStore.createIndex('key', 'key', { 'unique': true });
16
+ store.indexes.forEach((function(index) {
17
+ objStore.createIndex(index, index, { 'unique': false });
18
+ }).bind(this));
19
+ }).bind(this));
20
+ }).bind(this);
21
+ this._db = null;
22
+ this._idb.onsuccess = (function(evt) {
23
+ this._db = evt.target.result;
24
+ this._storeList.forEach((function(store) {
25
+ store.setGet(function(storeName) {
26
+ var tx = this._storage._db.transaction(storeName, 'readwrite');
27
+ return tx.objectStore(storeName);
28
+ });
29
+ }));
30
+ console.log("[GameLib] Initialized Database: " + this._name);
31
+ if(this.onload) {
32
+ this.onload();
33
+ }
34
+ }).bind(this);
35
+ }
36
+ delete() {
37
+ indexedDB.deleteDatabase(this._name);
38
+ }
39
+ getStore(storeName) {
40
+ return this._stores[storeName];
41
+ }
42
+ }
43
+ export {
44
+ Storage
45
+ }
@@ -0,0 +1,45 @@
1
+ import { GameLibCore } from "./index.js"
2
+ class Store {
3
+ constructor(name, ...indexes) {
4
+ this._name = name;
5
+ this._indexes = indexes;
6
+ this._get = null;
7
+ this._storage = null;
8
+ }
9
+ get name() {
10
+ return this._name;
11
+ }
12
+ get indexes() {
13
+ return this._indexes;
14
+ }
15
+ setStorage(storage) {
16
+ this._storage = storage;
17
+ }
18
+ setGet(func) {
19
+ this._get = func;
20
+ }
21
+ set(json) {
22
+ this._get(this._name).put(json);
23
+ }
24
+ delete(key) {
25
+ this._get(this._name).delete(key);
26
+ }
27
+ add(json) {
28
+ this._get(this._name).add(json);
29
+ }
30
+ get(key) {
31
+ return new Promise((function(resolve, reject) {
32
+ this._get(this._name).get(key).onsuccess = function(evt) {
33
+ if(evt.target.result) {
34
+ if(!evt.target.result.value) throw new GameLibCore.Error("IndexedDB get() result is missing \"value\" property!");
35
+ resolve(evt.target.result.value);
36
+ } else {
37
+ resolve(null);
38
+ }
39
+ };
40
+ }).bind(this));
41
+ }
42
+ }
43
+ export {
44
+ Store
45
+ }
@@ -0,0 +1,34 @@
1
+ import { GameLibCore } from "./index.js"
2
+ class Utils {
3
+ constructor() {
4
+ throw new GameLibCore.Error("Class Utils may not be constructed!");
5
+ }
6
+ static createCanvas(width, height) {
7
+ var element = document.createElement('canvas');
8
+ element.width = width;
9
+ element.height = height;
10
+ }
11
+ static getRandomListItem(list) {
12
+ var index = Math.floor(Math.random() * list.length);
13
+ return list[index];
14
+ }
15
+ static getRandomInt(min, max) {
16
+ return Math.floor(Math.random() * (max - min)) + min;
17
+ }
18
+ static getRandomDigits(numberOfDigits) {
19
+ var digits = "0123456789";
20
+ var resultString = "";
21
+ for (var i = 0; i < numberOfDigits; i++) {
22
+ var randomNumber = Utils.getRandomInt(0, digits.length);
23
+ var newDigit = digits.substring(randomNumber, randomNumber + 1);
24
+ resultString += newDigit;
25
+ }
26
+ return resultString;
27
+ }
28
+ static UUID() {
29
+ return crypto.randomUUID();
30
+ }
31
+ }
32
+ export {
33
+ Utils
34
+ }
@@ -0,0 +1,30 @@
1
+ import { GameLibError } from "./Error.js"
2
+ import { InputManager } from "./InputManager.js"
3
+ import { AudioManager } from "./AudioManager.js"
4
+ import { Utils } from "./Utils.js"
5
+ import { GameLibBase } from "./GameLibBase.js"
6
+ import { Storage } from "./Storage.js"
7
+ import { Store } from "./Store.js"
8
+ import { FullscreenManager } from "./FullscreenManager.js"
9
+ import { Multiplayer } from "./Multiplayer.js"
10
+ import { AssetManager } from "./AssetManager.js"
11
+ import { Matrix } from "./Matrix/index.js"
12
+ class GameLibCore {
13
+ constructor() {
14
+ throw new GameLibError("Class GameLibCore may not be constructed!");
15
+ }
16
+ static Error = GameLibError;
17
+ static Utils = Utils;
18
+ static InputManager = InputManager;
19
+ static AudioManager = AudioManager;
20
+ static GameLibBase = GameLibBase;
21
+ static Storage = Storage;
22
+ static Store = Store;
23
+ static FullscreenManager = FullscreenManager;
24
+ static Multiplayer = Multiplayer;
25
+ static AssetManager = AssetManager;
26
+ static Matrix = Matrix;
27
+ }
28
+ export {
29
+ GameLibCore
30
+ }