task PhotoShooting
{
loop
{
	if(GetCommonDataDefault("SnapShootingToStage",false)==true)
	{
		SnapShootingMove("Success");
		SetCommonData("SnapShootingToStage",false);
	}
	if(GetCommonDataDefault("SnapShootingToStageFalse",false)==true)
	{
		SnapShootingMove("False");
		SetCommonData("SnapShootingToStageFalse",false);
	}
yield;
}
}

task SnapShootingMove(Judg)
{
	let photoNo=GetCommonData("CD_photoNo");
	let dis2=GetCommonData("CD_dis2");
	let finderangle=GetCommonData("CD_finderangle");
	let CX=GetCommonData("CameraPositionX");
	let CY=GetCommonData("CameraPositionY");
	let CameraLong=GetCommonData("CameraLong");
	let CameraWide=GetCommonData("CameraWide");
	let CAngle=GetCommonData("CameraAngle");
	let CameraDir=GetCommonData("CameraDir");
	let size=GetCommonData("CameraSize");
	let imgPhoto = "snapshot\"~StagesnapFile[photoNo%20]~".bmp";
		LoadGraphic(imgPhoto);//撮った写真から読み込む
		let scax=1;
		let scay=1;
	//以下カメラの中心から画面端までの距離と角度を設定
		let OriAngle1=atan2(GetClipMinX-CX,GetClipMinY-CY);
		let OriDis1=((GetClipMinX-CX)^2+((GetClipMinY-CY)^2))^0.5;
		let OriAngle2=atan2(GetClipMaxX-CX,GetClipMinY-CY);
		let OriDis2=((GetClipMaxX-CX)^2+((GetClipMinY-CY)^2))^0.5;
		let OriAngle3=atan2(GetClipMaxX-CX,GetClipMaxY-CY);
		let OriDis3=((GetClipMaxX-CX)^2+((GetClipMaxY-CY)^2))^0.5;
		let OriAngle4=atan2(GetClipMinX-CX,GetClipMaxY-CY);
		let OriDis4=((GetClipMinX-CX)^2+((GetClipMaxY-CY)^2))^0.5;

		let obj=Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgPhoto);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	//	ObjEffect_SetRenderState(obj, ADD); 
//X,Yはカメラの４頂点の座標。PX,PYはカメラの中心から頂点までの成分の絶対値。
		let X=[CX+dxdy(0,0,dis2,finderangle,CAngle,CameraDir,size),CX+dxdy(CameraLong,0,dis2,finderangle,CAngle,CameraDir,size),CX+dxdy(CameraLong,CameraWide,dis2,finderangle,CAngle,CameraDir,size), CX+dxdy(0,CameraWide,dis2,finderangle,CAngle,CameraDir,size), ];
		let Y=[CY+dxdy2(0,0,dis2,finderangle,CAngle,CameraDir,size),CY+dxdy2(CameraLong,0,dis2,finderangle,CAngle,CameraDir,size),CY+dxdy2(CameraLong,CameraWide,dis2,finderangle,CAngle,CameraDir,size), CY+dxdy2(0,CameraWide,dis2,finderangle,CAngle,CameraDir,size)];
		let PX=CameraLong/2*size;
		let PY=CameraWide/2*size;
//写真の切り取り方を決定するタスク。
		let CDAngle=CameraDir;
		ShootRange(obj,CAngle,CDAngle,X,Y,PX,PY,OriAngle1,OriDis1,OriAngle2,OriDis2,OriAngle3,OriDis3,OriAngle4,OriDis4);

		ObjEffect_SetScale(obj, scax,scay);
		ObjEffect_SetAngle(obj,0,0,CAngle+CameraDir);
		ObjEffect_SetLayer(obj,8);
		Obj_SetPosition(obj,CX,CY);



		let DX=GetClipMaxX+40+65*((photoNo%10)%3);//一般スペルの時の写真Ｘ座標
		let DY=GetCenterY+50+65*trunc((photoNo%10)/3);//一般スペルの時の写真Ｘ座標
		let posX=Obj_GetX(obj);//徐々に移動させるために必要
		let posY=Obj_GetY(obj);
		let loopnum=30;//写真が規定座標に行くまでの時間スローのため体感時間は長いけど

		let SnapAngle=0;//回転させるための値
		let SnapSize=0.2;//そのまま。写真のサイズ

		SnapSize=0.4;
		if((photoNo%10)<=4)
		{
			DX=GetClipMaxX+70;
			DY=GetCenterY-170+(photoNo%10)*85;
		}
		else
		{
			DX=GetClipMaxX+155;
			DY=GetCenterY-170+((photoNo%10)-5)*85;
		}
			
		if(Judg=="False")
		{
			DX=CX;
			DY=CY+60;
		}

