#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Boss Nonspell 2"]
#Background["./Scene/Background.txt"]
#Text["  Letty Nonspell 2 "]
#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(5500);
		}
		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-128,10,5);
	wait(42);
	ConcentrateA1(bossObj,"Snow","AQUA",240,"Short");
	wait(60);
	SetBossHitbox(32,32);
	Burst(bossObj,"Snow","AQUA","Short");
	fire;
}

task fire{
	loop{
		FormMistAttack("LEFT");
		wait(200);
		RandMove(bossObj,rand(8,128),rand(8,96), 10, 5, 32, 64, GetStgFrameWidth-32, GetStgFrameHeight/2-72);
		wait(60);
		FormLaserAttack("LEFT");
		wait(200);
		RandMove(bossObj,rand(8,128),rand(8,96), 10, 5, 32, 64, GetStgFrameWidth-32, GetStgFrameHeight/2-72);
		wait(60);
		FormMistAttack("RIGHT");
		wait(200);
		RandMove(bossObj,rand(8,128),rand(8,96), 10, 5, 32, 64, GetStgFrameWidth-32, GetStgFrameHeight/2-72);
		wait(60);
		FormLaserAttack("RIGHT");
		wait(200);
		RandMove(bossObj,rand(8,128),rand(8,96), 10, 5, 32, 64, GetStgFrameWidth-32, GetStgFrameHeight/2-72);
		wait(60);
	}
}

task FormMistAttack(side){
	BossPhase = 2;
	if(side == "LEFT"){
		ascent(i in 0..20){
			let obj = mist(BossX+(200-i*10)*cos(90+i*18),BossY+(200-i*10)*sin(90+i*18),-1,90+i*18);
			SetMistGraphic(obj,rand(128,176),70);
			
			let obj1 = mist(BossX+(200-i*10)*cos(270+i*18),BossY+(200-i*10)*sin(270+i*18),-1,270+i*18);
			SetMistGraphic(obj1,rand(128,176),70);
			wait(3);
		}
	}
	if(side == "RIGHT"){
		ascent(i in 0..20){
			let obj = mist(BossX+(200-i*10)*cos(90-i*18),BossY+(200-i*10)*sin(90-i*18),-1,90-i*18);
			SetMistGraphic(obj,rand(128,176),70);
			
			let obj1 = mist(BossX+(200-i*10)*cos(270-i*18),BossY+(200-i*10)*sin(270-i*18),-1,270-i*18);
			SetMistGraphic(obj1,rand(128,176),70);
			wait(3);
		}
	}
	wait(30);
	BossPhase = 1;
}

task FormLaserAttack(side){

	BossPhase = 2;
	if(side == "LEFT"){
		let dir = rand(-90,90);
		PlaySnd("se_lazer01",55);
		ascent(i in 0..3){
			let obj = CreateDNHCurveLaserA1(BossX,BossY,8,dir,30,23,1017,0);
			CircleCurve(obj,8,"LEFT");
			ObjCrLaser_SetTipDecrement(obj,0.1);
			dir+=360/3;
		}
	}
	if(side == "RIGHT"){
		let dir = rand(-90,90);
		PlaySnd("se_lazer01",55);
		ascent(i in 0..3){
			let obj = CreateDNHCurveLaserA1(BossX,BossY,0,dir,26,23,1017,0);
			CircleCurve(obj,-8,"RIGHT");
			ObjCrLaser_SetTipDecrement(obj,0.1);
			dir+=360/3;
		}
	}
	wait(30);
	BossPhase = 1;
	
	
	task CircleCurve(id, curve, side){ //id = Bullet id, curve = Angular Velocity

		//Use : let id = CreateDNHShot/CreateCurveLaser... in order to recognize the bullets' ID
		ObjMove_SetAngularVelocity(id, curve); 
		ObjMove_SetSpeed(id, 0);
		ObjMove_SetAcceleration(id,curve/100);
		ObjMove_SetMaxSpeed(id,curve);
		ObjShot_SetIntersectionEnable(id,false);
		
		wait((360/curve)/2); //The angle has to attain 90° in order to start a circle delay
		
		ObjMove_SetAngularVelocity(id, curve/2); 
		wait(70);
		fireshots(id,side);
		
		task fireshots(ob,side){
			if(side == "LEFT"){
				ascent(i in 0..25){
					PlaySnd("se_tan02",45);
					ascent(k in 0..4){
						if(!Obj_IsDeleted(ob) && !Obj_IsDeleted(bossObj)){
							CreateDNHShotA1(ObjMove_GetX(ob),ObjMove_GetY(ob),4-k/2.5,ObjMove_GetAngle(ob)+i*k*3,23,5);
							
							yield;
						}
					}
				}
			}
			if(side == "RIGHT"){
				ascent(i in 0..25){
					PlaySnd("se_tan02",45);
					ascent(k in 0..4){
						if(!Obj_IsDeleted(ob) && !Obj_IsDeleted(bossObj)){
							CreateDNHShotA1(ObjMove_GetX(ob),ObjMove_GetY(ob),4-k/2.5,180+ObjMove_GetAngle(ob)-i*k*3,23,5);
							
							yield;
						}
					}
				}
			}
			kill(ob);
		}
		
		task kill(o){
			ascent(j in 0..64){
				ObjRender_SetAlpha(o,255-j*4);
				yield;
			}
			Obj_Delete(o);
		}
	}
}

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