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

#include "./functions2.txt"

timer = 95;

@Initialize
{
	Obj_SetValue(GetEnemyBossSceneObjectID,"INIT",true);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy,0,0);
	Obj_SetValue(objEnemy,"LAST_SPELL",true);

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	bspell = true;

	TEnemyWork;	
	ObjEnemy_Render; 
	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);
		}
	case(EV_REQUEST_IS_DURABLE_SPELL)
		{
		SetScriptResult(true);
		}
}

task TEnemyWork
{
	TXY;
	TSpellName;
	TSpellFace(pathReisen);
	loop(91){EnemySpell = -1;yield;}
	let objBack = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objBack,GetCurrentScriptDirectory ~ "../../../system/img/WhiteCircleAlpha.png");
	ObjSprite2D_SetSourceRect(objBack,0,0,64,64);	
	ObjRender_SetPosition(objBack,enmx,enmy,0);
	ObjSprite2D_SetDestRect(objBack,-0.5,-0.5,0.5,0.5);
	ObjRender_SetColorHSV(objBack,0,255,255);
	Obj_SetRenderPriority(objBack,0.29);
	ObjRender_SetBlendType(objBack,BLEND_INV_DESTRGB);
	PlaySound("se_LaserDelay",135);	
	ascent(i in 0..90)
		{
		ObjRender_SetScaleXYZ(objBack,i*(GetStgFrameHeight*3/64)/90,i*(GetStgFrameHeight*3/64)/90,0);
		ObjRender_SetAlpha(objBack,145+rand(0,15));
		ObjRender_SetColorHSV(img,0,i*255/90,255);
		ObjRender_SetAlpha(img,(90-i)*195/90+60);
		ObjSprite2D_SetDestCenter(objBack);
		yield;
		}
	PlaySound("se_LaserDelay",135);
	TGraph;
	ObjRender_SetColorHSV(img,0,255,255);
	ObjRender_SetAlpha(img,30);
	Obj_SetValue(objEnemy,"ICON",false);
	EnemySpell = 2;
	let spread = [1,7+Dif*2,1,16+Dif*2,3+Dif*2,1];	
	let counter = 0;
	let finalCounter = 0;
	let finalCount = 120;
	let alpha = 60;
	let count = 0;
	let type = 0;
	let angleM = rand(0,360);
	let soundS = 0;
	loop(90){yield;}
	loop
		{	
		if(count == 3)
			{
			if(soundS == 0)
				{
				PlaySound("se_Charge",100);
				soundS = 1;
				}
			TCharge(objEnemy);
			}
		if(count == 4)
			{
			spread[0] = 5;
			}
		if(count == 8)
			{
			if(soundS == 1)
				{
				PlaySound("se_Charge",100);
				soundS = 2;
				}
			TCharge(objEnemy);
			}
		if(count == 9)
			{
			type = 1;
			}
		if(count == 23)
			{
			if(soundS == 3)
				{
				PlaySound("se_Charge",100);
				soundS = 4;
				}
			TCharge(objEnemy);
			}
		if(count == 24)
			{
			spread[2] = 3;
			}
		if(count == 29)
			{
			if(soundS == 4)
				{
				PlaySound("se_Charge",100);
				soundS = 5;
				}
			TCharge(objEnemy);
			}
		if(count == 40)
			{
			if(soundS == 5)
				{
				PlaySound("se_Charge",100);
				soundS = 6;
				}
			TCharge(objEnemy);
			}
		if(count == 41)
			{
			finalCount = 60;
			}
		ObjRender_SetAlpha(img,alpha-rand(0,75));
		counter+=1;
		finalCounter+=1;
		if(counter%120 >= 45 && counter%120 < 60)
			{
			alpha+=195/15;
			}
		else if(alpha > 60)
			{
			alpha-=135/15;
			}	
		if(counter%120 == 60)
			{
			count+=1;
			PlaySound("se_Shot1",85);
			ascent(i0 in 0..spread[0])
				{
				let spreadA = spread[1]-i0;
				ascent(i1 in 0..spreadA)
					{
					i1 = i1-spread[1]/2+0.5+i0*0.5;
					TSpam(1.875-i0*(1/8),GetAngleToPlayer(objEnemy)+i1*180/spread[1],type);
					}
				}
			}
		if(count == 15)
			{
			if(soundS == 2)
				{
				PlaySound("se_Charge",100);
				soundS = 3;
				}
			TCharge(objEnemy);
			}
		ascent(i2 in 0..spread[2])
			{
			if(counter%120 == 75+i2*15 && count >= 16)
				{
				PlaySound("se_Shot7",55);
				ascent(i3 in 0..spread[3])
					{
					let angle = i3*360/spread[3]+angleM;
					let shot = CreateBulletA1(enmx+20*cos(angle),enmy+20*sin(angle),0.75-i2*(1/8),angle-i2*60/spread[3],DS_BALL_S_BLUE,8,1.125);
					ObjRender_SetAlpha(shot,85);
					ObjRender_SetColorHSV(shot,0,255,255);	
					}
				angleM+=15;
				}
			}
		if(finalCounter%finalCount == finalCount-1 && count >= 30)
			{
			PlaySound("se_Shot2",85);
			ascent(i4 in 0..spread[4])
				{	
				i4 = i4-spread[4]/2+0.5;
				let angle = i4*(4-Dif*0.67)+GetAngleToPlayer(objEnemy);
				let shot = CreateBulletOA2(objEnemy,1.33,angle,-1/60,0,1.125,DS_BALL_L_RED,8,1.125);
				ObjRender_SetAlpha(shot,85);
				ObjRender_SetColorHSV(shot,0,255,255);
				}
			finalCounter = 0;
			angleM-=(spread[2]+0.5)*60/spread[3];
			}
		yield;
		}
	task TSpam(speed,angle,type)
		{
		let shot = CreateBulletOA1(objEnemy,speed,angle,DS_MISSILE_RED,8,1.125);
		ObjRender_SetColorHSV(shot,0,255,255);
		ObjRender_SetAlpha(shot,85);	
		if(type < 1){return;}
		ObjShot_SetGraphic(shot,DS_MISSILE_PURPLE);
		let counter = 0;
		while(!Obj_IsDeleted(shot) && counter < 1)
			{
			if(ObjMove_GetY(shot) < 0)
				{
				counter+=1;
				ObjMove_SetAngle(shot,90+rand(-1,1));
				}
			if(ObjMove_GetX(shot) > GetStgFrameWidth)
				{
				counter+=1;
				ObjMove_SetAngle(shot,180+rand(-1,1));
				}
			if(ObjMove_GetX(shot) < 0)
				{
				counter+=1;
				ObjMove_SetAngle(shot,0+rand(-1,1));
				}
			yield;
			}
		if(Obj_IsDeleted(shot)){return;}
		PlaySound("se_Shot5",70);
		ObjMove_SetSpeed(shot,ObjMove_GetSpeed(shot)-0.5);
		return;
		}
	task TGraph
		{
		Obj_SetValue(objPlayer,"SPECIAL_VISIBLE",true);
		Obj_SetValue(objEnemy,"SPECIAL_VISIBLE",true);
		let objWall = [];
		let x = [EnemyX,GetPlayerX];
		let y = [EnemyY,GetPlayerY];
		let scale = 0;
		let time = 15;
		ascent(i in 0..2)
			{
			objWall = objWall ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
			ObjPrim_SetTexture(objWall[i],GetCurrentScriptDirectory ~ "../../../system/img/WhiteCircleAlpha.png");
			ObjSprite2D_SetSourceRect(objWall[i],0,0,64,64);
			ObjRender_SetBlendType(objWall[i],BLEND_INV_DESTRGB);
			Obj_SetRenderPriority(objWall[i],0.29);
			}	
		ObjRender_SetColorHSV(objWall[0],0,205,255);
		ObjRender_SetColorHSV(objWall[1],180,255,255);		
		while(!Obj_IsDeleted(objEnemy))
			{
			if(scale < 96)
				{
				scale+=96/20;
				ascent(i in 0..2)
					{
					ObjSprite2D_SetDestRect(objWall[i],-scale*(0.5+i*0.5),-scale*(0.5+i*0.5),scale*(0.5+i*0.5),scale*(0.5+i*0.5));
					}
				}
			ascent(i in 0..2)
				{
				let xP = GetPlayerX;
				let yP = GetPlayerY;		
				if(i == 0)
					{
					xP = enmx;
					yP = enmy;
					}
				let angle = atan2(yP-y[i],xP-x[i]);
				let dist =GetDistance(xP,yP,x[i],y[i]);
				if(i == 1)
					{
					dist/=time;
					}
				x[i] = x[i]+dist*cos(angle);
				y[i] = y[i]+dist*sin(angle);
				if(i == 1)
					{
					x[1] = x[1] +rand(-1,1)*0.33;
					y[1] = y[1] +rand(-1,1)*0.33;
					}
				}
			Obj_SetValue(objPlayer,"PLAYER_X",x[1]);
			Obj_SetValue(objPlayer,"PLAYER_Y",y[1]);
			ObjRender_SetPosition(objWall[1],x[1],y[1],0);
			local
				{
				let dist = GetDistance(GetPlayerY,GetPlayerX,enmy,enmx);
				let angle = atan2(GetPlayerY-enmy,GetPlayerX-enmx)+180;						
				Obj_SetValue(objEnemy,"ENEMY_X",x[1]+dist*cos(angle));
				Obj_SetValue(objEnemy,"ENEMY_Y",y[1]+dist*sin(angle));
				ObjRender_SetPosition(objWall[0],EnemyX,EnemyY,0);
				}
			let array = GetShotIdInCircleA2(GetPlayerX,GetPlayerY,scale*0.95,TARGET_ENEMY);
			ascent(i in 0..length(array))
				{
				let shot = array[i];
				if(!Obj_IsDeleted(shot) && Obj_IsVisible(shot))
					{
					let angle = atan2(GetPlayerY-ObjMove_GetY(shot),GetPlayerX-ObjMove_GetX(shot))+180;
					let dist = GetObjectDistance(shot,objPlayer);
					let graph = ObjShot_GetImageID(shot);
					let spam = CreateBulletA1(x[1]+dist*cos(angle),y[1]+dist*sin(angle),0,ObjMove_GetAngle(shot),graph,0,ObjShot_GetScale(shot)/GetShotDataInfoA1(graph,TARGET_ENEMY,INFO_COLLISION));
					ObjShot_SetIntersection(spam,false);
					Obj_DelayDelete(spam,1);
					}
				}
			array = GetShotIdInCircleA2(enmx,enmy,scale*0.95*0.5,TARGET_ENEMY);
			ascent(i in 0..length(array))
				{
				let shot = array[i];
				if(!Obj_IsDeleted(shot) && Obj_IsVisible(shot))
					{
					let angle = atan2(enmy-ObjMove_GetY(shot),enmx-ObjMove_GetX(shot))+180;
					let dist = GetObjectDistance(shot,objEnemy);
					let graph = ObjShot_GetImageID(shot);
					let spam = CreateBulletA1(EnemyX+dist*cos(angle),EnemyY+dist*sin(angle),0,ObjMove_GetAngle(shot),graph,0,ObjShot_GetScale(shot)/GetShotDataInfoA1(graph,TARGET_ENEMY,INFO_COLLISION));
					ObjShot_SetIntersection(spam,false);
					Obj_DelayDelete(spam,1);
					}
				}
			ObjRender_SetAlpha(objBack,145+rand(0,15));
			yield;
			}
		Obj_SetValue(objPlayer,"SPECIAL_VISIBLE",false);
		Obj_SetValue(objEnemy,"SPECIAL_VISIBLE",false);
		Obj_Delete(objBack);
		ascent(i in 0..2)
			{
			Obj_Delete(objWall[i]);
			}
		}
}	