#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 91;

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

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	bspell = true;

	TEnemyWork;
	ObjEnemy_Render; 
	TLife(180,30,20,0,0,13.5,13.5);
	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(pathSanae);
	loop(60){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-150,20,5);
	loop(120){yield;}
	let spread = [4+Dif,8+Dif*2];
	let angleA = 90;
	loop
		{
		loop(6)
			{
			CreatePumpkin(GetStgFrameWidth*rand(0.25,0.75),angleA);
			loop(75){yield;}
			}
		loop(150){yield;}
		PlaySound("se_Charge",105);
		loop(2)
			{
			ascent(t in 0..10)
				{
				ObjEffect_Charge(GetStgFrameWidth/2,GetStgFrameHeight*0.5+sin(angleA)*(t*300/10-150));
				loop(3){yield;}
				}
			}
		loop(15){yield;}
		ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight*0.5+sin(angleA)*150,10,6.5);
		loop(120){yield;}
		angleA+=180;
		}
	task CreatePumpkin(x,angleM)
		{
		PlaySound("se_LaserDelay",90);
		let scale = 96;
		let alpha = 0;
		let angleZ = rand(0,360);

		let obj = ObjEnemy_Create(OBJ_ENEMY);
		ObjEnemy_Regist(obj);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/Sprite_Pumpkin.png");
		ObjSprite2D_SetSourceRect(obj,0,0,166,172);
		ObjSprite2D_SetDestRect(obj,0,0,1,1);
		ObjMove_SetPosition(obj,enmx,enmy);
		ObjMove_SetDestAtWeight(obj,x,GetStgFrameHeight*0.5-sin(angleM)*(GetStgFrameHeight*0.5-scale*0.275),20,4);
		ObjEnemy_SetLife(obj,40);
		ObjEnemy_SetDamageRate(obj,0,0);

		while(!Obj_IsDeleted(objEnemy) && !Obj_IsDeleted(obj) && ObjMove_GetY(obj) < GetStgFrameHeight-16 && ObjMove_GetY(obj) > 16 && ObjEnemy_GetInfo(obj,INFO_LIFE) > 0)
			{
			angleZ+=2;
			alpha+=(1-alpha)/20;
			ObjRender_SetAlpha(obj,alpha*255);
			ObjRender_SetScaleXYZ(obj,scale*alpha,scale*alpha,0);
			ObjRender_SetAngleZ(obj,15*cos(angleZ));
			ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),alpha*scale*0.525);
			if(GetObjectDistance(obj,GetPlayerObjectID) <= alpha*scale*0.475)
				{
				let spam = CreateBulletA1(GetPlayerX,GetPlayerY,0,0,SMALL_RED,0,3);
				Obj_SetVisible(spam,false);
				Obj_DelayDelete(spam,2);
				}
			if(ObjMove_GetSpeed(obj) == 0)
				{
				ObjEnemy_SetDamageRate(obj,20,0);
				ObjMove_SetSpeed(obj,2);
				ObjMove_SetAngle(obj,angleM+rand(-1,1));
				PlaySound("se_Shot7",65);

				let dirA = 1;
				let xA = ObjMove_GetX(obj)-scale*0.45;
				let count = [6+Dif+floor(Dif/2),10,8];
				let counterM = floor(rand(0,count[1]))*count[2]-1;
				let counter = counterM;
				while(xA > -8)
					{
					xA-=1;
					counter+=1;
					if(counter%count[2] == 0 && floor(counter/count[2])%count[1] < count[0])
						{
						ascent(i in 0..2)
							{
							let shot = CreateBulletA1(xA+rand(-1,1)*2,ObjMove_GetY(obj)+rand(-1,1)*5+scale*0.05+sin(angleM)*scale*0.2,ObjMove_GetSpeed(obj)+rand(-1,1)*0.1-i*0.5,ObjMove_GetAngle(obj)+rand(-1,1)*0.75+i*180,FIRE_RED,8,1.125);
							ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
							ObjShot_SetSpellResist(shot,true);
							}
						}
					}
				xA = ObjMove_GetX(obj)+scale*0.45;
				counter = counterM;
				while(xA < GetStgFrameWidth+8)
					{
					xA+=1;
					counter+=1;
					if(counter%count[2] == 0 && floor(counter/count[2])%count[1] < count[0])
						{
						ascent(i in 0..2)
							{
							let shot = CreateBulletA1(xA+rand(-1,1)*2,ObjMove_GetY(obj)+rand(-1,1)*5+scale*0.05+sin(angleM)*scale*0.2,ObjMove_GetSpeed(obj)+rand(-1,1)*0.1-i*0.5,ObjMove_GetAngle(obj)+rand(-1,1)*0.75+i*180,FIRE_RED,8,1.125);
							ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
							ObjShot_SetSpellResist(shot,true);
							}
						}
					}
				}
			yield;
			}
		if(Obj_IsDeleted(objEnemy)){Obj_Delete(obj);return;}
		if(Obj_IsDeleted(obj)){return;}
		PlaySound("se_Die4",105);
		let rate = 0.4-Dif*0.067;
		let angleP = rand(0,360);
		ascent(i0 in 0..spread[0])
			{
			let speed = (spread[0]*rate+i0*(1-rate))*(3.5+Dif*0.25)/spread[0];
			let angleA = angleP+(i0%2)*180/spread[1];
			ascent(i1 in 0..spread[1])
				{
				let angle = angleA+i1*360/spread[1];
				let shot = CreateBulletA2(ObjMove_GetX(obj),ObjMove_GetY(obj),speed*0.15,angle,speed/30,0,speed,FIRE_RED,8,1);
				}
			}
		TDel;
		task TDel
			{
			ascent(t in 0..20)
				{
				ObjRender_SetScaleXYZ(obj,scale*(1+t/20),scale*(1+t/20),0);
				ObjRender_SetAlpha(obj,(1-t/20)*255);
				if(Obj_IsDeleted(objEnemy)){Obj_Delete(obj);return;}
				yield;
				}
			Obj_Delete(obj);
			}
		}
}	