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

	SetPlayerInvincibilityFrame(360);//無敵時間を設定

	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(objSpl, 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;
		ObjMove_SetSpeed(objSpl, speed);
		ObjMove_SetAngle(objSpl, angle);
		loop(120){
			if(Obj_IsDeleted(objSpl)){break;}
			if(enemyCount > 0){
				let Epos = GetEnemyIntersectionPosition(x, y, 1);
				ObjMove_SetSpeed(objSpl, min(8, ObjMove_GetSpeed(objSpl)));
				let sx = ObjMove_GetSpeed(objSpl)*cos(ObjMove_GetAngle(objSpl));
				let sy = ObjMove_GetSpeed(objSpl)*sin(ObjMove_GetAngle(objSpl));
				let ds = 0.5;
				let da = atan2(Epos[0][1]-ObjMove_GetY(objSpl), Epos[0][0]-ObjMove_GetX(objSpl));
				let dx = ds*cos(da);
				let dy = ds*sin(da);
				ObjMove_SetSpeed(objSpl, min(((sx+dx)^2 +(sy+dy)^2)^0.5, 99));
				ObjMove_SetAngle(objSpl, atan2(sy+dy, sx+dx));
			}else{
				ObjMove_SetSpeed(objSpl, min(8, ObjMove_GetSpeed(objSpl)));
				let sx = ObjMove_GetSpeed(objSpl)*cos(ObjMove_GetAngle(objSpl));
				let sy = ObjMove_GetSpeed(objSpl)*sin(ObjMove_GetAngle(objSpl));
				let ds = 0.5;
				let da = atan2(GetPlayerY()-ObjMove_GetY(objSpl), GetPlayerX()-ObjMove_GetX(objSpl));
				let dx = ds*cos(da);
				let dy = ds*sin(da);
				ObjMove_SetSpeed(objSpl, min(((sx+dx)^2 +(sy+dy)^2)^0.5, 99));
				ObjMove_SetAngle(objSpl, atan2(sy+dy, sx+dx));
			}
			x = ObjMove_GetX(objSpl);
			y = ObjMove_GetY(objSpl);
			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);
		}

		//エフェクト
		task Effect(objSpl, 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, ObjMove_GetX(objSpl)+r*cos(angle+r_a)-r*cos(angle+ 4*r_a), ObjMove_GetY(objSpl)+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, ObjMove_GetX(objSpl)+r*cos(angle+r_a)-r*cos(angle+ 4*r_a), ObjMove_GetY(objSpl)+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;
				}
			}
		}
	}
}

