#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 60;

@Initialize
{
	Obj_SetValue(GetEnemyBossSceneObjectID,"INIT",false);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	bspell = true;

	TEnemyWork;
	ObjEnemy_Render;
	TBackground;
	TLife(270,20,32,1,0.5);
	TEnd(false);
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(1850/spellLife);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(10^10);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}
task TEnemyWork
{
	TXY;
	TSpellName("\"The World Turns Like a Page in My Hand\"");
	loop(75){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-105,20,5);
	loop(90){yield;}
	let spread = [5+Dif];
	spread = spread ~ [floor(GetStgFrameWidth*(1/(26-Dif*6))/spread[0])];
	spread = spread ~ [7,2,14+Dif*6];

	let speedMA = 1.5+Dif*0.5;
	let timeTime = 265;
	let timeStop = false;
	let timeDir = 1;
	loop
		{	
		PlaySound("se_Shot2",70);
		PlaySound("se_Laser",25);
		TLaser;
		loop(210-Dif*25-30){yield;}
		PlaySound("se_Charge",100);
		loop(30){yield;}
		FTime;
		loop(ceil(GetStgFrameHeight*0.372/speedMA)-90){yield;}
		}
	task TLaser
		{
		let x = GetStgFrameWidth*0.5;
		let speed = GetStgFrameWidth/90;
		let len = 0;
		let laser = CreateLaserA2(x,0,0,0,0,10,10^10,ST_LASER,90,SHOT_ORANGE);
		ObjRender_SetColorHSV(laser,45,155,255);
		ObjStLaser_SetSource(laser,false);
		ObjShot_SetAutoDelete(laser,false);
		ascent(i in 0..2)
			{
			i = i*2-1;
			let speedY = (90^2-89^2)*(GetStgFrameHeight*0.5)/(90^2);
			let shot = CreateBulletA1(x,-16+speed*0.5,0,0,BL_SMALL,0,1.125,SHOT_BLUE);
			ObjShot_SetGraphic(shot,BL_SMALL_BLUE-1);
			ObjMove_AddPatternB2(shot,0,i*speed,speedY,0,-speedY/90,0,0);
			ObjShot_SetSpellResist(shot,true);
			ObjShot_SetDeathResist(shot,true);
			}
		descent(t in 0..90)
			{
			if(Obj_IsDeleted(laser)){return;}
			x -= speed;
			len += speed*2;
			let y = (90^2-t^2)*(GetStgFrameHeight*0.5)/(90^2)-16;
			ObjLaser_SetLength(laser,len);
			ObjMove_SetPosition(laser,x,y);
			yield;
			}
		PlaySound("se_Laser",75);
		local
			{
			ascent(dir in 0..2)
				{	
				let x = GetStgFrameWidth*dir;
				let y = ObjMove_GetY(laser);
				let angleP = atan2(GetPlayerY-y,GetPlayerX-x);
				ascent(i3 in 0..spread[3])
					{
					let speed = 0.3+Dif*0.125+i3*(0.75+Dif*0.125)/(spread[3]-1);
					ascent(i4 in 0..spread[4])
						{
						TimeShotA2(x,y,speed,(i4*180/spread[4]+angleP)%180-90+dir*180,speed/360,0,speed*2,BL_SMALL,8,1,SHOT_BLUE,1);
						}
					}
				}
			}
		let pattern = [];
		let num0 = [];
		let num1 = [];
		loop(spread[0])
			{
			pattern = pattern ~ [floor(rand(0,2))];
			num0 = num0 ~ [0];
			num1 = num1 ~ [0];
			}
		let c = -1;
		let count = 20-Dif*3;
		ascent(t in 0..75+30)
			{
			if(Obj_IsDeleted(laser)){return;}
			c+=1;
			let y = (75-t)*GetStgFrameHeight*0.5/(75)-16;
			ObjMove_SetPosition(laser,x,y);	
			if(c%3 == 0)
				{
				PlaySound("se_Shot2",45);
				}
			ascent(i0 in 0..spread[0])
				{
				if(c%count == 0)
					{
					pattern[i0] = floor(rand(0,2));
					if(pattern[i0] == 0)
						{
						num0[i0] = 1;
						num1[i0] = 0;
						}
					else
						{
						num0[i0] = 0;
						num1[i0] = floor(rand(0,spread[2]));
						}
					}
				if((c-floor(c/20)*count)%spread[2] == 0)
					{
					num1[i0] = floor(rand(0,spread[2]));
					}
				if((pattern[i0] == 1 && c%spread[2] == num1[i0]) || c%count == 0)
					{	
					let spreadA = 1;
					if(pattern[i0] == 0)	
						{
						spreadA = spread[1];
						}
					PlaySound("se_Shot3",65);
					ascent(i1 in 0..spreadA)
						{
						TimeShotA2((i0+(i1+rand(0,num0[i0]))/spreadA)*GetStgFrameWidth/(spread[0]-1),y,4.3,90,(speedMA-4.3)/20,0,speedMA,DOUBLE,8,1,SHOT_PURPLE,0);
						}
					}
				}
			yield;
			}		
		Obj_Delete(laser);
		}

	function FTime
		{
		loop(30){yield;}
		PlaySound("se_LaserDelay",120);
		timeStop = true;
		IsTimeStop = true;
		let x = enmx;
		let switch = false;
		ascent(t in 0..timeTime)
			{		
			let xA = x+timeDir*(GetStgFrameWidth+64)*(0.5-0.5*cos(180*(t^1.5)/(timeTime^1.5)));
			if(timeDir < 0 && xA < -32)
				{
				xA += GetStgFrameWidth+64;
				if(switch == false)
					{
					ObjMove_SetX(objEnemy,xA);
					switch = true;
					}
				}
			else if(timeDir > 0 && xA > GetStgFrameWidth+32)
				{
				xA -= GetStgFrameWidth+64;
				if(switch == false)
					{
					ObjMove_SetX(objEnemy,xA);
					switch = true;
					}
				}
			Obj_SetValue(objScene,"TIME_LOOP_X",xA);
			ObjMove_SetDestAtFrame(objEnemy,xA,enmy,1);
			yield;
			}
		loop(30){yield;}
		PlaySound("se_Shot7",80);
		timeStop = false;
		IsTimeStop = false;
		loop(30){yield;}
		timeDir*=-1;
		}
	task TimeShotA1(x,y,speed,angle,graph,delay,scale,g,type)
		{
		TimeShotA2(x,y,speed,angle,0,0,0,graph,delay,scale,g,type);
		}
	task TimeShotA2(x,y,speed,angle,accel,velo,mspeed,graph,delay,scale,g,type)
		{
		let count = 0;
		let shot = CreateBulletA2(x,y,speed,angle,accel,velo,mspeed,graph,delay,scale,g);
		
		while(!Obj_IsDeleted(shot))
			{
			while(timeStop == false){yield;if(Obj_IsDeleted(shot)){return;}}

			x = ObjMove_GetX(shot);
			speed = ObjMove_GetSpeed(shot);
			angle = ObjMove_GetAngle(shot);

			ObjMove_SetSpeed(shot,0);
			ObjMove_SetAngle(shot,angle);
			ObjMove_SetAcceleration(shot,0);
			ObjMove_SetAngularVelocity(shot,0);
			ObjMove_SetMaxSpeed(shot,0);
			ObjShot_SetGraphic(shot,graph[SHOT_GRAY]);
			ObjShot_SetSpellResist(shot,true);
	
			loop(15){yield;}
			ascent(t in 0..timeTime)
				{		
				let xA = x+timeDir*(GetStgFrameWidth+64)*(0.5-0.5*cos(180*(t^1.5)/(timeTime^1.5)));
				if(timeDir < 0 && xA < -32)
					{
					xA += GetStgFrameWidth+64;
					}
				else if(timeDir > 0 && xA > GetStgFrameWidth+32)
					{
					xA -= GetStgFrameWidth+64;
					}
				ObjMove_SetX(shot,xA);
				yield;
				if(Obj_IsDeleted(shot)){return;}
				}
			while(timeStop == true){yield;if(Obj_IsDeleted(shot)){return;}}

			ObjShot_SetGraphic(shot,graph[g]);

				ObjShot_SetSpellResist(shot,false);

			ObjMove_SetSpeed(shot,speed);
			ObjMove_SetAngle(shot,angle);
			ObjMove_SetAcceleration(shot,accel);
			ObjMove_SetAngularVelocity(shot,velo);
			ObjMove_SetMaxSpeed(shot,mspeed);
			}
		}
}