1 var jaws = (function(jaws) {
  2 
  3 /*
  4 * 2013-09-28:
  5 *
  6 * For a 10x10 sprite in the topleft corner, should sprite.rect().bottom be 9 or 10?
  7 * There's no right or wrong answer. In some cases 9 makes sense (if checking directly for pixel-values for example).
  8 * In other cases 10 makes sense (bottom = x + height).
  9 *
 10 * The important part is beeing consistent across the lib/game.
 11 * Jaws started out with bottom = x + height so we'll continue with that way until good reasons to change come up.
 12 * Therefore correction = 0 for now.
 13 */
 14 var correction = 0;
 15 
 16 /**
 17   @class A Basic rectangle.
 18   @example
 19   rect = new jaws.Rect(5,5,20,20)
 20   rect.right  // -> 25
 21   rect.bottom // -> 25
 22   rect.move(10,20)
 23   rect.right  // -> 35
 24   rect.bottom // -> 45
 25   rect.width  // -> 20
 26   rect.height // -> 20
 27 */
 28 jaws.Rect = function Rect(x, y, width, height) {
 29   if( !(this instanceof arguments.callee) ) return new arguments.callee(x, y, width, height);
 30   
 31   this.x = x
 32   this.y = y
 33   this.width = width
 34   this.height = height
 35   this.right = x + width - correction
 36   this.bottom = y + height - correction
 37 }
 38 
 39 /** Return position as [x,y] */
 40 jaws.Rect.prototype.getPosition = function() {
 41   return [this.x, this.y]
 42 }
 43 
 44 /** Move rect x pixels horizontally and y pixels vertically */
 45 jaws.Rect.prototype.move = function(x, y) {
 46   this.x += x
 47   this.y += y
 48   this.right += x
 49   this.bottom += y
 50   return this
 51 }
 52 
 53 /** Set rects x/y */
 54 jaws.Rect.prototype.moveTo = function(x, y) {
 55   this.x = x
 56   this.y = y
 57   this.right = this.x + this.width - correction
 58   this.bottom = this.y + this.height - correction
 59   return this
 60 }
 61 /** Modify width and height */
 62 jaws.Rect.prototype.resize = function(width, height) {
 63   this.width += width
 64   this.height += height
 65   this.right = this.x + this.width - correction
 66   this.bottom = this.y + this.height - correction
 67   return this
 68 }
 69 
 70 /** Returns a new rect witht he same dimensions */
 71 jaws.Rect.prototype.clone = function() {
 72   return new jaws.Rect(this.x, this.y, this.width, this.height)
 73 }
 74 
 75 /** Shrink rectangle on both axis with given x/y values  */
 76 jaws.Rect.prototype.shrink = function(x, y) {
 77   this.x += x
 78   this.y += y
 79   this.width -= (x+x)
 80   this.height -= (y+y)
 81   this.right = this.x + this.width - correction
 82   this.bottom = this.y + this.height - correction
 83   return this
 84 }
 85 
 86 /** Set width and height */
 87 jaws.Rect.prototype.resizeTo = function(width, height) {
 88   this.width = width
 89   this.height = height
 90   this.right = this.x + this.width - correction
 91   this.bottom = this.y + this.height - correction
 92   return this
 93 }
 94 
 95 /** Draw rect in color red, useful for debugging */
 96 jaws.Rect.prototype.draw = function() {
 97   jaws.context.strokeStyle = "red"
 98   jaws.context.strokeRect(this.x-0.5, this.y-0.5, this.width, this.height)
 99   return this
100 }
101 
102 /** Returns true if point at x, y lies within calling rect */
103 jaws.Rect.prototype.collidePoint = function(x, y) {
104   return (x >= this.x && x <= this.right && y >= this.y && y <= this.bottom)
105 }
106 
107 /** Returns true if calling rect overlaps with given rect in any way */
108 jaws.Rect.prototype.collideRect = function(rect) {
109   return ((this.x >= rect.x && this.x <= rect.right) || (rect.x >= this.x && rect.x <= this.right)) &&
110          ((this.y >= rect.y && this.y <= rect.bottom) || (rect.y >= this.y && rect.y <= this.bottom))
111 }
112 
113 /*
114 // Possible future functions
115 jaws.Rect.prototype.collideRightSide = function(rect)  { return(this.right >= rect.x && this.x < rect.x) }
116 jaws.Rect.prototype.collideLeftSide = function(rect)   { return(this.x > rect.x && this.x <= rect.right) }
117 jaws.Rect.prototype.collideTopSide = function(rect)    { return(this.y >= rect.y && this.y <= rect.bottom) }
118 jaws.Rect.prototype.collideBottomSide = function(rect) { return(this.bottom >= rect.y && this.y < rect.y) }
119 */
120 
121 jaws.Rect.prototype.toString = function() { return "[Rect " + this.x + ", " + this.y + ", " + this.width + ", " + this.height + "]" }
122 
123 return jaws;
124 })(jaws || {});
125 
126 // Support CommonJS require()
127 if(typeof module !== "undefined" && ('exports' in module)) { module.exports = jaws.Rect }
128 
129