
task TSpell{
	let current = GetCurrentScriptDirectory();
	let imgEffect = current ~ "./img/musou_effect.png";

	SetPlayerInvincibilityFrame(360);//GԂݒ
	CutIn(current~"./img/reimu_cutin.png",0,191,400,600);

	let angle = rand(0, 360);

	_PlaySE(SE_SPELL2);
	_PlaySE(SE_SPELL3);
	ascent(i in 0 .. 8){
		Musou(angle + 45*i);
	}
	loop(300){
		yield;
	}
	Obj_Delete(GetSpellManageObject());

	task Musou(angle){
		let x = GetPlayerX;
		let y = GetPlayerY;
		let px;
		let py;
		let r = 120;
		let r_angle = rand(0, 360);
		let objSpl = CreateShotA1(x, y, 0, 0, 4, 0);
		ObjRender_SetColor(objSpl, 0, 0, 0);
		ObjShot_SetEraseShot(objSpl, false);
		ObjShot_SetSpellFactor(objSpl, true);
		ObjShot_SetPenetration(objSpl, 9999);
		ObjShot_SetDamage(objSpl, 1.0);
		ObjShot_SetAutoDelete(objSpl, false);
		ascent(i in 0 .. 3){
			Effect(r_angle+120*i, i);
		}

		ascent(i in 0 .. 90){
			if(Obj_IsDeleted(objSpl)){return;}
			px = x;
			py = y;
			x = GetPlayerX+r*i/120*cos(angle);
			y = GetPlayerY+r*i/120*sin(angle);
			ObjMove_SetPosition(objSpl, x, y);
			DeleteShotInCircle(TYPE_SHOT, TYPE_ITEM, x, y, 48);
			angle += 5;
			yield;
		}
		angle = atan2(y-py, x-px);
		let speed = ((y-py)^2+ (x-px)^2)^0.5;
		ObjShot_SetPenetration(objSpl, 30);
		ObjShot_SetDamage(objSpl, 2.0);
		loop(120){
			if(Obj_IsDeleted(objSpl)){break;}
			if(enemyCount > 0){
				let Epos = GetEnemyIntersectionPosition(x, y, 1);
				x += speed*cos(angle);
				y += speed*sin(angle);
				ObjMove_SetPosition(objSpl, x, y);
				speed = min(speed+0.05, 10);
				angle = Horming(atan2(Epos[0][1]-y, Epos[0][0]-x), angle, 10);
			}else{
				x += speed*cos(angle);
				y += speed*sin(angle);
				ObjMove_SetPosition(objSpl, x, y);
				speed = min(speed+0.05, 10);
				angle = Horming(atan2(GetPlayerY-y, GetPlayerX-x), angle, 10);
			}
			DeleteShotInCircle(TYPE_SHOT, TYPE_ITEM, x, y, 48);
			yield;
		}
		Burst(x, y);
		_PlaySE(SE_SPELL4);
		Obj_Delete(objSpl);
		
		task Burst(x, y){
			let objSpell = ObjSpell_Create();
			ObjSpell_SetDamage(objSpell, 5.0);
			ObjSpell_Regist(objSpell);
			ascent(i in 0 .. 30){
				let scale = 1.0+2.0*i/30;
				ObjSpell_SetIntersectionCircle(objSpell, x, y, 48*scale);
				yield;
			}
			Obj_Delete(objSpell);
		}

		//GtFNg
		task Effect(angle, rect_N){
			let objEfc = ObjPrim_Create(OBJ_SPRITE_2D);
			let r_a = 0;
			let r = 0;
	
			let alpha = 255;
			ObjRender_SetColor(objEfc, alpha, alpha, alpha);
			ObjPrim_SetTexture(objEfc, imgEffect);
			Obj_SetRenderPriority(objEfc, 0.6);
			ObjRender_SetBlendType(objEfc, BLEND_ADD_RGB);
			ObjRender_SetAngleZ(objEfc, angle);
			ObjSprite2D_SetSourceRect(objEfc, 128*rect_N, 0, 128*(rect_N+1), 128);
			ObjSprite2D_SetDestRect(objEfc, -64, -64, 64, 64);
	
			ascent(i in 0 .. 60){
				let scale = 1.0*i/60;
				r = 4*i/60;
				ObjRender_SetPosition(objEfc, x+r*cos(angle+r_a)-r*cos(angle+ 4*r_a), y+r*sin(angle+r_a)-r*sin(angle+ 4*r_a), 0);
				ObjRender_SetScaleXYZ(objEfc, scale, scale, 1.0);
				r_a += 6;
				yield;
			}
			r = 4;
			ObjRender_SetScaleXYZ(objEfc, 1.0, 1.0, 1.0);
			while(! Obj_IsDeleted(objSpl)){
				ObjRender_SetPosition(objEfc, x+r*cos(angle+r_a)-r*cos(angle+ 4*r_a), y+r*sin(angle+r_a)-r*sin(angle+ 4*r_a), 0);
				r_a += 6;
				yield;
			}
			Burst(x+30*cos(angle), y+30*sin(angle), rect_N);
			let xx = x;
			let yy = y;
			descent(i in 1 .. 31){
				let scale = 1.0*sin(90*i/30);
				xx += 12*i/30*cos(angle);
				yy += 12*i/30*sin(angle);
				ObjRender_SetPosition(objEfc, xx, yy, 0);
				ObjRender_SetScaleXYZ(objEfc, scale, scale, 1.0);
				yield;
			}
			
			Obj_Delete(objEfc);
	
			task Burst(x, y, Num){
				let objEfc = ObjPrim_Create(OBJ_SPRITE_2D);
				let alpha = 1.0;
				ObjRender_SetColor(objEfc, Color(Num, 0)*alpha, Color(Num, 1)*alpha, Color(Num, 2)*alpha);
				ObjPrim_SetTexture(objEfc, imgEffect);
				Obj_SetRenderPriority(objEfc, 0.61);
				ObjRender_SetBlendType(objEfc, BLEND_ADD_RGB);
				ObjRender_SetAngleZ(objEfc, angle);
				ObjSprite2D_SetSourceRect(objEfc, 384, 0, 512, 128);
				ObjSprite2D_SetDestRect(objEfc, -64, -64, 64, 64);
				ObjRender_SetPosition(objEfc, x, y, 0);

				ascent(i in 0 .. 30){
					alpha = 1.0-1.0*sin(90*i/30);
					let scale = 1.0+2.0*i/30;
					ObjRender_SetColor(objEfc, Color(Num, 0)*alpha, Color(Num, 1)*alpha, Color(Num, 2)*alpha);
					ObjRender_SetScaleXYZ(objEfc, scale, scale, 1.0);
					yield;
				}
				Obj_Delete(objEfc);
				
				function Color(Num, color){
					let i;
					if(Num%3 == color){i= 255;}
					else{i= 127;}
					return i;
				}
			}
		}
	}
}

