//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/a5.png";
let overlay = GCSD ~ "/images/overlay.jpg";
let overlay2 = GCSD ~ "/images/overlay2.jpg";
let bossdeath = GCSD ~ "/sfx/bossdeath.wav";
let shoot = GCSD ~ "/sfx/shoot.wav";
let shoot2 = GCSD ~ "/sfx/shoot2.wav";
let laserSFX = GCSD ~ "/sfx/laser.wav";
let spellcard = GCSD ~ "/sfx/spellcard.wav";
let bgm1 = GCSD ~ "/sfx/bgm1.mp3";
let bgm2 = GCSD ~ "/sfx/bgm2.mp3";

//BOSS ANIMATION
task RenderBoss {
	// texture the boss, set centre as true centre.
	ObjPrim_SetTexture(bossObj,imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,108,71);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,1,1,0);
	
	let dir;
	let speed;
	
	while(!Obj_IsDeleted(bossObj)) {
	
		// update boss speed/dir locally
		dir = ObjMove_GetAngle(bossObj);
		speed = ObjMove_GetSpeed(bossObj);
		
		//left,top,right,bottom
		// animation handling
		if(speed == 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);
			//Row 1
			if(animFrame < 5) { ObjSprite2D_SetSourceRect(bossObj,0,1,108,71); }
			if(animFrame >= 5 && animFrame < 10) { ObjSprite2D_SetSourceRect(bossObj,108,1,216,71); }
			if(animFrame >= 10 && animFrame < 15) { ObjSprite2D_SetSourceRect(bossObj,216,1,324,71); }
			if(animFrame >= 15 && animFrame < 20) { ObjSprite2D_SetSourceRect(bossObj,324,1,432,71); }
			if(animFrame >= 20 && animFrame < 25) { ObjSprite2D_SetSourceRect(bossObj,432,1,540,71); }
			if(animFrame >= 25 && animFrame < 30) { ObjSprite2D_SetSourceRect(bossObj,540,1,648,71); }
			//Row 2
			if(animFrame >= 30 && animFrame < 35) { ObjSprite2D_SetSourceRect(bossObj,0,71,108,142); }
			if(animFrame >= 35 && animFrame < 40) { ObjSprite2D_SetSourceRect(bossObj,108,71,216,142); }
			if(animFrame >= 40 && animFrame < 45) { ObjSprite2D_SetSourceRect(bossObj,216,71,324,142); }
			if(animFrame >= 45 && animFrame < 50) { ObjSprite2D_SetSourceRect(bossObj,324,71,432,142); }
			if(animFrame >= 50 && animFrame < 55) { ObjSprite2D_SetSourceRect(bossObj,432,71,540,142); }
			if(animFrame >= 55 && animFrame < 60) { ObjSprite2D_SetSourceRect(bossObj,540,71,648,142); }
			//Row 3
			if(animFrame >= 60 && animFrame < 65) { ObjSprite2D_SetSourceRect(bossObj,0,142,108,213); }
			if(animFrame >= 65 && animFrame < 70) { ObjSprite2D_SetSourceRect(bossObj,108,142,216,213); }
			if(animFrame >= 70 && animFrame < 75) { ObjSprite2D_SetSourceRect(bossObj,216,142,324,213); }
			if(animFrame >= 75 && animFrame < 80) { ObjSprite2D_SetSourceRect(bossObj,324,142,432,213); }
			if(animFrame >= 80 && animFrame < 85) { ObjSprite2D_SetSourceRect(bossObj,432,142,540,213); }
			if(animFrame >= 85 && animFrame < 90) { ObjSprite2D_SetSourceRect(bossObj,540,142,648,213); }
			//Row 4
			if(animFrame >= 90 && animFrame < 95) { ObjSprite2D_SetSourceRect(bossObj,0,213,108,284); }
			if(animFrame >= 95 && animFrame < 100) { ObjSprite2D_SetSourceRect(bossObj,108,213,216,284); }
			if(animFrame >= 100 && animFrame < 105) { ObjSprite2D_SetSourceRect(bossObj,216,213,324,284); }
			if(animFrame >= 105 && animFrame < 110) { ObjSprite2D_SetSourceRect(bossObj,324,213,432,284); }
			if(animFrame >= 110 && animFrame < 115) { ObjSprite2D_SetSourceRect(bossObj,432,213,540,284); }
			if(animFrame >= 115 && animFrame < 120) { ObjSprite2D_SetSourceRect(bossObj,540,213,648,284); }
			//Row 5
			if(animFrame >= 120 && animFrame < 125) { ObjSprite2D_SetSourceRect(bossObj,0,284,108,355); }
			if(animFrame >= 125 && animFrame < 130) { ObjSprite2D_SetSourceRect(bossObj,108,284,216,355); }
			if(animFrame >= 130 && animFrame < 135) { ObjSprite2D_SetSourceRect(bossObj,216,284,324,355); }
			if(animFrame >= 135 && animFrame < 140) { ObjSprite2D_SetSourceRect(bossObj,324,284,432,355); }
			if(animFrame >= 140 && animFrame < 145) { ObjSprite2D_SetSourceRect(bossObj,432,284,540,355); }
			if(animFrame >= 145 && animFrame < 150) { ObjSprite2D_SetSourceRect(bossObj,540,284,648,355); }
			//Row 6
			if(animFrame >= 150 && animFrame < 155) { ObjSprite2D_SetSourceRect(bossObj,0,355,108,426); }
			if(animFrame >= 155 && animFrame < 160) { ObjSprite2D_SetSourceRect(bossObj,108,355,216,426); }
			if(animFrame >= 160 ) { ObjSprite2D_SetSourceRect(bossObj,216,355,324,426); }
			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);	// flip vertical
			TrenderBoss;
		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			TrenderBoss;
		}
		animFrame+=0.9;		// count animFrame. (++ is +1)
		animFrame2+=1.5;		// count animFrame2.
		if(animFrame > 165) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}

