Skip to content

Commit 782e2bc

Browse files
committed
use bower to manage quadtree and vec
1 parent 51b9968 commit 782e2bc

File tree

5 files changed

+119
-62
lines changed

5 files changed

+119
-62
lines changed

Gruntfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ module.exports = function(grunt) {
3131
main : {
3232
files: [
3333
{
34-
src: 'node_modules/quadtree2/quadtree2.js',
34+
src: 'bower_components/quadtree2/quadtree2.js',
3535
dest: 'include/quadtree2.js'
3636
},
3737
{
38-
src: 'node_modules/quadtree2/node_modules/vec2/vec2.js',
38+
src: 'bower_components/vec2/vec2.js',
3939
dest: 'include/vec2.js'
4040
}]
4141
}

bower.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
],
3737
"dependencies":{
3838
"threejs": "r66",
39-
"TweenJS": "release_v0.5.1"
39+
"TweenJS": "release_v0.5.1",
40+
"quadtree2": "burninggramma/quadtree2.js",
41+
"vec2": "tmpvar/vec2.js"
4042
}
4143
}

include/quadtree2.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (c) 2013-2014 burninggramma
55
* https://github.com/burninggramma/quadtree2.js
66
*
7-
* Compiled: 2014-03-21
7+
* Compiled: 2014-03-28
88
*
99
* quadtree2 is licensed under the MIT License.
1010
* http://www.opensource.org/licenses/mit-license.php
@@ -273,7 +273,7 @@
273273
}
274274
},
275275
updateObjectQuadrants: function(a) {
276-
var b, c = i.quadrants_[a[k.id]], d = m.getSmallestIntersectingQuadrants(a), f = Object.keys(c), g = Object.keys(d), h = e.arrayDiffs(f, g), j = h[0], l = h[1];
276+
var b, c = i.quadrants_[a[k.id]], d = m.getSmallestIntersectingQuadrants(a), f = e.getIdsOfObjects(c), g = e.getIdsOfObjects(d), h = e.arrayDiffs(f, g), j = h[0], l = h[1];
277277
for (b = 0; b < l.length; b++) m.populateSubtree(a, d[l[b]]);
278278
for (b = 0; b < j.length; b++) c[j[b]] && m.removeObjectFromQuadrant(a, c[j[b]]);
279279
},
@@ -431,6 +431,11 @@
431431
var d = a;
432432
throw c && (d += "_" + c), b && (d += " - "), b && c && (d += c + ": "), b && (d += b), new Error(d);
433433
},
434+
getIdsOfObjects: function(a) {
435+
var b = [];
436+
for (var c in a) b.push(a[c].id_);
437+
return b;
438+
},
434439
compare: function(a, b) {
435440
return a - b;
436441
},

include/vec2.js

Lines changed: 106 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
return Object.prototype.toString.call(a) === "[object Array]";
55
};
66

