| 1 | /** |
| 2 | * Extending exising functionality |
| 3 | */ |
| 4 | |
| 5 | Array.prototype.remove = function(element) |
| 6 | { |
| 7 | var index = this.indexOf(element); |
| 8 | |
| 9 | if (index > -1) |
| 10 | { |
| 11 | this.splice(index, 1); |
| 12 | } |
| 13 | else |
| 14 | { |
| 15 | console.error("Element not part of array!", this, element); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | Array.prototype.removeAt = function(index) |
| 20 | { |
| 21 | if (index > -1) |
| 22 | { |
| 23 | this.splice(index, 1); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | Array.prototype.contains = function(element) |
| 28 | { |
| 29 | var index = this.indexOf(element); |
| 30 | |
| 31 | return index > -1; |
| 32 | } |
| 33 | |
| 34 | Math.clamp = function(number, min, max) |
| 35 | { |
| 36 | return Math.min(max, Math.max(min, number)); |
| 37 | } |
| 38 | |
| 39 | Math.randomRange = function(min, max) |
| 40 | { |
| 41 | return min + (Math.random() * (max - min)); |
| 42 | } |
| 43 | |
| 44 | Array.prototype.random = function () |
| 45 | { |
| 46 | return this[Math.floor((Math.random()*this.length))]; |
| 47 | } |