
// surf2.js , three waves at once 
// for use with bezf.html, render.js,
// requestAnimationFrame.js and Tile99.png 
// Calculates Bezier control points for 
// waves (each has 7).
function surf(waves)
{
	this.waves = waves;
	this.getWaveCount = function() {
		return this.waves.length/2;    //
	};
	this.getWaves = function() {
		return this.waves;
	};
 
	// Right to Left
	this.updateStageObjects = function () {
		var nwaves = this.getWaveCount();
		alpha = 0.15  ;
		mot  = 3      ;        
		adjc = -1     ;
		var m ;
		var dumtoo = lmw ;
		//     rotate(angle alpha, Wave wv) { //, Point[] pts){
		//  alpha was 5 degrees in radians pi * r = 180
		//                    pi /180 * 5 = alpha
						   var rndup =  0.55  ;
				 //          var alpha =  0.15 ; //  0.09 ;
							var dx2, dy2;
							var hyp, rad;
							var x1, y1;
							var adjh = 5  ; 

	for(var j = 0 ; j < nwaves ; j = j + 1, dumtoo = dumtoo + 2 ){
			if(dumtoo > 3 ){
			   dumtoo = 0 ;                 }
			   m = dumtoo + 1 ;
					 // Wave's center of rotation
							 //    m = m + 2 ;
					x1 = waves[dumtoo][3] ;
					y1 = ((waves[m][0] +waves[m][2])/2) ;

			for(var i = 2; i < 5; i++) {
						dx2 = waves[dumtoo][i] - x1 ;
						dy2 = waves[m][i] - y1 ;
			  hyp = Math.sqrt((dx2 * dx2) + (dy2 * dy2) + rndup ) ;
				  rad = Math.atan2(dy2, dx2);     // atan2
						rad = rad - alpha;
						dx2 = hyp * Math.cos(rad) + rndup ;
						dy2 = hyp * Math.sin(rad) + rndup ;
						waves[dumtoo][i] = dx2 + rndup;
						waves[dumtoo][i] += x1 ;
						waves[m][i] = dy2 + rndup;
						waves[m][i] += y1 ;
					/*   adjust height  */
				   if((200 -waves[m][3])<75 ){
						waves[m][2] -= adjh ;
						waves[m][3] -= adjh ;
						waves[m][4] -= adjh ;             }

				  waves[dumtoo][0] -= mot  ;
				  waves[dumtoo][1] -= mot  ;
				  waves[dumtoo][2] -= (mot + adjc)  ;
				  waves[dumtoo][3] -= mot  ;
				  waves[dumtoo][4] -= mot  ;
				  waves[dumtoo][5] -= mot  ;
				  waves[dumtoo][6] -= mot  ; 
			} // for(var i  loop ends here
			if(waves[dumtoo][0] < 10 ){     //  < 10    ?
				waves[dumtoo] =
				[ 460,  430, 380, 380, 380, 330, 300 ];
				waves[m] =
				[ 200 , 200, 160, 100, 160, 200, 200 ] ;
				lmw =  lmw + 2 ;
				if(lmw > 3 ){
				  lmw   = 0 ;
				  dumtoo = -2 ; 
				}
			}  // if(waves[dumtoo]
		} // for(var j loop ends here
	}; // updateStageObjects
	 // left to right
	this.updateSO = function () {
		var nwaves = this.getWaveCount();
		alpha =  6.16 ; 
		mot = -3    ;        
		adjc = 1    ;
		// lmw = 0 ;
		var m ;
		var dumtoo = lmw ;
		var rndup =  0.55  ;
		//          var alpha =  0.15 ; //  0.09 ;
		var dx2, dy2;
		var hyp ;
		var rad = 0  ; //= 6.25 ;
		var x1, y1;
		//           var rlMot = 6 ; // was 5 new - l to r motion
		var adjh = 5  ; 
	for(var j = 0 ; j < nwaves ; j = j + 1, dumtoo = dumtoo + 2 ){
			if(dumtoo > 3 ){
			   dumtoo = 0 ;                 }
			   m = dumtoo + 1 ;
					 // Wave's center of rotation
					 //    m = m + 2 ;
					x1 =   waves[dumtoo][1] ;    // [3] ;
					y1 =   waves[m][3] ; //[2])/2) ;

				for(var i = 2; i < 5; i++) {
					dx2 = waves[dumtoo][i] - x1 ;
					dy2 = waves[m][i] - y1 ;
		  hyp = Math.sqrt((dx2 * dx2) + (dy2 * dy2) + rndup ) ;
				  rad = Math.atan2(dy2, dx2);     // atan2
				  rad = rad - 0.10 ;  // was - alpha
				  dx2 = hyp * Math.cos(rad) + rndup ;
				  dy2 = hyp * Math.sin(rad) + rndup ;
				waves[dumtoo][i] = dx2 + rndup;
				waves[dumtoo][i] += x1 ;
				waves[m][i] = dy2 + rndup;
				waves[m][i] += y1 ;
					/*   adjust height  */
				   /*if((200 -waves[m][3])<75 ){
						waves[m][2] -= adjh ;
						waves[m][3] -= adjh ;
						waves[m][4] -= adjh ;              }  */

				  waves[dumtoo][0] -= mot  ;
				  waves[dumtoo][1] -= mot  ;
				  waves[dumtoo][2] -= (mot + adjc)  ;
				  waves[dumtoo][3] -= mot  ;
				  waves[dumtoo][4] -= mot  ;
				  waves[dumtoo][5] -= mot  ;
				  waves[dumtoo][6] -= mot  ; 
						} // for(var i  loop ends here
			if(waves[dumtoo][0] > 380 ){     //  < 10    ?
				waves[dumtoo] =
				//   [ 20,  50, 80, 80, 80, 110, 140 ];
				   [140, 110, 80, 80, 80,  50,  20 ];
				waves[m] =
				[ 200 , 200, 160, 100, 160, 200, 200 ] ;
				lmw =  lmw + 2 ;
				if(lmw > 3 ){
					lmw   = 0 ;
					dumtoo = -2 ; 
				}
			}  // if(waves[dumtoo]
		} // for(var j loop ends here
	};  // updateSO()  Left to Right
};  // surf class
