big refactor
@@ -1,109 +0,0 @@
1
var Engine =
2
{
3
x: 0,
4
y: 0,
5
w: 0,
6
h: 0,//stored for window-building
7
}
8
9
var Game =
10
{
11
stepPrevious: 0,
12
13
activeScene: new Scene(),
14
userUpdateCallback: null,
15
16
stepsSinceStart: 0,
17
18
queuedCallbacks: [],
19
20
initialize: function(windowX, windowY, windowWidth, windowHeight, loadedCallback)
21
{
22
console.log("GAME: Initializing. Loading dependencies...");
23
Engine.x = windowX;
24
Engine.y = windowY;
25
Engine.w = windowWidth;
26
Engine.h = windowHeight;
27
28
GameWindow.init(Engine.x, Engine.y, Engine.w, Engine.h);
29
GameInput.g_init();
30
TextInput.g_init();
31
window.setInterval(Game.gameStep, 1);
32
GameWindow.redraw();
33
loadedCallback();
34
},
35
36
destroy: function()
37
{
38
Game.activeScene.destroy();
39
window.clearInterval(Game.windowIntervalId);
40
document.getElementById(GameWindow.elementID).outerHTML = "";
41
document.getElementById("g_gameTextInput").outerHTML = "";
42
},
43
44
loadScene: function(newScene, destroyOld = false)
45
{
46
if(destroyOld)
47
{
48
Game.activeScene.destroy();
49
}
50
51
Game.activeScene = newScene;
52
},
53
54
setUpdateFunction: function(callback)
55
{
56
Game.userUpdateCallback = callback;
57
},
58
59
clearScene: function()
60
{
61
Game.activeScene.destroy();
62
},
63
64
gameStep: function()
65
{
66
var stepTime = Date.now();
67
68
var stepTimeCurr = (stepTime - this.stepPrevious) * 0.001;
69
70
stepTimeCurr = Math.min(stepTimeCurr, 0.1);
71
72
if(isNaN(stepTimeCurr))
73
stepTimeCurr = 0;
74
75
Game.stepsSinceStart++;
76
77
TextInput.g_update();
78
79
for(var i = 0; i < Game.queuedCallbacks.length; i++)
80
{
81
Game.queuedCallbacks[i].callback.call(Game.queuedCallbacks[i].context);
82
}
83
84
Game.queuedCallbacks = [];
85
86
GameInput.g_updateKeys();
87
88
var toUpdate = Game.activeScene.activeObjects.slice();
89
90
for(var i = 0; i < toUpdate.length; i++)
91
{
92
if(toUpdate[i].active || toUpdate[i].destroyed)
93
toUpdate[i].g_updateStep(stepTimeCurr);
94
}
95
96
if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); }
97
98
this.stepPrevious = stepTime;
99
TWEEN.update();
100
},
101
102
reorderObjects: function()
103
{
104
this.activeScene.activeObjects.sort(function(a, b)
105
{
106
return a.renderer.layerDepth - b.renderer.layerDepth;
107
})
108
}
109
};
@@ -1,109 +0,0 @@1
var Engine = 2
{3
x: 0,4
y: 0,5
w: 0,6
h: 0,//stored for window-building7
}8
9
var Game = 10
{11
stepPrevious: 0,12
13
activeScene: new Scene(),14
userUpdateCallback: null,15
16
stepsSinceStart: 0,17
18
queuedCallbacks: [],19
20
initialize: function(windowX, windowY, windowWidth, windowHeight, loadedCallback)21
{22
console.log("GAME: Initializing. Loading dependencies...");23
Engine.x = windowX;24
Engine.y = windowY;25
Engine.w = windowWidth;26
Engine.h = windowHeight;27
28
GameWindow.init(Engine.x, Engine.y, Engine.w, Engine.h);29
GameInput.g_init();30
TextInput.g_init();31
window.setInterval(Game.gameStep, 1);32
GameWindow.redraw();33
loadedCallback();34
},35
36
destroy: function()37
{38
Game.activeScene.destroy();39
window.clearInterval(Game.windowIntervalId);40
document.getElementById(GameWindow.elementID).outerHTML = "";41
document.getElementById("g_gameTextInput").outerHTML = "";42
},43
44
loadScene: function(newScene, destroyOld = false)45
{46
if(destroyOld)47
{48
Game.activeScene.destroy();49
}50
51
Game.activeScene = newScene;52
},53
54
setUpdateFunction: function(callback)55
{56
Game.userUpdateCallback = callback;57
},58
59
clearScene: function()60
{61
Game.activeScene.destroy();62
},63
64
gameStep: function()65
{66
var stepTime = Date.now();67
68
var stepTimeCurr = (stepTime - this.stepPrevious) * 0.001;69
70
stepTimeCurr = Math.min(stepTimeCurr, 0.1);71
72
if(isNaN(stepTimeCurr))73
stepTimeCurr = 0;74
75
Game.stepsSinceStart++;76
77
TextInput.g_update();78
79
for(var i = 0; i < Game.queuedCallbacks.length; i++)80
{81
Game.queuedCallbacks[i].callback.call(Game.queuedCallbacks[i].context);82
}83
84
Game.queuedCallbacks = [];85
86
GameInput.g_updateKeys();87
88
var toUpdate = Game.activeScene.activeObjects.slice();89
90
for(var i = 0; i < toUpdate.length; i++)91
{ 92
if(toUpdate[i].active || toUpdate[i].destroyed)93
toUpdate[i].g_updateStep(stepTimeCurr);94
}95
96
if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); }97
98
this.stepPrevious = stepTime;99
TWEEN.update();100
},101
102
reorderObjects: function()103
{104
this.activeScene.activeObjects.sort(function(a, b)105
{106
return a.renderer.layerDepth - b.renderer.layerDepth;107
})108
}109
};@@ -0,0 +1,29 @@
1
class Game
2
{
3
activeScenes = [];
4
previousStep = 0; //used for calculating deltaTime
5
6
constructor(initialScene)
7
{
8
this.activeScenes.push(initialScene);
9
this.update();
10
}
11
12
update()
13
{
14
let stepTime = Date.now();
15
let stepTimeCurr = (stepTime - this.previousStep) * 0.001;
16
stepTimeCurr = Math.min(stepTimeCurr, 0.1);
17
if(isNaN(stepTimeCurr))
18
stepTimeCurr = 0;
19
20
for(let i = 0; i < this.activeScenes.length; i++)
21
{
22
this.activeScenes[i].update(stepTimeCurr);
23
this.activeScenes[i].draw();
24
}
25
26
this.previousStep = stepTime;
27
requestAnimationFrame(this.update);
28
}
29
}
@@ -0,0 +1,29 @@1
class Game2
{3
activeScenes = [];4
previousStep = 0; //used for calculating deltaTime5
6
constructor(initialScene)7
{8
this.activeScenes.push(initialScene);9
this.update();10
}11
12
update()13
{14
let stepTime = Date.now();15
let stepTimeCurr = (stepTime - this.previousStep) * 0.001;16
stepTimeCurr = Math.min(stepTimeCurr, 0.1);17
if(isNaN(stepTimeCurr))18
stepTimeCurr = 0;19
20
for(let i = 0; i < this.activeScenes.length; i++)21
{22
this.activeScenes[i].update(stepTimeCurr);23
this.activeScenes[i].draw();24
}25
26
this.previousStep = stepTime;27
requestAnimationFrame(this.update);28
}29
}@@ -0,0 +1,34 @@
1
class GameObject
2
{
3
transform = null;
4
renderer = null;
5
collider = null;
6
scene = null;
7
8
destroyed = false;
9
10
constructor(x, y, sprite, layer)
11
{
12
this.transform = new Transform(this, x, y, 1, 1, 0);
13
this.renderer = new Renderer(this, sprite)
14
this.collider = new Collider(this);
15
}
16
17
update(deltaTime)
18
{
19
this.transform.update(deltaTime);
20
this.collider.update(deltaTime);
21
}
22
23
destroy()
24
{
25
if(!this.destroyed)
26
{
27
this.destroyed = true;
28
29
this.onDestroy && this.onDestroy();
30
31
this.scene.removeObject(this);
32
}
33
}
34
}
@@ -0,0 +1,34 @@1
class GameObject2
{3
transform = null;4
renderer = null;5
collider = null;6
scene = null;7
8
destroyed = false;9
10
constructor(x, y, sprite, layer)11
{12
this.transform = new Transform(this, x, y, 1, 1, 0);13
this.renderer = new Renderer(this, sprite)14
this.collider = new Collider(this);15
}16
17
update(deltaTime)18
{19
this.transform.update(deltaTime);20
this.collider.update(deltaTime);21
}22
23
destroy()24
{25
if(!this.destroyed)26
{27
this.destroyed = true;28
29
this.onDestroy && this.onDestroy();30
31
this.scene.removeObject(this);32
}33
}34
}@@ -1,350 +0,0 @@
1
2
3
var GameObject =
4
{
5
textWarned: false,
6
7
destroyed: false,
8
9
create: function(template, x, y, image, layer, scene)
10
{
11
var newObj = $.extend(true, {}, template);
12
13
GameObject.object.call(newObj, x, y, image, scene);
14
15
layer = layer || 0;
16
17
newObj.renderer.layerDepth = layer;
18
19
if(!GameObject.allObjects)
20
GameObject.allObjects = [];
21
22
GameObject.allObjects.push(newObj);
23
24
Game.reorderObjects();
25
26
return newObj;
27
},
28
29
inherit: function(base, child)
30
{
31
var newObj = $.extend(true, {}, base);
32
33
var newObj = $.extend(true, newObj, child);
34
35
newObj.base = base;
36
37
return newObj
38
},
39
40
object: function(x, y, image, scene) //DO NOT use any functions with prefix g_ in your game!
41
{
42
this.active = true;
43
44
this.name = "Object";
45
46
this.image = "";
47
48
this.ignoreCollisions = false;
49
50
this.renderer = {
51
52
image: image,
53
54
subImage: 0,
55
56
alpha: 1,
57
58
visible: true,
59
60
//used to correct NaN
61
g_subImgOld: 0,
62
63
g_layerDepth: 0,
64
65
draw: function(x, y, w, h, rotation)
66
{
67
if(isNaN(this.subImage))
68
{
69
this.subImage = this.g_subImgOld;
70
}
71
else
72
{
73
this.g_subImgOld = this.subImage;
74
}
75
76
var subImage = Math.round(this.subImage);
77
subImage %= this.image.subImages;
78
79
var c = subImage % this.image.subImagesPerRow;
80
var r = Math.floor(subImage / this.image.subImagesPerRow);
81
var sw = (this.image.g_image.width / this.image.subImagesPerRow);
82
var sh = (this.image.g_image.height / Math.ceil(this.image.subImages / this.image.subImagesPerRow));
83
var sx = sw * c;
84
var sy = sh * r;
85
86
this.image.draw(sx, sy, sw, sh, x, y, w, h, rotation, this.alpha);
87
}
88
}
89
90
Object.defineProperty(this.renderer, "layerDepth",
91
{
92
get: function()
93
{
94
return this.g_layerDepth;
95
},
96
97
set: function(val)
98
{
99
if (this.g_layerDepth != val)
100
{
101
this.g_layerDepth = val;
102
103
Game.reorderObjects();
104
}
105
}
106
})
107
108
this.transform =
109
{
110
111
position: new vector(x, y),
112
113
velocity: new vector(0, 0),
114
115
rotation: 0,
116
117
118
width: image? image.width : 0,
119
120
height: image? image.height: 0,
121
122
scale: new vector(1, 1),
123
124
pointTowards: function(point)
125
{
126
var delta = point.subtract(this.position);
127
128
this.rotation = delta.toAngle();
129
},
130
131
left: function()
132
{
133
return this.g_dir(0);
134
},
135
136
right: function()
137
{
138
return this.g_dir(180);
139
},
140
141
forward: function()
142
{
143
return this.g_dir(90);
144
},
145
146
back: function()
147
{
148
return this.g_dir(270);
149
},
150
151
g_dir: function(offset)
152
{
153
var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180);
154
155
return angleVector = new vector( -Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians) );
156
}
157
};
158
159
this.g_destroy = false;
160
161
this.g_init = function(scene)
162
{
163
if(scene == null)
164
{
165
Game.activeScene.addObject(this);
166
}
167
else
168
{
169
scene.addObject(this);
170
}
171
172
if (typeof this.start == 'function') { this.start(); }
173
}
174
175
this.defaultDraw = function()
176
{
177
if(this.renderer.image)
178
{
179
var offX = GameWindow.cameraPosition.x;
180
var offY = GameWindow.cameraPosition.y;
181
182
this.renderer.draw(this.transform.position.x + offX, this.transform.position.y + offY,
183
this.transform.width * this.transform.scale.x,
184
this.transform.height * this.transform.scale.y,
185
this.transform.rotation);
186
}
187
}
188
189
this.g_draw = function()
190
{
191
if(!this.g_destroy)
192
{
193
if(this.draw)
194
{
195
this.draw();
196
}
197
else
198
{
199
this.defaultDraw();
200
/*else //??? makes entire screen colored.
201
{
202
GameWindow.canvasContext.fillStyle = "rgb("+ this.transform.position.y+" ," + this.transform.position.x + ",0)"; // sets the color to fill in the rectangle with
203
GameWindow.canvasContext.fillRect( this.transform.position.x - this.transform.width / 2,
204
this.transform.position.y - this.transform.height / 2,
205
this.transform.width,
206
this.transform.height);
207
}*/
208
}
209
}
210
}
211
212
this.g_checkClick = function(thisFrame)
213
{
214
var hit = false;
215
var clickPos = GameInput.mousePosition;
216
217
var hw = this.transform.width / 2;
218
var hh = this.transform.height / 2;
219
220
if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)
221
{
222
if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)
223
{
224
hit = true;
225
}
226
}
227
228
if (hit)
229
{
230
if (thisFrame)
231
{
232
this.onClick && this.onClick();
233
}
234
else
235
{
236
this.onMouseHeld && this.onMouseHeld();
237
}
238
}
239
}
240
241
this.g_updateStep = function(deltaTime)
242
{
243
if(this.g_destroy)
244
{
245
Game.activeScene.activeObjects.splice(Game.activeScene.activeObjects.indexOf(this), 1);
246
delete this;
247
}
248
else
249
{
250
251
if (typeof this.update == 'function') { this.update(deltaTime); }
252
253
this.transform.position.x += this.transform.velocity.x;
254
this.transform.position.y += this.transform.velocity.y;
255
256
//check for clicks
257
if (GameInput.mousePressed())
258
{
259
this.g_checkClick(true);
260
}
261
262
//check for clicks
263
if (GameInput.mouseHeld())
264
{
265
this.g_checkClick(false);
266
}
267
268
if(!this.ignoreCollisions && !this.destroyed)
269
{
270
for(var i = 0; i < Game.activeScene.activeObjects.length; i++)
271
{
272
if(Game.activeScene.activeObjects[i] != this && !Game.activeScene.activeObjects[i].ignoreCollisions)
273
{
274
var hit = false;
275
var o2 = Game.activeScene.activeObjects[i];
276
277
//hw = halfWidth, hh = halfHeight
278
var hw = (this.transform.width * this.transform.scale.x) / 2;
279
var hh = (this.transform.height * this.transform.scale.y) / 2;
280
281
var ohw = (o2.transform.width * o2.transform.scale.x) / 2;
282
var ohh = (o2.transform.height * o2.transform.scale.y) / 2;
283
284
//CHECK FROM OBJ PERSPECTIVE
285
var p =
286
[
287
[this.transform.position.x - hw, this.transform.position.y - hh],
288
[this.transform.position.x + hw, this.transform.position.y - hh],
289
[this.transform.position.x - hw, this.transform.position.y + hh],
290
[this.transform.position.x + hw, this.transform.position.y + hh]
291
];
292
293
for(var pi = 0; pi < p.length; pi++)
294
{
295
if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)
296
{
297
if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)
298
{
299
hit = true;
300
}
301
}
302
}
303
304
//CHECK FROM OTHER PERSPECTIVE
305
var p =
306
[
307
[o2.transform.position.x - ohw, o2.transform.position.y - ohh],
308
[o2.transform.position.x + ohw, o2.transform.position.y - ohh],
309
[o2.transform.position.x - ohw, o2.transform.position.y + ohh],
310
[o2.transform.position.x + ohw, o2.transform.position.y + ohh]
311
];
312
313
for(var pi = 0; pi < p.length; pi++)
314
{
315
if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)
316
{
317
if(p[pi][1] > this.transform.position.y - hh && p[pi][1] < this.transform.position.y + hh)
318
{
319
hit = true;
320
}
321
}
322
}
323
324
325
if(hit)
326
if (!this.destroyed && typeof this.onCollision == 'function') { this.onCollision(o2); }
327
}
328
}
329
}
330
}
331
}
332
333
334
this.destroy = function()
335
{
336
if(!this.g_destroy)
337
{
338
if (typeof this.onDestroy == 'function') { this.onDestroy(); }
339
340
this.destroyed = true;
341
342
this.g_destroy = true;
343
344
GameObject.allObjects.remove(this);
345
}
346
}
347
348
this.g_init(scene);
349
}
350
}
@@ -1,350 +0,0 @@1
2
3
var GameObject = 4
{5
textWarned: false,6
7
destroyed: false,8
9
create: function(template, x, y, image, layer, scene)10
{11
var newObj = $.extend(true, {}, template);12
13
GameObject.object.call(newObj, x, y, image, scene);14
15
layer = layer || 0;16
17
newObj.renderer.layerDepth = layer;18
19
if(!GameObject.allObjects)20
GameObject.allObjects = [];21
22
GameObject.allObjects.push(newObj);23
24
Game.reorderObjects();25
26
return newObj;27
},28
29
inherit: function(base, child)30
{31
var newObj = $.extend(true, {}, base);32
33
var newObj = $.extend(true, newObj, child);34
35
newObj.base = base;36
37
return newObj38
},39
40
object: function(x, y, image, scene) //DO NOT use any functions with prefix g_ in your game!41
{42
this.active = true;43
44
this.name = "Object";45
46
this.image = "";47
48
this.ignoreCollisions = false;49
50
this.renderer = {51
52
image: image,53
54
subImage: 0,55
56
alpha: 1,57
58
visible: true,59
60
//used to correct NaN61
g_subImgOld: 0,62
63
g_layerDepth: 0,64
65
draw: function(x, y, w, h, rotation)66
{67
if(isNaN(this.subImage))68
{69
this.subImage = this.g_subImgOld;70
}71
else72
{73
this.g_subImgOld = this.subImage;74
}75
76
var subImage = Math.round(this.subImage);77
subImage %= this.image.subImages;78
79
var c = subImage % this.image.subImagesPerRow;80
var r = Math.floor(subImage / this.image.subImagesPerRow);81
var sw = (this.image.g_image.width / this.image.subImagesPerRow);82
var sh = (this.image.g_image.height / Math.ceil(this.image.subImages / this.image.subImagesPerRow));83
var sx = sw * c;84
var sy = sh * r;85
86
this.image.draw(sx, sy, sw, sh, x, y, w, h, rotation, this.alpha);87
}88
}89
90
Object.defineProperty(this.renderer, "layerDepth", 91
{92
get: function()93
{94
return this.g_layerDepth;95
},96
97
set: function(val)98
{99
if (this.g_layerDepth != val)100
{101
this.g_layerDepth = val;102
103
Game.reorderObjects();104
}105
}106
})107
108
this.transform = 109
{110
111
position: new vector(x, y),112
113
velocity: new vector(0, 0),114
115
rotation: 0,116
117
118
width: image? image.width : 0,119
120
height: image? image.height: 0,121
122
scale: new vector(1, 1),123
124
pointTowards: function(point)125
{126
var delta = point.subtract(this.position);127
128
this.rotation = delta.toAngle();129
},130
131
left: function()132
{133
return this.g_dir(0);134
},135
136
right: function()137
{138
return this.g_dir(180);139
},140
141
forward: function()142
{143
return this.g_dir(90);144
},145
146
back: function()147
{148
return this.g_dir(270);149
},150
151
g_dir: function(offset)152
{153
var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180);154
155
return angleVector = new vector( -Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians) );156
}157
};158
159
this.g_destroy = false;160
161
this.g_init = function(scene)162
{163
if(scene == null)164
{165
Game.activeScene.addObject(this);166
}167
else168
{169
scene.addObject(this);170
}171
172
if (typeof this.start == 'function') { this.start(); }173
}174
175
this.defaultDraw = function()176
{177
if(this.renderer.image)178
{179
var offX = GameWindow.cameraPosition.x;180
var offY = GameWindow.cameraPosition.y;181
182
this.renderer.draw(this.transform.position.x + offX, this.transform.position.y + offY, 183
this.transform.width * this.transform.scale.x, 184
this.transform.height * this.transform.scale.y, 185
this.transform.rotation);186
} 187
}188
189
this.g_draw = function()190
{191
if(!this.g_destroy)192
{193
if(this.draw)194
{195
this.draw();196
}197
else198
{199
this.defaultDraw();200
/*else //??? makes entire screen colored.201
{202
GameWindow.canvasContext.fillStyle = "rgb("+ this.transform.position.y+" ," + this.transform.position.x + ",0)"; // sets the color to fill in the rectangle with203
GameWindow.canvasContext.fillRect( this.transform.position.x - this.transform.width / 2,204
this.transform.position.y - this.transform.height / 2,205
this.transform.width, 206
this.transform.height);207
}*/208
}209
}210
}211
212
this.g_checkClick = function(thisFrame)213
{214
var hit = false;215
var clickPos = GameInput.mousePosition;216
217
var hw = this.transform.width / 2;218
var hh = this.transform.height / 2;219
220
if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)221
{222
if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)223
{224
hit = true;225
}226
}227
228
if (hit)229
{230
if (thisFrame)231
{232
this.onClick && this.onClick();233
}234
else235
{236
this.onMouseHeld && this.onMouseHeld();237
}238
}239
}240
241
this.g_updateStep = function(deltaTime)242
{ 243
if(this.g_destroy)244
{245
Game.activeScene.activeObjects.splice(Game.activeScene.activeObjects.indexOf(this), 1);246
delete this;247
}248
else249
{250
251
if (typeof this.update == 'function') { this.update(deltaTime); }252
253
this.transform.position.x += this.transform.velocity.x;254
this.transform.position.y += this.transform.velocity.y;255
256
//check for clicks257
if (GameInput.mousePressed())258
{259
this.g_checkClick(true);260
}261
262
//check for clicks263
if (GameInput.mouseHeld())264
{265
this.g_checkClick(false);266
}267
268
if(!this.ignoreCollisions && !this.destroyed)269
{270
for(var i = 0; i < Game.activeScene.activeObjects.length; i++)271
{272
if(Game.activeScene.activeObjects[i] != this && !Game.activeScene.activeObjects[i].ignoreCollisions)273
{274
var hit = false;275
var o2 = Game.activeScene.activeObjects[i];276
277
//hw = halfWidth, hh = halfHeight278
var hw = (this.transform.width * this.transform.scale.x) / 2;279
var hh = (this.transform.height * this.transform.scale.y) / 2;280
281
var ohw = (o2.transform.width * o2.transform.scale.x) / 2;282
var ohh = (o2.transform.height * o2.transform.scale.y) / 2;283
284
//CHECK FROM OBJ PERSPECTIVE285
var p = 286
[287
[this.transform.position.x - hw, this.transform.position.y - hh],288
[this.transform.position.x + hw, this.transform.position.y - hh],289
[this.transform.position.x - hw, this.transform.position.y + hh],290
[this.transform.position.x + hw, this.transform.position.y + hh]291
];292
293
for(var pi = 0; pi < p.length; pi++)294
{295
if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)296
{297
if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)298
{299
hit = true;300
}301
}302
}303
304
//CHECK FROM OTHER PERSPECTIVE305
var p = 306
[307
[o2.transform.position.x - ohw, o2.transform.position.y - ohh],308
[o2.transform.position.x + ohw, o2.transform.position.y - ohh],309
[o2.transform.position.x - ohw, o2.transform.position.y + ohh],310
[o2.transform.position.x + ohw, o2.transform.position.y + ohh]311
];312
313
for(var pi = 0; pi < p.length; pi++)314
{315
if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)316
{317
if(p[pi][1] > this.transform.position.y - hh && p[pi][1] < this.transform.position.y + hh)318
{319
hit = true;320
}321
}322
}323
324
325
if(hit)326
if (!this.destroyed && typeof this.onCollision == 'function') { this.onCollision(o2); }327
}328
} 329
}330
}331
}332
333
334
this.destroy = function()335
{336
if(!this.g_destroy)337
{338
if (typeof this.onDestroy == 'function') { this.onDestroy(); }339
340
this.destroyed = true;341
342
this.g_destroy = true;343
344
GameObject.allObjects.remove(this);345
}346
}347
348
this.g_init(scene);349
}350
}@@ -0,0 +1,119 @@
1
class Collider
2
{
3
gameObject = null;
4
enabled = false;
5
6
constructor(gameObject)
7
{
8
this.gameObject = gameObject;
9
}
10
11
update(deltaTime)
12
{
13
if (GameInput.mousePressed())
14
{
15
this.checkClick(true);
16
}
17
18
if (GameInput.mouseHeld())
19
{
20
this.checkClick(false);
21
}
22
23
this.checkCollisions()
24
}
25
26
checkClick(thisFrame)
27
{
28
var hit = false;
29
var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
30
31
var hw = this.transform.size.x / 2;
32
var hh = this.transform.size.y / 2;
33
34
if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)
35
{
36
if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)
37
{
38
hit = true;
39
}
40
}
41
42
if (hit)
43
{
44
if (thisFrame)
45
{
46
this.onClick && this.onClick();
47
}
48
else
49
{
50
this.onMouseHeld && this.onMouseHeld();
51
}
52
}
53
}
54
55
checkCollisions()
56
{
57
if(this.enabled && !this.gameObject.destroyed)
58
{
59
for(var i = 0; i < this.gameObject.scene.activeObjects.length; i++)
60
{
61
if(this.gameObject.scene.activeObjects[i] != this && this.gameObject.scene.activeObjects[i].ignoreCollisions)
62
{
63
var hit = false;
64
var o2 = this.gameObject.scene.activeObjects[i];
65
66
//hw = halfWidth, hh = halfHeight
67
var hw = this.gameObject.transform.size.x / 2;
68
var hh = this.gameObject.transform.size.y / 2;
69
70
var ohw = o2.transform.size.x / 2;
71
var ohh = o2.transform.size.y / 2;
72
73
//CHECK FROM OBJ PERSPECTIVE
74
var p =
75
[
76
[this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh],
77
[this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh],
78
[this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh],
79
[this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh]
80
];
81
82
for(var pi = 0; pi < p.length; pi++)
83
{
84
if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)
85
{
86
if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)
87
{
88
hit = true;
89
}
90
}
91
}
92
93
//CHECK FROM OTHER PERSPECTIVE
94
var p =
95
[
96
[o2.transform.position.x - ohw, o2.transform.position.y - ohh],
97
[o2.transform.position.x + ohw, o2.transform.position.y - ohh],
98
[o2.transform.position.x - ohw, o2.transform.position.y + ohh],
99
[o2.transform.position.x + ohw, o2.transform.position.y + ohh]
100
];
101
102
for(var pi = 0; pi < p.length; pi++)
103
{
104
if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)
105
{
106
if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
107
{
108
hit = true;
109
}
110
}
111
}
112
113
if(hit)
114
if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') { this.gameObject.onCollision(o2); }
115
}
116
}
117
}
118
}
119
}
@@ -0,0 +1,119 @@1
class Collider2
{3
gameObject = null;4
enabled = false;5
6
constructor(gameObject)7
{8
this.gameObject = gameObject;9
}10
11
update(deltaTime)12
{13
if (GameInput.mousePressed())14
{15
this.checkClick(true);16
}17
18
if (GameInput.mouseHeld())19
{20
this.checkClick(false);21
}22
23
this.checkCollisions()24
}25
26
checkClick(thisFrame)27
{28
var hit = false;29
var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);30
31
var hw = this.transform.size.x / 2;32
var hh = this.transform.size.y / 2;33
34
if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)35
{36
if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)37
{38
hit = true;39
}40
}41
42
if (hit)43
{44
if (thisFrame)45
{46
this.onClick && this.onClick();47
}48
else49
{50
this.onMouseHeld && this.onMouseHeld();51
}52
}53
}54
55
checkCollisions()56
{57
if(this.enabled && !this.gameObject.destroyed)58
{59
for(var i = 0; i < this.gameObject.scene.activeObjects.length; i++)60
{61
if(this.gameObject.scene.activeObjects[i] != this && this.gameObject.scene.activeObjects[i].ignoreCollisions)62
{63
var hit = false;64
var o2 = this.gameObject.scene.activeObjects[i];65
66
//hw = halfWidth, hh = halfHeight67
var hw = this.gameObject.transform.size.x / 2;68
var hh = this.gameObject.transform.size.y / 2;69
70
var ohw = o2.transform.size.x / 2;71
var ohh = o2.transform.size.y / 2;72
73
//CHECK FROM OBJ PERSPECTIVE74
var p = 75
[76
[this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh],77
[this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh],78
[this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh],79
[this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh]80
];81
82
for(var pi = 0; pi < p.length; pi++)83
{84
if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)85
{86
if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)87
{88
hit = true;89
}90
}91
}92
93
//CHECK FROM OTHER PERSPECTIVE94
var p = 95
[96
[o2.transform.position.x - ohw, o2.transform.position.y - ohh],97
[o2.transform.position.x + ohw, o2.transform.position.y - ohh],98
[o2.transform.position.x - ohw, o2.transform.position.y + ohh],99
[o2.transform.position.x + ohw, o2.transform.position.y + ohh]100
];101
102
for(var pi = 0; pi < p.length; pi++)103
{104
if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)105
{106
if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)107
{108
hit = true;109
}110
}111
}112
113
if(hit)114
if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') { this.gameObject.onCollision(o2); }115
}116
} 117
}118
}119
}@@ -0,0 +1,62 @@
1
class Renderer
2
{
3
sprite = null;
4
gameObject = null;
5
subImage = 0;
6
alpha = 1;
7
8
visible = true;
9
10
constructor(parentObject, sprite)
11
{
12
this.gameObject = parentObject;
13
this.sprite = sprite;
14
}
15
16
draw()
17
{
18
if(this.sprite)
19
{
20
var offX = this.gameObject.scene.cameraPosition.x;
21
var offY = this.gameObject.scene.cameraPosition.y;
22
23
this.drawSprite(this.gameObject.transform.position.x + offX,
24
this.gameObject.transform.position.y + offY,
25
this.gameObject.transform.size.x,
26
this.gameObject.transform.size.y,
27
this.gameObject.transform.rotation);
28
}
29
}
30
31
drawSprite()
32
{
33
if(isNaN(this.subImage))
34
{
35
this.subImage = this.g_subImgOld;
36
}
37
else
38
{
39
this.g_subImgOld = this.subImage;
40
}
41
42
var subImage = Math.round(this.subImage);
43
subImage %= this.image.subImages;
44
45
var c = subImage % this.sprite.subImagesPerRow;
46
var r = Math.floor(subImage / this.sprite.subImagesPerRow);
47
var sw = (this.sprite.g_image.width / this.sprite.subImagesPerRow);
48
var sh = (this.sprite.g_image.height / Math.ceil(this.sprite.subImages / this.sprite.subImagesPerRow));
49
var sx = sw * c;
50
var sy = sh * r;
51
52
this.image.draw(
53
sx, sy, sw, sh,
54
this.gameObject.transform.position.x,
55
this.gameObject.transform.position.y,
56
this.gameObject.transform.size.x,
57
this.gameObject.transform.size.y,
58
this.gameObject.transform.rotation,
59
this.alpha
60
);
61
}
62
}
@@ -0,0 +1,62 @@1
class Renderer2
{3
sprite = null;4
gameObject = null;5
subImage = 0;6
alpha = 1;7
8
visible = true;9
10
constructor(parentObject, sprite)11
{12
this.gameObject = parentObject;13
this.sprite = sprite;14
}15
16
draw()17
{18
if(this.sprite)19
{20
var offX = this.gameObject.scene.cameraPosition.x;21
var offY = this.gameObject.scene.cameraPosition.y;22
23
this.drawSprite(this.gameObject.transform.position.x + offX, 24
this.gameObject.transform.position.y + offY, 25
this.gameObject.transform.size.x, 26
this.gameObject.transform.size.y, 27
this.gameObject.transform.rotation);28
} 29
}30
31
drawSprite()32
{33
if(isNaN(this.subImage))34
{35
this.subImage = this.g_subImgOld;36
}37
else38
{39
this.g_subImgOld = this.subImage;40
}41
42
var subImage = Math.round(this.subImage);43
subImage %= this.image.subImages;44
45
var c = subImage % this.sprite.subImagesPerRow;46
var r = Math.floor(subImage / this.sprite.subImagesPerRow);47
var sw = (this.sprite.g_image.width / this.sprite.subImagesPerRow);48
var sh = (this.sprite.g_image.height / Math.ceil(this.sprite.subImages / this.sprite.subImagesPerRow));49
var sx = sw * c;50
var sy = sh * r;51
52
this.image.draw(53
sx, sy, sw, sh, 54
this.gameObject.transform.position.x, 55
this.gameObject.transform.position.y, 56
this.gameObject.transform.size.x, 57
this.gameObject.transform.size.y, 58
this.gameObject.transform.rotation, 59
this.alpha60
);61
}62
}@@ -0,0 +1,61 @@
1
class Transform
2
{
3
position = null;
4
scale = null;
5
rotation = 0;
6
gameObject = null;
7
8
velocity = null;
9
angularVelocity = 0;
10
11
get size()
12
{
13
return new vector(
14
this.scale.x * this.gameObject.renderer.sprite.width,
15
this.scale.y * this.gameObject.renderer.sprite.height);
16
}
17
18
constructor(gameObject, x, y, w, h, r)
19
{
20
this.gameObject = gameObject;
21
this.position = new vector(x, y);
22
this.scale = new vector(w, h);
23
this.rotation = r;
24
25
this.velocity = new vector(0, 0)
26
}
27
28
pointTowards(point)
29
{
30
var delta = point.subtract(this.position);
31
32
this.rotation = delta.toAngle();
33
}
34
35
left()
36
{
37
return this.g_dir(0);
38
}
39
40
right()
41
{
42
return this.g_dir(180);
43
}
44
45
forward()
46
{
47
return this.g_dir(90);
48
}
49
50
back()
51
{
52
return this.g_dir(270);
53
}
54
55
g_dir(offset)
56
{
57
var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180);
58
59
return angleVector = new vector( -Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians) );
60
}
61
}
@@ -0,0 +1,61 @@1
class Transform2
{3
position = null;4
scale = null;5
rotation = 0;6
gameObject = null;7
8
velocity = null;9
angularVelocity = 0;10
11
get size()12
{13
return new vector(14
this.scale.x * this.gameObject.renderer.sprite.width,15
this.scale.y * this.gameObject.renderer.sprite.height);16
}17
18
constructor(gameObject, x, y, w, h, r)19
{20
this.gameObject = gameObject;21
this.position = new vector(x, y);22
this.scale = new vector(w, h);23
this.rotation = r;24
25
this.velocity = new vector(0, 0)26
}27
28
pointTowards(point)29
{30
var delta = point.subtract(this.position);31
32
this.rotation = delta.toAngle();33
}34
35
left()36
{37
return this.g_dir(0);38
}39
40
right()41
{42
return this.g_dir(180);43
}44
45
forward()46
{47
return this.g_dir(90);48
}49
50
back()51
{52
return this.g_dir(270);53
}54
55
g_dir(offset)56
{57
var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180);58
59
return angleVector = new vector( -Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians) );60
}61
}@@ -1,6 +1,7 @@
1
1
class Scene
2
2
{
3
3
static idCount = 0;
4
id = null;
4
5
activeObjects = [];
5
6
position = new vector(0, 0);
6
7
size = new vector(0, 0);
@@ -10,10 +11,12 @@ class Scene
10
11
11
12
cv_canvas = null;
12
13
cv_context = null;
14
destroyed = false;
13
15
14
16
constructor(x, y, width, height, fullscreen = false)
15
17
{
16
18
Scene.idCount++;
19
this.id = 'canvas_' + Scene.idCount;
17
20
18
21
this.position.x = x;
19
22
this.position.y = y;
@@ -23,12 +26,21 @@ class Scene
23
26
24
27
let body = document.body;
25
28
26
body.insertAdjacentHTML('beforeend', '<canvas id="canvas_' + Scene.idCount + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
29
body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');
27
30
28
this.cv_canvas = document.getElementById('canvas_' + Scene.idCount);
31
this.cv_canvas = document.getElementById(this.id);
29
32
this.cv_canvasContext = this.cv_canvas.getContext('2d');
30
33
}
31
34
35
destroy()
36
{
37
this.destroyed = true
38
for(var i = 0; i < this.activeObjects.length; i++)
39
{
40
this.activeObjects[i].destroy();
41
}
42
}
43
32
44
backgroundColor =
33
45
{
34
46
r: 255,
@@ -48,19 +60,34 @@ class Scene
48
60
49
61
addObject(obj)
50
62
{
63
if(this.destroyed)
64
return;
65
51
66
this.activeObjects.push(obj);
67
obj.scene = this;
52
68
}
53
69
54
destroy()
70
removeObject(obj)
55
71
{
56
for(var i = 0; i < this.activeObjects.length; i++)
72
if(this.activeObjects.contains(obj))
57
73
{
58
this.activeObjects[i].destroy();
74
this.activeObjects.remove(obj)
75
}
76
}
77
78
update(deltaTime)
79
{
80
for(let i = 0; i < this.activeObjects.length; i++)
81
{
82
this.activeObjects[i].update(deltaTime)
59
83
}
60
84
}
61
85
62
86
redraw()
63
87
{
88
if(this.destroyed)
89
return;
90
64
91
if(this.fullscreen)
65
92
{
66
93
this.size.width = window.innerWidth - 2;
@@ -80,7 +107,5 @@ class Scene
80
107
if(this.activeObjects[i].active && this.activeObjects[i].renderer.visible)
81
108
this.activeObjects[i].g_draw(this.canvasContext);
82
109
}
83
84
//requestAnimationFrame(GameWindow.redraw); // <- This must be called in global game update loop!
85
110
}
86
111
}
@@ -1,6 +1,7 @@1
class Scene1
class Scene2
{2
{3
static idCount = 0;3
static idCount = 0;4
id = null;4
activeObjects = [];5
activeObjects = [];5
position = new vector(0, 0);6
position = new vector(0, 0);6
size = new vector(0, 0);7
size = new vector(0, 0);@@ -10,10 +11,12 @@ class Scene10
11
11
cv_canvas = null;12
cv_canvas = null;12
cv_context = null;13
cv_context = null;14
destroyed = false;13
15
14
constructor(x, y, width, height, fullscreen = false)16
constructor(x, y, width, height, fullscreen = false)15
{17
{16
Scene.idCount++;18
Scene.idCount++;19
this.id = 'canvas_' + Scene.idCount;17
20
18
this.position.x = x;21
this.position.x = x;19
this.position.y = y;22
this.position.y = y;@@ -23,12 +26,21 @@ class Scene23
26
24
let body = document.body;27
let body = document.body;25
28
26
body.insertAdjacentHTML('beforeend', '<canvas id="canvas_' + Scene.idCount + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');29
body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');27
30
28
this.cv_canvas = document.getElementById('canvas_' + Scene.idCount);31
this.cv_canvas = document.getElementById(this.id);29
this.cv_canvasContext = this.cv_canvas.getContext('2d');32
this.cv_canvasContext = this.cv_canvas.getContext('2d');30
}33
}31
34
35
destroy()36
{37
this.destroyed = true38
for(var i = 0; i < this.activeObjects.length; i++)39
{40
this.activeObjects[i].destroy();41
}42
}43
32
backgroundColor = 44
backgroundColor = 33
{45
{34
r: 255,46
r: 255,@@ -48,19 +60,34 @@ class Scene48
60
49
addObject(obj)61
addObject(obj)50
{62
{63
if(this.destroyed)64
return; 65
51
this.activeObjects.push(obj);66
this.activeObjects.push(obj);67
obj.scene = this;52
}68
}53
69
54
destroy()70
removeObject(obj)55
{71
{56
for(var i = 0; i < this.activeObjects.length; i++)72
if(this.activeObjects.contains(obj))57
{73
{58
this.activeObjects[i].destroy();74
this.activeObjects.remove(obj)75
}76
}77
78
update(deltaTime)79
{80
for(let i = 0; i < this.activeObjects.length; i++)81
{82
this.activeObjects[i].update(deltaTime)59
}83
}60
}84
}61
85
62
redraw()86
redraw()63
{87
{88
if(this.destroyed)89
return; 90
64
if(this.fullscreen)91
if(this.fullscreen)65
{92
{66
this.size.width = window.innerWidth - 2;93
this.size.width = window.innerWidth - 2;@@ -80,7 +107,5 @@ class Scene80
if(this.activeObjects[i].active && this.activeObjects[i].renderer.visible)107
if(this.activeObjects[i].active && this.activeObjects[i].renderer.visible)81
this.activeObjects[i].g_draw(this.canvasContext);108
this.activeObjects[i].g_draw(this.canvasContext);82
}109
}83
84
//requestAnimationFrame(GameWindow.redraw); // <- This must be called in global game update loop!85
}110
}86
}111
}@@ -0,0 +1,2 @@
1
let game = new Game();
2
game.init();
@@ -0,0 +1,2 @@1
let game = new Game();2
game.init();@@ -66,8 +66,8 @@ var GameInput =
66
66
67
67
document.onmousemove = function(e)
68
68
{
69
GameInput.trueMousePosition.x = e.x + GameWindow.x;
70
GameInput.trueMousePosition.y = e.y + GameWindow.y;
69
GameInput.trueMousePosition.x = e.x;
70
GameInput.trueMousePosition.y = e.y;
71
71
}
72
72
73
73
var el = document.getElementsByTagName("canvas")[0];
@@ -89,7 +89,7 @@ var GameInput =
89
89
90
90
g_updateKeys: function()
91
91
{
92
GameInput.mousePosition = GameInput.trueMousePosition.subtract(GameWindow.cameraPosition);
92
GameInput.mousePosition = GameInput.trueMousePosition;
93
93
94
94
GameInput.g_pKeyStatus = GameInput.g_keyStatus;
95
95
GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
@@ -66,8 +66,8 @@ var GameInput =66
66
67
document.onmousemove = function(e)67
document.onmousemove = function(e)68
{68
{69
GameInput.trueMousePosition.x = e.x + GameWindow.x;69
GameInput.trueMousePosition.x = e.x;70
GameInput.trueMousePosition.y = e.y + GameWindow.y;70
GameInput.trueMousePosition.y = e.y;71
}71
}72
72
73
var el = document.getElementsByTagName("canvas")[0];73
var el = document.getElementsByTagName("canvas")[0];@@ -89,7 +89,7 @@ var GameInput =89
89
90
g_updateKeys: function()90
g_updateKeys: function()91
{91
{92
GameInput.mousePosition = GameInput.trueMousePosition.subtract(GameWindow.cameraPosition);92
GameInput.mousePosition = GameInput.trueMousePosition;93
93
94
GameInput.g_pKeyStatus = GameInput.g_keyStatus;94
GameInput.g_pKeyStatus = GameInput.g_keyStatus;95
GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;95
GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;@@ -1,10 +1,10 @@
1
1
// Basic event listeners, modeled after Phaser Signals
2
2
3
function signal()
3
class signal
4
4
{
5
this.listeners = [];
5
listeners = [];
6
6
7
this.dispatch = function()
7
dispatch()
8
8
{
9
9
for (var i = 0; i < this.listeners.length; i++)
10
10
{
@@ -20,7 +20,7 @@ function signal()
20
20
}
21
21
}
22
22
23
this.add = function(callback, context, once)
23
add(callback, context, once)
24
24
{
25
25
if (!this.contains(callback, context))
26
26
{
@@ -28,12 +28,12 @@ function signal()
28
28
}
29
29
}
30
30
31
this.addOnce = function(callback, context)
31
addOnce(callback, context)
32
32
{
33
33
this.add(callback, context, true);
34
34
}
35
35
36
this.contains = function(callback, context)
36
contains(callback, context)
37
37
{
38
38
for (var i = this.listeners.length - 1; i > 0; i--)
39
39
{
@@ -46,7 +46,7 @@ function signal()
46
46
return false;
47
47
}
48
48
49
this.remove = function(callback, context)
49
remove(callback, context)
50
50
{
51
51
for (var i = this.listeners.length - 1; i > 0; i--)
52
52
{
@@ -58,7 +58,7 @@ function signal()
58
58
}
59
59
}
60
60
61
this.removeAll = function()
61
removeAll()
62
62
{
63
63
this.listeners = [];
64
64
}
@@ -1,10 +1,10 @@1
// Basic event listeners, modeled after Phaser Signals1
// Basic event listeners, modeled after Phaser Signals2
2
3
function signal()3
class signal4
{4
{5
this.listeners = [];5
listeners = [];6
6
7
this.dispatch = function()7
dispatch()8
{8
{9
for (var i = 0; i < this.listeners.length; i++)9
for (var i = 0; i < this.listeners.length; i++)10
{10
{@@ -20,7 +20,7 @@ function signal()20
}20
}21
}21
}22
22
23
this.add = function(callback, context, once)23
add(callback, context, once)24
{24
{25
if (!this.contains(callback, context))25
if (!this.contains(callback, context))26
{26
{@@ -28,12 +28,12 @@ function signal()28
}28
}29
}29
}30
30
31
this.addOnce = function(callback, context)31
addOnce(callback, context)32
{32
{33
this.add(callback, context, true);33
this.add(callback, context, true);34
}34
}35
35
36
this.contains = function(callback, context)36
contains(callback, context)37
{37
{38
for (var i = this.listeners.length - 1; i > 0; i--)38
for (var i = this.listeners.length - 1; i > 0; i--)39
{39
{@@ -46,7 +46,7 @@ function signal()46
return false;46
return false;47
}47
}48
48
49
this.remove = function(callback, context)49
remove(callback, context)50
{50
{51
for (var i = this.listeners.length - 1; i > 0; i--)51
for (var i = this.listeners.length - 1; i > 0; i--)52
{52
{@@ -58,7 +58,7 @@ function signal()58
}58
}59
}59
}60
60
61
this.removeAll = function()61
removeAll()62
{62
{63
this.listeners = [];63
this.listeners = [];64
}64
}@@ -1,44 +0,0 @@
1
var TextInput =
2
{
3
el: null,
4
g_onChangeCallback: function(){},
5
g_text: "",
6
7
g_init: function()
8
{
9
var body = document.body;
10
body.insertAdjacentHTML('beforeend', '<input type="text" id="g_gameTextInput" style=""></input>');
11
},
12
13
open: function(defaultText, callback, context)
14
{
15
this.el = document.getElementById("g_gameTextInput");
16
this.el.value = defaultText;
17
this.el.focus();
18
19
var thisContext = this;
20
21
this.g_onChangeCallback = function()
22
{
23
context = context || thisContext.el;
24
callback && callback.call(context, thisContext.g_text);
25
}
26
},
27
28
close: function()
29
{
30
this.g_onChangeCallback = function(){};
31
},
32
33
g_update: function()
34
{
35
if(this.el != null)
36
{
37
if(this.g_text !== this.el.value)
38
{
39
this.g_text = this.el.value;
40
this.g_onChangeCallback();
41
}
42
}
43
}
44
}
@@ -1,44 +0,0 @@1
var TextInput = 2
{3
el: null,4
g_onChangeCallback: function(){},5
g_text: "",6
7
g_init: function()8
{9
var body = document.body;10
body.insertAdjacentHTML('beforeend', '<input type="text" id="g_gameTextInput" style=""></input>');11
},12
13
open: function(defaultText, callback, context)14
{15
this.el = document.getElementById("g_gameTextInput");16
this.el.value = defaultText;17
this.el.focus();18
19
var thisContext = this;20
21
this.g_onChangeCallback = function()22
{23
context = context || thisContext.el;24
callback && callback.call(context, thisContext.g_text);25
}26
},27
28
close: function()29
{30
this.g_onChangeCallback = function(){};31
},32
33
g_update: function()34
{35
if(this.el != null)36
{37
if(this.g_text !== this.el.value)38
{39
this.g_text = this.el.value;40
this.g_onChangeCallback();41
}42
}43
}44
}