#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Boss Spellcard 3"]
#Background["./Scene/Background.txt"]
#Text[" Flurry of Frost - \"Blizzard Barrage\" "]
#Player["./Player/Reimu/Reimu.txt"]
#System["./System/Default_System.txt"]

let bossObj;
let pobj = GetPlayerObjectID;

#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(9250);
		}
		case(EV_REQUEST_SPELL_SCORE) {
			SetScriptResult(100000);
		}
		case(EV_REQUEST_TIMER) {
			SetScriptResult(70);
			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);
	}
	SetCommonData("Boss Type","LETTY");
	let sceneObj = GetEnemyBossSceneObjectID;
	ObjEnemyBossScene_StartSpell(sceneObj);
	CutIn("Flurry of Frost - \"Blizzard Barrage\"");
	wait(75);
	SetBossHitbox(32,32);
	ConcentrateA1_Rainbow(bossObj,"Snow",240);
	wait(120);
	Burst_Rainbow(bossObj,"Snow","Short");
	summon;
	wait(90);
	blizzard;
}

task blizzard{

	let inc = 0;
	let arr = [11,10,9,5,2];
	let blizz;
	let blizz1;
	loop{
		lines;
		blizr;
		wait(960);
		fire;
		blizl;
		wait(960);
	}
	
	task blizr{
		PlaySnd("se_noise",60);
		loop(180){
			blizz = CreateBlizzard(inc*2,5.5);
			blizz1 = CreateBlizzard(inc*2+180,5.5);
			wait(5);
			inc+=5;
		}
		inc = 0;
	}
	
	task blizl{
		PlaySnd("se_noise",60);
		loop(180){
			blizz = CreateBlizzard(inc*2,5.5);
			blizz1 = CreateBlizzard(inc*2+180,5.5);
			wait(5);
			inc-=5;
		}
		inc = 0;
	}
	
	
	task lines{
		wait(60);
		ConcentrateA1(bossObj,"Snow","AQUA",240,"Short");
		wait(60);
		Burst(bossObj,"Snow","AQUA","Short");
		
		let a = 6;
		let x = 0;
		loop(5){
			let r = rand(0,30);
			ascent(i in 0..4){
				while(x<a){
				PlaySnd("se_tan02",25);
					let obj = CreateDNHShotA1(0,r+(i*(GetStgFrameWidth/3)+96/a*x),rand(1.25,2.5),0+10*sin(x),10,20);
					let obj1 = CreateDNHShotA1(GetStgFrameWidth,GetStgFrameHeight-r-(i*(GetStgFrameWidth/3)+96/a*x),rand(1.25,2.5),180+10*sin(x),10,20);
					
					x++;
					wait(6);
				}
				x = 0;
				wait(5);
			}
			wait(10);
		}
	}
	
	task fire{
		wait(60);
		ConcentrateA1(bossObj,"Snow","AQUA",240,"Short");
		wait(60);
		
		let a = 12;
		let x = 0;
		let twist = 172;
		let acc = 0.25;
		let dir = rand(0,359);
		ascent(i in 0..15){
			PlaySnd("se_kira01",40);
			while(x<a){
				let obj = CreateDNHShotA1(BossX+(30+(i*20))*cos(dir+i*twist),BossY+(30+(i*20))*sin(dir+i*twist),0,dir+i*twist,arr[floor(i/3)],30);
				ObjShot_SetAutoDelete(obj,false);
				ObjShot_SetSpellResist(obj,true);
				go(i,dir);
				
				task go(s,d){
					let inc = 0;
					loop(860){
						if(!Obj_IsDeleted(bossObj)){
						ObjMove_SetPosition(obj,BossX+(30+(s*20+inc/2.5))*cos(d+s*twist+inc),BossY+(30+(s*20+inc/2.5))*sin(d+s*twist+inc));
						ObjMove_SetAngle(obj,ObjMove_GetAngle(obj)+0.3);
						inc+=acc;
						yield;
						}
					}
					ObjShot_FadeDelete(obj);
				}
				x++;
				dir+=360/a;
			}
			x = 0;
			wait(5);
		}
	}
	
}

