-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
140 lines (120 loc) · 2.88 KB
/
util.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
//tók bara allt saman, við tökum bara út það sem við erum ekki að nota
// 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;
},*/
// Check whether there is a collision
areColliding: function(
a_x,
a_y,
a_width,
a_height,
b_x,
b_y,
b_width,
b_height
) {
return this.areIntersecting(
a_x,
a_x + a_width,
a_y,
a_y + a_height,
b_x,
b_x + b_width,
b_y,
b_y + b_height
);
},
// Check whether two boxes are intersecting
areIntersecting: function(a_x1, a_x2, a_y1, a_y2, b_x1, b_x2, b_y1, b_y2) {
return a_x1 < b_x2 && a_x2 > b_x1 && (a_y1 < b_y2 && a_y2 > b_y1);
},
// 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
// ==========
//Add the redraw of the background picture
clearBackgroundCanvas: function(ctx) {
var prevfillStyle = ctx.fillStyle;
g_background.draw(ctx);
ctx.fillStyle = prevfillStyle;
},
//add the redraw of the forground picture
clearCanvas: function(ctx) {
/*var prevfillStyle = ctx.fillStyle;
ctx.fillStyle = "rgb(255, 255,224)";
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) {
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;
},*/
};