let SE_SPELL1 = current ~ "./se/spellA1.wav";
let SE_SPELL2 = current ~ "./se/spellA2.wav";

function LoadSpellSE{
	LoadSound(SE_SPELL1);
	LoadSound(SE_SPELL2);
}

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

	SetPlayerInvincibilityFrame(300);//無敵時間を設定
	let way = 6;


	PlaySE(SE_SPELL1);
	PlaySE(SE_SPELL2);
	ascent(i in 0 .. way){
		Spell(-90 + 360*i/way, 4);
	}
	descent(i in 0 .. 120){
		Set2DCameraFocusX(192 + rand(-8*i/120, 8*i/120));
		Set2DCameraFocusY(224 + rand(-8*i/120, 8*i/120));
		yield;
	}
	loop(180){
		yield;
	}
	Obj_Delete(GetSpellManageObject());

	task Spell(angle, angle_r){
		let objSpell = ObjSpell_Create();
		ObjSpell_SetDamage(objSpell, 10);
		ObjSpell_Regist(objSpell);
		let base_x = GetPlayerX;
		let base_y = GetPlayerY;
		let x = base_x;
		let y = base_y;
		let r = 0;

		Effect(objSpell, angle);
		while(r < 600){
			ObjMove_SetX(objSpell, x);
			ObjMove_SetY(objSpell, y);
			ObjSpell_SetIntersectionCircle(objSpell, x, y, 60);

			x = base_x + r*cos(angle);
			y = base_y + r*sin(angle);
			r += 2;
			angle += angle_r;
			yield;
		}
		Obj_Delete(objSpell);

		//エフェクト
		task Effect(obj, angle){
			let objEfc = ObjPrim_Create(OBJ_SPRITE_2D);
			let render_angle = angle;
			let scale = 1.5;
			let grf = 0;

			ObjPrim_SetTexture(objEfc, imgEffect);
			Obj_SetRenderPriority(objEfc, 0.6);
			ObjRender_SetBlendType(objEfc, BLEND_ADD_RGB);
			ObjSprite2D_SetDestRect(objEfc, -32, -32, 32, 32);
			ObjRender_SetAngleZ(objEfc, angle);
			ObjRender_SetScaleX(objEfc, scale);
			ObjRender_SetScaleY(objEfc, scale);

			AI;
			while(! Obj_IsDeleted(obj)){
				ObjSprite2D_SetSourceRect(objEfc, 64*(truncate(grf)%8), 0, 64*(truncate(grf)%8) + 64, 64);
				ObjRender_SetX(objEfc, x);
				ObjRender_SetY(objEfc, y);
				ObjRender_SetAngleZ(objEfc, render_angle);

				render_angle += 3;
				grf += 0.25;
				yield;
			}
			Obj_Delete(objEfc);

			task AI{
				while(! Obj_IsDeleted(objEfc)){
					ShotAI(x, y, render_angle, truncate(grf)%8, 20);
					yield;
				}
			}

			task ShotAI(x, y, angle, Rect, time){
				let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
				let alpha = 255;
				let scale = 1.5;

				ObjPrim_SetTexture(obj2, imgEffect);
				Obj_SetRenderPriority(obj2, 0.59);
				ObjRender_SetBlendType(obj2, BLEND_ADD_RGB);
				ObjSprite2D_SetSourceRect(obj2, 64*Rect, 0, 64*(Rect + 1), 64);
				ObjSprite2D_SetDestRect(obj2, -32, -32, 32, 32);
				ObjRender_SetX(obj2, x);
				ObjRender_SetY(obj2, y);
				ObjRender_SetAngleZ(obj2, angle);
				while(alpha > 0){
					ObjRender_SetScaleX(obj2, scale);
					ObjRender_SetScaleY(obj2, scale);
					ObjRender_SetColor(obj2, alpha, alpha, alpha);
					alpha -= 255/time;
					scale -= 1.5/time;
					yield;
				}
				Obj_Delete(obj2);
			}
		}
	}
}

