|
4 | 4 | return Object.prototype.toString.call(a) === "[object Array]"; |
5 | 5 | }; |
6 | 6 |
|
| 7 | + var defined = function(a) { |
| 8 | + return a !== undef; |
| 9 | + }; |
| 10 | + |
7 | 11 | function Vec2(x, y) { |
8 | 12 | if (!(this instanceof Vec2)) { |
9 | 13 | return new Vec2(x, y); |
|
23 | 27 |
|
24 | 28 | Vec2.prototype = { |
25 | 29 | change : function(fn) { |
26 | | - if (fn) { |
| 30 | + if (typeof fn === 'function') { |
27 | 31 | if (this.observers) { |
28 | 32 | this.observers.push(fn); |
29 | 33 | } else { |
30 | 34 | this.observers = [fn]; |
31 | 35 | } |
32 | | - } else if (this.observers) { |
| 36 | + } else if (this.observers && this.observers.length) { |
33 | 37 | for (var i=this.observers.length-1; i>=0; i--) { |
34 | | - this.observers[i](this); |
| 38 | + this.observers[i](this, fn); |
35 | 39 | } |
36 | 40 | } |
37 | 41 |
|
|
40 | 44 |
|
41 | 45 | ignore : function(fn) { |
42 | 46 | 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 | + } |
46 | 54 | } |
47 | 55 | } |
48 | 56 | return this; |
49 | 57 | }, |
50 | 58 |
|
51 | 59 | // set x and y |
52 | | - set: function(x, y, silent) { |
| 60 | + set: function(x, y, notify) { |
53 | 61 | if('number' != typeof x) { |
54 | | - silent = y; |
| 62 | + notify = y; |
55 | 63 | y = x.y; |
56 | 64 | x = x.x; |
57 | 65 | } |
|
60 | 68 | return this; |
61 | 69 | } |
62 | 70 |
|
| 71 | + var orig = null; |
| 72 | + if (notify !== false && this.observers && this.observers.length) { |
| 73 | + orig = this.clone(); |
| 74 | + } |
| 75 | + |
63 | 76 | this.x = Vec2.clean(x); |
64 | 77 | this.y = Vec2.clean(y); |
65 | 78 |
|
66 | | - if(silent !== false) { |
67 | | - return this.change(); |
| 79 | + if(notify !== false) { |
| 80 | + return this.change(orig); |
68 | 81 | } |
69 | 82 | }, |
70 | 83 |
|
|
89 | 102 | }, |
90 | 103 |
|
91 | 104 | // 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 | + |
93 | 122 | if (!returnNew) { |
94 | | - this.x += vec2.x; this.y += vec2.y; |
95 | | - return this.change(); |
| 123 | + return this.set(x, y); |
96 | 124 | } else { |
97 | 125 | // 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); |
102 | 127 | } |
103 | 128 | }, |
104 | 129 |
|
105 | 130 | // 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 | + |
107 | 146 | if (!returnNew) { |
108 | | - this.x -= vec2.x; this.y -= vec2.y; |
109 | | - return this.change(); |
| 147 | + return this.set(x, y); |
110 | 148 | } else { |
111 | 149 | // 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); |
116 | 151 | } |
117 | 152 | }, |
118 | 153 |
|
119 | 154 | // 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; |
129 | 168 | } |
130 | 169 |
|
| 170 | + x *= this.x; |
| 171 | + y *= this.y; |
| 172 | + |
131 | 173 | if (!returnNew) { |
132 | | - return this.set(this.x * x, this.y * y); |
| 174 | + return this.set(x, y); |
133 | 175 | } else { |
134 | | - return new (this.constructor)( |
135 | | - this.x * x, |
136 | | - this.y * y |
137 | | - ); |
| 176 | + return new (this.constructor)(x, y); |
138 | 177 | } |
139 | 178 | }, |
140 | 179 |
|
|
207 | 246 | // Determine if another `Vec2`'s components match this one's |
208 | 247 | // also accepts 2 scalars |
209 | 248 | 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 | + } |
213 | 257 | } |
214 | 258 |
|
215 | 259 | return (Vec2.clean(v) === this.x && Vec2.clean(w) === this.y); |
|
289 | 333 |
|
290 | 334 | // Perform linear interpolation between two vectors |
291 | 335 | // 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); |
294 | 338 | }, |
295 | 339 |
|
296 | 340 | // 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 | + } |
300 | 347 | }, |
301 | 348 |
|
302 | 349 | // calculate the dot product between |
|
317 | 364 | }, |
318 | 365 |
|
319 | 366 | // 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; |
329 | 380 | } |
330 | 381 |
|
331 | 382 | if (x === 0 || y === 0) { |
|
0 commit comments