#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Boss Nonspell 4"]
#Background["./Scene/Background.txt"]
#Text["  Letty Nonspell 4 "]
#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 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(4150);
		}
		case(EV_REQUEST_TIMER) {
			SetScriptResult(35);
			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-64,10,5);
	wait(42);
	ConcentrateA1_Rainbow(bossObj,"Snow",240);
	wait(120);
	SetBossHitbox(32,32);
	Burst_Rainbow(bossObj,"Snow","Short");
	summon;
	wait(120);
	fire;
	FormMistSpree;
}

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,30);
	PlaySnd("se_boon01",60);
	
	stay;
	task stay{
		let inc = 0;
		alp;
		sca;
		
		task alp{
			ascent(i in 0..16){
				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,30);
			
			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,255-i*8);
					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 fire{
	loop{
		ascent(k in 0..20){
			PlaySnd("se_tan02",30);
			ascent(i in 0..9){
				let obj = CreateDNHShotA1(BossX,BossY,12,90-80+i*20-10+k,10,10);
				ObjMove_SetAcceleration(obj,-0.5);
				ObjMove_SetMaxSpeed(obj,0.6);
				ac;
				
				task ac{
					wait(70);
					ObjMove_SetAcceleration(obj,0.2);
					ObjMove_SetMaxSpeed(obj,5);
				}
			}
			wait(3);
		}
		wait(50);
		descent(k in 0..20){
			PlaySnd("se_tan02",30);
			descent(i in 0..9){
				let obj = CreateDNHShotA1(BossX,BossY,12,90-80+i*20-10+k,12,10);
				ObjMove_SetAcceleration(obj,-0.5);
				ObjMove_SetMaxSpeed(obj,0.6);
				ac;
				
				task ac{
					wait(70);
					ObjMove_SetAcceleration(obj,0.2);
					ObjMove_SetMaxSpeed(obj,5);
				}
			}
			wait(3);
		}
		wait(50);
	}
}

task FormMistSpree{
	loop{
		let obj = mist(rand(4,GetStgFrameWidth-4),rand(4,72),0,rand(0,359));
		SetMistGraphic(obj,rand(96,128),70);
		wait(13);
	}
}

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);
	Obj_SetVisible(shotObj,false);
	ObjShot_SetIntersectionEnable(shotObj,false);
	
	PlaySnd("se_kira00",25);
	
	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",20);
				let bulletObj = CreateDNHShotA1(ObjMove_GetX(obj)+rand(-32,32),ObjMove_GetY(obj)+rand(-32,32),8,rand(40,230),rand(39,41),0);
				ObjMove_SetAcceleration(bulletObj,-1);
				ObjMove_SetMaxSpeed(bulletObj,rand(0.5,1));
				accel;
				task accel{
					wait(130);
					ObjMove_SetAcceleration(bulletObj,0.0625);
					ObjMove_SetMaxSpeed(bulletObj,rand(2,4.25));
				}
				if(i % 3 == 0){yield;}
			}
		}
	}

	task delete{	
		Obj_Delete(obj);
	}

	
}


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