task TImage(image)
{
	ObjPrim_SetTexture(objPlayer, image);
	//ObjRender_SetBlendType(objPlayer, BLEND_ADD_RGB);
	loop
	{
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
		{
			ObjSprite2D_SetSourceRect(objPlayer, 0, 48, 48, 96);
		}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
		{
			ObjSprite2D_SetSourceRect(objPlayer, 0, 96, 48, 144);	
		}
		else
		{
			ObjSprite2D_SetSourceRect(objPlayer, 0, 0, 48, 48);
		}
		ObjSprite2D_SetDestCenter(objPlayer);

		yield;
	}
}

task TRenderOption(image)
{
	DrawOption(0);
	DrawOption(1);
	task DrawOption(num)
	{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		objOption=objOption~[obj];
		ObjPrim_SetTexture(obj, image);
	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		let OpX=[0,0,0,0,0];
		let OpY=[0,0,0,0,0];
		let Zangle=0;
		ObjSprite2D_SetSourceRect(obj, 0, 160, 20, 178);
		let count=0;
		loop
		{
			ObjRender_SetAngleZ(obj,Zangle);
			Zangle+=3;			
			ObjSprite2D_SetDestCenter(obj);
 			if(GetPlayerState==STATE_NORMAL)
			{
				ObjRender_SetAlpha(obj,255);
				ObjRender_SetColor(obj,255,255,255);
			}
			else
			{
				ObjRender_SetAlpha(obj,0);
			}

			ObjRender_SetPosition(obj, OpX[count%5],OpY[count%5], 0);
			OpX[count%5]=GetPlayerX+OptionX[num];
			OpY[count%5]=GetPlayerY+OptionY[num];
			count++;

			yield;
		}
	}
}

task GrazeEffect(BulletArray)
{
	ascent(i in 0..length(BulletArray))
	{
		if(!Obj_IsDeleted(BulletArray[i]))
		{
			Effect(BulletArray[i]);
		}
	}

	task Effect(Bobj)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"Light.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -32, -32, -0);
		ObjPrim_SetVertexPosition(obj, 1, 32, -32, -0);
		ObjPrim_SetVertexPosition(obj, 2, 32, 32, 0);
		ObjPrim_SetVertexPosition(obj, 3, -32, 32, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexUVT(obj, 1, 64, 0);
		ObjPrim_SetVertexUVT(obj, 2, 64, 64);
		ObjPrim_SetVertexUVT(obj, 3, 0, 64);

		let Alpha=1.0;
		let R=255;
		let G=255;
		let B=255;
		let x=(GetPlayerX+ObjRender_GetX(Bobj))/2;
		let y=(GetPlayerY+ObjRender_GetY(Bobj))/2;
		let speed=5;
		let angle=atan2(GetPlayerY-ObjRender_GetY(Bobj),GetPlayerX-ObjRender_GetX(Bobj))+rand(-5,5);

		let dis=((x-GetPlayerX)^2+(y-GetPlayerY)^2)^0.5;
		if(dis>30)
		{
			x=GetPlayerX+(x-GetPlayerX)*30/dis;
			y=GetPlayerY+(y-GetPlayerY)*30/dis;
		}
		ObjRender_SetPosition(obj,x,y,0);

		angle+=90*(floor(rand(0,2-0.001))*2-1);
		let Scale=0.1;
		let Zangle=angle+90;
		let color=GetShotDataInfoA1(ObjShot_GetImageID(Bobj),TARGET_ENEMY,INFO_DELAY_COLOR);
		R=(color[0]+255)/2;
		G=(color[1]+255)/2;
		B=(color[2]+255)/2;
		ObjRender_SetScaleXYZ(obj,Scale,Scale*4,1);
		ObjRender_SetAngleZ(obj,Zangle);

		loop(15)
		{
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			Alpha-=1.0/15;
			yield;
		}
		Obj_Delete(obj);
	}
}

task TExplosion
{
	let MS=15.0;
	loop(60){E_Dust;}
	loop(30)
	{
		if(GetPlayerState==STATE_NORMAL){break;}
		E_Effect(MS);
		MS-=0.5;
		yield;
	}

	task E_Effect(MaxScale)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"Marisa_shots.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, 64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obj, 3, -64, 64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 256);
		ObjPrim_SetVertexUVT(obj, 1, 128, 256);
		ObjPrim_SetVertexUVT(obj, 2, 128, 384);
		ObjPrim_SetVertexUVT(obj, 3, 0, 384);

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);

		let Alpha=0.2;
		let Scale=0;
		let R=255;
		let G=255;
		let B=255;

		let Xangle=rand(0,360);
		let Yangle=rand(0,360);
		let Zangle=rand(0,360);
		ObjRender_SetAngleXYZ(obj,Xangle,Yangle,Zangle);

		loop(20)
		{
			Alpha-=0.2/20;
			Scale+=MaxScale/20;
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			yield;
		}
		Obj_Delete(obj);
	}

	task E_Dust
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"Marisa_shots.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, 64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obj, 3, -64, 64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 256);
		ObjPrim_SetVertexUVT(obj, 1, 128, 256);
		ObjPrim_SetVertexUVT(obj, 2, 128, 384);
		ObjPrim_SetVertexUVT(obj, 3, 0, 384);

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);

		let speed=(rand(1,15));
		let Angle=rand(0,360);
		let Alpha=0.6;
		let Scale=0.2;
		let Zangle=0;
		let ZangleRotate=rand(-10,10);
		let R=255;
		let G=255;
		let B=255;
		loop(45)
		{
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(Angle),ObjRender_GetY(obj)+speed*sin(Angle),0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha-=0.6/45;
			Scale-=0.2/45;
			Zangle+=ZangleRotate;
			yield;
		}
		Obj_Delete(obj);
	}
}

task Play_SE(se)
{
	let objSE=ObjSound_Create;
	ObjSound_Load(objSE,se);
	let MaxVulume=GetAreaCommonData("TUNING","Setting",[])[1];
	ObjSound_SetVolumeRate(objSE,MaxVulume);
	ObjSound_Play(objSE);
}