#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Midboss Spellcard 1"]
#Background["./Scene/Background.txt"]
#Text[" Rainy Day Surprise - \"Sudden Storm\" "]
#Player["./Player/Reimu/Reimu.txt"]
#System["./System/Default_System.txt"]

let bossObj;
#include "./Lib/AllStarShot_Const.dnh"
#include "./Functions/Functions_Index.dnh"

@Initialize{
	// Initialize: Works when starting up Danmakufu PH3

	//  Defines a Boss and Registers it.
	bossObj = CreateBoss;
	
	let KogasaPath = GetCurrentScriptDirectory ~ "./Img/Boss/Kogasa Sprite.png"; //Place to select sprite 
	AnimateBossA2_ZUN(bossObj,KogasaPath,64,96);
	
	if(GetCommonData("Is Plural Script",false) == false){
		ObjMove_SetPosition(bossObj,GetStgFrameWidth/2,128);
	}
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	
	mainTask; //Runs Maintask
	
}

@Event{
	// Event: Sets the Boss's Specs
	
		// Sets Boss's Life and Timer
		alternative(GetEventType() )
		case(EV_REQUEST_LIFE) {
			SetScriptResult(4000);
		}
		case(EV_REQUEST_SPELL_SCORE) {
			SetScriptResult(100000);
		}
		case(EV_REQUEST_TIMER) {
			SetScriptResult(50);
			Timeout;
		}

}

@MainLoop{
	// MainLoop: Runs throughout the script.
	
	yield;

}

@Finalize{
}
task mainTask{
	end;	
	if(GetCommonData("Is Plural Script",false) == false){
		wait(30);
		ObjMove_SetDestAtWeight(bossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-72,10,5);
		wait(42);
	}
	else{
		ObjMove_SetDestAtWeight(bossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-72,10,5);
	}
	let sceneObj = GetEnemyBossSceneObjectID;
	SetCommonData("Boss Type","KOGASA");
	ObjEnemyBossScene_StartSpell(sceneObj);
	CutIn("Rainy Day Surprise - \"Sudden Storm\"");
	wait(75);
	summon;

}

task summon{
	ObjMove_SetAngle(bossObj,270);
	ObjMove_SetAcceleration(bossObj,0.12);
	ObjMove_SetMaxSpeed(bossObj,7);
	wait(15);
	ObjMove_SetAcceleration(bossObj,-0.12);
	ObjMove_SetMaxSpeed(bossObj,0);
	wait(15);
	SetBossHitbox(32,32);
	ConcentrateA1(bossObj,"Cherry","PINK",300,"Long");
	wait(120);
	Burst(bossObj,"Cherry","PINK","Long");
	PlaySnd("se_boon01",65);
	ascent(i in 0..GetStgFrameWidth/64+1){
		FormCloud(i*64,ObjMove_GetY(bossObj)-48);
		FormCloud(i*64,ObjMove_GetY(bossObj)-96);
	}
	wait(15);
	ObjMove_SetAcceleration(bossObj,0.4);
	ObjMove_SetMaxSpeed(bossObj,9);
	wait(30);
	ObjMove_SetAcceleration(bossObj,-0.2);
	ObjMove_SetMaxSpeed(bossObj,0);
	wait(10);
	fireloop;
}

task fireloop{
	while(!Obj_IsDeleted(bossObj)){
		cloudrain(700);
		wait(100);
		
		movement;
		wait(600);
		
		ObjMove_SetPosition(bossObj,GetStgFrameWidth/2,-256);
		ObjMove_SetAngle(bossObj,90);
		ObjMove_SetAcceleration(bossObj,0.2);
		ObjMove_SetMaxSpeed(bossObj,8);
		wait(55);
		ObjMove_SetAcceleration(bossObj,-0.2);
		ObjMove_SetMaxSpeed(bossObj,0);
		wait(40);
		shootlaser;
		ConcentrateA1(bossObj,"Cherry","AQUA",240,"Long");
		wait(120);
		Burst(bossObj,"Cherry","AQUA","Long");
		wait(50);
		ConcentrateA1(bossObj,"Cherry","AQUA",240,"Short");
		PlaySnd("se_lazer01",55);
		CreateStraightLaserA1(16,GetStgFrameHeight+24,94+180,GetStgFrameHeight+48,70,65,1057,57);
		CreateStraightLaserA1(GetStgFrameWidth-16,GetStgFrameHeight+24,86+180,GetStgFrameHeight+48,70,65,1057,57);
		wait(60);
		shake;
		Burst(bossObj,"Cherry","AQUA","Long");
		cascade;
		wait(120);
		ObjMove_SetAngle(bossObj,270);
		ObjMove_SetAcceleration(bossObj,0.4);
		ObjMove_SetMaxSpeed(bossObj,9);
		wait(50);
		ObjMove_SetAcceleration(bossObj,-0.2);
		ObjMove_SetMaxSpeed(bossObj,0);
		wait(30);
	}
}

task shake{
	let scale = 40;
	loop(50){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ 
			Set2DCameraFocusX(192 + rand(-scale, scale));
			Set2DCameraFocusY(224 + rand(-scale, scale));
			yield;
			scale-=0.8;
		}
		yield;
	}
	wait(10);
	Reset2DCamera();
}

