1 /**
  2  * The big red button
  3  * @param {BB_Sprite} sprite
  4  * @param {Number} x
  5  * @param {Number} y
  6  * @constructor
  7  */
  8 function BB_Ball(sprite, x, y) {
  9 	this.sprite = sprite;
 10 	this.x = x || 0;
 11 	this.y = y || 0;
 12 	this.r = 20;
 13 	this.a = 0;
 14 	this.l = .3;
 15 	this.s = .985;
 16 	this.t = null;
 17 	this.v = null;
 18 	this.o = {x:this.x, y:this.y};
 19 }
 20 
 21 /**
 22  * Extends CP_Item
 23  */
 24 BB_Ball.prototype = new CP_Item(40, 40);
 25 
 26 /**
 27  * Draw the button
 28  */
 29 BB_Ball.prototype.paint = function(ctx) {
 30 	this.sprite.paint(ctx, this.x, this.y, this.w, this.h, Math.round(this.a) * 40, this.t ? 40 : 0);
 31 	if (this.t) {
 32 		ctx.save();
 33 		ctx.strokeStyle = '#ddd';
 34 		ctx.lineCap = 'round';
 35 		ctx.lineWidth = 3;
 36 		ctx.beginPath();
 37 		ctx.moveTo(this.ox(), this.oy());
 38 		ctx.lineTo(this.t.x, this.t.y);
 39 		ctx.stroke();
 40 		ctx.restore();
 41 	}
 42 };
 43 
 44 /**
 45  * Animation thread
 46  */
 47 BB_Ball.prototype.run = function() {
 48 	if (this.v) {
 49 		this.x += this.v.x;
 50 		this.y += this.v.y;
 51 		this.v.x *= this.s;
 52 		this.v.y *= this.s;
 53 		this.v.d *= this.s;
 54 		var a = this.v.d * this.l;
 55 		this.a += a > 2 ? 2 : a;
 56 		if (this.a < 0) this.a = 11;
 57 		if (this.a > 11) this.a = 0;
 58 		if (this.v.d < 0.05) {
 59 			this.v = null;
 60 		}
 61 		return true;
 62 	}
 63 	return false;
 64 };
 65 
 66 /**
 67  * Stop the animation
 68  */
 69 BB_Ball.prototype.stop = function() {
 70 	if (this.v) {
 71 		this.v = {x:0,y:0,d:0};
 72 	}
 73 };
 74 
 75 /**
 76  * Check bouncs point
 77  * @param {Number} x
 78  * @param {Number} y
 79  * @param {Boolean} bounce
 80  * @returns {Boolean}
 81  */
 82 BB_Ball.prototype.check = function(x, y, bounce) {
 83 	var vx = this.ox() - x,
 84 		vy = this.oy() - y,
 85 		d = Math.sqrt((vx * vx) + (vy * vy)),
 86 		result = d <= this.r;
 87 	if (result && bounce) {
 88 		this.ox(vx / d * this.r + x);
 89 		this.oy(vy / d * this.r + y);
 90 		if (this.v) {
 91 			var a = this.v.d / d;
 92 			this.v.x = vx * a;
 93 			this.v.y = vy * a;
 94 		}
 95 	}
 96 	return result;
 97 };
 98 
 99 /**
100  * Set button bounce vector
101  * @param {Number} vx
102  * @param {Number} vy
103  */
104 BB_Ball.prototype.bounce = function(vx, vy) {
105 	if (this.v) {
106 		this.v.x *= vx;
107 		this.v.y *= vy;
108 		this.l = -this.l;
109 	}
110 };
111 
112 /**
113  * Stop he button and set the line end
114  * @param {Number} x
115  * @param {Number} y
116  */
117 BB_Ball.prototype.hold = function(x, y) {
118 	this.t = {x:x, y:y};
119 	this.v = null;
120 };
121 
122 /**
123  * Start button animation
124  * @param {Number} x
125  * @param {Number} y
126  */
127 BB_Ball.prototype.shot = function(x, y) {
128 	var vx = x - this.ox(),
129 		vy = y - this.oy(),
130 		d = Math.sqrt((vx * vx) + (vy * vy)),
131 		a = d / 20 * (250 / d);
132 	this.v = {x: vx/a, y: vy/a, d:d/a};
133 	this.t = null;
134 	this.l = -this.l;
135 };
136 
137 /**
138  * Reset button position to default values
139  * @param {Number} x
140  * @param {Number} y
141  */
142 BB_Ball.prototype.reset = function(x, y) {
143 	this.x = this.o.x;
144 	this.y = this.o.y;
145 	this.a = 0;
146 	this.v = null;
147 	this.t = null;
148 };