#include "./Conc.txt"

task renderBoss{

	let f = 0; 
	let f2 = 0;
	let f3 = 0;
	let dir;
	let speed;
	
	Name;
	EnemyMarker;
	
	let r = ObjPrim_Create(OBJ_SPRITE_2D);
	
		ObjPrim_SetTexture(r,GetCurrentScriptDirectory ~ "IMG/DSB.png");
		ObjSprite2D_SetSourceRect(r,0,0,640,480);
		ObjSprite2D_SetDestRect(r,0,0,640,480);
		ObjSprite2D_SetDestCenter(r);
		ObjRender_SetScaleXYZ(bossObj,1,1,1);
		ObjRender_SetBlendType(r,BLEND_ALPHA);
		ObjRender_SetPosition(r,GetStgFrameWidth-64,GetStgFrameHeight/2+16,0);
		Obj_SetRenderPriorityI(r,-1);
	
	//Textures the Boss, Indicates the Boss's True Center and Sets its scale.
	ObjPrim_SetTexture(bossObj,imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,110,110);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,0.7,0.7,0);
	
	// While the Boss is NOT(!)Deleted or alive, this loop plays.
	while(!Obj_IsDeleted(bossObj)){
	
		// Variables update the Speed and Direction of this boss (In this task (renderBoss))
		dir = ObjMove_GetAngle(bossObj);
		speed = ObjMove_GetSpeed(bossObj);
		
		// If the Boss is Idle, It shows this image.
		if(speed == 0){
		ObjRender_SetScaleXYZ(bossObj,1,1,1);
			if(f<5){ObjSprite2D_SetSourceRect(bossObj,0,128,64,192);}
			if(f>=5 && f<10){ObjSprite2D_SetSourceRect(bossObj,64,128,128,192);}
			if(f>=10 && f<15){ObjSprite2D_SetSourceRect(bossObj,128,128,192,192);}
			if(f>=15 && f<20){ObjSprite2D_SetSourceRect(bossObj,192,128,256,192);}
			
			f2=0;
			f3=0;
			f++;
	if(f==20){
		f = 0;
	}
		}
		
		// If the Boss is moving right, It shows this image.
		else if(cos(dir) > 0){
		ObjRender_SetScaleXYZ(bossObj,1,1,1);
			if(f2<5){ObjSprite2D_SetSourceRect(bossObj,256,128,320,192);}
			if(f2>=5 && f2<10){ObjSprite2D_SetSourceRect(bossObj,320,128,384,192);}
			if(f2>=10 && f2<15){ObjSprite2D_SetSourceRect(bossObj,384,128,448,192);}
			if(f2>=15 && f2<20){ObjSprite2D_SetSourceRect(bossObj,448,128,512,192);}
			if(f2>=20 && f2<25){ObjSprite2D_SetSourceRect(bossObj,384,0,448,64);}
			if(f2>=25 && f2<30){ObjSprite2D_SetSourceRect(bossObj,448,0,512,64);}
			if(f2>=30 && f2<35){ObjSprite2D_SetSourceRect(bossObj,384,64,448,128);}
			if(f2>=35 && f2<40){ObjSprite2D_SetSourceRect(bossObj,448,64,512,128);}
			
			f2++;
			if(f2==32){
				f2 = 16;
			}
		}
		
		else if(cos(dir) < 0){
		ObjRender_SetScaleXYZ(bossObj,1,1,1);
		ObjRender_SetAngleY(bossObj,180);
			if(f3<5){ObjSprite2D_SetSourceRect(bossObj,256,128,320,192);}
			if(f3>=5 && f3<10){ObjSprite2D_SetSourceRect(bossObj,320,128,384,192);}
			if(f3>=10 && f3<15){ObjSprite2D_SetSourceRect(bossObj,384,128,448,192);}
			if(f3>=15 && f3<20){ObjSprite2D_SetSourceRect(bossObj,448,128,512,192);}
			if(f3>=20 && f3<25){ObjSprite2D_SetSourceRect(bossObj,384,0,448,64);}
			if(f3>=25 && f3<30){ObjSprite2D_SetSourceRect(bossObj,448,0,512,64);}
			if(f3>=30 && f3<35){ObjSprite2D_SetSourceRect(bossObj,384,64,448,128);}
			if(f3>=35 && f3<40){ObjSprite2D_SetSourceRect(bossObj,448,64,512,128);}
			f3++;
			if(f3==40){
				f3 = 20;
			}
		}
		
		yield;
	}
	
}