task shootlaser{
	wait(60);
	PlaySnd("se_lazer01",55);
	ascent(i in 0..7){
		CreateStraightLaserA1(GetStgFrameWidth/7*(i+0.5),-16,rand(87,93),GetStgFrameHeight+48,40,65,1057,67);
	}
}

task cascade{
	ascent(i in 0..GetStgFrameWidth/8){
		PlaySnd("se_kira01",35);
		let objs = CreateDNHShotA1(i*4,0,rand(0.1,1),rand(87,93),rand(257,259),30);
		let objs1 = CreateDNHShotA1(GetStgFrameWidth-i*4,0,rand(0.1,1),rand(87,93),rand(257,259),30);
		
		ObjMove_AddPatternA3(objs,0,0.1,NO_CHANGE,0.1,0,rand(1,4),NO_CHANGE);
		ObjMove_AddPatternA3(objs1,0,0.1,NO_CHANGE,0.1,0,rand(1,4),NO_CHANGE);
		if(i%2 == 0){yield;}
	}
}

task FormCloud(x,y){
	let path = GetCurrentScriptDirectory ~ "./Img/Boss/Mist Sprite.png";
	let mistObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(mistObj,path);
	
	let r = rand(32,72);
	
	ObjSprite2D_SetSourceRect(mistObj,0,0,128,128);
	ObjSprite2D_SetDestCenter(mistObj);
	ObjRender_SetBlendType(mistObj,BLEND_ALPHA);
	ObjRender_SetAlpha(mistObj,0);
	ObjRender_SetColor(mistObj,r+64,r+48,r+64);
	ObjRender_SetScaleXYZ(mistObj,2,2,0);
	Obj_SetRenderPriorityI(mistObj,50);
	
	ObjRender_SetPosition(mistObj,x,y,0);
	form;
	sway;
	
	task form{
		let s = rand(2,5);
		let rs = 0;
		
		loop(255/s){
			ObjRender_SetAlpha(mistObj,rs);
			rs+=s;
			yield;
		}
			ObjRender_SetAlpha(mistObj,255);
	}
	
	task sway{
		let inc = 0;
		let rt = rand(2.5,4.5);
		while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ 
			ObjRender_SetY(mistObj,y+20*sin(inc));
			ObjRender_SetAngleZ(mistObj,-8+16*sin(inc));
			
			yield;
			inc+=rt;
		}
		Obj_Delete(mistObj);
	}
}

task cloudrain(dur){
	ascent(i in 0..dur/5){
		PlaySnd("se_tan02",40);
		
		let obj = CreateDNHShotA1(rand(-32,GetStgFrameWidth+32),rand(4,64),0,0,rand(257,259),5);
		ObjMove_AddPatternA3(obj,0,1,90+10*sin(i)+rand(-5,5),0.75,0,rand(2,4),rand(257,259));
		wait(5);
	}
}

task movement{
	swoop("LEFT");
	wait(350);
	swoop("RIGHT");
	wait(350);
}

task swoop(side){
	if(side == "LEFT"){
		ObjMove_SetPosition(bossObj,-256,-32);
		ObjMove_SetAngle(bossObj,15);
		ObjMove_SetAcceleration(bossObj,0.2);
		ObjMove_SetMaxSpeed(bossObj,4);
		fireleft;
		BossPhase = 3;
		wait(250);
		BossPhase = 1;
		ObjMove_SetAcceleration(bossObj,0);
		ObjMove_SetSpeed(bossObj,0);
	}
	if(side == "RIGHT"){
		ObjMove_SetPosition(bossObj,GetStgFrameWidth+256,-32);
		ObjMove_SetAngle(bossObj,180-15);
		ObjMove_SetAcceleration(bossObj,0.2);
		ObjMove_SetMaxSpeed(bossObj,4);
		fireright;
		BossPhase = 2;
		wait(250);
		BossPhase = 1;
		ObjMove_SetAcceleration(bossObj,0);
		ObjMove_SetSpeed(bossObj,0);
	}
}

task fireleft{
	let x = 0;
	let a = 30;
	let dir = 0;
	
	wait(25);
	loop(200){
		PlaySnd("se_kira00",35);
		ascent(i in 0..4){
			let shot = CreateDNHShotA1(BossX,BossY,3,dir,20,5+i*5);
			ObjMove_SetAcceleration(shot,-0.2);
			ObjMove_SetMaxSpeed(shot,0);
			ObjMove_AddPatternA3(shot,60,0.1,dir,0.13,0.07,5.5-i/5,21);
		}
		
		x++;
		dir+=360/a;
		yield;
	}
}

task fireright{
	let x = 0;
	let a = 30;
	let dir = 0;
	
	wait(25);
	loop(200){
		PlaySnd("se_kira00",35);
		ascent(i in 0..4){
			let shot = CreateDNHShotA1(BossX,BossY,3,dir+180,20,5+i*5);
			ObjMove_SetAcceleration(shot,-0.2);
			ObjMove_SetMaxSpeed(shot,0);
			ObjMove_AddPatternA3(shot,60,0.1,dir+180,0.13,-0.07,5.5-i/5,21);
		}
		
		x++;
		dir-=360/a;
		yield;
	}
}

task end{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ yield;}
	SetCommonData("Is Spell?",false);
	ExplodeA2(BossX,BossY,"Cherry","PURPLE","Short");
	wait(5);
	Obj_Delete(bossObj);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	CloseScript(GetOwnScriptID());
}