function wait(w) {
	loop(w){yield;}
}

function angleToPlayer {
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}

 function WaitForZeroEnemy{
     while(length(GetAllEnemyID()) > 0){yield;}
 }

 function GetCenterX{
     return GetStgFrameWidth/2;
 }

 function GetCenterY{
     return GetStgFrameHeight/2;
 }

 function rand_int(min, max){
     return round(rand(min, max))
 }

 function sec(n) { return 1/cos(n); }
 function csc(n) { return 1/sin(n); }
 function cot(n) { return 1/tan(n); }

 function GetAtanAngle(obj,obj2){
     let angle=atan2(ObjMove_GetY(obj2)-ObjMove_GetY(obj),ObjMove_GetX(obj2)-ObjMove_GetX(obj));
     return angle;
 }

 function GetPointAngle(x1,y1,x2,y2){
     let angle=atan2(y1-y2,x1-x2);
     return angle;
 }

 function GetDistance(x1,y1,x2,y2){
     return(((x2-x1)^2+(y2-y1)^2)^(1/2));
 }

 function DelaySE(sfx, time){//Assumes that sfx is the path of a loaded sound effect.
   loop(time){
     yield;
   }
   PlaySE(sfx);
 }


task rngBullets{
loop{
	let angle = rand(0,360);
	CreateShotA1(bossX,bossY,1.5,angle,8,0);
	wait(1.2);
	yield;
	}
}

task gravityBullet(startX,startY,startSpeed,angle,graphic,time,gravConst){
	let gravBullet = ObjShot_Create(OBJ_SHOT);
	let timer =0;
	
	ObjMove_SetPosition(gravBullet,startX,startY);
	ObjMove_SetAngle(gravBullet,angle);
	ObjShot_SetGraphic(gravBullet,graphic);
	while(timer<time){
		ObjMove_SetDestAtFrame(gravBullet,startX+angle*timer,startY+(-startSpeed*timer)+gravConst*(timer^2),1);
		timer++;
		yield;
	}
}

task line(x1,y1,x2,y2,density,speed,angle,graphic,delay){
	 let dist = GetDistance(x1,y1,x2,y2)/density;
	 let theta2 = GetPointAngle(x2,y2,x1,y1);
	 let i = 0;
	 loop(density){
		let shot = CreateShotA1(x1+dist*i*cos(theta2),y1+dist*i*sin(theta2),speed,angle,graphic,delay);
		i++;
	 }
 }

function bounceShot(x,y,speed,dir,graphic){
	let shot1 = ObjShot_Create(OBJ_SHOT);
	
	ObjMove_SetPosition(shot1,x,y);
	ObjMove_SetAngle(shot1,dir);
	ObjMove_SetSpeed(shot1,speed);
	ObjShot_SetGraphic(shot1,graphic);
	ObjShot_Regist(shot1);
	let timer=0;
	let side = "null";
	while(!Obj_IsDeleted(shot1)){
		if(ObjMove_GetX(shot1) >= GetStgFrameWidth && side!="right"){
			side="right";
			ObjMove_SetAngle(shot1,((180-ObjMove_GetAngle(shot1))%360));
			//bounceShot2(ObjMove_GetX(shot1),ObjMove_GetY(shot1),4,atan2(GetPlayerY-ObjMove_GetX(shot1),GetPlayerX-ObjMove_GetY(shot1)),1);
			//foo(ObjMove_GetX(shot1),ObjMove_GetY(shot1));
		}
		if(ObjMove_GetX(shot1) <= 0 && side!="left"){
			side="left";
			ObjMove_SetAngle(shot1,((180-ObjMove_GetAngle(shot1))%360));
			
			//foo(ObjMove_GetX(shot1),ObjMove_GetY(shot1));
		}
		if(ObjMove_GetY(shot1) >= GetStgFrameHeight && side!="bottom"){
			side="bottom";
			ObjMove_SetAngle(shot1,((360-ObjMove_GetAngle(shot1))%360));
			
			//foo(ObjMove_GetX(shot1),ObjMove_GetY(shot1));
		}
		if(ObjMove_GetY(shot1) <=0 && side != "top"){
			side="top";
			ObjMove_SetAngle(shot1,((360-ObjMove_GetAngle(shot1))%360));
			
			//foo(ObjMove_GetX(shot1),ObjMove_GetY(shot1));
		}
		yield;
	}
	
	//bounceShot2(ObjMove_GetX(shot1),ObjMove_GetY(shot1),4,angleToPlayer,1);
	yield;
}