function CreateBlizzard(ang,speed){
	let shotObj = ObjShot_Create(OBJ_SHOT);
	ObjShot_Regist(shotObj);
	
	ObjMove_SetSpeed(shotObj,speed);
	ObjMove_SetAngle(shotObj,ang);
	ObjMove_SetMaxSpeed(shotObj,0);
	ObjShot_SetDelay(shotObj,0);
	ObjShot_SetSpellResist(shotObj,true);
	Obj_SetVisible(shotObj,false);
	ObjShot_SetIntersectionEnable(shotObj,false);
	ObjShot_SetAutoDelete(shotObj,true);
	
	PlaySnd("se_kira00",25);
	SetBlizzardGraphic(shotObj,ang,48);
	ObjMove_SetPosition(shotObj,BossX,BossY);
	return shotObj;
}

task SetBlizzardGraphic(obj,ang,size){
	let path = GetCurrentScriptDirectory ~ "./Img/Boss/Mist Sprite.png";
	let blizzardObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(blizzardObj,path);
	pushaway(obj,size);
	
	ObjSprite2D_SetSourceRect(blizzardObj,0,0,(size+2)*2,(size+2)*2);
	ObjSprite2D_SetDestCenter(blizzardObj);
	ObjSprite2D_SetSourceRect(blizzardObj,0,0,128,128);
	ObjRender_SetBlendType(blizzardObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(blizzardObj,0);
	ObjRender_SetColor(blizzardObj,160,255,255);
	ObjRender_SetScaleXYZ(blizzardObj,1,1,0);
	ObjRender_SetAngleZ(blizzardObj,ang);
	Obj_SetRenderPriorityI(blizzardObj,30);
	
	form(blizzardObj);
	stick;
	
	task stick{
		let incr = 0;
		while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(blizzardObj) && !Obj_IsDeleted(bossObj)){
			ObjRender_SetPosition(blizzardObj,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			ObjRender_SetScaleXYZ(blizzardObj,1+incr,1+incr,0);
			incr+=1/32;
			yield;
		}
		Obj_Delete(obj);
		Obj_Delete(blizzardObj);
	}
	
	task form(obj1){
		let s = 20;
		let rs = 0;
		
		loop(255/s){
			ObjRender_SetAlpha(obj1,rs);
			rs+=s;
			yield;
		}
		ObjRender_SetAlpha(obj1,255);
		wait(65);
		Obj_Delete(obj1);
	}
	
}

task pushaway(obj,size){
	let angle = 0;
	let speed = 0;
	let acc = 0.2;
	let s = size;
	SetCommonData("Getting Pushed Away?",false);
	
	check;
	
	while(!Obj_IsDeleted(bossObj) && !Obj_IsDeleted(obj)){
		angle = atan2(GetPlayerY-BossY,GetPlayerX-BossX);
		if(GetPlayerState == STATE_NORMAL){
			ObjMove_SetPosition(GetPlayerObjectID,GetPlayerX+speed*cos(angle),GetPlayerY+speed*sin(angle));
		}
		
		if(GetCommonData("Getting Pushed Away?",false) == true){
			if(speed < 0.5){
				speed+=acc;
			}
		}
		else if(GetCommonData("Getting Pushed Away?",false) == false){
			if(speed > acc){
				speed-=acc;
			}
		}
		
		yield;
	}
	
	task check{
		while(!Obj_IsDeleted(bossObj) && !Obj_IsDeleted(obj)){
		s+=1;
			if(GetDistance(ObjMove_GetX(GetPlayerObjectID), ObjMove_GetY(GetPlayerObjectID), ObjMove_GetX(obj), ObjMove_GetY(obj)) <= s){
				SetCommonData("Getting Pushed Away?",true);
			}
			else{
				SetCommonData("Getting Pushed Away?",false);
			}
			yield;
		}
	}
}

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 end{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ yield;}
	SetCommonData("Is Spell?",false);
	Obj_Delete(bossObj);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	CloseScript(GetOwnScriptID());
}