-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
166 lines (134 loc) · 4.06 KB
/
Copy pathutil.js
File metadata and controls
166 lines (134 loc) · 4.06 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
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
// ====
// util
// ====
// 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";
/* jshint browser: true, devel: true, globalstrict: true */
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
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;
},
getRotation: function (origX, origY, x, y) {
var m = origY - y;
var a = origX - x + 0.001; // 0.001 is so that we don't devide with zero
var atan = Math.atan(m / a) - Math.PI / 2;
if (a <= 0) {
atan += Math.PI
};
return atan;
},
// 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
// ==========
// draws the background
renderBackground: function (ctx) {
g_sprites.levels[g_level].drawAt(ctx, 0, 0, g_gameWidth, g_gameHeight);
},
// Draws text at the given (x, y) with Berlin sans FB font
renderText: function (ctx, color, size, text, x, y) {
ctx.save();
ctx.fillStyle = color;
ctx.font = "450 " + size + "px Berlin sans FB";
ctx.fillText(text, x, y);
ctx.restore();
},
// Draws text in the menu with corresponding (x, y) cooredinates,
// text position, font, fill and stroke color
renderText2: function (ctx, textPosition, font, color1,
color2, text, xPosOnMenu, yPosOnMenu) {
ctx.save();
ctx.textAlign = textPosition;
ctx.font = font;
ctx.fillStyle = color1;
ctx.fillText(text, (xPosOnMenu + g_gameWidth), yPosOnMenu);
ctx.fillStyle = color2;
ctx.strokeText(text, (xPosOnMenu + g_gameWidth), yPosOnMenu);
ctx.restore();
},
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();
},
fillCircle: function (ctx, x, y, r, opacity) {
if (opacity === undefined) opacity = 1;
ctx.save();
ctx.globalAlpha = opacity;
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
},
fillBox: function (ctx, x, y, w, h, style, opacity = 1) {
var oldStyle = ctx.fillStyle;
var oldAlpha = ctx.globalAlpha;
ctx.globalAlpha = opacity;
ctx.fillStyle = style;
ctx.fillRect(x, y, w, h);
ctx.fillStyle = oldStyle;
ctx.globalAlpha = oldAlpha;
}
};