///////////////////////////////////////////////////////////////////////////////////
let CameraBack2=csd~"player\img\Back2.png";
LoadGraphic(CameraBack2);//撮った写真から読み込む
		let BGscax2=1.00;
		let BGscay2=1.00;
	//	let finderangle9=atan(1.15/1.5)+180+CameraDir-90;
		let obj9=Obj_Create(OBJ_EFFECT);
				ObjEffect_SetTexture(obj9, CameraBack2);
		ObjEffect_SetPrimitiveType(obj9, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj9, ADD); 
	//	ObjEffect_SetRenderState(obj9, MULTIPLY); 
		ObjEffect_CreateVertex(obj9, 16);

		let VertexX=PX/2;
		let VertexY=PY/2;

		ObjEffect_SetVertexXY(obj9, 0, -VertexX*2, -VertexY*2);
		ObjEffect_SetVertexXY(obj9, 1, -VertexX,  -VertexY*2);
		ObjEffect_SetVertexXY(obj9, 2, 0, -VertexY*2);
		ObjEffect_SetVertexXY(obj9, 3,  VertexX,  -VertexY*2);
		ObjEffect_SetVertexXY(obj9, 4, VertexX*2, -VertexY*2);
		ObjEffect_SetVertexXY(obj9, 5, VertexX*2,  -VertexY);
		ObjEffect_SetVertexXY(obj9, 6, VertexX*2, 0);
		ObjEffect_SetVertexXY(obj9, 7,  VertexX*2,  VertexY);
		ObjEffect_SetVertexXY(obj9, 8, VertexX*2, VertexY*2);
		ObjEffect_SetVertexXY(obj9, 9, VertexX,  VertexY*2);
		ObjEffect_SetVertexXY(obj9, 10, 0, VertexY*2);
		ObjEffect_SetVertexXY(obj9, 11,  -VertexX,  VertexY*2);
		ObjEffect_SetVertexXY(obj9, 12, -VertexX*2, VertexY*2);
		ObjEffect_SetVertexXY(obj9, 13, -VertexX*2,  VertexY);
		ObjEffect_SetVertexXY(obj9, 14, -VertexX*2, 0);
		ObjEffect_SetVertexXY(obj9, 15,  -VertexX*2,  -VertexY);

		ObjEffect_SetVertexUV(obj9, 0,  0,  0);
		ObjEffect_SetVertexUV(obj9, 1,  100, 0);
		ObjEffect_SetVertexUV(obj9, 2, 200,  0);
		ObjEffect_SetVertexUV(obj9, 3, 300, 0);
		ObjEffect_SetVertexUV(obj9, 4, 401,  0);
		ObjEffect_SetVertexUV(obj9, 5, 401,  100);
		ObjEffect_SetVertexUV(obj9, 6, 401, 200);
		ObjEffect_SetVertexUV(obj9, 7,  401,  300);
		ObjEffect_SetVertexUV(obj9, 8,  401, 401);
		ObjEffect_SetVertexUV(obj9, 9, 300,  401);
		ObjEffect_SetVertexUV(obj9, 10, 200, 401);
		ObjEffect_SetVertexUV(obj9, 11,  100,  401);
		ObjEffect_SetVertexUV(obj9, 12,  0, 401);
		ObjEffect_SetVertexUV(obj9, 13, 0,  300);
		ObjEffect_SetVertexUV(obj9, 14, 0, 200);
		ObjEffect_SetVertexUV(obj9, 15, 0, 100);

		ObjEffect_SetScale(obj9, BGscax2,BGscay2);
		ObjEffect_SetAngle(obj9,0,0,CAngle+CameraDir);
		ObjEffect_SetLayer(obj9,8);
		Obj_SetPosition(obj9,CX,CY);
			ascent(let i in 0..16)
			{
				ObjEffect_SetVertexColor(obj9,i,150,255,255,255);
			}
	ascent(let j in 0..4)
	{
		ascent(let i in 0..5)
		{
			if((X[j]*(5-i)+X[(j+1)%4]*i)/5<GetClipMinX || (Y[j]*(5-i)+Y[(j+1)%4]*i)/5<GetClipMinY || (X[j]*(5-i)+X[(j+1)%4]*i)/5>GetClipMaxX || (Y[j]*(5-i)+Y[(j+1)%4]*i)/5>GetClipMaxY)
			{
				ObjEffect_SetVertexColor(obj9,j*4+i,0,50,50,50);
			}
		}
	}
