let imgPlayer = GetCurrentScriptDirectory() ~ "./../img/reimu.png";

task TImage(obj){
	TMagicCircle;
	ObjPrim_SetTexture(obj, imgPlayer);
	Obj_SetRenderPriority(obj, 0.35);

	let RectY = 0;
	let A_time = 0;
	loop{
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH){
			RectY = 1;
			A_time = 0;
			XRender();
		}else if(GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
			RectY = 1;
			XRender();
		}else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH){
			RectY = 2;
			A_time = 0;
			XRender();
		}else if(GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
			RectY = 2;
			XRender();
		}else{
			RectY = 0
		}

		let InvFrame = GetPlayerInvincibilityFrame()%2;
		let RectX = (truncate(A_time/4))%8;
		A_time++;
		
		ObjSprite2D_SetSourceRect(obj, 32*RectX, 48*RectY, 32*RectX + 32, 48*RectY + 48);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetAlpha(obj, 255-223*InvFrame);
		yield;
		
		function XRender(){
			if(A_time >= 32){
				A_time -= 20;
			}
		}
	}

	task TMagicCircle{
		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj, imgPlayer);
		Obj_SetRenderPriority(obj, 0.7);
		let angle = 22.5;
		let scale = 3.2;
		let alpha = 0;
		let angle2 = 22.5;
		let scale2 = 3.0;
		let alpha2 = 0;
		loop{
			ObjSpriteList2D_ClearVertexCount(obj);
			if((GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			&& GetPlayerState() == STATE_NORMAL){
				TRender;
				scale = max(scale -2.0/10, 1.2);
				alpha = min(alpha +255/10, 255);
			}else{
				angle = 22.5;
				scale = 3.0;
				alpha = 0;
			}
			if(GetCommonData("SpiritAttack", false) && GetPlayerState() == STATE_NORMAL){
				TRender2;
				scale2 = max(scale2 -2.0/10, 1.0);
				alpha2 = min(alpha2 +255/10, 255);
			}else{
				angle2 = 22.5;
				scale2 = min(scale2 +2.0/10, 3.0);
				alpha2 = max(alpha2 -255/10, 0);
			}

			angle+= 3;
			angle2+= 2;
			yield;
		}

		function TRender{
			//魔方陣１
			ObjSpriteList2D_SetSourceRect(obj, 64, 192, 127, 255);
			ObjSpriteList2D_SetDestRect(obj, -32, -32, 32, 32);
			ObjRender_SetX(obj, GetPlayerX);
			ObjRender_SetY(obj, GetPlayerY);
			ObjRender_SetAngleZ(obj, angle);
			ObjRender_SetScaleXYZ(obj,scale, scale, 1.0);
			ObjRender_SetAlpha(obj, alpha);
			ObjSpriteList2D_AddVertex(obj);
			//魔方陣２ With 当たり判定表示
			ObjSpriteList2D_SetSourceRect(obj, 0, 192, 63, 255);
			ObjSpriteList2D_SetDestRect(obj, -32, -32, 32, 32);
			ObjRender_SetX(obj, GetPlayerX);
			ObjRender_SetY(obj, GetPlayerY);
			ObjRender_SetAngleZ(obj, -angle);
			ObjRender_SetScaleXYZ(obj,scale, scale, 1.0);
			ObjRender_SetAlpha(obj, alpha);
			ObjSpriteList2D_AddVertex(obj);
		}

		function TRender2{
			//魔方陣１
			ObjSpriteList2D_SetSourceRect(obj, 128, 192, 191, 255);
			ObjSpriteList2D_SetDestRect(obj, -32, -32, 32, 32);
			ObjRender_SetX(obj, GetPlayerX);
			ObjRender_SetY(obj, GetPlayerY);
			ObjRender_SetAngleZ(obj, angle2);
			ObjRender_SetScaleXYZ(obj,scale2, scale2, 1.0);
			ObjRender_SetAlpha(obj, alpha2);
			ObjSpriteList2D_AddVertex(obj);
			//魔方陣２ With 当たり判定表示
			ObjSpriteList2D_SetSourceRect(obj, 128, 192, 191, 255);
			ObjSpriteList2D_SetDestRect(obj, -32, -32, 32, 32);
			ObjRender_SetX(obj, GetPlayerX);
			ObjRender_SetY(obj, GetPlayerY);
			ObjRender_SetAngleZ(obj, -angle2);
			ObjRender_SetScaleXYZ(obj,scale2, scale2, 1.0);
			ObjRender_SetAlpha(obj, alpha2);
			ObjSpriteList2D_AddVertex(obj);
		}
	}
}

function Option(type){
	let imgPlayer = GetCurrentScriptDirectory() ~ "./../img/reimu.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, imgPlayer);
	Obj_SetRenderPriority(obj, 0.29);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(obj, 20*type, 160, 20*type+20, 180);
	ObjSprite2D_SetDestRect(obj, -10, -10, 10, 10);

	return obj;
}
