#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Boss Spellcard 4"]
#Background["./Scene/Background.txt"]
#Text[" \"Dance of the Iceborne\" "]
#Player["./Player/Reimu/Reimu.txt"]
#System["./System/Default_System.txt"]

let bossObj;
let pobj = GetPlayerObjectID;
let rendp;

#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;
	rendp = Obj_GetRenderPriorityI(bossObj);
	
	let LettyPath = GetCurrentScriptDirectory ~ "./Img/Boss/Letty Sprite.png"; //Place to select sprite 
	AnimateBossA1_CtC(bossObj,LettyPath);
	
	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(9250);
		}
		case(EV_REQUEST_SPELL_SCORE) {
			SetScriptResult(150000);
		}
		case(EV_REQUEST_IS_DURABLE_SPELL){
			SetScriptResult(true);
		}
		case(EV_REQUEST_TIMER) {
			SetScriptResult(70);
			Timeout;
		}

}

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

}

@Finalize{
	ObjRender_SetColor(bossObj,255,255,255);
	ObjRender_SetAlpha(bossObj,255);
	Obj_SetRenderPriorityI(bossObj,rendp);
}

task mainTask{
	end;	
	if(GetCommonData("Is Plural Script",false) == false){
		wait(30);
	}
	ObjMove_SetDestAtWeight(bossObj,GetStgFrameWidth/2,GetStgFrameHeight/2,10,5);
	wait(42);
	SetCommonData("Boss Type","LETTY");
	let sceneObj = GetEnemyBossSceneObjectID;
	ObjEnemyBossScene_StartSpell(sceneObj);
	CutIn("\"Dance of the Iceborne\""); 		
	wait(15);
	summon;
	wait(60);
	ConcentrateA1(bossObj,"Snow","AQUA",300,"Long");
	wait(120);
	Burst(bossObj,"Snow","AQUA","Long");
	leave;
	BossPhase = 2;
	freeze;
	wait(160);
	ConcentrateA1(bossObj,"Snow","AQUA",240,"Short");
	wait(60);
	Burst(bossObj,"Snow","AQUA","Short");
	fire;
	wait(90);
	fireicicle(1);
	wait(640);
	ConcentrateA1(bossObj,"Snow","AQUA",240,"Short");
	wait(60);
	Burst(bossObj,"Snow","AQUA","Short");
	fire2;
	wait(90);
	fireicicle(2);
	wait(600);
	ConcentrateA1(bossObj,"Snow","AQUA",240,"Short");
	wait(60);
	Burst(bossObj,"Snow","AQUA","Short");
	fire3;
	wait(120);
	fireicicle(3);
	wait(630);
	ConcentrateA1(bossObj,"Snow","GREEN",240,"Long");
	wait(60);
	Burst(bossObj,"Snow","GREEN","Long");
	fire4;
	wait(280);
	fireicicle(4);
}

task fireicicle(phase){	
	alternative(phase)
	case(1){
		loop(36){
			PlaySnd("se_kira02",30);
			let obj = CreateDNHShotA1(rand(0,GetStgFrameWidth),0,0,90,91,10);
			ObjRender_SetScaleY(obj,4);
			ObjMove_SetAcceleration(obj,0.055);
			ObjMove_SetMaxSpeed(obj,7);
			wait(15);
		}
	}
	
	case(2){
		loop(16){
			PlaySnd("se_kira02",30);
			let obj = CreateDNHShotA1(0,rand(0,GetStgFrameHeight),0,0,90,10);
			ObjRender_SetScaleY(obj,4);
			ObjMove_SetAcceleration(obj,0.055);
			ObjMove_SetMaxSpeed(obj,7);
			wait(18);
			
			PlaySnd("se_kira02",30);
			let obj1 = CreateDNHShotA1(GetStgFrameWidth,rand(0,GetStgFrameHeight),0,180,90,10);
			ObjRender_SetScaleY(obj1,4);
			ObjMove_SetAcceleration(obj1,0.055);
			ObjMove_SetMaxSpeed(obj1,7);
			wait(18);
		}
	}
	
	case(3){
		loop(43){
			PlaySnd("se_kira02",30);
			let obj = CreateDNHShotA1(rand(0,GetStgFrameWidth),0,0,90,91,10);
			ObjRender_SetScaleY(obj,4);
			ObjMove_SetAcceleration(obj,0.055);
			ObjMove_SetMaxSpeed(obj,7);
			wait(12);
		}
	}
	
	case(4){
		ascent(i in 0..100){
			PlaySnd("se_kira02",30);
			let obj = CreateDNHShotA1(BossX,BossY,0,i*84,91,10);
			ObjRender_SetScaleY(obj,3);
			ObjMove_SetAcceleration(obj,0.055);
			ObjMove_SetMaxSpeed(obj,7);
			
			if(i%4==0){wait(10);}
		}
	}
}