function circle(r1,cx,cy,theta,r3,r4,theta4,tIncrement){
	let bullets = [];
	let r2 = r1-r3;
	let c2x = cx+r2*cos(theta);
	let c2y = cy+r2*sin(theta);
	
	function getC2X{
		return cx+r2*cos(theta);
	}
	function getC2Y{
		return cy+r2*sin(theta);
	}
	//let c2y = cy+ r2*sin(theta);
	let theta3 = theta;
	
		loop(360){
			let shot = ObjShot_Create(OBJ_SHOT);
	
			ObjMove_SetPosition(shot,cx+r1*cos(theta),cy+r1*sin(theta));
			//ObjMove_SetSpeed(shot,2.5);
			ObjShot_SetGraphic(shot,8*6+1);
			//ObjMove_SetDestAtFrame(shot,GetCenterX(),GetCenterY(),80);
			//ObjShot_SetDeleteFrame(shot,90);
			ObjMove_SetAngle(shot,theta);
			ObjShot_Regist(shot);
			theta++;
			//theta+=(r3/r1);
			//theta3++;

		}
		theta = 90;
		/*loop(360){
			let shot = ObjShot_Create(OBJ_SHOT);
			ObjMove_SetPosition(shot,getC2X+r3*cos(theta3),getC2Y+r3*sin(theta3));
			//ObjShot_SetGraphic(shot,8*6+1);
			//ObjShot_SetDeleteFrame(shot,90);
			ObjMove_SetAngle(shot,theta3);
			ObjShot_Regist(shot);
			bullets = bullets ~ [shot];
			theta3++;
		}*/
		let count2=0;
		while(1==1){
			let count = 0;
			/*loop(360){
				ObjMove_SetPosition(bullets[count],getC2X+r3*cos(theta3),getC2Y+r3*sin(theta3));
				//ObjShot_SetGraphic(shot,8*6+1);
				//ObjShot_SetDeleteFrame(shot,90);
				//ObjMove_SetAngle(shot,theta3);
				
				theta3++;
				count++;
			}*/
			let shot2 = ObjShot_Create(OBJ_SHOT);
				ObjMove_SetPosition(shot2,getC2X+r4*cos(theta4),getC2Y+r4*sin(theta4));
				ObjShot_SetGraphic(shot2,8*0+count2%8);
				//ObjShot_SetDeleteFrame(shot2,720);
				ObjMove_SetAngle(shot2,theta4);
				ObjShot_Regist(shot2);
				
				theta4+=4;
				count2++;
			theta+= tIncrement*(r3/r1);
			yield;
		}
	
	
	yield;
}

function Sound(path){ //For music. 
     let obj = ObjSound_Create;
     ObjSound_Load(obj,path);
     ObjSound_SetSoundDivision(obj, SOUND_BGM);
     ObjSound_SetRestartEnable(obj, true);
   //  ObjSound_SetLoopEnable(obj, true);
    // ObjSound_SetLoopTime(obj, 0,300);
     ObjSound_Play(obj);
     ObjSound_SetVolumeRate(obj,95);
     return obj;
 }

 function CreateSFX(path, volume){
    let obj = ObjSound_Create();
    ObjSound_Load(obj, path);
    ObjSound_SetSoundDivision(obj, SOUND_SE);
    ObjSound_SetVolumeRate(obj, volume);
    return obj;
}
 
function PlaySyncBGM(path, loop_start, loop_end, skip_param){
	let bgm = ObjSound_Create();
	ObjSound_Load(bgm, path);
	ObjSound_SetRestartEnable(bgm, true);
	ObjSound_SetSoundDivision(bgm, SOUND_BGM);
	ObjSound_SetVolumeRate(bgm,100);
	if(loop_start >= 0 && loop_end > loop_start){
		ObjSound_SetLoopEnable(bgm, true);
		ObjSound_SetLoopTime(bgm, loop_start, loop_end);
	}
	ObjSound_Play(bgm);

	task run{
		let t1 = GetStageTime();
		let t2;
		let dt;
		let sdt = 0;
		let c = -1;
		while(!Obj_IsDeleted(bgm)){
			t2 = GetStageTime();
			yield;
			if(!ObjSound_IsPlaying(bgm)){
				if(c >= 0){ c++; }
				if(c >= skip_param){
					c = -1;
					ObjSound_Play(bgm);
				}
			}
			//t2 = GetStageTime();
			dt = (t2-t1)/1000;
			sdt += dt - 1/60;
			//t1 = t2;
			if(sdt*60 > skip_param/2){
				sdt -= skip_param/60;
				ObjSound_Stop(bgm);
				c = 0;
			}
			t1 = t2;
		}
	}
	run();

	return bgm;
}
/*
function forceFrameBGM(path, loop_start, loop_end, skip_param){
	let bgm = ObjSound_Create();
	ObjSound_Load(bgm, path);
	ObjSound_SetRestartEnable(bgm, true);
	ObjSound_SetSoundDivision(bgm, SOUND_BGM);
	ObjSound_SetVolumeRate(bgm,100);
	if(loop_start >= 0 && loop_end > loop_start){
		ObjSound_SetLoopEnable(bgm, true);
		ObjSound_SetLoopTime(bgm, loop_start, loop_end);
	}
	ObjSound_Play(bgm);

	task run{
		let t1 =0;
		let t2 =1;
	//make it play one frame loop of music per frame
		while(!Obj_IsDeleted(bgm)){
			t2 = GetStageTime();
			yield;
			if(!ObjSound_IsPlaying(bgm)){
				if(c >= 0){ c++; }
				if(c >= skip_param){
					c = -1;
					ObjSound_Play(bgm);
				}
			}
			//t2 = GetStageTime();
			dt = (t2-t1)/1000;
			sdt += dt - 1/60;
			//t1 = t2;
			if(sdt*60 > skip_param/2){
				sdt -= skip_param/60;
				ObjSound_Stop(bgm);
				c = 0;
			}
			t1 = t2;
		}
	}
	run();

	return bgm;
}
*/

