#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include"./functions.txt"

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	TXY;
	
	TEnemyWork;
	ObjEnemy_RenderYuk(objEnemy);
	SetShotAutoDeleteClip(64,64,64,64);
	TEnd;
}

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

task TEnemyWork
{
	ObjEnemy_SetDamageRate(objEnemy,0,0);
	
	yield;
	TCaution(1);
	let xC = GetStgFrameWidth*0.5;
	let yC = GetStgFrameHeight*0.5-105;

	ObjMove_SetDestAtWeight(objEnemy,xC,yC,15,5);
	
	TSpellName("Rainbow Rave Level 2 \"Electric Boogaloo\"");
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	loop(210){yield;}
	PlayMusic(3);

	ObjEnemy_SetDamageRate(objEnemy,3.5,0);

	let counter = -1;
	let spread = 8;

	let dir = 1;

	while(!Obj_IsDeleted(objEnemy))
		{
		counter+=1;
		if(counter%22 == 0)
			{
			if(counter > 0)
				{
				let x = GetStgFrameWidth*(0.5+dir*rand(0.1,0.15));
				let y = yC+rand(-1,1)*30;
				let angle = rand(0,360);
				ascent(i in 0..spread)
					{			
					let speed = rand(1.25,2.5);
					let shot = CreateShotA2(x,y,7,(i+rand(-1,1)*0.125)*360/spread+angle,(speed-7)/30,speed,KUNAI_RED-1+floor(rand(0,5)*2),8);
					ObjMove_SetAngularVelocity(shot,dir*1/45);
					ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
					ObjRender_SetScaleXYZ(shot,1.175,1.175,0);
					}	
				}
			ObjMove_SetDestAtFrame(objEnemy,xC+dir*75,yC+15*cos(counter*4),22);
			dir*=-1;
			}
		if(counter == 710)
			{
			TRin;
			}
		if(counter == 1440)
			{
			TClown;
			}
		if(counter == 2140)
			{
			TTenji;
			}
		if(counter == 2870)
			{
			TIku;
			ObjEnemy_SetDamageRate(objEnemy,4.5,2.5);
			}
		yield;
		}

	task TIku
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetPosition(obj,xC+200,yC+75,0);
		let img = GetCurrentScriptDirectory ~ "img/rave/iku.png";
		ObjPrim_SetTexture(obj,img);
		ObjRender_SetScaleXYZ(obj,-0.85,0.85,0);
		Obj_SetRenderPriority(obj,0.35);
		
		let rx;
		let ry;
		let rxA = 324/6;
		let ryA = 300/3;
	
		let counter = 0;	

		while(!Obj_IsDeleted(objEnemy))
			{
			counter+=1;
			rx = floor(counter/5)%6;
			ry = floor(counter/(5*6))%3;
			ObjRender_SetAlpha2(obj,min(60,counter)*255/60);
			ObjSprite2D_SetSourceRect(obj,rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA);
			ObjSprite2D_SetDestCenter(obj);
			if(ObjEnemy_GetGlow(obj) == true && counter%4 == 0)
				{
				TGlow(obj,img,[rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA],[285,-135],1.5);
				}
			if(counter > 90 && counter%5 == 0)
				{
				ascent(i0 in 0..2)
					{
					ascent(i1 in 0..2)
						{
						let angle = i0*180+i1*90+45-(i1*2-1)*(counter-90)*1.33;	
						let dist = rand(0,8);
						let x = dist*cos(angle)+min(GetStgFrameWidth-8,max(8,GetStgFrameWidth*0.5+GetStgFrameWidth*0.5*cos(angle)));	
						let y = dist*sin(angle)+min(GetStgFrameHeight-8,max(8,GetStgFrameHeight*0.5+GetStgFrameHeight*0.5*sin(angle)));
						angle = atan2(y-GetStgFrameHeight*0.5,x-GetStgFrameWidth*0.5)+rand(-1,1)*15;
						let shot = CreateShotB2(x,y,-1*cos(angle),-1*sin(angle),cos(angle)/40,sin(angle)/40,4*cos(angle),4*sin(angle),FIRE_RED+floor(rand(0,6)),45);
						ObjRender_SetScaleXYZ(shot,1.25,1.25,0);
						ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
						ObjShot_SetSpellResist(shot,true);
						}
					}
				}
			yield;	
			}
		Obj_Delete(obj);
		}
	task TTenji
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetPosition(obj,xC-200,yC+75,0);
		let img = GetCurrentScriptDirectory ~ "img/rave/tenji.png";
		ObjPrim_SetTexture(obj,img);
		ObjRender_SetScaleXYZ(obj,0.85,0.85,0);
		Obj_SetRenderPriority(obj,0.35);
		
		let rx;
		let ry;
		let rxA = 256/4;
		let ryA = 233/2;
		
		let counter = 0;	

		while(!Obj_IsDeleted(objEnemy))
			{
			counter+=1;
			rx = floor(counter/3)%4;
			ry = floor(counter/(3*4))%2;
			ObjRender_SetAlpha2(obj,min(60,counter)*255/60);
			ObjSprite2D_SetSourceRect(obj,rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA);
			ObjSprite2D_SetDestCenter(obj);
			if(ObjEnemy_GetGlow(obj) == true && counter%4 == 0)
				{
				TGlow(obj,img,[rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA],[15,180],1.25);
				}
			if(counter%15 == 0)
				{
				TBubble;
				}
			yield;	
			}
		Obj_Delete(obj);
		task TBubble
			{
			let dir = rand(-1,1);
			let shot = CreateShotA1(GetStgFrameWidth*0.5+dir*150,rand(0,100),0,90-dir*45,BUBBLE_RED+floor(counter/20)%4,8);
			ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
			ObjShot_SetIntersectionEnable(shot,false);
			let time = 30;
			descent(t in 0..time)
				{
				ObjRender_SetScaleXYZ(shot,1+t/time,1+t/time,0);
				ObjRender_SetAlpha(shot,(time-t)*255/time);
				ObjShot_SetGraphic(shot,BUBBLE_RED+floor(counter/20)%4);
				yield;
				}
			ObjShot_SetIntersectionEnable(shot,true);
			ObjMove_SetAcceleration(shot,1/40);
			ObjMove_SetMaxSpeed(shot,rand(1.25,2.5));
			while(!Obj_IsDeleted(shot))
				{
				ObjShot_SetGraphic(shot,BUBBLE_RED+floor(counter/20)%4);
				yield;
				}
			}
		}
	task TClown
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetPosition(obj,xC+135,yC+25,0);
		let img = GetCurrentScriptDirectory ~ "img/rave/clown.png";
		ObjPrim_SetTexture(obj,img);
		Obj_SetRenderPriority(obj,0.35);
		
		let nuke = [];
		ascent(i0 in 0..2)
			{
			ascent(i1 in 0..2)
				{
				nuke = nuke ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
				}	
			ObjPrim_SetTexture(nuke[i0*2+0],GetCurrentScriptDirectory ~ "img/nuke1.png");
			ObjPrim_SetTexture(nuke[i0*2+1],GetCurrentScriptDirectory ~ "img/nuke2.png");	
				Obj_SetRenderPriority(nuke[i0*2+0],0.55);
				Obj_SetRenderPriority(nuke[i0*2+1],0.38);
			ascent(i1 in 0..2)
				{
				ObjSprite2D_SetSourceRect(nuke[i0*2+i1],0,0,256,256);
				ObjRender_SetPosition(nuke[i0*2+i1],i0*GetStgFrameWidth,0,0);
				ObjRender_SetBlendType(nuke[i0*2+i1],BLEND_ADD_ARGB);
				ObjRender_SetScaleXYZ(nuke[i0*2+i1],0,0,0);
				ObjSprite2D_SetDestCenter(nuke[i0*2+i1]);
				}
			}
	
		let rx;
		let ry;
		let rxA = 512/8;
		let ryA = 102/1;

		let spread = 4;
		
		let counter = 0;
		let angleA = 0;	

		while(!Obj_IsDeleted(objEnemy))
			{
			counter+=1;
			rx = floor(counter/4)%14;
			if(floor(counter/4)%14 >= 7)
				{
				rx = 13-rx;
				}
			ry = 0;
			ObjRender_SetAlpha2(obj,min(60,counter)*255/60);
			ObjSprite2D_SetSourceRect(obj,rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA);
			ObjSprite2D_SetDestCenter(obj);
			if(ObjEnemy_GetGlow(obj) == true && counter%4 == 0)
				{
				TGlow(obj,img,[rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA],[0,60],1.25);
				}
			if(counter >= 30)
				{
				let c = min(40,counter-30);
				ascent(i0 in 0..2)
					{
					ascent(i1 in 0..2)
						{
						ObjRender_SetScaleXYZ(nuke[i0*2+i1],c*(1.5+i1*0.35)/40,c*(1.25+i1*0.35)/40,0);
						}
					ObjRender_SetColorHSV(nuke[i0*2+0],(floor(counter/20)*20)%360,255,255);
					ObjRender_SetColorHSV(nuke[i0*2+1],(counter+180)%360,255,255);
					}
				}
			if(counter >= 70)
				{
				if(counter%(spread*5+150) <= spread*5)
					{
					if(counter%5 == 0)
						{
						ascent(i in 0..2)
							{
							TLaser(i,(counter%(spread*5+150))/5,angleA);
							}
						}
					}
				else
					{
					angleA = rand(-1,1)*45/spread;
					}
				}
			yield;	
			}
		Obj_Delete(obj);
		ascent(i in 0..length(nuke))
			{
			Obj_Delete(nuke[i]);
			}

		task TLaser(dir,i,angleA)
			{
			let angle = 90+angleA+(dir*2-1)*i*90/spread;
			let shot = CreateLaserA2(dir*GetStgFrameWidth+128*cos(angle),128*sin(angle),40,angle,GetStgFrameWidth*2,25,10^10,ST_LASER_GRAY,60);
			loop(120){ObjRender_SetColorHSV(shot,(counter+i*180/spread)%360,255,255);yield;}
			ObjMove_SetAngle(shot,angle);
			ObjMove_SetSpeed(shot,40);
			}
		}
	task TRin
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetPosition(obj,xC-150,yC+25,0);
		let img = GetCurrentScriptDirectory ~ "img/rave/rin.png";
		ObjPrim_SetTexture(obj,img);
		ObjRender_SetScaleXYZ(obj,0.85,0.85,0);
		Obj_SetRenderPriority(obj,0.35);
		
		let rx;
		let ry;
		let rxA = 300/3;
		let ryA = 246/2;
		let spread = [3,3];
		
		let counter = 0;	

		while(!Obj_IsDeleted(objEnemy))
			{
			counter+=1;
			rx = floor(counter/7)%3;
			ry = floor(counter/(7*3))%2;
			ObjRender_SetAlpha2(obj,min(60,counter)*255/60);
			ObjSprite2D_SetSourceRect(obj,rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA);
			ObjSprite2D_SetDestCenter(obj);
			if(ObjEnemy_GetGlow(obj) == true && counter%4 == 0)
				{
				TGlow(obj,img,[rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA],[30,90],1.5);
				}
			if(counter%35 == 0)
				{
				let x = GetStgFrameWidth*(0.5+dir*rand(0.25,0.375));
				let y = rand(0,100);
				ascent(i0 in 0..spread[0])
					{
					ascent(i1 in 0..spread[1])
						{
						i1 = i1-spread[1]/2+0.5;
						let speed = (spread[0]*0.5+(i0+rand(0,1))*0.5)*2.25/spread[0];
						let angle = 90+(i1+rand(-1,1)*0.5)*135/spread[1];
						let shot = CreateShotB2(x,y,speed*cos(angle),speed*sin(angle),0,1/180,0,5,R_DOT_RED+floor(rand(0,4))*2,8);
						ObjRender_SetScaleXYZ(shot,1.125,1.125,0);
						}
					}
				}
			yield;	
			}
		Obj_Delete(obj);
		}

	task TGlow(obj,img,rect,color,scale)
		{
		let glw = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(glw,img);
		ObjSprite2D_SetSourceRect(glw,rect[0],rect[1],rect[2],rect[3]);
		ObjSprite2D_SetDestCenter(glw);
		ObjRender_SetPosition(glw,ObjRender_GetX(obj),ObjRender_GetY(obj),0);
		Obj_SetRenderPriority(glw,0.28);
		ObjRender_SetBlendType(glw,BLEND_ADD_ARGB);
		let time = 20;
		ascent(t in 0..time)
			{
			ObjRender_SetColorHSV(glw,color[0]+t*color[1]/time,255,255);
			ObjRender_SetAlpha(glw,(time-t)*ObjRender_GetAlpha(obj)/time);
			ObjRender_SetScaleXYZ(glw,1+t*scale/time,1+t*scale/time,0);
			if(Obj_IsDeleted(obj) || Obj_IsDeleted(objEnemy))
				{
				Obj_Delete(glw);
				}
			yield;
			}
		Obj_Delete(glw);
		}
}	