task TMagicCircle
{
	//無敵時間の魔法陣
	//頂点奇数：偶数：円の外側、円の内側
	let countVertex = 64;
	let listRadius = [];
	loop(countVertex)
	{
		listRadius = listRadius ~ [0];
	}

	let path = current ~ "./img/Default_Player_MagicCircle.png";
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, countVertex);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.60);
	ObjPrim_SetTexture(obj, path);
	ascent(iVert in 0..countVertex / 2)
	{
		let left = iVert * 128;
		let indexVert = iVert * 2;
		ObjPrim_SetVertexUVT(obj, indexVert + 0, left, 0);
		ObjPrim_SetVertexUVT(obj, indexVert + 1, left, 64);		
	}

	let cx = 0;
	let cy = 0;
	let maxRadius = 128;
	let alpha = 0;
	let frame = 0;
	let angleRender = 0;
	let frameInvOld = 0;

	while(true)
	{
		let frameInv = GetPlayerInvincibilityFrame();

		if(frameInv <= 0)
		{
			Obj_SetVisible(obj, false);
			frameInvOld = 0;
		}
		else
		{
			if(frameInvOld == 0)
			{
				//初期化
				ascent(iVert in 0..countVertex)
				{
					listRadius[iVert] = 0;
				}
				alpha = 0;
				frame = 0;
			}
			frameInvOld = frameInv;

			let px = GetPlayerX();
			let py = GetPlayerY();

			alpha += 1 / 120;
			alpha = min(alpha, 1);
			angleRender += 360 / countVertex / 2;
			Obj_SetVisible(obj, true);

			frameInv = min(100, frameInv);
			let rRate = frameInv / 100;

			ascent(iVert in 0..countVertex / 2)
			{
				let indexVert = iVert * 2;
				let angle = 360 / (countVertex / 2 - 1) * iVert;

				let vx1 = listRadius[indexVert] * cos(angle);
				let vy1 = listRadius[indexVert] * sin(angle);
				ObjPrim_SetVertexPosition(obj, indexVert + 0, vx1, vy1, 0);

				let vx2 = listRadius[indexVert+1] * cos(angle);
				let vy2 = listRadius[indexVert+1] * sin(angle);
				ObjPrim_SetVertexPosition(obj, indexVert + 1, vx2, vy2, 0);

				//魔方陣拡大
				if(frame >= 0)
				{//外側
					let dr = (maxRadius * rRate - listRadius[indexVert]) / 8;
					listRadius[indexVert] = listRadius[indexVert] + dr;
				}
				if(frame > 10)
				{//内側
					let rRateIn = rRate - 0.12;
					if(rRateIn < 0){rRateIn=0;}
					let dr= (maxRadius * rRateIn - listRadius[indexVert + 1]) / 16;
					listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr;
				}

			}

			ObjRender_SetColor(obj, 192 * alpha, 192 * alpha, 255 * alpha);
			ObjRender_SetPosition(obj, px, py, 0);
			ObjRender_SetAngleZ(obj, angleRender);

			frame++;
		}

		yield;
	}
}

task TExplosion
{
	let path = GetCurrentScriptDirectory() ~ "./../img/Default_Player_Rumia.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.60);
	ObjPrim_SetTexture(obj, path);
	ObjSprite2D_SetSourceRect(obj, 192, 0, 255, 63);
	ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);

	let alpha = 255;
	let scale = 0;
	loop(30)
	{
		scale += 0.2;
		alpha -= 8;
		ObjRender_SetColor(obj, alpha, alpha, alpha);
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);
		yield;
	}
	Obj_Delete(obj);
}

task ChargeEffect(way, time){
	let angleX = rand(0, 360);
	let angleY = rand(0, 360);
	let angleZ = rand(0, 360);
	let rMax = rand(80, 120);
	let X = GetPlayerX;
	let Y = GetPlayerY;
	ascent(i in 0 .. way){
		Effect(angleX, angleY, angleZ+360*i/way, rMax, time);
	}
	
	ascent(i in 0 .. time-10){
		X += (GetPlayerX()-X)*0.1*(1-cos(90*i/(time-10)));
		Y += (GetPlayerY()-Y)*0.1*(1-cos(90*i/(time-10)));
		yield;
	}
	ascent(i in 1 .. 11){
		X += (GetPlayerX()-X)*(1-cos(90*i/10));
		Y += (GetPlayerY()-Y)*(1-cos(90*i/10));
		yield;
	}
	
	task Effect(angleX, angleY, angleZ, r, time){
		let path = GetCurrentScriptDirectory() ~ "./../img/charge.png";
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriority(obj, 0.60);
		ObjPrim_SetTexture(obj, path);
		ObjSprite2D_SetSourceRect(obj, 0, 0, 31, 31);
		ObjSprite2D_SetDestRect(obj, -16, -16, 16, 16);
		ascent(i in 0 .. time){
			let x = cos(angleY)*cos(angleZ) -cos(angleX)*sin(angleY)*sin(angleZ);
			let y = sin(angleY)*cos(angleZ) +cos(angleX)*cos(angleY)*sin(angleZ);
			let scale = sin(180*i/time);
			let r = rMax*scale;
			ObjRender_SetScaleXYZ(obj, scale, scale, 1.0);
			ObjRender_SetColor(obj, 31, 127-96*i/time, 31+96*i/time);
			ObjRender_SetPosition(obj, X+r*x, Y+r*y, 0);
			angleZ += 5*(time-i)/time;
			if(i%2 == 0){AI(X+r*x, Y+r*y, scale, 10, i/time);}
			yield;
		}
		Obj_Delete(obj);
		
		task AI(x, y, scale, AItime, cRate){
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			Obj_SetRenderPriority(obj, 0.60);
			ObjPrim_SetTexture(obj, path);
			ObjSprite2D_SetSourceRect(obj, 0, 0, 31, 31);
			ObjSprite2D_SetDestRect(obj, -16, -16, 16, 16);
			ObjRender_SetColor(obj, 31, 127-96*cRate, 31+96*cRate);
			ObjRender_SetPosition(obj, x, y, 0);
			descent(i in 0 .. AItime){
				ObjRender_SetScaleXYZ(obj, scale*i/AItime, scale*i/AItime, 1.0);
				yield;
			}
			Obj_Delete(obj);
		}
	}
}
