@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,195 @@
1
+ import { GameLibCore } from "./index.js"
2
+ class InputManager {
3
+ constructor() {
4
+ if (GameLibCore.InputManager._instance) {
5
+ return GameLibCore.InputManager._instance;
6
+ } else {
7
+ console.log("[GameLib] No InputManager instance found. Creating new instance...");
8
+ this._keys = [];
9
+ this._keyBinds = {};
10
+ this._keyBindCallbacks = {};
11
+ this._active = false;
12
+ this._mouseX = -1;
13
+ this._mouseY = -1;
14
+ this._mouseListeners = [];
15
+ this.bindEvents();
16
+ GameLibCore.InputManager._instance = this;
17
+ }
18
+ }
19
+ static KEYS = {
20
+ 'ESCAPE': 27,
21
+ 'BACKTICK': 192,
22
+ 'ONE': 49,
23
+ 'TWO': 50,
24
+ 'THREE': 51,
25
+ 'FOUR': 52,
26
+ 'FIVE': 53,
27
+ 'SIX': 54,
28
+ 'SEVEN': 55,
29
+ 'EIGHT': 56,
30
+ 'NINE': 57,
31
+ 'ZERO': 48,
32
+ 'MINUS': 189,
33
+ 'EQUALS': 187,
34
+ 'TAB': 9,
35
+ 'LEFTBRACKET': 219,
36
+ 'RIGHTBRACKET': 221,
37
+ 'BACKSLASH': 220,
38
+ 'META': 91,
39
+ 'SEMICOLON': 186,
40
+ 'APOSTROPE': 222,
41
+ 'ENTER': 13,
42
+ 'SHIFT': 16,
43
+ 'COMMA': 188,
44
+ 'PERIOD': 190,
45
+ 'SLASH': 191,
46
+ 'CONTROL': 17,
47
+ 'ALT': 18,
48
+ 'UP': 38,
49
+ 'DOWN': 40,
50
+ 'LEFT': 37,
51
+ 'RIGHT': 39,
52
+ 'SPACE': 32,
53
+ 'A': 65,
54
+ 'B': 66,
55
+ 'C': 67,
56
+ 'D': 68,
57
+ 'E': 69,
58
+ 'F': 70,
59
+ 'G': 71,
60
+ 'H': 72,
61
+ 'I': 73,
62
+ 'J': 74,
63
+ 'K': 75,
64
+ 'L': 76,
65
+ 'M': 77,
66
+ 'N': 78,
67
+ 'O': 79,
68
+ 'P': 80,
69
+ 'Q': 81,
70
+ 'R': 82,
71
+ 'S': 83,
72
+ 'W': 87,
73
+ 'X': 88,
74
+ 'Y': 89,
75
+ 'Z': 90
76
+ };
77
+ get keys() {
78
+ return this._keys;
79
+ }
80
+ setActive(active) {
81
+ this._active = active;
82
+ }
83
+ clearKeys() {
84
+ this._keys = [];
85
+ }
86
+ keyUp(evt) {
87
+ if (!this._active) {
88
+ return;
89
+ }
90
+ this._keys = this._keys.filter(key => key != evt.keyCode);
91
+ this._triggerKeyBind(evt.keyCode, 'up');
92
+ }
93
+ keyDown(evt) {
94
+ if (!this._active) {
95
+ return;
96
+ }
97
+ if (this._keys.indexOf(evt.keyCode) == -1) {
98
+ this._keys.push(evt.keyCode);
99
+ }
100
+ this._triggerKeyBind(evt.keyCode, 'down');
101
+ }
102
+ _fireMouseListener(type) {
103
+ for (var i = 0; i < this._mouseListeners.length; i++) {
104
+ var listener = this._mouseListeners[i];
105
+ if (listener.type === type) {
106
+ if (listener.callback && typeof listener.callback === "function") {
107
+ listener.callback();
108
+ }
109
+ }
110
+ }
111
+ }
112
+ mouseMove(evt) {
113
+ this._mouseX = evt.clientX;
114
+ this._mouseY = evt.clientY;
115
+ this._fireMouseListener('move');
116
+ }
117
+ mouseDown(evt) {
118
+ this._mouseX = evt.clientX;
119
+ this._mouseY = evt.clientY;
120
+ this._fireMouseListener('down');
121
+ }
122
+ mouseUp(evt) {
123
+ this._mouseX = evt.clientX;
124
+ this._mouseY = evt.clientY;
125
+ this._fireMouseListener('up');
126
+ }
127
+ click(evt) {
128
+ this._mouseX = evt.clientX;
129
+ this._mouseY = evt.clientY;
130
+ this._fireMouseListener('click');
131
+ }
132
+ getMousePos() {
133
+ return [this._mouseX, this._mouseY];
134
+ }
135
+ getCanvasMousePos(canvas) {
136
+ var boundingRect = canvas.getBoundingClientRect();
137
+ return [this._mouseX - boundingRect.x, this._mouseY - boundingRect.y];
138
+ }
139
+ _callBind(keyBindName) {
140
+ if (this._keyBindCallbacks[keyBindName]) {
141
+ for (var i = 0; i < this._keyBindCallbacks[keyBindName].length; i++) {
142
+ var bindCallback = this._keyBindCallbacks[keyBindName][i];
143
+ if (bindCallback && typeof bindCallback === "function") bindCallback();
144
+ }
145
+ }
146
+ }
147
+ _triggerKeyBind(key, type) {
148
+ if (this._keyBinds[key]) {
149
+ for (var i = 0; i < this._keyBinds.length; i++) {
150
+ var keyBind = this._keyBinds[i];
151
+ if (keyBind.type === type) {
152
+ this.__callBind(this._keyBinds[i].name);
153
+ }
154
+ }
155
+ }
156
+ }
157
+ isKeyDown(key) {
158
+ return (this._keys.indexOf(key) != -1);
159
+ }
160
+ bindKey(key, type, name) {
161
+ if (!this._keyBinds[key]) this._keyBinds[key] = [];
162
+ this._keyBinds[key].push({
163
+ name: name,
164
+ type: type
165
+ });
166
+ }
167
+ registerKeyBindCallback(name, callback) {
168
+ if (!this._keyBindCallbacks[name]) this._keyBindCallbacks[name] = [];
169
+ this._keyBindCallbacks[name].push({ callback: callback });
170
+ }
171
+ static isKeyDown(key) {
172
+ if (GameLibCore.InputManager._instance != null) {
173
+ return (GameLibCore.InputManager._instance._keys.indexOf(key) != -1);
174
+ } else {
175
+ throw new GameLibCore.Error("An InputManager instance must exist to use GameLibCore.InputManager.isKeyDown(key)!");
176
+ }
177
+ }
178
+ addMouseListener(type, callback) {
179
+ this._mouseListeners.push({
180
+ type: type,
181
+ callback: callback
182
+ });
183
+ }
184
+ bindEvents() {
185
+ window.addEventListener('keyup', this.keyUp.bind(this), false);
186
+ window.addEventListener('keydown', this.keyDown.bind(this), false);
187
+ window.addEventListener('mousemove', this.mouseMove.bind(this), false);
188
+ window.addEventListener('mousedown', this.mouseDown.bind(this), false);
189
+ window.addEventListener('mouseup', this.mouseUp.bind(this), false);
190
+ window.addEventListener('click', this.click.bind(this), false);
191
+ }
192
+ }
193
+ export {
194
+ InputManager
195
+ }
@@ -0,0 +1,111 @@
1
+ import { GameLibCore } from "./index.js"
2
+ class Loading {
3
+ constructor() {
4
+ throw new GameLibCore.Error("Class Loading may not be constructed!");
5
+ }
6
+ static DEFAULT_CONFIG = {
7
+ background: "#10131c",
8
+ logo: null,
9
+ status: "Loading…",
10
+ progress: null,
11
+ textColor: "#ffffff",
12
+ mutedTextColor: "#aab3c5",
13
+ progressTrackColor: "#343b4d",
14
+ progressColor: "#78e0a3",
15
+ accentColor: "#78e0a3",
16
+ font: "16px sans-serif",
17
+ title: "LOADING",
18
+ titleFont: "bold 36px sans-serif",
19
+ progressWidth: 280,
20
+ progressHeight: 12,
21
+ logoWidth: 96,
22
+ logoHeight: 96,
23
+ padding: 24
24
+ };
25
+ static render(canvas, ctx, config = {}) {
26
+ if (!canvas || !ctx) {
27
+ return;
28
+ }
29
+ var options = { ...Loading.DEFAULT_CONFIG, ...(config || {}) };
30
+ var width = canvas.width;
31
+ var height = canvas.height;
32
+ var centerX = width / 2;
33
+ var centerY = height / 2;
34
+ var progressHeight = Math.max(1, options.progressHeight);
35
+ var percentageWidth = Number.isFinite(options.progress) ? 42 : 0;
36
+ var percentageGap = percentageWidth > 0 ? 12 : 0;
37
+ var maxBarWidth = Math.max(0, width - options.padding * 2 - percentageWidth - percentageGap);
38
+ var progressWidth = Math.min(Math.max(0, options.progressWidth), maxBarWidth);
39
+ var groupWidth = progressWidth + percentageGap + percentageWidth;
40
+ var barX = centerX - progressWidth / 2;
41
+ var progressY = centerY + 36;
42
+ var radius = progressHeight / 2;
43
+ ctx.save();
44
+ ctx.fillStyle = options.background || Loading.DEFAULT_CONFIG.background;
45
+ ctx.fillRect(0, 0, width, height);
46
+ if (options.backgroundImage && options.backgroundImage.complete !== false) {
47
+ ctx.save();
48
+ ctx.globalAlpha = options.backgroundImageOpacity == null ? 1 : options.backgroundImageOpacity;
49
+ ctx.drawImage(options.backgroundImage, 0, 0, width, height);
50
+ ctx.restore();
51
+ }
52
+ var titleY = centerY - 30;
53
+ if (options.logo && options.logo.complete !== false) {
54
+ var logoWidth = Math.min(options.logoWidth, Math.max(0, width - options.padding * 2));
55
+ var logoHeight = Math.min(options.logoHeight, Math.max(0, height - options.padding * 2));
56
+ ctx.drawImage(options.logo, centerX - logoWidth / 2, centerY - 112, logoWidth, logoHeight);
57
+ titleY = centerY + 10;
58
+ }
59
+ if (options.title) {
60
+ ctx.fillStyle = options.textColor;
61
+ ctx.font = options.titleFont;
62
+ ctx.textAlign = "center";
63
+ ctx.textBaseline = "middle";
64
+ ctx.fillText(options.title, centerX, titleY);
65
+ }
66
+ ctx.fillStyle = options.progressTrackColor;
67
+ ctx.beginPath();
68
+ ctx.roundRect(barX, progressY, progressWidth, progressHeight, radius);
69
+ ctx.fill();
70
+ if (Number.isFinite(options.progress)) {
71
+ var progress = Math.min(1, Math.max(0, options.progress));
72
+ var fillWidth = progressWidth * progress;
73
+ if (fillWidth > 0) {
74
+ ctx.fillStyle = options.progressColor;
75
+ ctx.beginPath();
76
+ ctx.roundRect(barX, progressY, fillWidth, progressHeight, Math.min(radius, fillWidth / 2));
77
+ ctx.fill();
78
+ }
79
+ ctx.fillStyle = options.mutedTextColor;
80
+ ctx.font = "12px sans-serif";
81
+ ctx.textAlign = "left";
82
+ ctx.textBaseline = "middle";
83
+ ctx.fillText(`${Math.round(progress * 100)}%`, barX + progressWidth + percentageGap, progressY + progressHeight / 2);
84
+ } else {
85
+ var segmentWidth = Math.max(24, progressWidth * 0.28);
86
+ var travel = progressWidth + segmentWidth;
87
+ var offset = (Date.now() / 8) % travel;
88
+ var segmentX = barX + offset - segmentWidth;
89
+ ctx.save();
90
+ ctx.beginPath();
91
+ ctx.roundRect(barX, progressY, progressWidth, progressHeight, radius);
92
+ ctx.clip();
93
+ ctx.fillStyle = options.progressColor;
94
+ ctx.beginPath();
95
+ ctx.roundRect(segmentX, progressY, segmentWidth, progressHeight, radius);
96
+ ctx.fill();
97
+ ctx.restore();
98
+ }
99
+ if (options.status) {
100
+ ctx.fillStyle = options.mutedTextColor;
101
+ ctx.font = options.font;
102
+ ctx.textAlign = "center";
103
+ ctx.textBaseline = "top";
104
+ ctx.fillText(options.status, centerX, progressY + progressHeight + 14);
105
+ }
106
+ ctx.restore();
107
+ }
108
+ }
109
+ export {
110
+ Loading
111
+ }
@@ -0,0 +1,11 @@
1
+ import { mat4 } from "./mat4.js"
2
+ import { GameLibError } from "../Error.js"
3
+ class Matrix {
4
+ constructor() {
5
+ throw new GameLibError("Class Matrix may not be constructed!");
6
+ }
7
+ static mat4 = mat4;
8
+ }
9
+ export {
10
+ Matrix
11
+ }
@@ -0,0 +1,178 @@
1
+ import { GameLibError } from "../Error.js";
2
+ class mat4 {
3
+ varructor() {
4
+ throw new GameLibError("Class mat4 may not be varructed!");
5
+ }
6
+ static EPSILON = 1e-6;
7
+ static blank() {
8
+ return new Float32Array(16);
9
+ }
10
+ static create(out = mat4.blank()) {
11
+ out[0] = 1; out[1] = 0; out[2] = 0; out[3] = 0;
12
+ out[4] = 0; out[5] = 1; out[6] = 0; out[7] = 0;
13
+ out[8] = 0; out[9] = 0; out[10] = 1; out[11] = 0;
14
+ out[12] = 0; out[13] = 0; out[14] = 0; out[15] = 1;
15
+ return out;
16
+ }
17
+ static multiply(out, a, b) {
18
+ var aCopy = Array.from(a);
19
+ var bCopy = Array.from(b);
20
+ for(var col = 0; col < 4; col++) {
21
+ for (var row = 0; row < 4; row++) {
22
+ var sum = 0;
23
+ for (var k = 0; k < 4; k++) {
24
+ sum += aCopy[k * 4 + row] * bCopy[col * 4 + k];
25
+ }
26
+ out[col * 4 + row] = sum;
27
+ }
28
+ }
29
+ return out;
30
+ }
31
+ static translate(out, a, v) {
32
+ var x = v[0], y = v[1], z = v[2];
33
+ if (out !== a) {
34
+ for (var i = 0; i < 12; i++) out[i] = a[i];
35
+ }
36
+ out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
37
+ out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
38
+ out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
39
+ out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
40
+ return out;
41
+ }
42
+ static rotateX(out, a, rad) {
43
+ var s = Math.sin(rad);
44
+ var c = Math.cos(rad);
45
+ var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
46
+ var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
47
+ if (out !== a) {
48
+ out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; out[3] = a[3];
49
+ out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15];
50
+ }
51
+ out[4] = a10 * c + a20 * s;
52
+ out[5] = a11 * c + a21 * s;
53
+ out[6] = a12 * c + a22 * s;
54
+ out[7] = a13 * c + a23 * s;
55
+ out[8] = a20 * c - a10 * s;
56
+ out[9] = a21 * c - a11 * s;
57
+ out[10] = a22 * c - a12 * s;
58
+ out[11] = a23 * c - a13 * s;
59
+ return out;
60
+ }
61
+ static rotateY(out, a, rad) {
62
+ var s = Math.sin(rad);
63
+ var c = Math.cos(rad);
64
+ var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
65
+ var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
66
+ if (out !== a) {
67
+ out[4] = a[4]; out[5] = a[5]; out[6] = a[6]; out[7] = a[7];
68
+ out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15];
69
+ }
70
+ out[0] = a00 * c - a20 * s;
71
+ out[1] = a01 * c - a21 * s;
72
+ out[2] = a02 * c - a22 * s;
73
+ out[3] = a03 * c - a23 * s;
74
+ out[8] = a00 * s + a20 * c;
75
+ out[9] = a01 * s + a21 * c;
76
+ out[10] = a02 * s + a22 * c;
77
+ out[11] = a03 * s + a23 * c;
78
+ return out;
79
+ }
80
+ static rotateZ(out, a, rad) {
81
+ var s = Math.sin(rad);
82
+ var c = Math.cos(rad);
83
+ var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
84
+ var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
85
+ if (out !== a) {
86
+ out[8] = a[8]; out[9] = a[9]; out[10] = a[10]; out[11] = a[11];
87
+ out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15];
88
+ }
89
+ out[0] = a00 * c + a10 * s;
90
+ out[1] = a01 * c + a11 * s;
91
+ out[2] = a02 * c + a12 * s;
92
+ out[3] = a03 * c + a13 * s;
93
+ out[4] = a10 * c - a00 * s;
94
+ out[5] = a11 * c - a01 * s;
95
+ out[6] = a12 * c - a02 * s;
96
+ out[7] = a13 * c - a03 * s;
97
+ return out;
98
+ }
99
+ static scale(out, a, v) {
100
+ var x = v[0], y = v[1], z = v[2];
101
+ out[0] = a[0] * x; out[1] = a[1] * x;
102
+ out[2] = a[2] * x; out[3] = a[3] * x;
103
+ out[4] = a[4] * y; out[5] = a[5] * y;
104
+ out[6] = a[6] * y; out[7] = a[7] * y;
105
+ out[8] = a[8] * z; out[9] = a[9] * z;
106
+ out[10] = a[10] * z; out[11] = a[11] * z;
107
+ out[12] = a[12]; out[13] = a[13];
108
+ out[14] = a[14]; out[15] = a[15];
109
+ return out;
110
+ }
111
+ static transpose(out, a) {
112
+ var values = Array.from(a);
113
+ for (var col = 0; col < 4; col++) {
114
+ for (var row = 0; row < 4; row++) {
115
+ out[col * 4 + row] = values[row * 4 + col];
116
+ }
117
+ }
118
+ return out;
119
+ }
120
+ static invert(out, a) {
121
+ var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
122
+ var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
123
+ var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
124
+ var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
125
+ var b00 = a00 * a11 - a01 * a10;
126
+ var b01 = a00 * a12 - a02 * a10;
127
+ var b02 = a00 * a13 - a03 * a10;
128
+ var b03 = a01 * a12 - a02 * a11;
129
+ var b04 = a01 * a13 - a03 * a11;
130
+ var b05 = a02 * a13 - a03 * a12;
131
+ var b06 = a20 * a31 - a21 * a30;
132
+ var b07 = a20 * a32 - a22 * a30;
133
+ var b08 = a20 * a33 - a23 * a30;
134
+ var b09 = a21 * a32 - a22 * a31;
135
+ var b10 = a21 * a33 - a23 * a31;
136
+ var b11 = a22 * a33 - a23 * a32;
137
+ var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
138
+ if (det === 0) return null;
139
+ det = 1 / det;
140
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
141
+ out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
142
+ out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
143
+ out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
144
+ out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
145
+ out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
146
+ out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
147
+ out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
148
+ out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
149
+ out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
150
+ out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
151
+ out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
152
+ out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
153
+ out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
154
+ out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
155
+ out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
156
+ return out;
157
+ }
158
+ static perspective(out, fovy, aspect, near, far) {
159
+ var f = 1 / Math.tan(fovy / 2);
160
+ out[0] = f / aspect;
161
+ out[1] = 0; out[2] = 0; out[3] = 0;
162
+ out[4] = 0; out[5] = f; out[6] = 0; out[7] = 0;
163
+ out[8] = 0; out[9] = 0; out[11] = -1;
164
+ out[12] = 0; out[13] = 0; out[15] = 0;
165
+ if (far == null || far === Infinity) {
166
+ out[10] = -1;
167
+ out[14] = -2 * near;
168
+ } else {
169
+ var nf = 1 / (near - far);
170
+ out[10] = (far + near) * nf;
171
+ out[14] = 2 * far * near * nf;
172
+ }
173
+ return out;
174
+ }
175
+ }
176
+ export {
177
+ mat4
178
+ };
@@ -0,0 +1,54 @@
1
+ import { PeerNetwork } from "./Network/PeerNetwork.js"
2
+ class Multiplayer {
3
+ constructor(options = {}) {
4
+ this.network = new PeerNetwork(options);
5
+ }
6
+ getPeerId() {
7
+ return this.network.id;
8
+ }
9
+ getConnectedIds() {
10
+ return this.network.getConnectedIds();
11
+ }
12
+ bindDataCallback(callback) {
13
+ this.network.off("data", this._dataListener);
14
+ this._dataListener = (data, connection) => callback(data, connection);
15
+ this.network.on("data", this._dataListener);
16
+ return this;
17
+ }
18
+ setConnectPeerCallback(callback) {
19
+ this.network.off("connection", this._connectListener);
20
+ this._connectListener = (connection) => callback(connection);
21
+ this.network.on("connection", this._connectListener);
22
+ return this;
23
+ }
24
+ setPeerCloseCallback(callback) {
25
+ this.network.off("connectionclose", this._closeListener);
26
+ this._closeListener = (connection) => callback(connection);
27
+ this.network.on("connectionclose", this._closeListener);
28
+ return this;
29
+ }
30
+ createPeer(peerOptions = {}) {
31
+ this.network.options = Object.assign({}, this.network.options, peerOptions);
32
+ return this.network.start();
33
+ }
34
+ connectPeer(peerId, peerOptions = {}) {
35
+ this.network.options = Object.assign({}, this.network.options, peerOptions);
36
+ return this.network.connect(peerId);
37
+ }
38
+ sendToAll(data) {
39
+ return this.network.broadcast(data);
40
+ }
41
+ sendToPeer(data, peerId) {
42
+ return peerId == null ? this.network.broadcast(data) : this.network.send(peerId, data);
43
+ }
44
+ closePeer(peerId) {
45
+ return this.network.close(peerId);
46
+ }
47
+ isPeerDisconnected() {
48
+ return this.network.state !== "ready";
49
+ }
50
+ destroy() {
51
+ return this.network.destroy();
52
+ }
53
+ }
54
+ export { Multiplayer }