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
|
if(typeof require !== "undefined") { var jaws = require("./core.js"); }
/**
* @fileOverview Collision Detection
*
* Collision detection helpers.
*
* @example
* // collision helper exampels:
* collideOneWithOne(player, boss) // -> false
* collideOneWithMany(player, bullets) // -> [bullet1, bullet1]
* collideManyWithMany(bullets, enemies) // -> [ [bullet1, enemy1], [bullet2, enemy2] ]
* collide(player, boss) // -> false
* collide(player,
* bullets,
* function(player, bullet) {}) // Callback: arguments[0] -> player
* // arguments[1] -> bullets[i]
*
*/
var jaws = (function(jaws) {
/**
* Collides two objects by reading x, y and either method rect() or property radius.
* @public
* @param {object} object1 An object with a 'radius' or 'rect' property
* @param {object} object2 An object with a 'radius' or 'rect' property
* @returns {boolean} If the two objects are colliding or not
*/
jaws.collideOneWithOne = function(object1, object2) {
if (object1.radius && object2.radius && object1 !== object2 && jaws.collideCircles(object1, object2))
return true;
if (object1.rect && object2.rect && object1 !== object2 && jaws.collideRects(object1.rect(), object2.rect()))
return true;
return false;
};
/**
* Compares an object against a list, returning those from list that collide with object, and
* calling 'callback' per collision (if set) with object and item from list.
* (Note: Will never collide objects with themselves.)
* @public
* @param {object} object An object with a 'radius' or 'rect' property
* @param {array|object} list A collection of objects with a 'length' property
* @param {function} callback The function to be called per collison detected
* @returns {array} A collection of items colliding with object from list
* @example
* collideOneWithMany(player, bullets) // -> [bullet1, bullet1]
* collideOneWithMany(player, bullets, function(player, bullet) {
* //player and bullet (bullets[i])
* });
*/
jaws.collideOneWithMany = function(object, list, callback) {
var a = [];
if (callback) {
for (var i = 0; i < list.length; i++) {
if (jaws.collideOneWithOne(object, list[i])) {
callback(object, list[i]);
a.push(list[i])
}
}
return a;
}
else {
return list.filter(function(item) {
return jaws.collideOneWithOne(object, item);
});
}
};
/**
* Compares two lists, returning those items from each that collide with each other, and
* calling 'callback' per collision (if set) with item from list1 and item from list2.
* (Note: Will never collide objects with themselves.)
* @public
* @param {array|object} list1 A collection of objects with a 'forEach' property
* @param {array|object} list2 A collection of objects with a 'forEach' property
* @param {function} callback The function to be called per collison detected
* @returns {array} A collection of items colliding with list1 from list2
* @example
* jaws.collideManyWithMany(bullets, enemies) // --> [[bullet, enemy], [bullet, enemy]]
*/
jaws.collideManyWithMany = function(list1, list2, callback) {
var a = [];
if (list1 === list2) {
combinations(list1, 2).forEach(function(pair) {
if (jaws.collideOneWithOne(pair[0], pair[1])) {
if (callback) {
callback(pair[0], pair[1]);
}
else {
a.push([pair[0], pair[1]]);
}
}
});
}
else {
list1.forEach(function(item1) {
list2.forEach(function(item2) {
if (jaws.collideOneWithOne(item1, item2)) {
if (callback) {
callback(item1, item2);
}
else {
a.push([item1, item2]);
}
}
});
});
}
return a;
};
/**
* Returns if two circle-objects collide with each other
* @public
* @param {object} object1 An object with a 'radius' property
* @param {object} object2 An object with a 'radius' property
* @returns {boolean} If two circle-objects collide or not
*/
jaws.collideCircles = function(object1, object2) {
return (jaws.distanceBetween(object1, object2) < object1.radius + object2.radius);
};
/**
* Returns if two Rects collide with each other or not
* @public
* @param {object} rect1 An object with 'x', 'y', 'right' and 'bottom' properties
* @param {object} rect2 An object with 'x', 'y', 'right' and 'bottom' properties
* @returns {boolean} If two Rects collide with each other or not
*/
jaws.collideRects = function(rect1, rect2) {
return ((rect1.x >= rect2.x && rect1.x <= rect2.right) || (rect2.x >= rect1.x && rect2.x <= rect1.right)) &&
((rect1.y >= rect2.y && rect1.y <= rect2.bottom) || (rect2.y >= rect1.y && rect2.y <= rect1.bottom));
};
/**
* Returns the distance between two objects
* @public
* @param {object} object1 An object with 'x' and 'y' properties
* @param {object} object2 An object with 'x' and 'y' properties
* @returns {number} The distance between two objects
*/
jaws.distanceBetween = function(object1, object2) {
return Math.sqrt(Math.pow(object1.x - object2.x, 2) + Math.pow(object1.y - object2.y, 2));
};
/**
* Creates combinations of items from a list of a specific size
* @private
* @param {array|object} list An object with a 'length' property
* @param {number} n The size of the array to return
* @returns {Array} An array of items having a specific size number of its own entries
*/
function combinations(list, n) {
var f = function(i) {
if (list.isSpriteList !== undefined) {
return list.at(i);
} else { // s is an Array
return list[i];
}
};
var r = [];
var m = new Array(n);
for (var i = 0; i < n; i++)
m[i] = i;
for (var i = n - 1, sn = list.length; 0 <= i; sn = list.length) {
r.push(m.map(f));
while (0 <= i && m[i] === sn - 1) {
i--;
sn--;
}
if (0 <= i) {
m[i] += 1;
for (var j = i + 1; j < n; j++)
m[j] = m[j - 1] + 1;
i = n - 1;
}
}
return r;
}
/**
* If an object has items or not
* @private
* @param {array|object} array An object with a 'length' property
* @returns {boolean} If the object has items (length > 0)
*/
function hasItems(array) {
return (array && array.length > 0);
}
/**
* Compares two objects or lists, returning if they collide, and
* calling 'callback' per collision (if set) between objects or lists.
* @param {array|object} x An object with either 'rect' or 'forEach' property
* @param {array|object} x2 An object with either 'rect' or 'forEach' property
* @param {function} callback
* @returns {boolean}
* @examples
* jaws.collide(player, enemy, function(player, enemy) { ... } )
* jaws.collide(player, enemies, function(player, enemy) { ... } )
* jaws.collide(bullets, enemies, function(bullet, enemy) { ... } )
*/
jaws.collide = function(x, x2, callback) {
if ((x.rect || x.radius) && x2.forEach)
return (jaws.collideOneWithMany(x, x2, callback).length > 0);
if (x.forEach && x2.forEach)
return (jaws.collideManyWithMany(x, x2, callback).length > 0);
if (x.forEach && (x2.rect || x2.radius))
return (jaws.collideOneWithMany(x2, x, callback).length > 0);
if ((x.rect && x2.rect) || (x.radius && x2.radius)) {
var result = jaws.collideOneWithOne(x, x2);
if (callback && result)
callback(x, x2);
else
return result;
}
};
return jaws;
})(jaws || {});
|