task fire{	
	ascent(k in 0..7){
				PlaySnd("se_tan02",26);
		ascent(j in 0..4){
			ascent(i in 0..8){
				let obj = CreateDNHShotA1(BossX,BossY,9,90+k*45-20+(i*5)+(j*90),279,10);
				ObjMove_SetAcceleration(obj,-0.85);
				ObjMove_SetMaxSpeed(obj,0.75);
				
				let obj1 = CreateDNHShotA1(BossX,BossY,9,90+k*45-20+(i*5)+(j*90),279,10);
				ObjMove_SetAcceleration(obj1,-0.85);
				ObjMove_SetMaxSpeed(obj1,2.5);
			}
		}
		wait(90);
	}
}

/*task nicelooking{
	let x = 0;
	let dir = 0;
	let a = 10;
	let spin = 0;
	
	loop(10){
		ascent(i in 0..8){
			while(x<a){
				CreateDNHShotA1(BossX+0*cos(dir+spin),BossY+0*sin(dir+spin),1.5-i/a,dir+spin,14,10);
				//CreateDNHShotA1(BossX+200*cos(dir-spin),BossY+200*sin(dir-spin),1.5,dir-spin+180,14,10);
				
				x++;
				dir+=360/a;
				spin+=1/(a/2);
			}
			x = 0;
			dir = 0;
			wait(2);
		}
		wait(30);
	}
}*/

task fire2{
	let x = 0;
	let dir = 0;
	let a = 8;
	let spin = 0;
	
	while(spin<359){
			PlaySnd("se_kira01",26);
		while(x<a){
			let obj = CreateDNHShotA1(BossX+0*cos(dir+spin),BossY+0*sin(dir+spin),2,dir+spin,14,10);
			let obj2 = CreateDNHShotA1(BossX+256*cos(dir+spin),BossY+256*sin(dir+spin),2,dir+spin+180,14,10);
			del(obj);
			del(obj2);
			
			task del(ob){
				wait(256/ObjMove_GetSpeed(ob));
				ObjShot_FadeDelete(ob);
			}
			
			x++;
			dir+=360/a;
			spin+=2/(a/1.45);
		}
		x = 0;
		dir = 0;
		wait(6);
	}
}

task fire3{
	let x = 0;
	let a = 24;
	
	task snd{
		PlaySnd("se_tan02",26);
		wait(30);
		PlaySnd("se_tan02",26);
	}
		loop(10){
				snd;
				let r = rand(-50,50);
			while(x<a){
				let obj = CreateDNHShotA1(GetStgFrameWidth*2-(GetStgFrameWidth*2/a*x*2)+r,-24,0,0,311,0);
				let obj1 = CreateDNHShotA1(GetStgFrameWidth*2/a*x*2+r,-24,0,180,310,0);
				ObjShot_SetAutoDelete(obj,false);
				ObjShot_SetSpellResist(obj,true);
				ObjShot_SetAutoDelete(obj1,false);
				ObjShot_SetSpellResist(obj1,true);
				
				move;
				
				task move{
					ob;
					ob1;
					task ob{
						let down = 1.5;
						let s = 0.0;
						loop(400){
							ObjMove_SetPosition(obj,ObjMove_GetX(obj)+0.75,ObjMove_GetY(obj)+down);
							ObjMove_SetAngle(obj,90);
							down+=s;
							yield;
						}
						Obj_Delete(obj);
					}
					
					task ob1{
						wait(30);
						let down = 1.5;
						let s = 0.00;
						loop(400){
							ObjMove_SetPosition(obj1,ObjMove_GetX(obj1)-0.75,ObjMove_GetY(obj1)+down);
							ObjMove_SetAngle(obj1,90);
							down+=s;
							yield;
						}
						Obj_Delete(obj1);
					}
				}
				
				
				x++;
			}
			x = 0;
			wait(60);
		}
}