//////////////////////////////////////////////////////////////////////////
		loop(loopnum)
		{
			Obj_SetPosition(obj,Obj_GetX(obj)+(DX-posX)/loopnum,Obj_GetY(obj)+(DY-posY)/loopnum);
			ObjEffect_SetScale(obj, scax,scay);
			scax-=(1-SnapSize)/loopnum;
			scay-=(1-SnapSize)/loopnum;
			SnapAngle+=360/loopnum;
			ObjEffect_SetAngle(obj,0,0,CAngle+CameraDir+SnapAngle);

			Obj_SetPosition(obj9,Obj_GetX(obj9)+(DX-posX)/loopnum,Obj_GetY(obj9)+(DY-posY)/loopnum);
			ObjEffect_SetScale(obj9, BGscax2,BGscay2);
			BGscax2-=(1-SnapSize)/loopnum;
			BGscay2-=(1-SnapSize)/loopnum;
			ObjEffect_SetAngle(obj9,0,0,CAngle+CameraDir+SnapAngle);

			yield;
		}
ObjEffect_SetScale(obj, BGscax2,BGscay2);
ObjEffect_SetScale(obj9, BGscax2,BGscay2);
if(Judg=="Success")
{
wait(10);
}
Obj_Delete(obj);
Obj_Delete(obj9);
}


task MenuEffect//写真枚数表示の紅葉を表示する
{
let DX=0;
let DY=0;
let No=0;
while(GetCommonDataDefault("BossExist",false)==false){yield;}
let EnemyLife=GetCommonDataDefault("EnemyLife",1);
wait(1);
loop(ceil(EnemyLife/500))
{
	if((No%10)<=4)
	{
		DX=GetClipMaxX+70;
		DY=GetCenterY-170+(No%10)*85;
	}
	else
	{
		DX=GetClipMaxX+155;
		DY=GetCenterY-170+((No%10)-5)*85;
	}
	EffectMomiji(DX,DY,No);
	No+=1;
}
}

task EffectMomiji(X,Y,No)//紅葉の設定
{
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgMenuEffect);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -15, -15);
		ObjEffect_SetVertexXY(obj, 1, 15,  -15);
		ObjEffect_SetVertexXY(obj, 2, 15, 15);
		ObjEffect_SetVertexXY(obj, 3,  -15,  15);
		
		ObjEffect_SetVertexUV(obj, 0,  2,  24);
		ObjEffect_SetVertexUV(obj, 1,  32, 24);
		ObjEffect_SetVertexUV(obj, 2, 32,  55);
		ObjEffect_SetVertexUV(obj, 3, 2, 55);
		ObjEffect_SetLayer(obj,8);
		ObjEffect_SetScale(obj, 1,1);
		
		ObjEffect_SetVertexColor(obj,0,255,255,50,50);
		ObjEffect_SetVertexColor(obj,1,255,255,50,50);
		ObjEffect_SetVertexColor(obj,2,255,255,50,50);
		ObjEffect_SetVertexColor(obj,3,255,255,50,50);

		Obj_SetPosition(obj,X,Y);
		ascent(let i in 0..30)
		{
			ObjEffect_SetScale(obj, i/30,i/30);
			yield;
		}
		while(GetEnemyInfoDefault(1,ENEMY_LIFE,10000)-1000+501*No>=GetCommonDataDefault("EnemyLife",1) && GetCommonDataDefault("ShootingEnd",false)==false){yield;}
		Obj_Delete(obj);
}
//////////////////////////////////////////////////////////////////////////////////////////
task DefeatEffect//撃破時紅葉を表示する
{
let No=0;
while(GetCommonDataDefault("DefeatBoss",false)==false){yield;}
let DX=GetEnemyInfo(1,ENEMY_X);
let DY=GetEnemyInfo(1,ENEMY_Y);
let Alpha;
let Angle;
let Speed=0;
wait(1);
loop(30)
{
let Alpha=rand(150,255);
ascent(let i in 0..10)
{
DefeatMomiji(DX,DY,Speed,2.5+0.25*i,Alpha);
}
wait(1);
Speed+=0.05;
}
}

