#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Boss Spellcard 2"]
#Background["./Scene/Background.txt"]
#Text[" Freezing Blizzard - \"Cold-Induced Paralysis\" "]
#Player["./Player/Reimu/Reimu.txt"]
#System["./System/Default_System.txt"]

let bossObj;
let pobj = GetPlayerObjectID;

SetCommonData("IsFrozen",false);
SetCommonData("AlreadyFrozen",false);

#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(6750);
		}
		case(EV_REQUEST_SPELL_SCORE) {
			SetScriptResult(100000);
		}
		case(EV_REQUEST_TIMER) {
			SetScriptResult(60);
			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("Freezing Blizzard - \"Cold-Induced Paralysis\"");
	wait(75);
	SetBossHitbox(32,32);
	ConcentrateA1(bossObj,"Snow","AQUA",300,"Long");
	wait(120);
	Burst(bossObj,"Snow","AQUA","Long");
	
	summon;
	wait(160);
	loop{
		fire;
		wait(40);
		RandMove(bossObj,rand(72,160),rand(32,128), 50, 5, 32, 64, GetStgFrameWidth-32, GetStgFrameHeight/2-96);
		wait(310);
		fire2;
		wait(40);
		RandMove(bossObj,rand(72,160),rand(32,128), 50, 5, 32, 64, GetStgFrameWidth-32, GetStgFrameHeight/2-96);
		wait(310);
	}
}

task fire{
	let a = 13;
	let x = a*8;
	let dir = rand(0,359);
	let spin = 0;
	loop(a*4){
		PlaySnd("se_tan00",25);
		ascent(i in 0..4){
			let shot = CreateDNHShotA1(BossX+x*cos(spin),BossY+x*sin(spin),1+i/2,dir-16+i*8,60,5);
			let shot2 = CreateDNHShotA1(BossX+x*cos(spin+180),BossY+x*sin(spin+180),1+i/2,dir+180-16+i*8,60,5);
			yield;
			spin+=4;
		}
		x-=2;
		dir+=360/a;
	}
	x = 0;
	a = 14;
	dir = rand(0,359);
	PlaySnd("se_kira00",40);
	while(x<a){
		ascent(i in 0..8){
			let shot = CreateDNHShotA1(BossX,BossY,1+i/2,dir,278,5);
		}
		x++;
		dir-=360/a;
	}
}

task fire2{
	let a = 13;
	let x = a*8;
	let dir = rand(0,359);
	let spin = 0;
	loop(a*4){
		PlaySnd("se_tan00",25);
		ascent(i in 0..4){
			let shot = CreateDNHShotA1(BossX+x*cos(spin),BossY+x*sin(spin),1+i/2,dir+16-i*8,55,5);
			let shot2 = CreateDNHShotA1(BossX+x*cos(spin-180),BossY+x*sin(spin-180),1+i/2,dir-180+16-i*8,55,5);
			yield;
			spin-=4;
		}
		x-=2;
		dir-=360/a;
	}
	x = 0;
	a = 14;
	dir = rand(0,359);
	PlaySnd("se_kira00",40);
	while(x<a){
		ascent(i in 0..8){
			let shot = CreateDNHShotA1(BossX,BossY,1+i/2,dir,280,5);
		}
		x++;
		dir-=360/a;
	}
}

task summon{
	let a = 6;
	BossPhase = 2;
	ascent(i in 0..a){
		CreateIceSpot(i*360/a,4.5,"LEFT");			
		CreateIceSpot(i*360/a+360/(a*1.7),5.5,"RIGHT");
		
		yield;
	}
	wait(60);
	BossPhase = 1;
	ConcentrateA1(bossObj,"Snow","AQUA",240,"Short");
	wait(60);
	Burst(bossObj,"Snow","AQUA","Short");
}

function CreateIceSpot(ang,sp,dire){
	let shotObj = ObjShot_Create(OBJ_SHOT);
	ObjShot_Regist(shotObj);
	
	ObjMove_SetSpeed(shotObj,sp);
	ObjMove_SetAngle(shotObj,ang);
	ObjMove_SetAcceleration(shotObj,-0.1);
	ObjMove_SetMaxSpeed(shotObj,0);
	ObjShot_SetDelay(shotObj,0);
	ObjShot_SetSpellResist(shotObj,true);
	Obj_SetVisible(shotObj,false);
	ObjShot_SetIntersectionEnable(shotObj,false);
	ObjShot_SetAutoDelete(shotObj,false);
	
	ObjMove_SetPosition(shotObj,BossX,BossY);
	PlaySnd("se_kira00",50);
	SetIceGraphic(shotObj,ang,dire,32);
	return shotObj;
}

