//#include"script/default_system/Default_ShotConst.txt"
#include".\Resources.txt"
#include".\GizmoSpriteLibrary.txt"
#include".\shot\AllStarShot_Const.txt"


let shou;
let kaguya;
let TIME = 99;
let shouX = 0;
let shouY = 0;
let kaguyaX = 0;
let kaguyaY = 0;

@Event
{
	SetTimer(TIME);
    SetLife(471);
    SetScore(10000000);	
}

@Initialize
{
	DeleteShotAll(TYPE_ALL, TYPE_ITEM); 
	SetAutoDeleteObject(true);
	ObjSound_Stop(GetCommonData("music", 0));
	let music1 = ObjSound_Create;
	//let music1fadeout = false;
	ObjSound_Load(music1,CSD ~ "./bgm/boss2.mp3");
	ObjSound_SetRestartEnable(music1,true);
	ObjSound_SetVolumeRate(music1,100);
	ObjSound_SetLoopEnable(music1,true);
	ObjSound_Play(music1);
	SetCommonData("music", music1);
	/*
	shou = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	kaguya = ObjEnemy_Create(OBJ_ENEMY);
	
	ObjEnemy_Regist(shou);
	ObjEnemy_Regist(kaguya);
	
	ObjMove_SetPosition(shou,GetCommonData("ShouX",0),GetCommonData("ShouY",0));
	ObjMove_SetPosition(kaguya,GetCommonData("KaguyaX",0),GetCommonData("KaguyaY",0));
	let cx = GetStgFrameWidth() / 3;
	
	renderShouUFO(shou);
	renderKaguyaIN(kaguya);
	
	
	//ObjMove_SetDestAtFrame(shou, cx, 100, 60);
	//ObjMove_SetDestAtFrame(kaguya, GetStgFrameWidth()-cx, 100, 60);
	
	
	ObjEnemy_SetLife(kaguya, 471);*/
	//BossManage;
	TMain();
	
	wait(60);

}

@MainLoop
{

	yield;
}

task TMain(){
	/*ObjEnemy_SetDamageRate(shou,0,0);
	ObjEnemy_SetDamageRate(kaguya,0,0);
	let bossHP = 471*2;
	loop(120){
		bossHP-=471*2/120;
		SetCommonData("BossHealth",bossHP);
		yield;
	}
	ObjEnemy_SetDamageRate(shou,8,100);
	ObjEnemy_SetDamageRate(kaguya,8,100);
	loop{
		if(ObjEnemy_GetInfo(kaguya, INFO_LIFE) < ObjEnemy_GetInfo(shou, INFO_LIFE)){ObjEnemy_SetLife(shou, ObjEnemy_GetInfo(kaguya, INFO_LIFE));}
		else if(ObjEnemy_GetInfo(kaguya, INFO_LIFE) > ObjEnemy_GetInfo(shou, INFO_LIFE)){ObjEnemy_SetLife(kaguya, ObjEnemy_GetInfo(shou, INFO_LIFE));}
		SetCommonData("BossHealth",471-ObjEnemy_GetInfo(shou,INFO_LIFE));
	yield;}	
	*/
	wait(60);
	spellCast("The Most Glorious Treasure");
	wait(60*5);
	WhiteOut;
	wait(60*24);
	fadeIn;
	wait(60);
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	ObjSound_Stop(GetCommonData("music", 0));
	let music1 = ObjSound_Create;
	//let music1fadeout = false;
	ObjSound_Load(music1,CSD ~ "./bgm/boss3.mp3");
	ObjSound_SetRestartEnable(music1,true);
	ObjSound_SetVolumeRate(music1,100);
	ObjSound_SetLoopEnable(music1,true);
	ObjSound_Play(music1);
	wait(60);
	spellCast("GET STICKBUGGED LOL");
	WhiteOut2;
	wait(60*4);
	pattern;
	wait(60*6.5);
	pattern2;
	wait(60*6);
	pattern3;
	wait(60*11.5);
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	SetCommonData("LifebarsRemaining",GetCommonData("LifebarsRemaining",0)-1);
	SetCommonData("SpellsRemaining", 0);
	SetCommonData("SpellsRemaining", GetCommonData("SpellsRemaining", 0)-1);
	CloseScript(GetOwnScriptID()); 
}
/*
task BossManage{

	while(ObjEnemy_GetInfo(shou,INFO_LIFE)>0){
        shouX = ObjMove_GetX(shou);
        shouY = ObjMove_GetY(shou);
		kaguyaX = ObjMove_GetX(kaguya);
		kaguyaY = ObjMove_GetY(kaguya);
		
		ObjEnemy_SetIntersectionCircleToShot(shou, shouX, shouY, 40);
        ObjEnemy_SetIntersectionCircleToPlayer(shou, shouX, shouY, 30);
		ObjEnemy_SetIntersectionCircleToShot(kaguya, kaguyaX, kaguyaY, 40);
        ObjEnemy_SetIntersectionCircleToPlayer(kaguya, kaguyaX, kaguyaY, 30);
		yield;
	}
	SetCommonData("ShouX",ObjMove_GetX(shou));
	SetCommonData("ShouY",ObjMove_GetY(shou));
	
	SetCommonData("KaguyaX",ObjMove_GetX(kaguya));
	SetCommonData("KaguyaY",ObjMove_GetY(kaguya));
	
	Obj_Delete(shou);
	Obj_Delete(kaguya);
	
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	SetCommonData("LifebarsRemaining",GetCommonData("LifebarsRemaining",0)-1);
	SetCommonData("SpellsRemaining", GetCommonData("SpellsRemaining", 0)-1);
    CloseScript(GetOwnScriptID()); 
}
*/
task WhiteOut{
	let path1 = GetCurrentScriptDirectory() ~ "img/boss.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.61);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetDestRect(obj1, 0, 0, 32, 32);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 32, 32);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	ObjRender_SetScaleXYZ(obj1, 0, 0, 0);
	let size = 0;
	let rotation = 0;
	loop(60*25){
		ObjRender_SetScaleXYZ(obj1, size, size, 0);
		ObjRender_SetAngleZ(obj1, rotation);
		rotation++;
		size+= 0.02;
		yield;
	}
	Obj_Delete(obj1);
}

