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

#include "./functions2.txt"

timer = 41+Dif;

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy,0,0);

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	bspell = true;

	TEnemyWork;
	ObjEnemy_Render; 
	TLife(600,20,20,0,0,10,10);
	TEnd;
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(500/spellLife);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(timer);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}

task TEnemyWork
{
	TXY;
	TSpellName;
	TSpellFace(pathReisen);
	loop(60){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-90,15,5);
	loop(75){yield;}
	let density = 2+Dif;

	let time1 = 240;
	let angleA = 15;
	let angleB;
	let time2;
	let time3 = 30;
	let spreadM = 7;
	let spread = 0;
	let dirM = 1;

	let xM;
	let yM;
	loop(5)
		{
		angleB = (360-(angleA*2)%360)/time1;
		time2 = floor((360-angleB*time1)/(angleB));
		EnemySpell = 2;
		loop(30){yield;}
		xM = 0;
		yM = 0;		
		PlaySound("se_Laser",80);
		PlaySound("se_LaserDelay",130);
		ascent(i in 0..2)
			{
			TLaser(i*2-1);
			}
		loop(time1-70){yield;}
		PlaySound("se_Charge",135);
		loop(6)
			{
			TCharge(objEnemy);
			loop(10){yield;}
			}
		loop(10){yield;}
		PlaySound("se_Phantom1",200);
		let sound = PlaySound("se_Phantom2",175);
		Spell = true;
		loop(time2+30)
			{
			xM = rand(-1,1)*1;
			yM = rand(-1,1)*1;
			yield;
			}	
		loop(time3){yield;}
		if(spread > 0)
			{
			PlaySound("se_Shot7",115);
			}
		Spell = false;
		descent(t in 0..15)
			{
			ObjSound_SetVolumeRate(musicObj,t*175/15);
			yield;
			}
		loop(30){yield;}
		EnemySpell = 0;
		loop(15){yield;}
		ObjEnemy_Move(objEnemy,10,2.5,20,20,75,105,125,0);
		dirM = -absolute(cos(ObjMove_GetAngle(objEnemy)))/cos(ObjMove_GetAngle(objEnemy));
		loop(30){yield;}	
		spreadM-=1;
		if((spreadM <= 0 && spread < 1+Dif) || spread == 0)
			{
			spread += 1;
			}
		angleA+=5+Dif*1.25;
		if(time3 > 10)	
			{
			time3-=10;
			}
		}
	task TLaser(dir)
		{
		let angleM = atan2(GetPlayerY-(enmy-8),GetPlayerX-enmx);
		let angle = dir*angleA;
		let laser = CreateLaserA3(enmx,enmy-8,20,angleM+angle,GetStgFrameHeight*1.5,5,time1+time2,DS_BEAM_RED,0,45);
		let counter = 0;
		let angleS = 0;
		while(Spell == false)
			{
			counter+=1;
			angle+=dir*angleB;
			ObjStLaser_SetAngle(laser,angleM+angle);
			if(counter%max(1,spreadM) == 0 && spread > 0)
				{
				if(dir == 1)
					{
					PlaySound("se_Shot2",75);
					}
				loop(spread)
					{
					ascent(i in 0..density)
						{
						TSpam(angleM+angleS+(i+dir*1/16)*360/density,time1-counter,dir*(((angleS/12.5)%2)*2-1),2.75+0.25*(counter%4));
						}
					angleS += dirM*12.5;
					}
				}
			yield;
			}
		ObjRender_SetColorHSV(laser,0,255,255);
		ObjShot_SetIntersection(laser,false);
		ObjShot_SetScale(laser,3);
		descent(t in 0..time2)
			{
			angle+=dir*angleB;
			ObjStLaser_SetAngle(laser,angleM+angle);
			if(t%2 == 0)
				{
				TLaser(t);
				}
			yield;
			}
		task TLaser(timeA)
			{		
			let shot = CreateLaserA3(enmx,enmy-8,20,ObjStLaser_GetAngle(laser),GetStgFrameHeight*1.5,3,timeA*2+time3*2+30,DS_BEAM_RED,0,20);
			ObjRender_SetColorHSV(shot,0,255,255);
			ObjShot_SetIntersection(shot,false);
			while(Spell == true){yield;}
			ObjShot_SetScale(shot,5);
			ObjRender_SetColorHSV(shot,0,0,255);
			ObjShot_SetIntersection(shot,true);
			loop(timeA)
				{
				ObjStLaser_SetAngle(shot,ObjStLaser_GetAngle(shot)+dir*angleB);
				yield;
				}
			}
		}
	task TSpam(angle,time,dir,speed)
		{
		let scale = 0.225;
		let shot = CreateBulletA2(enmx,enmy-8,speed,angle,-speed/time,0,0,DS_BALL_L_BLUE,4,scale);	
		while(Spell == false){if(Obj_IsDeleted(shot)){return;}yield;}
		let x = ObjMove_GetX(shot);
		let y = ObjMove_GetY(shot);
		ObjRender_SetColorHSV(shot,35,155,255);
		ObjMove_SetSpeed(shot,0);
		ObjMove_SetAcceleration(shot,0);
		ObjMove_SetMaxSpeed(shot,0);
		ObjMove_SetAngle(shot,angle+dir*90);		
		let scaleM = 0.625+Dif*0.15;
		while(Spell == true)
			{
			if(Obj_IsDeleted(shot)){return;}
			if(scale < scaleM)
				{
				scale+=(scaleM-0.225)/(time2-10);
				}
			else
				{
				scale = scaleM;
				}
			ObjShot_SetScale(shot,scale);
			ObjMove_SetPosition(shot,x+xM,y+yM);
			yield;
			}
		ObjRender_SetColorHSV(shot,0,0,255);
		ObjMove_SetPosition(shot,x,y);
		ObjMove_SetAcceleration(shot,1/(45-Dif*10));
		ObjMove_SetMaxSpeed(shot,1.5);
		}
			
}	