task DefeatMomiji(X,Y,speed,Scale,Alpha)//紅葉の設定
{
		let Angle=rand(0,360);
		let Speed=speed+rand(3,10);
		let RotateAngle=rand(2,5);
		let Zangle=rand(0,360);
		let dir=rand_int(0,1)*2-1;
		let YRandamColor=rand(50,200);

		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgMenuEffect);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -15, -15);
		ObjEffect_SetVertexXY(obj, 1, 15,  -15);
		ObjEffect_SetVertexXY(obj, 2, 15, 15);
		ObjEffect_SetVertexXY(obj, 3,  -15,  15);
		
		ObjEffect_SetVertexUV(obj, 0,  2,  24);
		ObjEffect_SetVertexUV(obj, 1,  32, 24);
		ObjEffect_SetVertexUV(obj, 2, 32,  55);
		ObjEffect_SetVertexUV(obj, 3, 2, 55);
		ObjEffect_SetLayer(obj,4);
		ObjEffect_SetScale(obj, Scale,Scale);
		
		ObjEffect_SetVertexColor(obj,0,Alpha,255,YRandamColor,50);
		ObjEffect_SetVertexColor(obj,1,Alpha,255,YRandamColor,50);
		ObjEffect_SetVertexColor(obj,2,Alpha,255,YRandamColor,50);
		ObjEffect_SetVertexColor(obj,3,Alpha,255,YRandamColor,50);

		Obj_SetPosition(obj,X,Y);
		Obj_SetAngle(obj,Angle);
		Obj_SetSpeed(obj,Speed);

		let StandardScale=Scale;
		loop(45)
		{
			ObjEffect_SetAngle(obj,0,0,Zangle);
			ObjEffect_SetScale(obj, Scale,Scale);
			Zangle+=RotateAngle*dir;
			Scale-=StandardScale/45;
			yield;
		}
		Obj_Delete(obj);
}

task MenuBackDraw
{//メニューの黒背景を描画するタスク。現在未使用。
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgBG);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	//	ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, 0, 0);
		ObjEffect_SetVertexXY(obj, 1, 640,  0);
		ObjEffect_SetVertexXY(obj, 2, 640, 480);
		ObjEffect_SetVertexXY(obj, 3,  0,  480);
		
		ObjEffect_SetVertexUV(obj, 0,  0,  0);
		ObjEffect_SetVertexUV(obj, 1,  384, 0);
		ObjEffect_SetVertexUV(obj, 2, 384,  448);
		ObjEffect_SetVertexUV(obj, 3, 0, 448);
		ObjEffect_SetLayer(obj,8);
		ObjEffect_SetScale(obj, 1.0,1.0);

		Obj_SetPosition(obj,0,0);
	/*	loop
		{
		if(Select==No+2000 && StageState==Menu && MenuState==Menu && DetailDrawing)
		{
			ascent(let i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,255,255,255,255);
			}
		}
		else
		{
			ascent(let i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,0,255,255,255);
			}
		}
		yield;
		}
*/
}


task BestPhotoDraw
{//ベストショット及びその情報を描画する関数を呼ぶ
yield;
	ascent(let i in 1..100)
	{
	BestPhotoDrawing(i);
	BestPhotoDetailDrawing(i);
	}	
}

task BestPhotoDrawing(No)
{//写真を描画する
	let DrawShootPhoto=false;
	if(SelectedCharacter==DefinedPlayer1)
	{
		if(GetCommonDataDefaultEx(NsScore,Player1AllPhoto[No],0)!=0)
		{
		DrawShootPhoto=true;
		}
	}
	if(SelectedCharacter==DefinedPlayer2)
	{
		if(GetCommonDataDefaultEx(NsScore,Player2AllPhoto[No],0)!=0)
		{
		DrawShootPhoto=true;
		}
	}
		let X=((170/2)^2+(225/2)^2)^0.5;
		let X=((160/2)^2+(215/2)^2)^0.5;
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgBestShot[No]);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -X, -X);
		ObjEffect_SetVertexXY(obj, 1, X,  -X);
		ObjEffect_SetVertexXY(obj, 2, X, X);
		ObjEffect_SetVertexXY(obj, 3,  -X,  X);
		
		ObjEffect_SetVertexUV(obj, 0,  GetCenterX-X,  GetCenterY-55-X);
		ObjEffect_SetVertexUV(obj, 1,  GetCenterX+X, GetCenterY-55-X);
		ObjEffect_SetVertexUV(obj, 2, GetCenterX+X,  GetCenterY-55+X);
		ObjEffect_SetVertexUV(obj, 3, GetCenterX-X, GetCenterY-55+X);
		ObjEffect_SetLayer(obj,8);
		ObjEffect_SetScale(obj, 0.9,0.9);

		Obj_SetPosition(obj,GetCenterX+70,GetCenterY-55);
		loop
		{
		if(SpellSelect+(SpellLevel)*10==No && StageState==Menu && MenuState==Menu && PhotoDrawing && ShootFlag[SpellLevel] && DrawShootPhoto)
		{
			ascent(let i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,255,255,255,255);
			}
		}
		else
		{
			ascent(let i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,0,255,255,255);
			}
		}
		yield;
		}
}

