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

#include "./functions2.txt"

timer = 52;

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

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	bspell = true;

	TEnemyWork;
	ObjEnemy_Render; 
	TLife(480,20,20,0,0,11,11);
	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-110,15,5);
	loop(45){yield;}
	let alphaM = 0.33;
	
	let spread = [3+Dif,5];
	let angleM = 0;
	let IsShooting = false;	
	TFam;
	let counter = 0;
	loop
		{
		counter+=1;
		if(counter%(22) == 0)
			{
			IsShooting = true;	
			PlaySound("se_Shot1",50);
			TShot(enmx,enmy,0);
			angleM+=360/spread[1];
			}
		else
			{
			IsShooting = false;
			}
		if(counter >= 360 && counter%105 == 60)
			{
			ObjEnemy_Move(objEnemy,10,3,30,30,90,120,125,0);
			}
		yield;
		}

	task TShot(x,y,type)
		{
		let graph = [DS_MISSILE_BLUE,DS_MISSILE_SKY,DS_MISSILE_RED,DS_MISSILE_PURPLE];	
		let spreadA = spread[1];
		ascent(i0 in 0..spread[0])
			{
			let speed = (i0*0.5+spread[0]*0.5)*3/spread[0];
			ascent(i1 in 0..spreadA)
				{
				i1 = i1-spreadA/2+0.5;
				let angle = (i1+0.5)*360/spreadA+angleM*(1+i0*0.125)+90+((spread[1]+1)%2)*180/spread[1];
				TSpam(speed,angle,graph[i0%2+type*2]);
				}
			}
		task TSpam(speed,angle,graph)
			{
			let shot = CreateBulletA1(x,y,speed,angle,graph,8,1.125);
			Obj_SetValue(shot,"ILLUSION",type==1);
			if(type == 0){return;}
			ObjShot_SetIntersection(shot,alphaM==1);
			ObjRender_SetAlpha(shot,255*alphaM);
			ObjRender_SetColorHSV(shot,0,(1.25-alphaM)*255,255);
			}
		}
	task TFam
		{
		loop(180){yield;}
		PlaySound("se_LaserDelay",135);
		let distX = 0;
		let dist = 0;
		let renderAlphaM = 0;
		let renderAlpha = 0;
		let renderType = 0;
		let graph = [BLEND_ALPHA,BLEND_ADD_ARGB];
		TOrb;
		ascent(i in 0..2)
			{
			TObj(i*2-1);
			}
		TColor;
		task TObj(dir)
			{
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			CreateEnemyIcon(obj);
			ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/Sprite_Boss.png");
			ObjSprite2D_SetDestRect(obj,-32,-32,32,32);
			Obj_SetRenderPriority(obj,0.37);
			while(!Obj_IsDeleted(objEnemy))
				{
				let x = enmx+dir*dist;
				let y = enmy;
				ObjRender_SetAlpha(obj,renderAlpha*alphaM);
				ObjRender_SetBlendType(obj,graph[renderType]);
				ObjRender_SetPosition(obj,x+distX,y,0);
				ObjSprite2D_SetSourceRect(obj,render_x*128+32,render_y*128+32,(render_x+1)*128-32,(render_y+1)*128-32);	
				if(IsShooting == true)
					{
					if(dir == 1)
						{
						TSound;
						}	
					TShot(x,y,1);
					}
				yield;
				}
			Obj_Delete(obj);
			}	
		task TSound
			{
			loop(11){yield;}
			PlaySound("se_Shot2",40);
			}
		task TOrb
			{
			let objBack = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(objBack,GetCurrentScriptDirectory ~ "../../../system/img/WhiteCircleAlpha.png");
			ObjSprite2D_SetSourceRect(objBack,0,0,64,64);
			Obj_SetRenderPriority(objBack,0.35);
			ObjRender_SetColorHSV(objBack,295,255,255);

			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			CreateEnemyIcon(obj);
			ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/Sprite_Boss.png");
			ObjSprite2D_SetDestRect(obj,-32,-32,32,32);
			Obj_SetRenderPriority(obj,0.37);
			ObjRender_SetColorHSV(obj,295,125,255);

			let shot = CreateBulletA1(enmx,enmy,0,0,DS_BALL_S_WHITE,0,20/16);
			Obj_SetVisible(shot,false);
			ObjShot_SetSpellResist(shot,true);	
			ObjShot_SetAutoDelete(shot,false);

			let x = enmx;
			let y = enmy;
			let speed = 0;
			let scale = 0;
			let scaleM = 80+Dif*15;
			let angle = 0;
			let counter = 0;
			while(!Obj_IsDeleted(objEnemy))
				{
				alphaM = 0.25;
				Spell = false;
				if(GetDistance(GetPlayerX,GetPlayerY,x,y) <= scale*0.66)
					{
					alphaM = 1;
					Spell = true;
					}
				if(scale < scaleM)
					{
					scale+=scaleM/90;
					}
				else
					{
					scale=scaleM;
					}
				distX = rand(-1,1)*7.5;
				if(dist < 125)
					{
					dist+=125/90;
					renderAlphaM+=155/90;
					}
				counter+=1;
				if(counter%180 == 0)
					{
					PlaySound("se_Shot7",55);
					PlaySound("se_LaserDelay",180);
					angle = atan2(GetPlayerY-y,GetPlayerX-x);			
					speed = 3;
					}
				if(speed > 0)	
					{
					speed-=3/60;
					}
				else
					{
					speed = 0;
					}
				x+=speed*cos(angle);
				y+=speed*sin(angle);
				renderAlpha = renderAlphaM+rand(25,100);
				renderType = floor(rand(0,2));
				ObjRender_SetAlpha(objBack,renderAlpha*0.5);
				ObjRender_SetBlendType(objBack,graph[renderType]);
				ObjRender_SetPosition(objBack,x,y,0);
				ObjSprite2D_SetDestRect(objBack,-scale,-scale,scale,scale);

				ObjRender_SetAlpha(obj,renderAlpha*0.75);
				ObjRender_SetBlendType(obj,graph[renderType]);
				ObjRender_SetPosition(obj,x+distX*0.5,y,0);
				ObjMove_SetPosition(shot,x,y);
				if(speed > 0.1)
					{
					if(cos(angle) > cos(75))
						{
						ObjRender_SetScaleX(obj,absolute(ObjRender_GetScaleX(obj)));	
						}
					else if(cos(angle) < cos(105))
						{
						ObjRender_SetScaleX(obj,-absolute(ObjRender_GetScaleX(obj)));	
						}
					ObjSprite2D_SetSourceRect(obj,2*128+32,3*128+32,(2+1)*128-32,(3+1)*128-32);
					}
				else
					{
					ObjSprite2D_SetSourceRect(obj,0*128+32,0*128+32,(1)*128-32,(1)*128-32);
					}
				yield;
				}
			Obj_Delete(obj);
			Obj_Delete(objBack);
			Obj_Delete(shot);
			}
		}

	task TColor
		{
		let alpha = alphaM;
		let sound1;
		let sound2;
		loop
			{
			if(alpha != alphaM)
				{
				if(alphaM == 1)
					{
					sound1 = PlaySound("se_Phantom1",175);
					sound2 = PlaySound("se_Phantom2",175);
					}
				else
					{
					Obj_Delete(sound1);
					}
				let array = GetShotIdInCircleA2(GetStgFrameWidth/2,GetStgFrameHeight/2,GetStgFrameHeight*1.5,TARGET_ENEMY);
				ascent(i in 0..length(array))
					{
					let shot = array[i];
					if(Obj_GetValueD(shot,"ILLUSION",false) == true)
						{
						ObjShot_SetIntersection(shot,alphaM==1);
						ObjRender_SetAlpha(shot,255*alphaM);
						ObjRender_SetColorHSV(shot,0,(1.25-alphaM)*255,255);
						}
					}
				alpha = alphaM;
				}
			yield;
			}
		}	
}	