task WhiteOut2{
	let path1 = GetCurrentScriptDirectory() ~ "img/boss3.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.31);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetDestRect(obj1, 0, 0, 261, 190);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 261, 190);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	ObjRender_SetScaleXYZ(obj1, 0.5, 0.5, 0);
	let size = 0;
	let rotation = 0;/*
	loop{
		ObjRender_SetScaleXYZ(obj1, size, size, 0);
		ObjRender_SetAngleZ(obj1, rotation);
		rotation++;
		size+= 0.02;
		yield;
	}*/
}
task fadeIn{
	let path1 = GetCurrentScriptDirectory() ~ "img/mima.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.62);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetDestRect(obj1, 0, 0, 459, 783);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 459, 783);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	ObjRender_SetScaleXYZ(obj1, 0.5, 0.5, 0);
	ObjRender_SetAlpha(obj1, 0);
	let alpha = 0;
	loop(60){
		ObjRender_SetAlpha(obj1, alpha);
		alpha += 255/120;
		yield;
	}
	Obj_Delete(obj1);
}

task spellCast(name){
	let objText = ObjText_Create();
			Obj_SetRenderPriorityI(objText, 80);
			ObjText_SetFontType(objText, "8bitoperator JVE");
			ObjText_SetFontSize(objText, 16);
			ObjText_SetFontBorderType(objText, BORDER_FULL);
			ObjText_SetFontBorderWidth(objText, 3);
			ObjText_SetFontBorderColor(objText, 0,0,0);
			ObjText_SetFontColorTop(objText, 255,255,255);
			ObjText_SetFontColorBottom(objText, 255,255,255);
			ObjText_SetMaxWidth(objText, 500);
			ObjRender_SetPosition(objText,GetStgFrameWidth/2-237,300,0);
			ObjText_SetText(objText, name);
			wait(60);
			let y = 300;
			let dist = 260/60;
			loop(60){
				ObjRender_SetPosition(objText,GetStgFrameWidth/2-237,y,0);
				y-= dist;
				yield;
			}
			ObjRender_SetPosition(objText,GetStgFrameWidth/2-237,40,0);
			wait(60*28);
			Obj_Delete(objText);
}

task pattern{
	wait(60);
	loop{
		let angle = rand(0, 360);
		loop(30){
			CreateShotA1(GetStgFrameWidth/2, GetStgFrameHeight/2, 4, angle, LARGE_GREEN, 0);
			angle += 360/30
		}
		wait(20);
	}
	
}

task pattern2{
	let angle = 90;
	loop{
		angle += 360/12;
		let n = 1;
		loop(2){
			
			loop(6){
						let cLaser2 = CreateCurveLaserA1(GetStgFrameWidth/2, GetStgFrameHeight/2, 8, angle, 30, 15, C_LASER_GREEN, 0);
						ObjCrLaser_SetTipDecrement(cLaser2, 0);
						ObjMove_SetAngularVelocity(cLaser2, 10.1*n);
						ObjMove_SetMaxSpeed(cLaser2, 0.5);
						ObjMove_SetAcceleration(cLaser2, -0.1);
						moveShot(cLaser2);
						angle += 360/6;
					}
					n*= -1;
					
			}
			wait(30);
	}
		task moveShot(shot){
			while(!Obj_IsDeleted(shot)){
				if(ObjMove_GetSpeed(shot) <= 0.5){
					wait(30);
					ObjMove_SetAngularVelocity(shot, 0);
					ObjMove_SetAcceleration(shot, 0.4);
					ObjMove_SetMaxSpeed(shot, 30);
					break;
				}
				yield;
			}
		} 
	
}

task pattern3{
	let angle = 90;
	loop{
		angle += 360/12;
		let n = 1;
		loop(2){
			
			loop(6){
						let cLaser2 = CreateCurveLaserA1(GetStgFrameWidth/2, GetStgFrameHeight/2, 1, angle, 30, 15, C_LASER_GREEN, 0);
						ObjCrLaser_SetTipDecrement(cLaser2, 0);
						ObjMove_SetAngularVelocity(cLaser2, 1.1*n);
						ObjMove_SetMaxSpeed(cLaser2, 10);
						ObjMove_SetAcceleration(cLaser2, 0.1);
						moveShot(cLaser2);
						angle += 360/6;
					}
					n*= -1;
					
			}
			wait(30);
	}
		task moveShot(shot){
			while(!Obj_IsDeleted(shot)){
				if(ObjMove_GetSpeed(shot) <= 0.5){
					wait(30);
					ObjMove_SetAngularVelocity(shot, 0);
					ObjMove_SetAcceleration(shot, 0.4);
					ObjMove_SetMaxSpeed(shot, 30);
					break;
				}
				yield;
			}
		} 
	
}