task fire4{
	let x = 0;
	let dir = 0;
	let a = 15;
	charge;
	closein;
	
	task charge{
		wait(120);
		ConcentrateA1(bossObj,"Snow","AQUA",180,"Short");
		wait(60);
		Burst(bossObj,"Snow","AQUA","Short");
	}
	
	ascent(i in 2..7){
				PlaySnd("se_kira00",26);
		while(x<a){
			let obj = CreateDNHShotA1(BossX+(i*40)*cos(dir+i*9),BossY+(i*40)*sin(dir+i*9),0,dir+i*9+90,276+i/1.5,60);
			ObjShot_SetAutoDelete(obj,false);
			ObjShot_SetSpellResist(obj,true);
			go(dir);
			
			task go(ang){
				let spin = 0;
				let acc = 0;
				wait(180);
				PlaySnd("se_kira01",26);
				loop{
					ObjMove_SetPosition(obj,BossX+(i*40)*cos(ang+i*9+spin*(i/3)),BossY+(i*40)*sin(ang+i*9+spin*(i/3)));
					yield;
					if(acc < 0.1825){acc+=0.002;}
					spin+=acc;
				}
			}
			dir+=360/a;
			x++;
		}
		x = 0;
		dir = 0;
		wait(10);
	}
	
	task closein{
		wait(660);
		let z = 285;
		let inc = 0;
		loop(140){
			let t = mist(BossX+z*cos(inc),BossY+z*sin(inc),0,inc+180);
			SetMistGraphic(t,rand(184,233),70);
			let t1 = mist(BossX+z*cos(inc+180),BossY+z*sin(inc+180),0,inc);
			SetMistGraphic(t1,rand(184,233),70);
			inc+=20;
			z-=2;
			wait(7);
		}
	}
	
}

function mist(x,y,v,dir){
	let shotObj = ObjShot_Create(OBJ_SHOT);
	ObjShot_Regist(shotObj);
	
	ObjMove_SetSpeed(shotObj,v);
	ObjMove_SetAngle(shotObj,dir);
	ObjShot_SetDelay(shotObj,0);
	ObjShot_SetSpellResist(shotObj,true);
	ObjShot_SetAutoDelete(shotObj,false);
	Obj_SetVisible(shotObj,false);
	ObjShot_SetIntersectionEnable(shotObj,false);
	
	PlaySnd("se_kira00",30);
	
	ObjMove_SetPosition(shotObj,x,y);
	return shotObj;
}

