Repository navigation
Expand file tree
/
Copy pathasteroidUtils.js
More file actions
58 lines (51 loc) · 1.7 KB
/
Copy pathasteroidUtils.js
File metadata and controls
58 lines (51 loc) · 1.7 KB
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
/*********************************************************************
* Utils for the Asteroids game
* Copyright (c) 2012 Steve Purkis
*/
/* cache some commonly used calculations in the global namespace */
var PI = Math.PI;
var halfPI = PI/2;
var deg_to_rad = [];
for (var i=-360; i<=360; i++) {
deg_to_rad[i] = i*PI/180;
}
deg_to_rad[0.75] = 0.75*PI/180;
deg_to_rad[0.5] = 0.50*PI/180;
deg_to_rad[0.25] = 0.25*PI/180;
/*********************************************************************
* Make inheritance a bit easier
*/
Function.prototype.inheritsFrom = function( parentClassOrObject ) {
// thanks to http://phrogz.net/js/classes/OOPinJS2.html
if ( parentClassOrObject.constructor == Function ) {
// Normal Inheritance
this.prototype = new parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject.prototype;
} else {
// Pure Virtual Inheritance
this.prototype = parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject;
}
return this;
}
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/random
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Returns the class name of the argument or undefined if
* it's not a valid JavaScript object.
* http://blog.magnetiq.com/post/514962277/finding-out-class-names-of-javascript-objects
*/
function getObjectClass(obj) {
if (obj && obj.constructor && obj.constructor.toString) {
var arr = obj.constructor.toString().match(
/function\s*(\w+)/);
if (arr && arr.length == 2) {
return arr[1];
}
}
return undefined;
}