task TrenderBoss{
	//Row 1
			if(animFrame2 < 5) { ObjSprite2D_SetSourceRect(bossObj,0,1,108,71); }
			if(animFrame2 >= 5 && animFrame2 < 10) { ObjSprite2D_SetSourceRect(bossObj,108,1,216,71); }
			if(animFrame2 >= 10 && animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,216,1,324,71); }
			if(animFrame2 >= 15 && animFrame2 < 20) { ObjSprite2D_SetSourceRect(bossObj,324,1,432,71); }
			if(animFrame2 >= 20 && animFrame2 < 25) { ObjSprite2D_SetSourceRect(bossObj,432,1,540,71); }
			if(animFrame2 >= 25 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,540,1,648,71); }
			//Row 2
			if(animFrame2 >= 30 && animFrame2 < 35) { ObjSprite2D_SetSourceRect(bossObj,0,71,108,142); }
			if(animFrame2 >= 35 && animFrame2 < 40) { ObjSprite2D_SetSourceRect(bossObj,108,71,216,142); }
			if(animFrame2 >= 40 && animFrame2 < 45) { ObjSprite2D_SetSourceRect(bossObj,216,71,324,142); }
			if(animFrame2 >= 45 && animFrame2 < 50) { ObjSprite2D_SetSourceRect(bossObj,324,71,432,142); }
			if(animFrame2 >= 50 && animFrame2 < 55) { ObjSprite2D_SetSourceRect(bossObj,432,71,540,142); }
			if(animFrame2 >= 55 && animFrame2 < 60) { ObjSprite2D_SetSourceRect(bossObj,540,71,648,142); }
			//Row 3
			if(animFrame2 >= 60 && animFrame2 < 65) { ObjSprite2D_SetSourceRect(bossObj,0,142,108,213); }
			if(animFrame2 >= 65 && animFrame2 < 70) { ObjSprite2D_SetSourceRect(bossObj,108,142,216,213); }
			if(animFrame2 >= 70 && animFrame2 < 75) { ObjSprite2D_SetSourceRect(bossObj,216,142,324,213); }
			if(animFrame2 >= 75 && animFrame2 < 80) { ObjSprite2D_SetSourceRect(bossObj,324,142,432,213); }
			if(animFrame2 >= 80 && animFrame2 < 85) { ObjSprite2D_SetSourceRect(bossObj,432,142,540,213); }
			if(animFrame2 >= 85 && animFrame2 < 90) { ObjSprite2D_SetSourceRect(bossObj,540,142,648,213); }
			//Row 4
			if(animFrame2 >= 90 && animFrame2 < 95) { ObjSprite2D_SetSourceRect(bossObj,0,213,108,284); }
			if(animFrame2 >= 95 && animFrame2 < 100) { ObjSprite2D_SetSourceRect(bossObj,108,213,216,284); }
			if(animFrame2 >= 100 && animFrame2 < 105) { ObjSprite2D_SetSourceRect(bossObj,216,213,324,284); }
			if(animFrame2 >= 105 && animFrame2 < 110) { ObjSprite2D_SetSourceRect(bossObj,324,213,432,284); }
			if(animFrame2 >= 110 && animFrame2 < 115) { ObjSprite2D_SetSourceRect(bossObj,432,213,540,284); }
			if(animFrame2 >= 115 && animFrame2 < 120) { ObjSprite2D_SetSourceRect(bossObj,540,213,648,284); }
			//Row 5
			if(animFrame2 >= 120 && animFrame2 < 125) { ObjSprite2D_SetSourceRect(bossObj,0,284,108,355); }
			if(animFrame2 >= 125 && animFrame2 < 130) { ObjSprite2D_SetSourceRect(bossObj,108,284,216,355); }
			if(animFrame2 >= 130 && animFrame2 < 135) { ObjSprite2D_SetSourceRect(bossObj,216,284,324,355); }
			if(animFrame2 >= 135 && animFrame2 < 140) { ObjSprite2D_SetSourceRect(bossObj,324,284,432,355); }
			if(animFrame2 >= 140 && animFrame2 < 145) { ObjSprite2D_SetSourceRect(bossObj,432,284,540,355); }
			if(animFrame2 >= 145 && animFrame2 < 150) { ObjSprite2D_SetSourceRect(bossObj,540,284,648,355); }
			//Row 6
			if(animFrame2 >= 150 && animFrame2 < 155) { ObjSprite2D_SetSourceRect(bossObj,0,355,108,426); }
			if(animFrame2 >= 155 && animFrame2 < 160) { ObjSprite2D_SetSourceRect(bossObj,108,355,216,426); }
			if(animFrame2 >= 160 ) { ObjSprite2D_SetSourceRect(bossObj,216,355,324,426); }
}