7+
var defined = function(a) {
8+
return a !== undef;
9+
};
10+
711
function Vec2(x, y) {
812
if (!(this instanceof Vec2)) {
913
return new Vec2(x, y);
@@ -23,15 +27,15 @@
2327

2428
Vec2.prototype = {
2529
change : function(fn) {
26-
if (fn) {
30+
if (typeof fn === 'function') {
2731
if (this.observers) {
2832
this.observers.push(fn);
2933
} else {
3034
this.observers = [fn];
3135
}
32-
} else if (this.observers) {
36+
} else if (this.observers && this.observers.length) {
3337
for (var i=this.observers.length-1; i>=0; i--) {
34-
this.observers[i](this);
38+
this.observers[i](this, fn);
3539
}
3640
}
3741

@@ -40,18 +44,22 @@
4044

4145
ignore : function(fn) {
4246
if (this.observers) {
43-
var o = this.observers, l = o.length;
44-
while(l--) {
45-
o[l] === fn && o.splice(l, 1);
47+
if (!fn) {
48+
this.observers = [];
49+
} else {
50+
var o = this.observers, l = o.length;
51+
while(l--) {
52+
o[l] === fn && o.splice(l, 1);
53+
}
4654
}
4755
}
4856
return this;
4957
},
5058

5159
// set x and y
52-
set: function(x, y, silent) {
60+
set: function(x, y, notify) {
5361
if('number' != typeof x) {
54-
silent = y;
62+
notify = y;
5563
y = x.y;
5664
x = x.x;
5765
}
@@ -60,11 +68,16 @@
6068
return this;
6169
}
6270

71+
var orig = null;
72+
if (notify !== false && this.observers && this.observers.length) {
73+
orig = this.clone();
74+
}
75+
6376
this.x = Vec2.clean(x);
6477
this.y = Vec2.clean(y);
6578

66-
if(silent !== false) {
67-
return this.change();
79+
if(notify !== false) {
80+
return this.change(orig);
6881
}
6982
},
7083

@@ -89,52 +102,78 @@
89102
},
90103

91104
// Add the incoming `vec2` vector to this vector
92-
add : function(vec2, returnNew) {
105+
add : function(x, y, returnNew) {
106+
107+
if (typeof x != 'number') {
108+
returnNew = y;
109+
if (isArray(x)) {
110+
y = x[1];
111+
x = x[0];
112+
} else {
113+
y = x.y;
114+
x = x.x;
115+
}
116+
}
117+
118+
x += this.x;
119+
y += this.y;
120+
121+
93122
if (!returnNew) {
94-
this.x += vec2.x; this.y += vec2.y;
95-
return this.change();
123+
return this.set(x, y);
96124
} else {
97125
// Return a new vector if `returnNew` is truthy
98-
return new (this.constructor)(
99-
this.x + vec2.x,
100-
this.y + vec2.y
101-
);
126+
return new (this.constructor)(x, y);
102127
}
103128
},
104129

105130
// Subtract the incoming `vec2` from this vector
106-
subtract : function(vec2, returnNew) {
131+
subtract : function(x, y, returnNew) {
132+
if (typeof x != 'number') {
133+
returnNew = y;
134+
if (isArray(x)) {
135+
y = x[1];
136+
x = x[0];
137+
} else {
138+
y = x.y;
139+
x = x.x;
140+
}
141+
}
142+
143+
x = this.x - x;
144+
y = this.y - y;
145+
107146
if (!returnNew) {
108-
this.x -= vec2.x; this.y -= vec2.y;
109-
return this.change();
147+
return this.set(x, y);
110148
} else {
111149
// Return a new vector if `returnNew` is truthy
112-
return new (this.constructor)(
113-
this.x - vec2.x,
114-
this.y - vec2.y
115-
);
150+
return new (this.constructor)(x, y);
116151
}
117152
},
118153

119154
// Multiply this vector by the incoming `vec2`
120-
multiply : function(vec2, returnNew) {
121-
var x,y;
122-
if ('number' !== typeof vec2) { //.x !== undef) {
123-
x = vec2.x;
124-
y = vec2.y;
125-
126-
// Handle incoming scalars
127-
} else {
128-
x = y = vec2;
155+
multiply : function(x, y, returnNew) {
156+
if (typeof x != 'number') {
157+
returnNew = y;
158+
if (isArray(x)) {
159+
y = x[1];
160+
x = x[0];
161+
} else {
162+
y = x.y;
163+
x = x.x;
164+
}
165+
} else if (typeof y != 'number') {
166+
returnNew = y;
167+
y = x;
129168
}
130169

170+
x *= this.x;
171+
y *= this.y;
172+
131173
if (!returnNew) {
132-
return this.set(this.x * x, this.y * y);
174+
return this.set(x, y);
133175
} else {
134-
return new (this.constructor)(
135-
this.x * x,
136-
this.y * y
137-
);
176+
return new (this.constructor)(x, y);
138177
}
139178
},
140179

@@ -207,9 +246,14 @@
207246
// Determine if another `Vec2`'s components match this one's
208247
// also accepts 2 scalars
209248
equal : function(v, w) {
210-
if (w === undef) {
211-
w = v.y;
212-
v = v.x;
249+
if (typeof v != 'number') {
250+
if (isArray(v)) {
251+
w = v[1];
252+
v = v[0];
253+
} else {
254+
w = v.y;
255+
v = v.x;
256+
}
213257
}
214258

215259
return (Vec2.clean(v) === this.x && Vec2.clean(w) === this.y);
@@ -289,14 +333,17 @@
289333

290334
// Perform linear interpolation between two vectors
291335
// amount is a decimal between 0 and 1
292-
lerp : function(vec, amount) {
293-
return this.add(vec.subtract(this, true).multiply(amount), true);
336+
lerp : function(vec, amount, returnNew) {
337+
return this.add(vec.subtract(this, true).multiply(amount), returnNew);
294338
},
295339

296340
// Get the skew vector such that dot(skew_vec, other) == cross(vec, other)
297-
skew : function() {
298-
// Returns a new vector.
299-
return new (this.constructor)(-this.y, this.x);
341+
skew : function(returnNew) {
342+
if (!returnNew) {
343+
return this.set(-this.y, this.x)
344+
} else {
345+
return new (this.constructor)(-this.y, this.x);
346+
}
300347
},
301348

302349
// calculate the dot product between
@@ -317,15 +364,19 @@
317364
},
318365

319366
// Divide this vector's components by a scalar
320-
divide : function(vec2, returnNew) {
321-
var x,y;
322-
if ('number' !== typeof vec2) {
323-
x = vec2.x;
324-
y = vec2.y;
325-
326-
// Handle incoming scalars
327-
} else {
328-
x = y = vec2;
367+
divide : function(x, y, returnNew) {
368+
if (typeof x != 'number') {
369+
returnNew = y;
370+
if (isArray(x)) {
371+
y = x[1];
372+
x = x[0];
373+
} else {
374+
y = x.y;
375+
x = x.x;
376+
}
377+
} else if (typeof y != 'number') {
378+
returnNew = y;
379+
y = x;
329380
}
330381

331382
if (x === 0 || y === 0) {

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"main": "encom-globe.js",
66
"dependencies": {
77
"grunt": "~0.4.2",
8-
"quadtree2": "~0.5.2"
98
},
109
"devDependencies": {
1110
"grunt": "~0.4.2",
@@ -18,7 +17,7 @@
1817
},
1918
"repository": {
2019
"type": "git",
21-
"url": "git://github.com//arscan/encom-globe.git"
20+
"url": "git://github.com/arscan/encom-globe.git"
2221
},
2322
"author": "robscanlon@gmail.com",
2423
"license": "MIT"

0 commit comments

Comments
 (0)