task SetMistGraphic(obj,ran,dur){
	let path = GetCurrentScriptDirectory ~ "./Img/Boss/Mist Sprite.png";
	let mistObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(mistObj,path);
	
	ObjSprite2D_SetSourceRect(mistObj,0,0,128,128);
	ObjSprite2D_SetDestCenter(mistObj);
	ObjRender_SetBlendType(mistObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(mistObj,0);
	ObjRender_SetColor(mistObj,160,255,255);
	ObjRender_SetScaleXYZ(mistObj,1.3,1.3,0);
	Obj_SetRenderPriorityI(mistObj,50);
	
	form(mistObj,ran);
	stick;
	
	task stick{
		while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(mistObj) && !Obj_IsDeleted(bossObj)){
			ObjRender_SetPosition(mistObj,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			yield;
		}
		Obj_Delete(obj);
		Obj_Delete(mistObj);
	}
	wait(dur-10);
	randomshoot;
	wait(10);
	dissolve(mistObj,ran);
	
	task form(obj,r){
		let s = 8;
		let rs = 0;
		
		loop(r/s){
			ObjRender_SetAlpha(obj,rs);
			rs+=s;
			yield;
		}
		ObjRender_SetAlpha(obj,r);
	}
	
	task dissolve(obj,r){
		let s = 8;
		let rs = 0;
		
		loop(r/s){
			ObjRender_SetAlpha(obj,r-rs);
			rs+=s;
			yield;
		}
		ObjRender_SetAlpha(obj,0);
		Obj_Delete(obj);
		delete;
	}
	
	task randomshoot{
		ascent(i in 0..6){
			if(!Obj_IsDeleted(obj) && !Obj_IsDeleted(mistObj) && !Obj_IsDeleted(bossObj)){
				PlaySnd("se_tan01",30);
				let bulletObj = CreateDNHShotA1(ObjMove_GetX(obj)+rand(-32,32),ObjMove_GetY(obj)+rand(-32,32),8,ObjMove_GetAngle(obj)+rand(90,270),rand(39,41),0);
				ObjMove_SetAcceleration(bulletObj,-1);
				ObjMove_SetMaxSpeed(bulletObj,rand(0.5,1));
				accel;
				task accel{
					wait(130);
					ObjMove_SetAcceleration(bulletObj,0.075);
					ObjMove_SetMaxSpeed(bulletObj,rand(2,5));
				}
				if(i % 3 == 0){yield;}
			}
		}
	}

	task delete{	
		Obj_Delete(obj);
	}
	
	
}

task freeze{
	let path = GetCurrentScriptDirectory ~ "./Img/Boss/Ice Sprite.png";
	let iceObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(iceObj,path);
	PlaySnd("se_kira00",60);
	
	ObjSprite2D_SetSourceRect(iceObj,0,0,512,512);
	ObjSprite2D_SetDestCenter(iceObj);
	ObjRender_SetBlendType(iceObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(iceObj,0);
	ObjRender_SetColor(iceObj,160,255,255);
	ObjRender_SetScaleXYZ(iceObj,1.185,1.185,0);
	Obj_SetRenderPriorityI(iceObj,23);
	ObjRender_SetPosition(iceObj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	move;
	icy;
	
	PlaySnd("se_tan02",30);
	ascent(i in 0..10){
		CreateDNHShotA1(BossX,BossY,1,GetAngleToPlayer(bossObj)-36+i*8,280,30);
		CreateDNHShotA1(BossX,BossY,2,GetAngleToPlayer(bossObj)-72+i*16,280,30);
	}
	
	ascent(i in 0..30){
		ObjRender_SetAlpha(iceObj,122/30*i);
		yield;
	}
	task move{
		let x = 0;
		while(!Obj_IsDeleted(bossObj)){
			ObjRender_SetAngleZ(iceObj,x);
			x+=0.12;
			yield;
		}
		Obj_Delete(iceObj);
	}
}

task icy{
	let speeds = GetPlayerSpeed;
	let playerObj = GetPlayerObjectID;
	SetPlayerSpeed(0,0);
	resolve;
	
	let x_var = 0;
	let y_var = 0;
	let speed;
	
	while(!Obj_IsDeleted(bossObj)){
		if(GetVirtualKeyState(VK_SLOWMOVE) != KEY_FREE){
			speed = 1;
		}
		else{
			speed = 1.5;
		}
		
		if(IsPlayerSpellActive){
			SetPlayerSpeed(speeds[0],speeds[1]);
		}
		else{
			SetPlayerSpeed(0,0);
		}
		
		if(ObjRender_GetX(playerObj) <= 16 || ObjRender_GetX(playerObj) >= GetStgFrameWidth-16 || GetPlayerState != STATE_NORMAL){
			x_var = 0;
		}
			
		if(ObjRender_GetY(playerObj) <= 16 || ObjRender_GetY(playerObj) >= GetStgFrameHeight-16 || GetPlayerState != STATE_NORMAL){
			y_var = 0;
		}
		
		if(GetPlayerState == STATE_NORMAL && !IsPlayerSpellActive){
			if(GetVirtualKeyState(VK_RIGHT) != KEY_FREE && x_var <= speed*25){
				x_var+=speed;
			}
			else if(GetVirtualKeyState(VK_RIGHT) == KEY_FREE && x_var > 0){
				x_var-=speed/1.75;
			}
			
			if(GetVirtualKeyState(VK_LEFT) != KEY_FREE && x_var >= -speed*25){
				x_var-=speed;
			}
			else if(GetVirtualKeyState(VK_LEFT) == KEY_FREE && x_var < 0){
				x_var+=speed/1.75;
			}
			
			if(GetVirtualKeyState(VK_DOWN) != KEY_FREE && y_var <= speed*25){
				y_var+=speed;
			}
			else if(GetVirtualKeyState(VK_DOWN) == KEY_FREE && y_var > 0){
				y_var-=speed/1.75;
			}
			
			if(GetVirtualKeyState(VK_UP) != KEY_FREE && x_var >= -speed*25){
				y_var-=speed;
			}
			else if(GetVirtualKeyState(VK_UP) == KEY_FREE && y_var < 0){
				y_var+=speed/1.75;
			}
			ObjMove_SetPosition(playerObj,GetPlayerX+(x_var/10),GetPlayerY+(y_var/10));
			}
		
		yield;
	}
	
	task resolve{
		while(!Obj_IsDeleted(bossObj)){
			yield;
		}
		SetPlayerSpeed(speeds[0],speeds[1]);
	}
}

sub leave{
	ObjRender_SetColor(bossObj,72,160,255);
	ObjRender_SetAlpha(bossObj,192);
	Obj_SetRenderPriorityI(bossObj,29);
}

sub summon{
	summonsigil(0,[64,160,192],"MIDDLE",90);
	wait(30);
	summonsigil(80,[102,72,128],"LEFT",60);
	summonsigil(280,[102,72,128],"RIGHT",60);
}

task summonsigil(ang,colour,type,duration){
	let path = GetCurrentScriptDirectory ~ "./Img/Boss/Sigil.png";
	let sigilObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(sigilObj,path);
	
	ObjSprite2D_SetSourceRect(sigilObj,0,0,384,384);
	ObjSprite2D_SetDestRect(sigilObj,-256,-534,256,24);
	ObjSprite2D_SetSourceRect(sigilObj,0,0,512,512);
	ObjRender_SetBlendType(sigilObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(sigilObj,0);
	ObjRender_SetColor(sigilObj,colour[0],colour[1],colour[2]);
	ObjRender_SetScaleXYZ(sigilObj,0.4,0.4,0);
	ObjRender_SetAngleZ(sigilObj,ang);
	Obj_SetRenderPriorityI(sigilObj,23);
	PlaySnd("se_boon01",60);
	
	stay;
	task stay{
		let inc = 0;
		alp;
		sca;
		
		task alp{
			ascent(i in 0..12){
				ObjRender_SetPosition(sigilObj,BossX,BossY,0);
				ObjRender_SetAlpha(sigilObj,i*16);
				yield;
			}
		}
		task sca{
			ascent(i in 0..34){
				ObjRender_SetScaleXYZ(sigilObj,1-0.6*sin(i*3),1-0.6*sin(i*3),0);
				yield;
			}
		}
		
		task expand{
			let sigilObj2 = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(sigilObj2,path);
			
			ObjSprite2D_SetSourceRect(sigilObj2,0,0,384,384);
			ObjSprite2D_SetDestRect(sigilObj2,-256,-534,256,24);
			ObjSprite2D_SetSourceRect(sigilObj2,0,0,512,512);
			ObjRender_SetBlendType(sigilObj2,BLEND_ADD_ARGB);
			ObjRender_SetColor(sigilObj2,colour[0],colour[1],colour[2]);
			ObjRender_SetScaleXYZ(sigilObj2,0.4,0.4,0);
			Obj_SetRenderPriorityI(sigilObj2,24);
			
			ascent(i in 0..32){
				if(!Obj_IsDeleted(bossObj)){
					ObjRender_SetPosition(sigilObj2,ObjRender_GetX(sigilObj),ObjRender_GetY(sigilObj),0);
					ObjRender_SetAngleZ(sigilObj2,ObjRender_GetAngleZ(sigilObj));
					ObjRender_SetScaleXYZ(sigilObj2,0.4+i/135,0.4+i/135,0);
					ObjRender_SetAlpha(sigilObj2,172-i*6);
					yield;
				}
			}
			Obj_Delete(sigilObj2);
		}
		
		wait(duration);
		expand;
		
		PlaySnd("se_kira00",45);
		while(!Obj_IsDeleted(bossObj)){
			ObjRender_SetPosition(sigilObj,BossX,BossY,0);
			if(type == "LEFT"){
				ObjRender_SetAngleZ(sigilObj,ang+12*sin(inc));
			}
			if(type == "RIGHT"){
				ObjRender_SetAngleZ(sigilObj,ang-12*sin(inc));
			}
			yield;
			inc+=1;
			if(inc % 160 == 0){expand;}
		}
		Obj_Delete(sigilObj);
	}
}


task end{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ yield;}
	SetCommonData("Is Spell?",false);
	Obj_Delete(bossObj);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	CloseScript(GetOwnScriptID());
}