@cjcoding-git/gamelib 0.0.0-stage → 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.
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.github/dependabot.yml +6 -0
- package/.github/workflows/build.yml +54 -0
- package/.github/workflows/release.yml +79 -0
- package/LICENSE +21 -0
- package/README.md +6 -2
- package/assets/test-frog.gltf +1921 -0
- package/assets/test-frog.gml +0 -0
- package/buildtime.txt +1 -0
- package/buildversion.txt +1 -0
- package/dist/GameLib.js +71599 -0
- package/dist/GameLib.min.js +8 -0
- package/docs/2D/API.md +1 -0
- package/docs/2D/GETTING_STARTED.md +1 -0
- package/docs/3D/API.md +1 -0
- package/docs/3D/GETTING_STARTED.md +23 -0
- package/docs/Core/API.md +1 -0
- package/docs/Core/GETTING_STARTED.md +1 -0
- package/docs/README.md +32 -0
- package/package.json +21 -4
- package/scripts/gltf2gml.html +312 -0
- package/src/2D/Box.js +42 -0
- package/src/2D/Coord.js +17 -0
- package/src/2D/GameLib.js +317 -0
- package/src/2D/Renderable.js +31 -0
- package/src/2D/Rotation.js +42 -0
- package/src/2D/Size.js +17 -0
- package/src/2D/Sprite.js +56 -0
- package/src/2D/UI/Button.js +52 -0
- package/src/2D/UI/TextInput.js +77 -0
- package/src/2D/UI/UIElement.js +10 -0
- package/src/2D/UI/index.js +15 -0
- package/src/2D/index.js +24 -0
- package/src/3D/Camera.js +25 -0
- package/src/3D/GameLib.js +202 -0
- package/src/3D/Geometry/Box.js +39 -0
- package/src/3D/Geometry/Cone.js +45 -0
- package/src/3D/Geometry/ModelLoaders/GLBLoader.js +12 -0
- package/src/3D/Geometry/ModelLoaders/GLTFLoader.js +12 -0
- package/src/3D/Geometry/ModelLoaders/GMLLoader.js +114 -0
- package/src/3D/Geometry/ModelLoaders/OBJLoader.js +12 -0
- package/src/3D/Geometry/ModelLoaders/index.js +17 -0
- package/src/3D/Geometry/Pyramid.js +55 -0
- package/src/3D/Geometry/Sphere.js +37 -0
- package/src/3D/Geometry/index.js +19 -0
- package/src/3D/Lighting/AmbientLight.js +17 -0
- package/src/3D/Lighting/DirectionalLight.js +17 -0
- package/src/3D/Lighting/PointLight.js +17 -0
- package/src/3D/Lighting/index.js +15 -0
- package/src/3D/Material.js +181 -0
- package/src/3D/Mesh.js +142 -0
- package/src/3D/Object.js +13 -0
- package/src/3D/Position.js +15 -0
- package/src/3D/Rotation.js +21 -0
- package/src/3D/Scale.js +10 -0
- package/src/3D/ShaderFactory/FunctionParam.js +14 -0
- package/src/3D/ShaderFactory/Modifiers/Variable.js +85 -0
- package/src/3D/ShaderFactory/Modifiers/index.js +11 -0
- package/src/3D/ShaderFactory/Node.js +25 -0
- package/src/3D/ShaderFactory/NodeTypes/A.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/Add.js +13 -0
- package/src/3D/ShaderFactory/NodeTypes/AssignVariable.js +11 -0
- package/src/3D/ShaderFactory/NodeTypes/Clamp.js +14 -0
- package/src/3D/ShaderFactory/NodeTypes/Constant.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/Define.js +11 -0
- package/src/3D/ShaderFactory/NodeTypes/Divide.js +13 -0
- package/src/3D/ShaderFactory/NodeTypes/Dot.js +14 -0
- package/src/3D/ShaderFactory/NodeTypes/Exp2.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/Function.js +36 -0
- package/src/3D/ShaderFactory/NodeTypes/Group.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/Header.js +12 -0
- package/src/3D/ShaderFactory/NodeTypes/Length.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/MainFunction.js +7 -0
- package/src/3D/ShaderFactory/NodeTypes/Max.js +14 -0
- package/src/3D/ShaderFactory/NodeTypes/Mix.js +14 -0
- package/src/3D/ShaderFactory/NodeTypes/Multiply.js +13 -0
- package/src/3D/ShaderFactory/NodeTypes/Negative.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/NodeTypes.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/Normalize.js +10 -0
- package/src/3D/ShaderFactory/NodeTypes/Precision.js +11 -0
- package/src/3D/ShaderFactory/NodeTypes/RGB.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/Subtract.js +13 -0
- package/src/3D/ShaderFactory/NodeTypes/Symbol.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/Texture2D.js +10 -0
- package/src/3D/ShaderFactory/NodeTypes/Variable.js +22 -0
- package/src/3D/ShaderFactory/NodeTypes/Vector.js +15 -0
- package/src/3D/ShaderFactory/NodeTypes/XYZ.js +9 -0
- package/src/3D/ShaderFactory/NodeTypes/index.js +32 -0
- package/src/3D/ShaderFactory/PrecisionTypes.js +8 -0
- package/src/3D/ShaderFactory/Program.js +41 -0
- package/src/3D/ShaderFactory/VariableTypes.js +49 -0
- package/src/3D/ShaderFactory/index.js +45 -0
- package/src/3D/Texture.js +59 -0
- package/src/3D/TextureFactory.js +22 -0
- package/src/3D/Transform.js +35 -0
- package/src/3D/UI/UIImage.js +24 -0
- package/src/3D/UI/UILoading.js +41 -0
- package/src/3D/UI/UIObject.js +16 -0
- package/src/3D/UI/UIPath.js +25 -0
- package/src/3D/UI/UIRenderer.js +207 -0
- package/src/3D/UI/UIText.js +35 -0
- package/src/3D/UI/index.js +19 -0
- package/src/3D/Utils.js +9 -0
- package/src/3D/index.js +43 -0
- package/src/Core/AssetManager.js +116 -0
- package/src/Core/AudioManager.js +151 -0
- package/src/Core/Error.js +9 -0
- package/src/Core/FullscreenManager.js +49 -0
- package/src/Core/GameLibBase.js +29 -0
- package/src/Core/InputManager.js +195 -0
- package/src/Core/Loading.js +111 -0
- package/src/Core/Matrix/index.js +11 -0
- package/src/Core/Matrix/mat4.js +178 -0
- package/src/Core/Multiplayer.js +54 -0
- package/src/Core/Network/PeerNetwork.js +410 -0
- package/src/Core/Network/index.js +11 -0
- package/src/Core/Storage.js +45 -0
- package/src/Core/Store.js +45 -0
- package/src/Core/Utils.js +34 -0
- package/src/Core/index.js +30 -0
- package/src/Lib/Ammo.js +72 -0
- package/src/Lib/Matter.js +1 -0
- package/src/Shaders/Material/Fragment/GL1.glsl +49 -0
- package/src/Shaders/Material/Fragment/GL2.glsl +51 -0
- package/src/Shaders/Material/Vertex/GL1.glsl +19 -0
- package/src/Shaders/Material/Vertex/GL2.glsl +20 -0
- package/src/Shaders/UI/Fragment/GL1.glsl +5 -0
- package/src/Shaders/UI/Fragment/GL2.glsl +7 -0
- package/src/Shaders/UI/Vertex/GL1.glsl +10 -0
- package/src/Shaders/UI/Vertex/GL2.glsl +11 -0
- package/src/Shaders/UITexture/Fragment/GL1.glsl +6 -0
- package/src/Shaders/UITexture/Fragment/GL2.glsl +8 -0
- package/src/Shaders/UITexture/Vertex/GL1.glsl +10 -0
- package/src/Shaders/UITexture/Vertex/GL2.glsl +11 -0
- package/src/index.js +19 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { GameLibCore } from "../Core/index.js"
|
|
2
|
+
import { GameLib2D } from "./index.js"
|
|
3
|
+
import { Coord } from "./Coord.js"
|
|
4
|
+
import { Size } from "./Size.js"
|
|
5
|
+
import { Box } from "./Box.js"
|
|
6
|
+
import { Renderable } from "./Renderable.js"
|
|
7
|
+
import { Sprite } from "./Sprite.js"
|
|
8
|
+
import { Loading } from "../Core/Loading.js"
|
|
9
|
+
import { InputManager } from "../Core/InputManager.js"
|
|
10
|
+
class GameLib extends GameLibCore.GameLibBase {
|
|
11
|
+
constructor(renderCanvas, renderCtx, viewportCanvas, viewportCtx) {
|
|
12
|
+
if (GameLib2D.GameLib._instance != null) {
|
|
13
|
+
return GameLib2D.GameLib._instance;
|
|
14
|
+
} else {
|
|
15
|
+
super();
|
|
16
|
+
console.log("[GameLib] No GameLib instance found. Creating new instance.");
|
|
17
|
+
this._scene = "loading";
|
|
18
|
+
this._sprites = [];
|
|
19
|
+
this.clearColor = "#dddddd";
|
|
20
|
+
renderCanvas.setAttribute('style', 'image-rendering: pixelated;');
|
|
21
|
+
this._renderCanvas = renderCanvas;
|
|
22
|
+
this._renderCtx = renderCtx;
|
|
23
|
+
viewportCanvas.setAttribute('style', 'image-rendering: pixelated;');
|
|
24
|
+
this._viewportCanvas = viewportCanvas;
|
|
25
|
+
this._viewportCtx = viewportCtx;
|
|
26
|
+
this._viewport = new Box(new Coord(0, 0), new Size(this._viewportCanvas.width, this._viewportCanvas.height));
|
|
27
|
+
this._renderables = [];
|
|
28
|
+
this._background = {};
|
|
29
|
+
this._loadingScreen = Loading.DEFAULT_CONFIG;
|
|
30
|
+
this.oncontextmenu = null;
|
|
31
|
+
this.inputManager = new InputManager();
|
|
32
|
+
this.inputManager.addMouseListener('move', this.hoverButtons.bind(this), false);
|
|
33
|
+
this.inputManager.addMouseListener('click', this.checkButtons.bind(this), false);
|
|
34
|
+
window.addEventListener('contextmenu', this.contextMenu.bind(this), false);
|
|
35
|
+
this._listeners = {};
|
|
36
|
+
this.registerMainloop();
|
|
37
|
+
GameLib._instance = this;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
contextMenu(evt) {
|
|
41
|
+
evt.preventDefault();
|
|
42
|
+
evt.returnValue = '';
|
|
43
|
+
if (this.oncontextmenu) {
|
|
44
|
+
this.oncontextmenu(evt);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
static cursor(cursorType) {
|
|
48
|
+
document.body.setAttribute('style', 'cursor: ' + cursorType + ';');
|
|
49
|
+
}
|
|
50
|
+
hoverButtons(evt) {
|
|
51
|
+
var mousePos = this.inputManager.getCanvasMousePos(this._viewportCanvas);
|
|
52
|
+
var buttons = this._renderables.filter(renderable => typeof renderable.handleClick === "function" && typeof renderable.handleHover === "function");
|
|
53
|
+
var calcXY = new Coord(mousePos[0], mousePos[1]);
|
|
54
|
+
if (calcXY.x <= canvasRect.width && calcXY.y <= canvasRect.height) {
|
|
55
|
+
var buttonHover = false;
|
|
56
|
+
buttons.forEach((function(button) {
|
|
57
|
+
if (this._scene == button.scene && button.shouldRender() && button.isActive()) {
|
|
58
|
+
if (button.handleHover(evt, calcXY)) {
|
|
59
|
+
buttonHover = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}).bind(this));
|
|
63
|
+
if (!buttonHover) {
|
|
64
|
+
GameLib.cursor("default");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
checkButtons(evt) {
|
|
69
|
+
var mousePos = this.inputManager.getCanvasMousePos(this._viewportCanvas);
|
|
70
|
+
var buttons = this._renderables.filter(renderable => typeof renderable.handleClick === "function" && typeof renderable.handleHover === "function");
|
|
71
|
+
var calcXY = new Coord(mousePos[0], mousePos[1]);
|
|
72
|
+
if (calcXY.x <= canvasRect.width && calcXY.y <= canvasRect.height) {
|
|
73
|
+
var buttonClicked = false;
|
|
74
|
+
buttons.forEach((function(button) {
|
|
75
|
+
if (!buttonClicked && this._scene == button.scene && button.shouldRender() && button.isActive()) {
|
|
76
|
+
if (button.handleClick(evt, calcXY)) {
|
|
77
|
+
buttonClicked = true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}).bind(this));
|
|
81
|
+
this.hoverButtons(evt);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
setScene(scene) {
|
|
85
|
+
this._scene = scene;
|
|
86
|
+
GameLib.cursor("default");
|
|
87
|
+
}
|
|
88
|
+
getScene() {
|
|
89
|
+
return this._scene;
|
|
90
|
+
}
|
|
91
|
+
static getContext(canvas) {
|
|
92
|
+
return canvas.getContext('2d', {
|
|
93
|
+
'willReadFrequently': true
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
static spriteImage(sprite, url) {
|
|
97
|
+
return GameLibCore.AssetManager.shared.loadImage(url).then(image => {
|
|
98
|
+
sprite.image = image;
|
|
99
|
+
return image;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
addRenderable(renderable) {
|
|
103
|
+
if (renderable instanceof Renderable) {
|
|
104
|
+
this._renderables.push(renderable);
|
|
105
|
+
} else {
|
|
106
|
+
throw new GameLibCore.Error("Parameter renderable must be an instance of Renderable");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
get sprites() {
|
|
110
|
+
return this._sprites;
|
|
111
|
+
}
|
|
112
|
+
get renderables() {
|
|
113
|
+
return this._renderables;
|
|
114
|
+
}
|
|
115
|
+
get canvas() {
|
|
116
|
+
return this._renderCanvas;
|
|
117
|
+
}
|
|
118
|
+
get ctx() {
|
|
119
|
+
return this._renderCtx;
|
|
120
|
+
}
|
|
121
|
+
get viewportCanvas() {
|
|
122
|
+
return this._viewportCanvas;
|
|
123
|
+
}
|
|
124
|
+
get viewportCtx() {
|
|
125
|
+
return this._viewportCtx;
|
|
126
|
+
}
|
|
127
|
+
moveViewport(dx, dy) {
|
|
128
|
+
this._viewport.move(dx, dy);
|
|
129
|
+
}
|
|
130
|
+
moveViewportTo(x, y) {
|
|
131
|
+
this._viewport.moveTo(x, y);
|
|
132
|
+
}
|
|
133
|
+
moveViewportToSprite(sprite) {
|
|
134
|
+
if (!(sprite instanceof Sprite)) {
|
|
135
|
+
throw new GameLibCore.Error("Parameter sprite must be an instance of Sprite!");
|
|
136
|
+
} else {
|
|
137
|
+
var x = sprite.x - this._viewportCanvas.width / 2;
|
|
138
|
+
var y = sprite.y - this._viewportCanvas.height / 2;
|
|
139
|
+
var width = this._viewportCanvas.width;
|
|
140
|
+
var height = this._viewportCanvas.height;
|
|
141
|
+
if (x < 0) {
|
|
142
|
+
x = 0;
|
|
143
|
+
} else if (x + width > this._renderCanvas.width) {
|
|
144
|
+
x = this._renderCanvas.width - width;
|
|
145
|
+
}
|
|
146
|
+
if (y < 0) {
|
|
147
|
+
y = 0;
|
|
148
|
+
} else if (y + height > this._renderCanvas.height) {
|
|
149
|
+
y = this._renderCanvas.height - height;
|
|
150
|
+
}
|
|
151
|
+
this._viewport.moveTo(x, y);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
renderViewport() {
|
|
155
|
+
var newImageData = this._renderCtx.getImageData(this._viewport.x, this._viewport.y, this._viewport.width, this._viewport.height);
|
|
156
|
+
this._viewportCtx.putImageData(newImageData, 0, 0);
|
|
157
|
+
}
|
|
158
|
+
getSpritesOfType(spriteClass) {
|
|
159
|
+
return this._sprites.filter(sprite => sprite instanceof spriteClass);
|
|
160
|
+
}
|
|
161
|
+
getRenderablesOfType(renderableClass) {
|
|
162
|
+
return this._renderables.filter(renderable => renderable instanceof renderableClass);
|
|
163
|
+
}
|
|
164
|
+
clearRenderablesOfType(renderableClass) {
|
|
165
|
+
this._renderables = this._renderables.filter(renderable => !(renderable instanceof renderableClass));
|
|
166
|
+
}
|
|
167
|
+
addSprite(sprite) {
|
|
168
|
+
if (sprite instanceof Sprite) {
|
|
169
|
+
this._sprites.push(sprite);
|
|
170
|
+
} else {
|
|
171
|
+
throw new GameLibCore.Error("Parameter sprite must be an instance of Sprite");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
clearCanvas(clearViewport = true) {
|
|
175
|
+
this._renderCtx.fillStyle = this.clearColor;
|
|
176
|
+
this._renderCtx.fillRect(0, 0, this._renderCanvas.width, this._renderCanvas.height);
|
|
177
|
+
if (clearViewport) {
|
|
178
|
+
this._viewportCtx.fillStyle = this.clearColor;
|
|
179
|
+
this._viewportCtx.fillRect(0, 0, this._viewportCanvas.width, this._viewportCanvas.height);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
resizeViewPort(size) {
|
|
183
|
+
this._viewport.size = size;
|
|
184
|
+
}
|
|
185
|
+
mainloop() {
|
|
186
|
+
this.update();
|
|
187
|
+
this.draw();
|
|
188
|
+
this.registerMainloop();
|
|
189
|
+
}
|
|
190
|
+
stopLoading() {
|
|
191
|
+
this._scene = "init";
|
|
192
|
+
}
|
|
193
|
+
update() {
|
|
194
|
+
if (!this._active) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
this.callHook('pre-update');
|
|
198
|
+
this._sprites.forEach((function(sprite) {
|
|
199
|
+
if (this._scene == sprite.scene) {
|
|
200
|
+
sprite.update();
|
|
201
|
+
}
|
|
202
|
+
}).bind(this));
|
|
203
|
+
this.callHook('post-update');
|
|
204
|
+
}
|
|
205
|
+
setBackground(image, scene, repeat = "repeat") {
|
|
206
|
+
this._background[scene] = this._renderCtx.createPattern(image, repeat);
|
|
207
|
+
}
|
|
208
|
+
setClearColor(color) {
|
|
209
|
+
this.clearColor = color;
|
|
210
|
+
}
|
|
211
|
+
renderBackground() {
|
|
212
|
+
if (this._background[this._scene] != null) {
|
|
213
|
+
this._renderCtx.fillStyle = this._background[this._scene];
|
|
214
|
+
this._renderCtx.fillRect(0, 0, this._renderCanvas.width, this._renderCanvas.height);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
drawGrid(size, color) {
|
|
218
|
+
this._renderCtxstrokeStyle = color;
|
|
219
|
+
for (var i = 0; i < this._renderCanvas.height; i += size) {
|
|
220
|
+
this._renderCtxbeginPath();
|
|
221
|
+
this._renderCtxmoveTo(0, i);
|
|
222
|
+
this._renderCtxlineTo(this._renderCanvas.width, i);
|
|
223
|
+
this._renderCtxclosePath();
|
|
224
|
+
this._renderCtxstroke();
|
|
225
|
+
}
|
|
226
|
+
for (var i = 0; i < this._renderCanvas.width; i += size) {
|
|
227
|
+
this._renderCtxbeginPath();
|
|
228
|
+
this._renderCtxmoveTo(i, 0);
|
|
229
|
+
this._renderCtxlineTo(i, this._renderCanvas.height);
|
|
230
|
+
this._renderCtxclosePath();
|
|
231
|
+
this._renderCtxstroke();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
draw() {
|
|
235
|
+
if (!this._rendering) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this.clearCanvas(false);
|
|
239
|
+
this.callHook('pre-render');
|
|
240
|
+
this.renderBackground();
|
|
241
|
+
this._sprites.forEach((function(sprite) {
|
|
242
|
+
if (sprite.checkCollision(this._viewport)) {
|
|
243
|
+
if (this._scene == sprite.scene) {
|
|
244
|
+
sprite.draw();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}).bind(this));
|
|
248
|
+
this._renderables.forEach((function(renderable) {
|
|
249
|
+
if (!renderable.overlay) {
|
|
250
|
+
if (renderable.isActive()) {
|
|
251
|
+
if (renderable.shouldRender()) {
|
|
252
|
+
if (this._scene == renderable.scene) {
|
|
253
|
+
renderable.draw();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}).bind(this));
|
|
259
|
+
this.callHook('post-render');
|
|
260
|
+
this.renderViewport();
|
|
261
|
+
this._renderables.forEach((function(renderable) {
|
|
262
|
+
if (renderable.overlay) {
|
|
263
|
+
if (renderable.isActive()) {
|
|
264
|
+
if (renderable.shouldRender()) {
|
|
265
|
+
if (this._scene == renderable.scene) {
|
|
266
|
+
renderable.draw();
|
|
267
|
+
} else {
|
|
268
|
+
renderable.overlaySkipped();
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
renderable.overlaySkipped();
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
renderable.overlaySkipped();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}).bind(this));
|
|
278
|
+
if (this._scene == "loading") {
|
|
279
|
+
Loading.render(this._viewportCanvas, this._viewportCtx, this._loadingScreen);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
registerMainloop() {
|
|
283
|
+
window.requestAnimationFrame(this.mainloop.bind(this));
|
|
284
|
+
}
|
|
285
|
+
setLoadingScreen(config) {
|
|
286
|
+
this._loadingScreen = config;
|
|
287
|
+
}
|
|
288
|
+
listen(type, callback) {
|
|
289
|
+
if(!this._listeners[type]) this._listeners[type] = [];
|
|
290
|
+
this._listeners[type].push(callback);
|
|
291
|
+
}
|
|
292
|
+
_fire(type) {
|
|
293
|
+
if(!this._listeners[type]) return;
|
|
294
|
+
for(var i=0;i<this._listeners[type].length;i++) {
|
|
295
|
+
var listener = this._listeners[type][i];
|
|
296
|
+
if(listener && typeof listener === "function") {
|
|
297
|
+
listener();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
resizeToWindow() {
|
|
302
|
+
this._viewportCanvas.width = window.innerWidth;
|
|
303
|
+
this._viewportCanvas.height = window.innerHeight;
|
|
304
|
+
this._viewport.size = new Size(window.innerWidth, window.innerHeight);
|
|
305
|
+
this._viewportCanvas.style.position = "fixed";
|
|
306
|
+
this._viewportCanvas.style.top = "0";
|
|
307
|
+
this._viewportCanvas.style.left = "0";
|
|
308
|
+
this._fire('resize');
|
|
309
|
+
}
|
|
310
|
+
fillWindow() {
|
|
311
|
+
window.addEventListener('resize', this.resizeToWindow.bind(this), false);
|
|
312
|
+
this.resizeToWindow();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
export {
|
|
316
|
+
GameLib
|
|
317
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { GameLib } from "./GameLib.js"
|
|
2
|
+
class Renderable {
|
|
3
|
+
constructor(scene) {
|
|
4
|
+
this.gameLib = new GameLib();
|
|
5
|
+
this.gameLib.addRenderable(this);
|
|
6
|
+
this._active = true;
|
|
7
|
+
this._canvas = this.gameLib.canvas;
|
|
8
|
+
this._ctx = this.gameLib.ctx;
|
|
9
|
+
this.overlay = false;
|
|
10
|
+
this.scene = scene;
|
|
11
|
+
}
|
|
12
|
+
makeOverlay() {
|
|
13
|
+
this.overlay = true;
|
|
14
|
+
this._canvas = this.gameLib.viewportCanvas;
|
|
15
|
+
this._ctx = this.gameLib.viewportCtx;
|
|
16
|
+
}
|
|
17
|
+
overlaySkipped() {}
|
|
18
|
+
shouldRender() {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
isActive() {
|
|
22
|
+
return this._active;
|
|
23
|
+
}
|
|
24
|
+
setActive(active) {
|
|
25
|
+
this._active = active;
|
|
26
|
+
}
|
|
27
|
+
draw() {}
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
Renderable
|
|
31
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
class Rotation {
|
|
2
|
+
constructor(deg) {
|
|
3
|
+
this._deg = deg;
|
|
4
|
+
this.rotateTo(this._deg);
|
|
5
|
+
}
|
|
6
|
+
static d2r(deg) {
|
|
7
|
+
return (deg * Math.PI) / 180;
|
|
8
|
+
}
|
|
9
|
+
static r2d(rad) {
|
|
10
|
+
return rad * (180 / Math.PI);
|
|
11
|
+
}
|
|
12
|
+
rotate(deg) {
|
|
13
|
+
this.rotateTo(this._deg + deg);
|
|
14
|
+
}
|
|
15
|
+
rotateTo(deg) {
|
|
16
|
+
this._deg = deg;
|
|
17
|
+
if (this._deg >= 360) {
|
|
18
|
+
this.rotateTo(this._deg - 360);
|
|
19
|
+
}
|
|
20
|
+
if (this._deg < 0) {
|
|
21
|
+
this.rotateTo(this._deg + 360);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
valueOf() {
|
|
25
|
+
return this._deg;
|
|
26
|
+
}
|
|
27
|
+
get degrees() {
|
|
28
|
+
return this._deg;
|
|
29
|
+
}
|
|
30
|
+
get radians() {
|
|
31
|
+
return GameLib2D.Rotation.d2r(this._deg);
|
|
32
|
+
}
|
|
33
|
+
getVelocityMod() {
|
|
34
|
+
var rad = GameLib2D.Rotation.d2r(this._deg);
|
|
35
|
+
var vx = Math.sin(rad);
|
|
36
|
+
var vy = -Math.cos(rad);
|
|
37
|
+
return [vx, vy];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export {
|
|
41
|
+
Rotation
|
|
42
|
+
}
|
package/src/2D/Size.js
ADDED
package/src/2D/Sprite.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Box } from "./Box.js"
|
|
2
|
+
import { GameLib } from "./GameLib.js"
|
|
3
|
+
class Sprite extends Box {
|
|
4
|
+
constructor(pos, size, scene) {
|
|
5
|
+
super(pos, size);
|
|
6
|
+
this.scene = scene;
|
|
7
|
+
this.image = null;
|
|
8
|
+
this.fillColor = "#000000";
|
|
9
|
+
this.gameLib = new GameLib();
|
|
10
|
+
this.gameLib.addSprite(this);
|
|
11
|
+
this.ctx = this.gameLib.ctx;
|
|
12
|
+
this.triggers = [];
|
|
13
|
+
}
|
|
14
|
+
update() {}
|
|
15
|
+
draw() {
|
|
16
|
+
if(this.image != null) {
|
|
17
|
+
this.ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
|
|
18
|
+
} else {
|
|
19
|
+
this.ctx.fillStyle = this.fillColor;
|
|
20
|
+
this.ctx.fillRect(this.x, this.y, this.width, this.height);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
addTrigger(triggerID, callback) {
|
|
24
|
+
this.triggers[triggerID] = callback;
|
|
25
|
+
}
|
|
26
|
+
trigger(triggerID) {
|
|
27
|
+
if(this.triggers[triggerID]) {
|
|
28
|
+
this.triggers[triggerID]();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
checkBounds() {
|
|
32
|
+
if(this.x < 0) {
|
|
33
|
+
this.moveTo(0, this.y);
|
|
34
|
+
this.trigger('bounds');
|
|
35
|
+
}
|
|
36
|
+
if(this.x > this.gameLib.canvas.width - this.width) {
|
|
37
|
+
this.moveTo(this.gameLib.canvas.width - this.width, this.y);
|
|
38
|
+
this.trigger('bounds');
|
|
39
|
+
}
|
|
40
|
+
if(this.y < 0) {
|
|
41
|
+
this.moveTo(this.x, 0);
|
|
42
|
+
this.trigger('bounds');
|
|
43
|
+
}
|
|
44
|
+
if(this.y > this.gameLib.canvas.height - this.height) {
|
|
45
|
+
this.moveTo(this.x, this.gameLib.canvas.height - this.height);
|
|
46
|
+
this.trigger('bounds');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
drawHitbox() {
|
|
50
|
+
this.ctx.strokeStyle = this.fillColor;
|
|
51
|
+
this.ctx.strokeRect(this.x, this.y, this.width, this.height);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
Sprite
|
|
56
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Renderable } from "../Renderable.js"
|
|
2
|
+
import { GameLib } from "../GameLib.js"
|
|
3
|
+
class Button extends Renderable {
|
|
4
|
+
constructor(pos, size, scene) {
|
|
5
|
+
super(scene);
|
|
6
|
+
this.overlay = true;
|
|
7
|
+
this._canvas = this.gameLib.viewportCanvas;
|
|
8
|
+
this._ctx = this.gameLib.viewportCtx;
|
|
9
|
+
this._box = new Box(pos, size);
|
|
10
|
+
this.hover = false;
|
|
11
|
+
this.cursor = "default";
|
|
12
|
+
}
|
|
13
|
+
moveTo(x, y) {
|
|
14
|
+
this._box.moveTo(x, y);
|
|
15
|
+
}
|
|
16
|
+
get x() {
|
|
17
|
+
return this._box.x;
|
|
18
|
+
}
|
|
19
|
+
get y() {
|
|
20
|
+
return this._box.y;
|
|
21
|
+
}
|
|
22
|
+
get width() {
|
|
23
|
+
return this._box.width;
|
|
24
|
+
}
|
|
25
|
+
get height() {
|
|
26
|
+
return this._box.height;
|
|
27
|
+
}
|
|
28
|
+
handleClick(evt, coord) {
|
|
29
|
+
if(coord.x >= this._box.x && coord.y >= this._box.y && coord.x <= this._box.x + this._box.width && coord.y <= this._box.y + this._box.height) {
|
|
30
|
+
this.clicked();
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
handleHover(evt, coord) {
|
|
36
|
+
if(coord.x >= this._box.x && coord.y >= this._box.y && coord.x <= this._box.x + this._box.width && coord.y <= this._box.y + this._box.height) {
|
|
37
|
+
this.hover = true;
|
|
38
|
+
GameLib.cursor(this.cursor);
|
|
39
|
+
return true;
|
|
40
|
+
} else {
|
|
41
|
+
this.hover = false;
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
clicked() {
|
|
46
|
+
this.hover = false;
|
|
47
|
+
GameLib.cursor("default");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
Button
|
|
52
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Renderable } from "../Renderable.js"
|
|
2
|
+
class TextInput extends Renderable {
|
|
3
|
+
constructor(pos, size, scene) {
|
|
4
|
+
super(scene);
|
|
5
|
+
this.overlay = true;
|
|
6
|
+
this._canvas = this.gameLib.viewportCanvas;
|
|
7
|
+
this._canvasRect = this._canvas.getBoundingClientRect();
|
|
8
|
+
this.canvasX = this._canvasRect.x;
|
|
9
|
+
this.canvasY = this._canvasRect.y;
|
|
10
|
+
this._ctx = this.gameLib.viewportCtx;
|
|
11
|
+
this._box = new Box(pos, size);
|
|
12
|
+
this._active = false;
|
|
13
|
+
this._value = null;
|
|
14
|
+
this._element = null;
|
|
15
|
+
}
|
|
16
|
+
createElement() {
|
|
17
|
+
this._element = document.createElement('input');
|
|
18
|
+
this._element.setAttribute('style', this.getStyle());
|
|
19
|
+
this._element.setAttribute('type', 'text');
|
|
20
|
+
document.body.appendChild(this._element);
|
|
21
|
+
}
|
|
22
|
+
getStyle() {
|
|
23
|
+
return 'box-sizing: border-box;padding-left: 5px;padding-right: 5px;z-index: 999;display: block;position: absolute;left: ' + (this.canvasX + this.x) + 'px;top: ' + (this.canvasY + this.y) + 'px;width: ' + this.width + 'px;height: ' + this.height + 'px;outline: none;background: transparent;border: 0;';
|
|
24
|
+
}
|
|
25
|
+
draw() {
|
|
26
|
+
if(this._element) {
|
|
27
|
+
this._canvasRect = this._canvas.getBoundingClientRect();
|
|
28
|
+
this.canvasX = this._canvasRect.x;
|
|
29
|
+
this.canvasY = this._canvasRect.y;
|
|
30
|
+
this._element.setAttribute('style', this.getStyle());
|
|
31
|
+
} else {
|
|
32
|
+
this.createElement();
|
|
33
|
+
}
|
|
34
|
+
this._ctx.fillStyle = "#ffffff";
|
|
35
|
+
this._ctx.fillRect(this.x, this.y, this.width, this.height);
|
|
36
|
+
}
|
|
37
|
+
get value() {
|
|
38
|
+
if(this._element) {
|
|
39
|
+
return this._element.value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
overlaySkipped() {
|
|
43
|
+
if(this._element) {
|
|
44
|
+
this.removeElement();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
removeElement() {
|
|
48
|
+
this._element.remove();
|
|
49
|
+
this._element = null;
|
|
50
|
+
}
|
|
51
|
+
setActive(active) {
|
|
52
|
+
super.setActive(active);
|
|
53
|
+
if(active) {
|
|
54
|
+
this.createElement();
|
|
55
|
+
} else {
|
|
56
|
+
this.removeElement();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
moveTo(x, y) {
|
|
60
|
+
this._box.moveTo(x, y);
|
|
61
|
+
}
|
|
62
|
+
get x() {
|
|
63
|
+
return this._box.x;
|
|
64
|
+
}
|
|
65
|
+
get y() {
|
|
66
|
+
return this._box.y;
|
|
67
|
+
}
|
|
68
|
+
get width() {
|
|
69
|
+
return this._box.width;
|
|
70
|
+
}
|
|
71
|
+
get height() {
|
|
72
|
+
return this._box.height;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export {
|
|
76
|
+
TextInput
|
|
77
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { GameLibCore } from "../../Core/index.js"
|
|
2
|
+
import { UIElement } from "./UIElement.js"
|
|
3
|
+
import { Button } from "./Button.js"
|
|
4
|
+
import { TextInput } from "./TextInput.js"
|
|
5
|
+
class UI {
|
|
6
|
+
constructor() {
|
|
7
|
+
throw new GameLibCore.Error("Class UI may not be constructed!");
|
|
8
|
+
}
|
|
9
|
+
static UIElement = UIElement;
|
|
10
|
+
static Button = Button;
|
|
11
|
+
static TextInput = TextInput;
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
UI
|
|
15
|
+
}
|
package/src/2D/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Coord } from "./Coord.js"
|
|
2
|
+
import { Size } from "./Size.js"
|
|
3
|
+
import { Box } from "./Box.js"
|
|
4
|
+
import { Rotation } from "./Rotation.js"
|
|
5
|
+
import { Renderable } from "./Renderable.js"
|
|
6
|
+
import { Sprite } from "./Sprite.js"
|
|
7
|
+
import { GameLib } from "./GameLib.js"
|
|
8
|
+
import { UI } from "./UI/index.js"
|
|
9
|
+
class GameLib2D {
|
|
10
|
+
constructor() {
|
|
11
|
+
throw new GameLibCore.Error("Wrapper class GameLib2D may not be constructed!");
|
|
12
|
+
}
|
|
13
|
+
static Coord = Coord;
|
|
14
|
+
static Size = Size;
|
|
15
|
+
static Box = Box;
|
|
16
|
+
static Rotation = Rotation;
|
|
17
|
+
static Renderable = Renderable;
|
|
18
|
+
static Sprite = Sprite;
|
|
19
|
+
static GameLib = GameLib;
|
|
20
|
+
static UI = UI;
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
GameLib2D
|
|
24
|
+
}
|
package/src/3D/Camera.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Position } from "./Position.js"
|
|
2
|
+
import { Transform } from "./Transform.js"
|
|
3
|
+
import { Rotation } from "./Rotation.js"
|
|
4
|
+
import { mat4 } from "../Core/Matrix/mat4.js"
|
|
5
|
+
class Camera {
|
|
6
|
+
constructor(fovDegrees=45, near=0.1, far=100.0) {
|
|
7
|
+
this.transform = new Transform(new Position(0, 0, 0));
|
|
8
|
+
this.fov = Rotation.d2r(fovDegrees);
|
|
9
|
+
this.near = near;
|
|
10
|
+
this.far = far;
|
|
11
|
+
}
|
|
12
|
+
getProjectionMatrix(aspectRatio) {
|
|
13
|
+
var proj = mat4.create();
|
|
14
|
+
mat4.perspective(proj, this.fov, aspectRatio, this.near, this.far);
|
|
15
|
+
return proj;
|
|
16
|
+
}
|
|
17
|
+
getViewMatrix() {
|
|
18
|
+
var view = this.transform.getModelMatrix();
|
|
19
|
+
mat4.invert(view, view);
|
|
20
|
+
return view;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
Camera
|
|
25
|
+
}
|