-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhydroxide.js
532 lines (405 loc) · 11.4 KB
/
hydroxide.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
* HTML5 2D canvas game engine that stays out of your way
*/
var Hydroxide = (function() {
//number of frames completed; use for animations
var frameNum = 0;
/* canvas context */
var context;
var canvas_height;
var canvas_width;
//canvas ID on DOM (e.g. "canvas")
var canvas_id;
//whether or not to check for collisions
var collisionToggle = true;
/* These are objects that are being used in the game */
/* Must contain specific public properties:
* x - x coordinate of object (used for collision detection), bottom left corner
* y - y coordinate of object (used for collision detection), bottom left corner
* width - width (for collision detection)
* height - height (for collision detection)
* draw - function used to draw the object (called every frame draw)
* update - called before every draw call, should be used for things like updating coordinates
* type - a string denoting what type of GameObject it is
* onCollision - called by the engine when the GameObject "collides" with something else
* screenClicked - called by the engine whenever *anything* is clicked, passed
* true or false depending on if the object was clicked
*/
var GameObjects = [];
/* These are generic objects onto which the game can tack on some data */
/* They can be literally anything, and, each structure has a name associated with it, so that it may be found again */
var DataObjects = {};
/*
* An integer, read by all functions
*/
var state;
/*
* Threading system structure; holds all running thread objects
*/
var runningThreads = [];
/*
* Amount of time between thread calls in milliseconds
*/
var threadTime = 15;
/*
* id: id of the canvas tag
* ct: canvas context
* fd: delay between each frame being drawn
* cd: delay between engine checking for collision and such
* w: width of canvas
* h: height of canvas
*/
var start = function (id, ct, fd, cd, w, h) {
context = ct;
canvas_id = id;
canvas_height = h;
canvas_width = w;
setInterval(drawFrame, fd);
setInterval(engineCheck, cd);
};
var collisionDetectionOff() = function(){
collisionToggle = false;
}
var collisionDetectionOn() = function() {
collisionToggle = true;
}
/* check if a point with coordinates x and y is in rectangle A */
var inRect = function(x, y, A) {
if(A.x <= x && A.y <= y && y <= (A.y + A.height) && x <= (A.x + A.width)) {
return true;
}
return false;
};
/* this needs to be connected to a mouse click on the canvas by the user of OH */
var mouseClick = function (e) {
var x;
var y;
if (e.pageX || e.pageY) {
x = e.pageX;
y = e.pageY;
}
else {
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
var gCanvasElement = $("#"+canvas_id)[0];
x -= gCanvasElement.offsetLeft;
y -= gCanvasElement.offsetTop;
for(var i = 0; i<GameObjects.length; i++) {
GameObjects[i].screenClicked(x, y, inRect(x, y, GameObjects[i]));
}
};
/* check if "value" is in the range from min to max, inclusive */
var valueInRange = function(value, min, max) {
return (value >= min) && (value <= max);
};
/* returns distance between (x1, y1) and (x2, y2) */
var distance = function(x1, y1, x2, y2) {
var dist = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
return dist;
};
/* returns midpoint coordinates as array of (x1, y1) - (x2, y2) */
var midpoint = function(x1, y1, x2, y2) {
var res = [Math.abs((x1-x2)/2), Math.abs((y1-y2)/2)]
return res;
};
/* returns hyptoteneuse length of a right triangle with bases a and b */
var rightTriangleBB = function(a, b) {
return Math.sqrt(a*a + b*b);
};
/* returns base length of a right triangle with base a and hypoteneuse c */
var rightTriangleBH = function(a, c) {
return Math.sqrt(c*c - a*a);
};
/* check if two rectangles overlap, aka collide */
var checkOverlap = function(A, B) {
var xOverlap = valueInRange(A.x, B.x, B.x+B.width) || valueInRange(B.x, A.x, A.x+A.width);
var yOverlap = valueInRange(A.y, B.y, B.y + B.height) || valueInRange(B.y, A.y, A.y + B.height);
return xOverlap && yOverlap;
};
/* check collision between all GameObjects */
var checkCollision = function(selectedObject) {
for(var i = 0; i<GameObjects.length; i++) {
if(selectedObject == GameObjects[i]) {
continue; //don't compare with itself
}
else {
if(checkOverlap(selectedObject, GameObjects[i])) {
selectedObject.onCollision(GameObjects[i]);
}
}
}
};
/* check if object has hit an edge */
var checkEdge = function(go) {
if(go.y <= 0 || go.y+go.height >= canvas_height) {
go.onEdgeY();
}
else if(go.x <= 0 || go.x+go.width >= canvas_width) {
go.onEdgeX();
}
}
/* engine checking; checks bounds, collisions, clicks, etc. */
/* basically fires events on objects */
var engineCheck = function() {
//check for collision
for(var i = 0; i<GameObjects.length; i++) {
var selectedObject = GameObjects[i];
checkEdge(selectedObject);
if(!collisionToggle) {
checkCollision(selectedObject);
}
}
};
/* clear screen out */
var clearContext = function() {
context.clearRect(0, 0, canvas_width, canvas_height);
};
/* draw a frame, immediately, ahead of the loop */
/* do not use externally unless a forced draw frame (which shouldn't happen) is needed */
var drawFrame = function() {
//clear context
clearContext();
//loop over all the GameObjects
for(var i = 0; i < GameObjects.length; i++) {
context.save();
GameObjects[i].update();
GameObjects[i].draw(context);
context.restore();
}
frameNum++;
};
/*
* Register GameObject with Hydroxide
* GameObject must follow the laid out properties, or you will all kinds of errors
*/
var registerObject = function(GameObject) {
GameObjects.push(GameObject);
};
/*
* Set state, as an integer
*/
var setState = function(s) {
state = s;
};
/*
* Get state, as an integer
*/
var getState = function() {
return state;
};
/*
* Get number of frames elapsed
*/
var getFrameNum = function() {
return frameNum;
};
/*
* "threading" subsystem
* thrTime: amount of time between each thread function call, default 15
*/
var initThreading = function(thrTime) {
threadTime = thrTime;
};
/*
* Add thread, or, basically start interval
*/
var addThread = function(threadObj) {
id = setInterval(threadObj.main, threadTime);
threadObj.id = id;
runningThreads.push(threadObj);
return id;
};
/*
* Remove thread from running
* calls killed() in thread object
*/
var removeThread = function(id) {
for(var i = 0; i<runningThreads.length; i++) {
var rt = runningThreads[i];
if(rt.id == id) {
rt.killed();
//remove running thread
clearInterval(id);
runningThreads.splice(i, 1);
}
}
};
/*
* Get array of running threads
*/
var getRunningThreads = function() {
return runningThreads;
};
/* Code section for DataObjects */
/* Do not use DataObject to register game objects, because the name system is meant for programmers, not computers! */
var registerDataObject = function(name, obj) {
if(name in DataObjects) {
return -1;
}
DataObjects[name] = obj;
return 0;
};
var getDataObject = function(name) {
if(name in DataObjects) {
return DataObjects[name];
}
else {
return -1;
}
};
/* essentially the same as registerDataObject */
var updateDataObject = function(name, obj) {
return registerDataObject(name, obj);
}
/* exposed functions that external code can use */
var exposed = {
start: start,
drawFrame: drawFrame,
engineCheck: engineCheck,
registerObject: registerObject,
checkCollision: checkCollision,
inRect: inRect,
valueInRange: valueInRange,
distance: distance,
setState: setState,
getState: getState,
mouseClick: mouseClick,
clearContext: clearContext,
getFrameNum: getFrameNum,
initThreading: initThreading,
addThread: addThread,
removeThread: removeThread,
getRunningThreads: getRunningThreads,
registerDataObject: registerDataObject,
getDataObject: getDataObject,
updateDataObject: updateDataObject
};
return exposed;
})();
/* Hydroxide thread object
* All threads should derive from this object
* use Object.create
* paused() is currently unused
*/
var OHThread = (function() {
//called when thread is started
var main = function() {};
//called when thread is killed
var killed = function() {};
//called when thread is paused
var paused = function () {}
var exposed = {
main: main,
killed: killed,
paused: paused
};
return exposed;
});
/* Use as prototype for new GameObjects
* Use Object.create which is now part of most browsers,
* including IE
*/
var OHGameObj = (function() {
var x = 0;
var y = 0;
var width = 0;
var height = 0;
var draw = function (context) {};
var update = function () {};
var onCollision = function () {};
var screenClicked = function () {};
var onEdgeX = function () {};
var onEdgeY = function () {};
var exposed = {
x:x,
y:y,
width: width,
height: height,
draw: draw,
update: update,
onCollision: onCollision,
screenClicked: screenClicked
};
return exposed;
})();
/* Specific purpose objects derived from OHGameObj - make your life easier */
/* You don't have to use these; in fact, it may be beneficial to not use them, but, using them makes development quicker */
/* for low-level animation; set number of frames, calls a function on draw with a frame number */
var OHBasicAnimatedObj = (function() {
var obj = Object.create(OHGameObj);
obj.frames = 1;
obj.setFrames = function(f) {
this.frames = f;
};
//override!
obj.drawFrame = function(frameNum) {};
obj.draw = function(f) {
for(var i = 0; i<this.frames; i++) {
obj.drawFrame(i);
}
};
return obj;
})();
/* an edge bouncing object; used a lot, so, why not abstract it? */
var OHWallBouncingObj = (function() {
var obj = Object.create(OHGameObj);
obj.xspeed = 1;
obj.yspeed = 1;
obj.onEdgeX = function() {
this.xspeed = -this.xspeed;
};
obj.onEdgeY = function() {
this.yspeed = -this.yspeed;
};
obj.update = function() {
this.x += obj.xspeed;
this.y += obj.yspeed;
};
return obj;
})();
/* adds a draw swtich; if you don't want to draw the object anymore, just set this.draw to false */
var OHDrawSwitchObj = (function() {
var obj = Object.create(OHGameObj);
obj.drawSwitch = true;
obj.draw = function() {
if(obj.drawSwitch) {
return;
}
obj.drawSwitchOn();
};
return obj;
})();
/* adds a update switch; if you don't want to update the object anymore, just set this.update to false */
var OHUpdateSwitchObj = (function() {
var obj = Object.create(OHGameObj);
obj.updateSwitch = true;
obj.update = function() {
if(obj.updateSwitch) {
return;
}
obj.updateSwitchOn();
};
return obj;
})();
var OHMovingImageObj = (function() {
var obj = Object.create(OHGameObj);
obj.image = "";
obj.imageObj = null;
obj.setImage = function(im) {
this.imageObj = new Image();
};
obj.draw = function() {
context.drawImage(this.imageObj, this.x, this.y);
};
return obj;
})();
var OHSquareObj = (function() {
var obj = Object.create(OHGameObj);
this.width = 0;
this.height = 0;
obj.setDimension = function(x) {
this.width = x;
this.height = x;
}
})();