task EnemyMarker{
		let spin = 0;

		let EM = ObjPrim_Create(OBJ_SPRITE_2D);
		
		ObjPrim_SetTexture(EM,GetCurrentScriptDirectory ~ "IMG/EM.png");
		ObjSprite2D_SetSourceRect(EM,0,0,64,16);
		ObjSprite2D_SetDestRect(EM,0,0,64,16);
		ObjSprite2D_SetDestCenter(EM);
		ObjRender_SetBlendType(EM,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(EM,19);
		ObjRender_SetPosition(EM,GetStgFrameWidth,GetStgFrameHeight+24,0);
		
		while(!Obj_IsDeleted(bossObj)){
		spin+=1;
		let angle = 255*sin(spin);
		let angle2 = 255*cos(spin);
		BossX = ObjMove_GetX(bossObj);
		BossY = ObjMove_GetY(bossObj);
			
			ObjRender_SetPosition(EM,BossX+32,GetStgFrameHeight+24,0);
			ObjRender_SetScaleXYZ(EM,1,1,1);
			ObjRender_SetColor(EM,255,angle,angle2);
			ObjRender_SetAngleXYZ(EM,0,0,0);
			yield;
		}
}

task Name{

	let textObj = ObjText_Create;
	let spin = 0;
	let p = 255*sin(spin);
	
	ObjText_SetText(textObj, "Death Fairy of Doom");
	ObjText_SetFontSize(textObj, 10);
	Obj_SetRenderPriorityI(textObj,70);
	ObjText_SetFontColorTop(textObj, 255, 0, 0);
	ObjText_SetFontColorBottom(textObj, 1, 0, 0);
	ObjText_SetFontBorderType(textObj, BORDER_SHADOW);
	ObjText_SetFontBorderWidth(textObj, 1);
	ObjText_SetFontBorderColor(textObj,255, 255, 255);
	ObjText_SetFontBorderWidth(textObj, 1);
	ObjRender_SetPosition(textObj,10,15,0);
	loop(128){
	spin+=1;
	p = 255*sin(spin);
	ObjRender_SetAlpha(textObj,p);
	yield;
	}
}

task Death{
	let dir = 0;
	let x = 0;
	con1(GetStgFrameWidth/2,GetStgFrameHeight/2,80);

			LoadSound(GetCurrentScriptDirectory ~ "SE/explode.wav");
			PlaySE(GetCurrentScriptDirectory ~ "SE/explode.wav");
			while(x<50){
				con2(BossX,BossY,dir);
				dir+=360/50;
				wait(2);
				x++;
			}
			wait(5);
			

			LoadSound(GetCurrentScriptDirectory ~ "SE/explode.wav");
			PlaySE(GetCurrentScriptDirectory ~ "SE/explode.wav");
				con1(GetStgFrameWidth/2,GetStgFrameHeight/2,80);
			while(x<150){
				con2(BossX+cos(dir+rand(0,360)),BossY+sin(dir+rand(0,360)),dir);
				wait(0);
				dir+=360/150;
				x++;
			}
			x = 0;
			dir = 0;

	
		task con2(x,y,r){
		
			let batObj = ObjPrim_Create(OBJ_SPRITE_2D);
			let q = rand(0,360);
			let w = 0;
			let down = 155;
			let scale = rand(2,2.5);
			
			ObjPrim_SetTexture(batObj,GetCurrentScriptDirectory ~ "IMG/Bat.png");
			ObjSprite2D_SetDestRect(batObj,0,0,64,27);
			ObjSprite2D_SetSourceRect(batObj,0,0,64,27);
			ObjSprite2D_SetDestCenter(batObj);
			ObjRender_SetBlendType(batObj,BLEND_ADD_ARGB);
			ObjRender_SetAlpha(batObj,255);
			Obj_SetRenderPriorityI(batObj,70);
			ObjRender_SetPosition(batObj,x+rand(0,10),y+rand(0,10),0);
			ObjRender_SetScaleXYZ(batObj,rand(0,10),rand(0,10),rand(0,10));
				ObjRender_SetAngleXYZ(batObj,rand(0,60),rand(0,60),rand(0,360));
				ObjRender_SetPosition(batObj,x+cos(q)*w,y+sin(q)*w,0);
			
			loop(150){
				scale-=1/45;
				down-=2;
				w-=scale;
			
				ObjRender_SetScaleXYZ(batObj,scale,scale,scale);
				ObjRender_SetPosition(batObj,x+cos(q)*w,y+sin(q)*w,0);
				ObjRender_SetAlpha(batObj,down);
				yield;
			}
			Obj_Delete(batObj);
		}
		
		task con1(x,y,w){
			let concObj = ObjPrim_Create(OBJ_SPRITE_2D);
			let spin = 0;
			let dir = 0;
			let p = 0;
			let a = 7;
			let down = 0;
			let scale = 0;
			
			ObjPrim_SetTexture(concObj,GetCurrentScriptDirectory ~ "IMG/Con1.png");
			ObjSprite2D_SetDestRect(concObj,0,0,128,128);
			ObjSprite2D_SetSourceRect(concObj,0,0,128,128);
			ObjSprite2D_SetDestCenter(concObj);
			ObjRender_SetBlendType(concObj,BLEND_ADD_ARGB);
			ObjRender_SetAlpha(concObj,255);
			Obj_SetRenderPriorityI(concObj,70);
			ObjRender_SetPosition(concObj,x,y,0);
			ObjRender_SetColor(concObj,255,10,65);
			
			loop(120){
				spin+=0.03;
				dir+=360/a;
				scale-=0.005+(spin/100);
				let down = 155*sin(dir/100);
				ObjRender_SetAngleXYZ(concObj,0,0,spin*100+(spin*20));
				ObjRender_SetScaleXYZ(concObj,scale,scale,scale);
				ObjRender_SetAlpha(concObj,155-down);
				yield;
			}
			Obj_Delete(concObj);
		}
	}

task Hard{
	let spin = 0;
	let hardObj = ObjPrim_Create(OBJ_SPRITE_2D);
	
		ObjPrim_SetTexture(hardObj,GetCurrentScriptDirectory ~ "IMG/Difficulty.png");
		ObjSprite2D_SetSourceRect(hardObj,0,73,144,104);
		ObjSprite2D_SetDestRect(hardObj,0,73,144,104);
		ObjSprite2D_SetDestCenter(hardObj);
		ObjRender_SetBlendType(hardObj,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(hardObj,19);
		ObjRender_SetScaleXYZ(hardObj,0.75,0.75,0);
		ObjRender_SetPosition(hardObj,GetStgFrameWidth+144,32,0);
		
		loop(96){
			spin+=1;
			let p = 255*sin(spin);
			ObjRender_SetAlpha(hardObj,p);
			yield;
		}
}	

task Lunatic{
	let spin = 0;
	let lunaticObj = ObjPrim_Create(OBJ_SPRITE_2D);
	
		ObjPrim_SetTexture(lunaticObj,GetCurrentScriptDirectory ~ "IMG/Difficulty.png");
		ObjSprite2D_SetSourceRect(lunaticObj,0,105,144,137);
		ObjSprite2D_SetDestRect(lunaticObj,0,105,144,137);
		ObjSprite2D_SetDestCenter(lunaticObj);
		ObjRender_SetBlendType(lunaticObj,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(lunaticObj,19);
		ObjRender_SetScaleXYZ(lunaticObj,0.75,0.75,0);
		ObjRender_SetPosition(lunaticObj,GetStgFrameWidth+144,32,0);
		
		loop(96){
			spin+=1;
			let p = 255*sin(spin);
			ObjRender_SetAlpha(lunaticObj,p);
			yield;
		}
}	
	
task Normal{
	let spin = 0;
	let normalObj = ObjPrim_Create(OBJ_SPRITE_2D);
	
		ObjPrim_SetTexture(normalObj,GetCurrentScriptDirectory ~ "IMG/Difficulty.png");
		ObjSprite2D_SetSourceRect(normalObj,0,40,144,72);
		ObjSprite2D_SetDestRect(normalObj,0,40,144,72);
		ObjSprite2D_SetDestCenter(normalObj);
		ObjRender_SetBlendType(normalObj,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(normalObj,19);
		ObjRender_SetScaleXYZ(normalObj,0.75,0.75,0);
		ObjRender_SetPosition(normalObj,GetStgFrameWidth+144,32,0);
		
		loop(96){
			spin+=1;
			let p = 255*sin(spin);
			ObjRender_SetAlpha(normalObj,p);
			yield;
		}
}		
	
task MagicCircle{

	BossX = ObjMove_GetX(bossObj);
	BossY = ObjMove_GetY(bossObj);
	
	let spin = 0;
	let angle = 255*sin(spin);
	let bigspin = 1440*sin(spin);
	let scale = (0.5)*sin(spin);
	let MC = ObjPrim_Create(OBJ_SPRITE_2D);
	
		ObjPrim_SetTexture(MC,GetCurrentScriptDirectory ~ "IMG/MC.png");
		ObjSprite2D_SetSourceRect(MC,0,0,256,256);
		ObjSprite2D_SetDestRect(MC,0,0,256,256);
		ObjSprite2D_SetDestCenter(MC);
		ObjRender_SetBlendType(MC,BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(MC,25);
		
		while(!Obj_IsDeleted(bossObj)){
			spin+=2;
			angle = 255*sin(spin/1.5);
			scale = (0.25)*sin(spin/1.5);
			bigspin = 1440*sin(spin/10);
			
			ObjRender_SetPosition(MC,BossX,BossY,0);
			ObjRender_SetScaleXYZ(MC,scale+1,scale+1,0);
			ObjRender_SetColor(MC,255,angle,angle);
			ObjRender_SetAngleXYZ(MC,0,0,bigspin);
			
			yield;
		}
		Obj_Delete(MC);
}

task CutIn(text){
	let textObj = ObjText_Create;
	let finalx = 374;
	let finaly = 25;
	let spin = 0;
	let e = 255*sin(spin);
	let t = 255*sin(spin);
	
	SCAttack;
	ObjText_SetText(textObj,text);
	ObjText_SetFontSize(textObj,12);
	ObjText_SetFontColorTop(textObj,247,148,29);
	ObjText_SetFontColorBottom(textObj,255,255,255);
	ObjText_SetFontBorderType(textObj,BORDER_SHADOW);
	ObjText_SetFontBorderWidth(textObj,1);
	ObjText_SetFontBorderColor(textObj,200,10,70);
	ObjText_SetHorizontalAlignment(textObj,ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(textObj,300);
	ObjRender_SetBlendType(textObj,BLEND_ALPHA);
	Obj_SetRenderPriorityI(textObj,70);
	ObjText_SetAutoTransCenter(textObj,true);
	
	let spellObj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(spellObj,69);
	ObjPrim_SetTexture(spellObj,GetCurrentScriptDirectory ~ "IMG/spellbar.png");
	ObjRender_SetBlendType(spellObj,BLEND_ALPHA);
	ObjRender_SetAlpha(spellObj,0);
	ObjSprite2D_SetSourceRect(spellObj,0,0,256,38);
	ObjSprite2D_SetSourceRect(spellObj,0,0,256,38);
	ObjSprite2D_SetDestCenter(spellObj);
	ObjRender_SetScaleXYZ(spellObj,1,1,1);
	
	loop(250){
		spin+=1;
		e = 255*sin(spin);
		ObjRender_SetPosition(spellObj,ObjRender_GetX(textObj)+150,ObjRender_GetY(textObj),0);
		ObjRender_SetPosition(textObj,GetStgFrameWidth/2-150,GetStgFrameHeight/2-5,0);
		ObjRender_SetAlpha(textObj,e);
		yield;
	}
	
	loop(128){
		spin+=0.01;
		t = 255*sin(spin*128);
		ObjText_SetHorizontalAlignment(textObj,ALIGNMENT_RIGHT);
		ObjText_SetMaxWidth(textObj,300);
		ObjRender_SetPosition(spellObj,ObjRender_GetX(textObj)+178,ObjRender_GetY(textObj)+1,0);
		ObjRender_SetPosition(textObj,finalx-300,finaly,0);
		ObjRender_SetAlpha(textObj,t);
		ObjRender_SetAlpha(spellObj,t);
		yield;
	}
	
	while(!Obj_IsDeleted(bossObj)){
		yield;
	}
	Obj_Delete(textObj);
	Obj_Delete(spellObj);
}

task SCAttack{
	let i = 0;
	let x = 0;
	let dir = 0;
	let spin = 0;
	let a = 8;
	let p = 200*sin(dir);
	let t = 105*sin(dir);
	
	dark;
	ascent(i in 0..7){
		loop(a){
			scattack(GetStgFrameWidth/2,GetStgFrameHeight/2,140+i*60,dir);
			scattack2(GetStgFrameWidth/2,GetStgFrameHeight/2,170+i*60,dir);

			x++;
			dir+=360/a;
		}
		dir = 0;
		x = 0;
	}
	
	task scattack(x,y,w,l){
		let scObj = ObjPrim_Create(OBJ_SPRITE_2D);
		
		ObjPrim_SetTexture(scObj,GetCurrentScriptDirectory ~ "IMG/SCA.png");
		ObjSprite2D_SetDestRect(scObj,0,0,128,16);
		ObjSprite2D_SetSourceRect(scObj,0,0,128,16);
		ObjSprite2D_SetDestCenter(scObj);
		ObjRender_SetBlendType(scObj,BLEND_ALPHA);
		ObjRender_SetAlpha(scObj,155);
		loop(255){
		spin+=0.02;
		dir+=360/a;
		p = 200*sin(spin/1.5);
		ObjRender_SetPosition(scObj,x+w*cos(dir+spin),y+w*sin(dir+spin),0);
		ObjRender_SetAngleXYZ(scObj,0,0,dir+spin+90);
		ObjRender_SetScaleXYZ(scObj,1,1,1);
		ObjRender_SetAlpha(scObj,p);
		yield;
		}
		Obj_Delete(scObj);
	}
	
	task scattack2(x,y,w,l){
		let scObj = ObjPrim_Create(OBJ_SPRITE_2D);
		
		ObjPrim_SetTexture(scObj,GetCurrentScriptDirectory ~ "IMG/SCA.png");
		ObjSprite2D_SetDestRect(scObj,0,0,128,16);
		ObjSprite2D_SetSourceRect(scObj,0,0,128,16);
		ObjSprite2D_SetDestCenter(scObj);
		ObjRender_SetBlendType(scObj,BLEND_ALPHA);
		ObjRender_SetAlpha(scObj,155);
		loop(255){
		ObjRender_SetPosition(scObj,x+w*cos(dir-spin),y+w*sin(dir-spin),0);
		ObjRender_SetAngleXYZ(scObj,180,180,dir-spin-90);
		ObjRender_SetScaleXYZ(scObj,1,1,1);
		ObjRender_SetAlpha(scObj,p);
		yield;
		}
		Obj_Delete(scObj);
	}
	
	task dark{
		let scObj = ObjPrim_Create(OBJ_SPRITE_2D);
		let scObj2 = ObjPrim_Create(OBJ_SPRITE_2D);
		
		ObjPrim_SetTexture(scObj2,GetCurrentScriptDirectory ~ "IMG/SCAlay.png");
		ObjSprite2D_SetDestRect(scObj2,0,0,2000,2000);
		ObjSprite2D_SetSourceRect(scObj2,0,0,2000,2000);
		ObjSprite2D_SetDestCenter(scObj2);
		ObjRender_SetBlendType(scObj2,BLEND_ALPHA);
		ObjRender_SetAlpha(scObj2,25);
		
		ObjPrim_SetTexture(scObj,GetCurrentScriptDirectory ~ "IMG/SCAlay.png");
		ObjSprite2D_SetDestRect(scObj,0,0,2000,2000);
		ObjSprite2D_SetSourceRect(scObj,0,0,2000,2000);
		ObjSprite2D_SetDestCenter(scObj);
		ObjRender_SetBlendType(scObj,BLEND_ALPHA);
		ObjRender_SetAlpha(scObj,25);
		
		loop(510){
		t = 115*sin(spin/1.5);
		ObjRender_SetPosition(scObj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		ObjSprite2D_SetDestRect(scObj,0,0,2000,2000-spin*2);
		ObjSprite2D_SetSourceRect(scObj,0,0,2000,2000-spin*2);
		ObjSprite2D_SetDestCenter(scObj);
		ObjRender_SetScaleXYZ(scObj,0.3,0.3,0.3);
		ObjRender_SetAlpha(scObj,t);
		
		ObjRender_SetPosition(scObj2,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		ObjRender_SetScaleXYZ(scObj2,0.3,0.3,0.3);
		ObjSprite2D_SetDestRect(scObj2,0,0,2000,2000+spin*2);
		ObjSprite2D_SetSourceRect(scObj2,0,0,2000,2000+spin*2);
		ObjSprite2D_SetDestCenter(scObj2);
		ObjRender_SetAlpha(scObj2,t);
		yield;
		}
		Obj_Delete(scObj);
	}
}