task SetIceGraphic(obj,ang,dire,size){
	let path = GetCurrentScriptDirectory ~ "./Img/Boss/Ice Sprite.png";
	let mistObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(mistObj,path);
	Freeze(obj,size);
	
	ObjSprite2D_SetSourceRect(mistObj,0,0,(size+2)*2,(size+2)*2);
	ObjSprite2D_SetDestCenter(mistObj);
	ObjSprite2D_SetSourceRect(mistObj,0,0,512,512);
	ObjRender_SetBlendType(mistObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(mistObj,0);
	ObjRender_SetColor(mistObj,160,255,255);
	ObjRender_SetScaleXYZ(mistObj,1,1,0);
	Obj_SetRenderPriorityI(mistObj,30);
	
	form(mistObj);
	stick;
	yield;
	wait(119);
	move(obj,ang);
	
	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);
	}
	
	task form(obj1){
		let s = 2;
		let rs = 0;
		
		loop(192/s){
			ObjRender_SetAlpha(obj1,rs);
			rs+=s;
			yield;
		}
		ObjRender_SetAlpha(obj1,192);
	}
	
	task move(obje,ang){
		if(dire == "LEFT"){
			ObjMove_SetAngle(obje,ang+135);
		}
		if(dire == "RIGHT"){
			ObjMove_SetAngle(obje,ang-135);
		}
		ObjMove_SetAcceleration(obje,0.04);
		ObjMove_SetMaxSpeed(obje,1.5);
		reflect(obje,mistObj);
	}
	
	
}

task reflect(obj,obj2){ 
		while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(obj2) && !Obj_IsDeleted(bossObj)){
			if(ObjMove_GetX(obj) <= 0){
				ObjMove_SetAngle(obj, -ObjMove_GetAngle(obj) + 180);
			}

			if(ObjMove_GetX(obj) >= GetStgFrameWidth){
				ObjMove_SetAngle(obj, -ObjMove_GetAngle(obj) + 180);
			}
			
			if(ObjMove_GetY(obj) <= 0){
				ObjMove_SetAngle(obj, -ObjMove_GetAngle(obj));
			}
			
			if(ObjMove_GetY(obj) >= GetStgFrameHeight){
				ObjMove_SetAngle(obj, -ObjMove_GetAngle(obj));
			}
			
			yield;
		}
	}

task Freeze(obj,siz){
		let PX = ObjMove_GetX(pobj);
		let PY = ObjMove_GetY(pobj);
		while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(bossObj)){
			if(GetDistance(ObjMove_GetX(pobj), ObjMove_GetY(pobj), ObjMove_GetX(obj), ObjMove_GetY(obj)) <= siz){
				SetCommonData("IsFrozen",true);
				if(GetCommonData("IsFrozen",true) == true && GetCommonData("AlreadyFrozen",false) == false){
				let objText = FrozenText(obj,PX-16,PY-24,siz);
				}
				SetCommonData("AlreadyFrozen",true);
				
				while(GetDistance(ObjMove_GetX(pobj), ObjMove_GetY(pobj), ObjMove_GetX(obj), ObjMove_GetY(obj)) <= siz){
					ObjRender_SetPosition(pobj,PX,PY,0);
					yield;
				}
				SetCommonData("AlreadyFrozen",false);
				SetCommonData("IsFrozen",false);
			}
			else{
				PX = ObjMove_GetX(pobj);
				PY = ObjMove_GetY(pobj);
				SetCommonData("AlreadyFrozen",false);
				SetCommonData("IsFrozen",false);
			}
			yield;
		}
	}

function FrozenText(obj,x,y,s){
		InstallFont(GetCurrentScriptDirectory ~ "./Lib/RussoOne-Regular.ttf");
		let textObj = ObjText_Create;
		ObjText_SetText(textObj,"Frozen!!");
		ObjText_SetFontType(textObj,"Russo One");
		
		ObjText_SetFontSize(textObj,18);
		ObjText_SetFontBold(textObj, true);
		ObjText_SetAutoTransCenter(textObj, true);		
		ObjText_SetHorizontalAlignment(textObj, ALIGNMENT_CENTER);
		ObjText_SetMaxWidth(textObj, 96);
		ObjText_SetFontColorTop(textObj,128,128,255);
		ObjText_SetFontColorBottom(textObj,0,255,255);
		ObjText_SetFontBorderColor(textObj,0,0,0);
		ObjText_SetFontBorderType(textObj,BORDER_FULL);
		ObjText_SetFontBorderWidth(textObj,1);
		
		Obj_SetRenderPriorityI(textObj,91);
		ObjRender_SetPosition(textObj,x,y,0);
		check;
		task check{
			while(GetDistance(ObjMove_GetX(pobj), ObjMove_GetY(pobj), ObjMove_GetX(obj), ObjMove_GetY(obj)) <= s){
				ObjRender_SetColor(pobj,64,64,255);
				yield;
			}
			SetCommonData("AlreadyFrozen",false);
			SetCommonData("IsFrozen",false);
			if(GetCommonData("IsFrozen",false) == false){
				ObjRender_SetColor(pobj,255,255,255);
				Obj_Delete(textObj);
			}
		}
		return textObj;
	}
	
	
task end{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){ yield;}
	SetCommonData("Is Spell?",false);
	Obj_Delete(bossObj);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	CloseScript(GetOwnScriptID());
}