task setOverlay {	
	let scrollTex = 0;
	// overlay 
	wait(5);
	let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,overlay); 
	ObjRender_SetBlendType(overlayObj,BLEND_ALPHA);
	ObjRender_SetAlpha(overlayObj,150);
	ObjSprite2D_SetSourceRect(overlayObj,0,0,236,314);
	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 += 3;
		yield;
	}
	Obj_Delete(overlayObj);
} 

//SCRIPT ENDING PROCESS
task TEnd {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(120);	
	CloseScript(GetOwnScriptID());
}
task TFinalize {
	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_FADE);	
	wait(120);	
	CloseScript(GetOwnScriptID());
}

//ACTUAL FUNCTIONS

// your best friend, forever.
function wait(w) {
	loop(w) { 
	yield; 
	}	
} 
function waitS(w) {
	loop(w*60) { 
	yield; 
	}	
} 
function angleToPlayer {
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}
function GetCenterX {
	let centerX = GetStgFrameWidth/2;
	return centerX;
}
function GetCenterY {
	let centerY = GetStgFrameHeight/2;
	return centerY;
}
//SOUNDS AND SFX

	//Load sfx
	LoadSound(bossdeath);
	LoadSound(shoot);
	LoadSound(shoot2);
	LoadSound(laserSFX);
	LoadSound(spellcard);
	LoadSound(bgm1);
	LoadSound(bgm2);
	
task BGM_Play(let path, let volume){
	
	let seobject1 = ObjSound_Create;
	ObjSound_Load(seobject1,path);
	ObjSound_Play(seobject1);
	ObjSound_SetVolumeRate(seobject1,volume);
	ObjSound_SetFade(path,60);

}

	
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);

}

