-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
115 lines (88 loc) · 2.11 KB
/
util.js
File metadata and controls
115 lines (88 loc) · 2.11 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
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
// util.js
//
// A module of utility functions, with no private elements to hide.
// An easy case; just return an object containing the public stuff.
"use strict";
var util = {
// RANGES
// ======
clampRange: function(value, lowBound, highBound) {
if (value < lowBound) {
value = lowBound;
} else if (value > highBound) {
value = highBound;
}
return value;
},
wrapRange: function(value, lowBound, highBound) {
while (value < lowBound) {
value += (highBound - lowBound);
}
while (value > highBound) {
value -= (highBound - lowBound);
}
return value;
},
isBetween: function(value, lowBound, highBound) {
if (value < lowBound) { return false; }
if (value > highBound) { return false; }
return true;
},
// RANDOMNESS
// ==========
randRange: function(min, max) {
return (min + Math.random() * (max - min));
},
// MISC
// ====
square: function(x) {
return x*x;
},
// DISTANCES
// =========
distSq: function(x1, y1, x2, y2) {
return this.square(x2-x1) + this.square(y2-y1);
},
wrappedDistSq: function(x1, y1, x2, y2, xWrap, yWrap) {
var dx = Math.abs(x2-x1),
dy = Math.abs(y2-y1);
if (dx > xWrap/2) {
dx = xWrap - dx;
};
if (dy > yWrap/2) {
dy = yWrap - dy;
}
return this.square(dx) + this.square(dy);
},
// CANVAS OPS
// ==========
clearCanvas: function (ctx) {
var prevfillStyle = ctx.fillStyle;
ctx.fillStyle = "black";
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fillStyle = prevfillStyle;
},
strokeCircle: function (ctx, x, y, r) {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.stroke();
},
strokeRect: function (ctx, x, y, w, h) {
ctx.strokeRect(x, y, w, h);
},
fillCircle: function (ctx, x, y, r) {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fill();
},
fillBox: function (ctx, x, y, w, h, style) {
var oldStyle = ctx.fillStyle;
ctx.fillStyle = style;
ctx.fillRect(x, y, w, h);
ctx.fillStyle = oldStyle;
},
// draw the game over screen
gameOverScreen: function(ctx){
g_sprites.gameOver.drawCentredAt(ctx, 450, 360, this.rotation);
}
};