task BestPhotoDetailDrawing(No)
{//写真の詳細を描画する
	let DrawShootPhoto=false;
	if(SelectedCharacter==DefinedPlayer1)
	{
		if(GetCommonDataDefaultEx(NsScore,Player1AllPhoto[No],0)!=0)
		{
		DrawShootPhoto=true;
		}
	}
	if(SelectedCharacter==DefinedPlayer2)
	{
		if(GetCommonDataDefaultEx(NsScore,Player2AllPhoto[No],0)!=0)
		{
		DrawShootPhoto=true;
		}
	}
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgBestShotDetail[No]);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -75, -87);
		ObjEffect_SetVertexXY(obj, 1, 75,  -87);
		ObjEffect_SetVertexXY(obj, 2, 75, 82);
		ObjEffect_SetVertexXY(obj, 3,  -75,  82);
		
		ObjEffect_SetVertexUV(obj, 0,  220,  295);
		ObjEffect_SetVertexUV(obj, 1,  370, 295);
		ObjEffect_SetVertexUV(obj, 2, 370,  464);
		ObjEffect_SetVertexUV(obj, 3, 220, 464);
		ObjEffect_SetLayer(obj,8);
		ObjEffect_SetScale(obj, 0.9,0.9);

		Obj_SetPosition(obj,GetCenterX+110,GetCenterY-25);
		loop
		{
		if(No==SpellSelect+(SpellLevel)*10 && StageState==Menu && MenuState==Menu && DetailDrawing && ShootFlag[SpellLevel] && DrawShootPhoto)
		{
			ascent(let i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,255,255,255,255);
			}
		}
		else
		{
			ascent(let i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,0,255,255,255);
			}
		}
		yield;
		}
}

task DetailDraw//スペル終了時に写真のスコア詳細を表示するかどうかの切り替え
{
loop
{
	while(GetKeyState(VK_SKIP)!=KEY_PUSH){yield;}
		DetailDrawing=false;
		wait(1);
	while(GetKeyState(VK_SKIP)!=KEY_PUSH){yield;}
		DetailDrawing=true;
		PhotoDrawing=false;
		wait(1);
	while(GetKeyState(VK_SKIP)!=KEY_PUSH){yield;}
		DetailDrawing=true;
		PhotoDrawing=true;
		wait(1);
yield;
}
}

task DrawSpellName(img,left,up,right,down)//スペル名を表示する。入力座標を左揃えで表示する。
{
		let bx=GetClipMaxX-8;
		let by=302;
		let sx=0.5;
		let sy=0.5;
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgSpellName);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);

		ObjEffect_SetVertexXY(obj, 0, left-right, -(down-up)/2);
		ObjEffect_SetVertexXY(obj, 1, 0,  -(down-up)/2);
		ObjEffect_SetVertexXY(obj, 2, 0, (down-up)/2);
		ObjEffect_SetVertexXY(obj, 3,  left-right,  (down-up)/2);
		
		ObjEffect_SetVertexUV(obj, 0,  left,  up);
		ObjEffect_SetVertexUV(obj, 1,  right, up);
		ObjEffect_SetVertexUV(obj, 2, right,  down);
		ObjEffect_SetVertexUV(obj, 3, left,down);

		ObjEffect_SetScale(obj, sx,sy);
		Obj_SetPosition(obj,bx,by);
		ObjEffect_SetLayer(obj,8);
		
		yield;
		Obj_Delete(obj);
}