function geometry(x,y,radius,sides,density,speed,angle,graphic,delay){//if density==1 is a circle with sides bullets
	 let theta = angle;
	 let lastAngle = 90-(360/(sides*2));
	 let geometryBulletArray = [];
	 loop(sides){
		 let x1 = x+radius*cos(theta);
		 let y1 = y+radius*sin(theta);
		 let x2 = x+radius*cos(theta+(360/sides));
		 let y2 = y+radius*sin(theta+(360/sides));
		 let dist = GetDistance(x1,y1,x2,y2)/density;
		let theta2 = GetPointAngle(x2,y2,x1,y1);
		let i = 1;
		let angle2 = theta;
		loop(density){
			let pi = (180-(360/sides))/2;
			let gamma = (i*360)/(density*sides);
			let delta = 180-(pi+gamma);
			let shot = CreateShotA1(x1+dist*i*cos(theta2),y1+dist*i*sin(theta2),(speed*sin(pi))/(sin(delta)),angle2,graphic,delay);
			//geometryBullets(x,y,x1,y1,shot,radius,speed,sides,density,dist,i,theta,theta2,angle2,graphic,delay);
			let dist2 = GetDistance(x,y,ObjMove_GetX(shot),ObjMove_GetY(shot));
			//ObjMove_SetSpeed(shot,speed*dist2^2/radius^2);
			//ObjMove_SetDestAtFrame(shot,x1+(i*dist*GetStgFrameHeight/radius)*cos(angle2),y1+(i*dist*GetStgFrameHeight/radius)*sin(angle2),100);
			/**/
			angle2 += 360/(density*sides);
			geometryBulletArray = geometryBulletArray ~ [shot];
			i++;
		}
		 theta += (360/sides);
	 }
	
	 return geometryBulletArray;
	 
 }
 
 task bound(obj){
	while(ObjMove_GetX(obj)>0 && ObjMove_GetX(obj)<GetStgFrameWidth && ObjMove_GetY(obj)>0 && ObjMove_GetY(obj)< GetStgFrameHeight){yield;}
	if(ObjMove_GetX(obj)<=0){
		ObjMove_SetX(obj,0);
		ObjMove_SetAngle(obj,((180-ObjMove_GetAngle(obj))%360));
	}
	else if(ObjMove_GetX(obj)>=GetStgFrameWidth){
		ObjMove_SetX(obj,GetStgFrameWidth);
		ObjMove_SetAngle(obj,((180-ObjMove_GetAngle(obj))%360));
	}
	else if(ObjMove_GetY(obj)<=0){
		ObjMove_SetY(obj,0);
		ObjMove_SetAngle(obj,((360-ObjMove_GetAngle(obj))%360));
	}
	else if(ObjMove_GetY(obj)>=GetStgFrameHeight){
		ObjMove_SetY(obj,GetStgFrameHeight);
		ObjMove_SetAngle(obj,((360-ObjMove_GetAngle(obj))%360));
	}
}

    
/*
function animate(string){
string = string~[@];
let pointer = 0;
let boolean = false;
let timer = 0;
while(string[pointer]!=@){
	if(!boolean){drawRectLeft= 128*string[pointer];
		drawRectTop = 128*string[pointer+1];
		boolean = true;
		timer = string[pointer+2];
	}else{
		timer--;
		if(timer == 0){
			pointer += 3;
			boolean = false;
		}
	}
	
	yield;
}

}*/