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

#include "./functions2.txt"

timer = 57;

@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,9,9);
	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;}
	EnemySpell = 1;
	loop(40){yield;}
	let timeS = (7-floor(Dif/2)*2)*(9+(Dif^2)*3)+90;
	let distm = 0;
	TShot1;
	TColor;
	loop(250){yield;}
	loop
		{
		PlaySound("se_Charge",135);
		loop(5)
			{	
			TCharge(objEnemy);
			loop(10){yield;}
			}
		EnemySpell = 2;
		Spell = true;
		loop(40){yield;}
		let spread = 1;	
		let angle = atan2(GetPlayerY-(enmy-8),GetPlayerX-enmx);
		let angleA = 10-(ceil(Dif/2)-1)*3.5;
		if(Dif == 0)
			{
			angleA = 1;
			}
		loop(9+(Dif^2)*3)
			{
			ascent(i in 0..spread)
				{
				i = i-spread/2+0.5;
				TShot(0,3,angle+i*angleA*(floor(Dif/2)+1),6,0);	
				}
			spread+=2-floor(Dif/2);
			angleA+=0.5*((1-floor(Dif/2))*2-1);
			loop(7-floor(Dif/2)*2){yield;}
			}
		loop(50){yield;}
		PlaySound("se_Shot7",115);
		EnemySpell = 1;
		Spell = false;
		loop(175){yield;}
		}
	task TShot1
		{
		let spread = 2+floor(Dif/2);
		let angle = 270+((spread+1)%2)*180/spread;
		loop
			{
			ascent(i in 0..spread)
				{
				TShot(32,2.5+floor(Dif/2),i*360/spread+angle,5,1);
				}
			loop(45)
				{
				angle+=0.75;
				yield;
				}
			}
		}

	task TShot(dist,speedM,angle,color,type)
		{
		PlaySound("se_Shot1",75);
		let distY = -(1-type)*8;
		let distM = 125;
		let speedm = 0.65+(Dif+floor(Dif/2))*0.125+type*0.125;
		let angleA = rand(0.5,1)*20;
		let shot = CreateBulletA2(enmx+dist*cos(angle),enmy+dist*sin(angle)+distY,speedM+2,angle,-2/30,0,speedM,DS_MISSILE_BLUE+color,8,1.125);	
		if(type == 0)
			{
			Obj_SetValue(shot,"ILLUSION",true);
			}
		let counter = 0;
		let phase = false;
		while(!Obj_IsDeleted(shot) && phase == false)
			{
			if(counter < 1)
				{
				if(ObjMove_GetX(shot) < distm)
					{
					counter+=1;
					if(type == 1)
						{
						ObjMove_SetAngle(shot,90-angleA);
						}
					else
						{
						ObjMove_SetAngle(shot,180-ObjMove_GetAngle(shot));
						if(Dif > 0)	
							{
							ObjMove_SetMaxSpeed(shot,1);
							}
						}
					}
				if(ObjMove_GetX(shot) > GetStgFrameWidth-distm)
					{
					counter+=1;
					if(type == 1)
						{
						ObjMove_SetAngle(shot,90+angleA);
						}
					else
						{
						ObjMove_SetAngle(shot,180-ObjMove_GetAngle(shot));
						if(Dif > 0)	
							{
							ObjMove_SetMaxSpeed(shot,1);
							}
						}
					}
				if(ObjMove_GetY(shot) < 0)
					{
					counter+=1;
					if(type == 1)
						{
						ObjMove_SetAngle(shot,90+rand(-1,1)*angleA);	
						}
					else
						{
						ObjMove_SetAngle(shot,-ObjMove_GetAngle(shot));
						}
					}
				}
			if(ObjMove_GetY(shot) >= GetStgFrameHeight)
				{
				phase = true;
				counter = 100;
				ObjMove_SetAngle(shot,270+rand(-1,1)*type*10);
				ObjMove_SetAcceleration(shot,(ObjMove_GetSpeed(shot)-speedm)/20);
				ObjMove_SetMaxSpeed(shot,speedm);
				ObjShot_SetGraphic(shot,DS_MISSILE_BLUE);
				Obj_SetRenderPriority(shot,0.49);
				ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
				TSpam(ObjMove_GetX(shot),ObjMove_GetY(shot),ObjMove_GetAngle(shot));
				}
			angle = ObjMove_GetAngle(shot);
			yield;
			}
		}

	task TSpam(x,y,angle)		
		{
		let scale = 0;
		let obj = CreateBulletA1(x,y,0,angle,DS_BALL_S_RED,0,0);
		Obj_SetVisible(obj,false);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		let shot = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(shot,GetCurrentScriptDirectory ~ "../../../system/img/WhiteCircleAlpha.png");
		ObjSprite2D_SetSourceRect(shot,0,0,64,64);
		ObjRender_SetPosition(shot,x,y,0);	
		ObjRender_SetAngleZ(shot,angle);
		ObjRender_SetColorHSV(shot,0,205,255);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		let alpha = 175;
		ObjRender_SetAlpha(shot,alpha);
		ascent(t in 0..45)
			{	
			if(t >= 30)	
				{
				alpha-=175/15;
				ObjRender_SetAlpha(shot,alpha);
				}
			if(t < 30)
				{
				scale += ((30-t)^2)*4/(30^2);
				ObjSprite2D_SetDestRect(shot,-scale,-scale,scale,scale);
				if(GetDistance(GetPlayerX,GetPlayerY,x,y) <= scale*0.66 && Obj_GetValueD(objPlayer,"IS_HIT",false) == false && GetPlayerInvincibilityFrame <= 0)
					{
					let spam = CreateBulletA1(GetPlayerX,GetPlayerY,0,0,DS_BALL_SS_RED,0,5);
					Obj_SetVisible(spam,false);
					Obj_DelayDelete(spam,2);
					}
				}
			if(Obj_IsDeleted(obj)){break;}
			if(Obj_IsDeleted(shot)){break;}
			yield;
			}
		Obj_Delete(obj);
		Obj_Delete(shot);
		}

	task TColor
		{
		loop
			{
			if(Spell == true)
				{
				let sound = PlaySound("se_Phantom1",175);
				PlaySound("se_Phantom2",175);
				ascent(t in 0..timeS)
					{
					let array = GetShotIdInCircleA2(GetStgFrameWidth/2,GetStgFrameHeight/2,GetStgFrameHeight*1.5,TARGET_ENEMY);
					ascent(i in 0..length(array))
						{
						let shot = array[i];
						if(!Obj_IsDeleted(shot))
							{
							if(Obj_GetValueD(shot,"ILLUSION",false) == true)
								{
								if(t == timeS-1)
									{
									ObjShot_SetIntersection(shot,true);
									ObjRender_SetAlpha(shot,255);	
									ObjRender_SetColorHSV(shot,0,0,255);
									}
								else
									{
									ObjRender_SetAlpha(shot,125);
									ObjRender_SetColorHSV(shot,0,125,255);
									ObjShot_SetIntersection(shot,false);	
									}
								}
							else
								{
								if(t == timeS-1)
									{
									ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
									}
								else
									{
									ObjRender_SetBlendType(shot,BLEND_ALPHA);
									}
								}
							}
						}
					yield;
					}
				Obj_Delete(sound);
				}
			else
				{
				yield;
				}		
			}
		}
}	