#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell5H"]

#include"./functions1.txt"

timer = 180;
let enemy_life_M = 1750;
let enemy_life = enemy_life_M;

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy,0,0);
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	
	TEnemyWork;
	ObjEnemy_Render;
	ObjEffect_Shadow(false);
	TLife(objEnemy,540,20,20,0,0,11.75,0);
	TEnd;
}

@MainLoop
{
	timer-=1/60;
	enemy_life = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
	if(IsPlayerSpellActive == false)
		{
		EnemySpell3 = false;
		ObjEnemy_SetDamageRate(objEnemy,11.75,0);
		}
	else
		{
		EnemySpell3 = true;
		ObjEnemy_SetDamageRate(objEnemy,0,0);
		}
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(enemy_life_M);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(timer);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
	case(EV_START_BOSS_SPELL)
		{
		SetScriptResult(true);
		}
}

task TEnemyWork
{
	TSpellName("\"Praise the Goddess of Inovation\"");
	TXY;
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/8,15,5);
	loop(90){yield;}
	ascent(i in 0..2)
		{
		i = i*2-1;
		CreateStraightLaserA2(GetStgFrameWidth/2+i*3*GetStgFrameWidth/7,GetStgFrameHeight+32,20,270-i*2.5,GetStgFrameHeight+64,24,10^10,DS_ST_BEAM_RED,90);
		CreateStraightLaserA2(GetStgFrameWidth/2+i*(GetStgFrameWidth/2+32),GetStgFrameHeight/8+i*24,20,90+i*90,GetStgFrameWidth+64,24,10^10,DS_ST_BEAM_RED,90);
		}
	loop(90){yield;}
	PlaySound(se_Laser,80);
	loop(60){yield;}
	let graph = 0;
	let dir = 1;	
	let numb = 0.275;
	let accel = 0.0125;
	let switch = false;
	while(enemy_life > enemy_life_M*0.8 && timer > 30)
		{
		TFam(dir,MAGIC_BLUE+graph%4);
		loop(180)
			{
			if(enemy_life > enemy_life_M*0.8 && timer > 30)
				{
				yield;
				}
			}
		dir*=-1;
		graph+=1;
		}
	switch = true;
	loop(1){yield;}

	numb = 0.225;
	accel = 0.0175;
	switch = false;
	while(enemy_life > enemy_life_M*0.4 && timer > 30)
		{
		TFam(dir,MAGIC_BLUE+graph%4);
		loop(180)
			{
			if(enemy_life > enemy_life_M*0.4 && timer > 30)
				{
				yield;
				}
			}
		dir*=-1;
		graph+=1;
		}
	switch = true;
	loop(1){yield;}

	numb = 0.175;
	accel = 0.0175;
	switch = false;
	while(enemy_life > 0 && timer > 30)
		{
		TFam(dir,MAGIC_BLUE+graph%4);
		loop(180)
			{
			if(enemy_life > 0 && timer > 30)
				{
				yield;
				}
			}
		dir*=-1;
		graph+=1;
		}
	switch = true;
	loop(1){yield;}

	numb = 0.5;
	accel = 0.0225;
	switch = false;
	while(timer > 0)
		{
		TFam(dir,MAGIC_BLUE+graph%4);
		loop(60)
			{
			if(timer > 0)
				{
				yield;
				}
			}
		dir*=-1;
		graph+=1;
		}
	switch = true;
	loop(1){yield;}

	task TFam(dir,graph)
		{
		PlaySound(se_DeepShotHigh,80);
		let graphB = DS_BILL_RED;
		alternative(graph)
			case(MAGIC_BLUE)
				{
				graphB = DS_BILL_SKY;
				}
			case(MAGIC_GREEN)
				{
				graphB = DS_BILL_YELLOW;
				}
			case(MAGIC_PURPLE)
				{
				graphB = DS_BILL_PURPLE;
				}
		let obj = CreateEnemy(graph,false);
		let x = GetStgFrameWidth/2;
		let y = GetStgFrameHeight/2-15;
		let dist = GetDistance(enmx,enmy,x,y);
		let angleM = 270-dir*rand(0,15);
		let angle = 0;
		let counter = -1;
		let counter2 = 0;
		let spread = 4;
		let sound = 0;
		while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) > 0 && dist > 0 && switch == false)
			{
			sound+=1;
			ObjMove_SetPosition(obj,x+dist*cos(angleM),y+dist*sin(angleM));
			if(dist < 75)
				{
				ObjEnemy_SetIntersectionCircleToPlayer(obj,x+dist*cos(angleM),y+dist*sin(angleM),16);
				}
			angleM+=dir*3;
			dist-=numb;
			if(counter%4 == 0)
				{
				counter2+=1;
				let obj2 = CreateShotOA1(obj,0,atan2(ObjMove_GetY(obj)-y,ObjMove_GetX(obj)-x)-dir*(counter2-spread/2+0.5)*35/spread+dir*angle,graphB,16);
				ObjMove_AddPatternA2(obj2,spread-counter2,NO_CHANGE,NO_CHANGE,accel,0,10);
				ObjShot_SetSpellResist(obj2,true);
				if(counter2 == spread)	
					{
					if(sound < 180)
						{
						PlaySound(se_CuteShotLong,75);
						}
					counter2 = 0;
					counter+=1;
					angle+=numb*6.25;
					}
				}
			else
				{
				counter+=1;
				}
			yield;
			}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
			{
			Obj_Delete(obj);
			return;
			}
		let alph = 255;
		let scale = 1;
		loop(30)
			{
			alph-=255/30;
			scale+=1/30;
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			ObjRender_SetAlpha(obj,alph);
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
				{
				Obj_Delete(obj);
				return;
				}
			yield;
			}
		Obj_Delete(obj);
		}
}		

task TMovement
{
	let distM = 25;
	let distm =distM/2;
	let numb = round(rand(0,1))*2-1;
		if(ObjMove_GetX(objEnemy) < 175)
			{
			numb =1;	
			}
		else if (ObjMove_GetX(objEnemy) > GetStgFrameWidth-175)
			{	
			numb = -1;
			}
	ObjMove_SetDestAtWeight(objEnemy,ObjMove_GetX(objEnemy)+numb*rand(distm,distM),rand(120,170),30,5);
		
}