//GLOBAL VARIABLES FOR ANIMATION AND LINKS
#include "./default_system/Default_Effect.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let animFrame = 0;
let animFrame2 = 0;
let GCSD = GetCurrentScriptDirectory;
let imgBoss = GCSD ~ "images/rumia.png";
let moon = GCSD ~ "images/moon.png";
let cloud = GCSD ~ "/images/smoke.png";
let bossdeath = GCSD ~ "/sfx/bossdeath.wav";
let shoot = GCSD ~ "/sfx/shoot.wav";
let shoot2 = GCSD ~ "/sfx/shoot2.wav";
let bgm1 = GCSD ~ "/sfx/bgm.mp3";
let laser = GCSD ~ "/sfx/laser.wav";

//BOSS ANIMATION
task RenderBoss {
	// texture the boss, set centre as true centre.
	ObjPrim_SetTexture(bossObj,imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,128,128);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,1.2,1.2,0);
	
	let dir;
	let speed;
	
	while(!Obj_IsDeleted(bossObj)) {
	
		// update boss speed/dir locally
		dir = ObjMove_GetAngle(bossObj);
		speed = ObjMove_GetSpeed(bossObj);
		
		
		// animation handling
		if(speed == 0){ 
			//left,top,right,bottom
			ObjRender_SetAngleXYZ(bossObj,0,0,0);
			if(animFrame < 12) { ObjSprite2D_SetSourceRect(bossObj,0,0,128,128); }
			if(animFrame >= 12 && animFrame < 24) { ObjSprite2D_SetSourceRect(bossObj,0,128,128,256); }
			if(animFrame >= 24 && animFrame < 36) { ObjSprite2D_SetSourceRect(bossObj,0,256,128,384); }
			if(animFrame >= 48) { ObjSprite2D_SetSourceRect(bossObj,0,384,128,512); }
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			if(animFrame2 < 12 && animFrame2 <24) { ObjSprite2D_SetSourceRect(bossObj,256,0,384,128); }
			if(animFrame2 >= 48) { ObjSprite2D_SetSourceRect(bossObj,256,128,384,256); }

		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 12 && animFrame2 <24) { ObjSprite2D_SetSourceRect(bossObj,256,0,384,128); }
			if(animFrame2 >= 48) { ObjSprite2D_SetSourceRect(bossObj,256,128,384,256); } 
		}
		animFrame+=1.8;		// count animFrame. (++ is +1)
		animFrame2+=6.9;		// count animFrame2.
		if(animFrame > 60) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}
task RenderBossFade {
	// texture the boss, set centre as true centre.
	ObjPrim_SetTexture(bossObj,imgBoss);
	ObjRender_SetBlendType(bossObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(bossObj,100);
	ObjSprite2D_SetSourceRect(bossObj,0,0,128,128);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,1.2,1.2,0);
	
	let dir;
	let speed;
	
	while(!Obj_IsDeleted(bossObj)) {
	
		// update boss speed/dir locally
		dir = ObjMove_GetAngle(bossObj);
		speed = ObjMove_GetSpeed(bossObj);
		
		
		// animation handling
		if(speed == 0){ 
			//left,top,right,bottom
			ObjRender_SetAngleXYZ(bossObj,0,0,0);
			if(animFrame < 12) { ObjSprite2D_SetSourceRect(bossObj,0,0,128,128); }
			if(animFrame >= 12 && animFrame < 24) { ObjSprite2D_SetSourceRect(bossObj,0,128,128,256); }
			if(animFrame >= 24 && animFrame < 36) { ObjSprite2D_SetSourceRect(bossObj,0,256,128,384); }
			if(animFrame >= 48) { ObjSprite2D_SetSourceRect(bossObj,0,384,128,512); }
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			if(animFrame2 < 12 && animFrame2 <24) { ObjSprite2D_SetSourceRect(bossObj,256,0,384,128); }
			if(animFrame2 >= 48) { ObjSprite2D_SetSourceRect(bossObj,256,128,384,256); }

		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 12 && animFrame2 <24) { ObjSprite2D_SetSourceRect(bossObj,256,0,384,128); }
			if(animFrame2 >= 48) { ObjSprite2D_SetSourceRect(bossObj,256,128,384,256); } 
		}
		animFrame+=1.8;		// count animFrame. (++ is +1)
		animFrame2+=6.9;		// count animFrame2.
		if(animFrame > 60) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}

task overlay {	
	let scrollTex = 0;
	// overlay 
	wait(5);
	let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,cloud); 
	ObjRender_SetBlendType(overlayObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(overlayObj,255);
	ObjSprite2D_SetSourceRect(overlayObj,0,0,400,400);
	ObjSprite2D_SetDestRect(overlayObj,0,0,GetStgFrameWidth,GetStgFrameHeight);
	
	// As long as the boss is alive, scroll the overlay diagonally towards bottom right.
	while(!Obj_IsDeleted(bossObj)) {
		ObjSprite2D_SetSourceRect(overlayObj,0-scrollTex,0+scrollTex,512-scrollTex,512+scrollTex);
		scrollTex += 7.5;
		yield;
	}
	Obj_Delete(overlayObj);
} 
task overlayMoon {	
	// overlay 
	wait(120);
	let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(overlayObj,22); 
	ObjPrim_SetTexture(overlayObj,moon); 
	ObjRender_SetBlendType(overlayObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(overlayObj,205);
	ObjSprite2D_SetSourceRect(overlayObj,0,0,237,237);
	ObjRender_SetScaleXYZ(overlayObj,0.5,0.5,0);
	ObjSprite2D_SetDestRect(overlayObj,0,100,GetStgFrameWidth*2,GetStgFrameHeight*2);
	
	// As long as the boss is alive, scroll the overlay diagonally towards bottom right.
	while(!Obj_IsDeleted(bossObj)) {
		yield;
	}
	Obj_Delete(overlayObj);
} 

//SCRIPT ENDING PROCESS
task TEnd {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	SE_Play(bossdeath,40);
	TExplosionB(bossX,bossY,10,0.5);
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(120);	
	CloseScript(GetOwnScriptID());
}
task TSpell {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	CloseScript(GetOwnScriptID());
}
task TFinish{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	SE_Play(bossdeath,60);
	TExplosionB(bossX,bossY,10,0.5);
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	
	wait(120);	
	CloseScript(GetOwnScriptID());
}

//ACTUAL FUNCTIONS

// your best friend, forever.
function wait(w) {
	loop(w) { 
	yield; 
	}	
} 

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

//SOUNDS AND SFX

	//Load sfx
	LoadSound(bossdeath);
	LoadSound(shoot);
	LoadSound(bgm1);
	LoadSound(laser);
	
task BGM_Play(let path, let volume){
	let seobject1 = ObjSound_Create;
	ObjSound_Load(seobject1,path);
	ObjSound_Play(seobject1);
	ObjSound_SetVolumeRate(seobject1,volume);
	loop(9999999){
		yield;
	}
	RemoveSound(path);

}	
task SE_Play(let path, let volume){
	let seobject = ObjSound_Create;
	ObjSound_Load(seobject,path);
	ObjSound_Play(seobject);
	ObjSound_SetVolumeRate(seobject,volume);
	loop(120){
		yield;
	}
	RemoveSound(path);

}

