//撮影タスクを分離

//ショットタスク
task shotRoutine(){

	//
	let minX = GetClipMinX();
	let maxX = GetClipMaxX();
	let minY = GetClipMinY();
	let maxY = GetClipMaxY();

	//ファインダーサイズ
	let finH = finderHorizonalMax;
	let finV = finderVerticalMax;

	//ボーナス用
	let photoNo = 0;
	let bossShotBonus = [];
	let selfShotBonus = false;
	let riskShotBonus = 0;

	//弾カウント用配列
	let deleteShotTotal = 0;
	let deleteShotKind = [];
	let deleteShotColor = [];
	let deleteShotChip = [];
	loop(12){deleteShotKind = deleteShotKind ~ [0];}
	loop(8){deleteShotColor = deleteShotColor ~[0];}
	loop(8){deleteShotChip = deleteShotChip ~[0];}

	//ファインダーオブジェクト初期化
	let objShot = initEffect(imgAya,10,5);
	local{
		ObjEffect_SetPrimitiveType(objShot, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetVertexXY(objShot,0, 0,0);
		ObjEffect_SetVertexUV(objShot,0, 74,200);
		let angle = -90;	
		let angle01 = [atan2(finV,finH), atan2(finH,finV)];
		ascent(i in 0..9){
			let rad = [finV/2 , ((finV/2)^2+(finH/2)^2)^0.5, finH/2, ((finV/2)^2+(finH/2)^2)^0.5][i%4];
			let u = [74,125,125,125][i%4];
			let v = [161,161,200,161][i%4];
		
			ObjEffect_SetVertexXY(objShot,i + 1, rad * cos(angle),rad * sin(angle));
			ObjEffect_SetVertexUV(objShot,i + 1, u,v);
			angle += chAngle(angle01[[1,0,0,1][i%4]]);
		}
	}

	//ファインダーの色変更ルーチン
	sub vertexColor(){
		ObjEffect_SetAngle(objShot,0,0,0);
		ascent(i in 0..10){
			ObjEffect_SetVertexColor(objShot,i,[0,200][finderFlag],250,250,250);
		}
	}//end sub

	//制限時間円エフェクトのタスクを起動
	timeEffect();


	loop{

		vertexColor();

		//ファインダーモードに入るまで待機
		while(!keyStatus[4] || keyStatus[6] || filmCharge != 100){
			if(filmStop){photoNo = 0;}
			yield;
		}

		if(!doSlow){Slow(2);}

		//変数初期化
		let finderScale = 0;
		let hitCheck = false;
		timeOut = false;
		finderFlag = true;
		shotBonus = [0,0,0,0,0,0,0];
		bossShotBonus = [];
		selfShotBonus = false;
		riskShotBonus = 0;
		resultPoint = -1;

		//周辺エフェクトの起動
		ascent(i in 0..4){shootEffect(i);}

		PlaySE(seFinder);

		vertexColor();

		//ファインダーモード
		ascent(i in 0..finderCounterMax){

			let posX = Obj_GetX(objCursor);
			let posY = Obj_GetY(objCursor);

			finderScale = (1 - 0.75 * (i/finderCounterMax)^2);
			finH = finderHorizonalMax * finderScale;
			finV = finderVerticalMax * finderScale;

			Obj_SetPosition(objShot, posX, posY);
			ObjEffect_SetScale(objShot,finderScale,finderScale);

			yield;

			if(OnMissed()){
				StopSE(seFinder);
				break;

			}else if(!keyStatus[4]){

				Slow(0);
				finderFlag=false;
				StopSE(seFinder);
				PlaySE(seShoot);
				blankCount = NULL;

				yield;
				//撮影する
				if(takePhoto){takeSnapShot();}
				//ヒットチェック
				checkHitObject01();
				yield;
	
				let dx = (finH-finV)/2;
				deleteBullets(posX+dx, posY, finV/2, 0);
				deleteBullets(posX-dx, posY, finV/2, 0);

				waitTime(5);
				//写真の判定
				judgeSnapShot();
				break;

			}else if(i == finderCounterMax - 1){

				timeOut = true;
			}
		}

		Slow(0);
		finderFlag=false;

		blankCount = blankTime * [-1,1][hitCheck];

		if(timeOut){
			loop(20){yield;}
			timeOut = false;
		}

		if(!hitCheck){
			ascent(i in 0..21){
				let scale = finderScale * (1-i/20);
				ObjEffect_SetScale(objShot, scale, scale);
				ObjEffect_SetAngle(objShot, 0,0, 180 * (i/20));
				yield;
			}
			PlaySE(seFail);
		}else{

			if(takePhoto){
				vertexColor();
				waitTime(30);
			}else{
				waitTime(30);
				ascent(i in 0..11){
					let scale = finderScale * (1-i/10);
					ObjEffect_SetScale(objShot, scale, finderScale);
					yield;
				}
			}

			PlaySE(seSuccess);
		}


	//-------------------------------
		//ヒットチェック
		task checkHitObject01(){

			yield;
			let posX = Obj_GetX(objShot);
			let posY = Obj_GetY(objShot);

			let x1 = posX-(finH/2) +10;
			if(x1 < minX){x1 = minX + subtractoin(minX,x1,10);}
			let x2 = posX+(finH/2) -10;
			if(x2 > maxX){x2 = maxX - subtractoin(x2,maxX,10);}
			let y1 = posY-(finV/2) +20;
			if(y1 < minY){y1 = minY + subtractoin(minY,y1,20);}
			let y2 = posY+(finV/2) -20;
			if(y2 > maxY){y2 = maxY - subtractoin(y2,maxY,20);}

			function subtractoin(let value1, let value2, let num){
				let subtruct = num - (value1-value2);
				if(subtruct > 0){return subtruct;}
				else{return 0;}
			}

			ascent(enemy in EnumEnemyBegin..EnumEnemyEnd){

				let enemyID = EnumEnemyGetID(enemy);
				let enemyX = GetEnemyInfoDefault(enemyID, ENEMY_X,NULL);
				let enemyY = GetEnemyInfoDefault(enemyID, ENEMY_Y,NULL);
	
				let checkX = (enemyX > x1 && enemyX < x2);
				let checkY = (enemyY > y1 && enemyY < y2);

				if(checkX && checkY){
					let distX = absolute(enemyX - Obj_GetX(objShot));
					let distY = absolute(enemyY - Obj_GetY(objShot));
					checkHitObject02(enemyX, enemyY, distX, distY);
				}

			}
	
			local{
				//セルフショット
				let distX = absolute(GetPlayerX() - Obj_GetX(objShot));
				let distY = absolute(GetPlayerY() - Obj_GetY(objShot));
				if(distX < (finH/2 - 10) && distY < (finV/2 - 20)){
					selfShotBonus = true;
				}

				//リスクショット
				ascent(i in 1..6){
					let radius = 2 + i * 3;
					if(GetEnemyShotCountEx(GetPlayerX(),GetPlayerY(), radius, ALL) > 0){
						riskShotBonus = i;
						break;
					}
				}
			}

		}//end task

		//オブジェクト
		task checkHitObject02(let hitX,let hitY,let distX,let distY){

			let objHit = Obj_Create(OBJ_SHOT);
			ObjShot_SetGraphic(objHit, 1);
			ObjShot_SetDamage(objHit, 501);
			ObjShot_SetPenetration(objHit, 100);
			Obj_SetSpeed(objHit, 0);
			Obj_SetAngle(objHit, 0);
			Obj_SetAlpha(objHit, 0);
			Obj_SetPosition(objHit, hitX,hitY);
	
			yield;

			//ボスショットボーナスの判定
			if(Obj_IsIntersected(objHit)){
				let distMax = finV/2;
				let distXY = (distX^2+distY^2)^0.5;
				bossShotBonus = bossShotBonus ~ [trunc(11 * (1 - distXY/distMax)^2)/10];			
				hitCheck = true;
			}
			Obj_Delete(objHit)

		}//end task

	}//end loop

    //-------------------------------
	//弾消し
	task deleteBullets(let dx,let dy,let rad,let num){

		//弾カウント
		deleteShotTotal += GetEnemyShotCountEx(dx,dy,rad,ALL);
		ascent(i in 0..8){
			ascent(j in 0..12){
				let deleteShotNum = GetEnemyShotCountEx(dx,dy,rad,shotKind[i*12+j]);
				deleteShotKind[j] = deleteShotKind[j] + deleteShotNum;
				deleteShotColor[i] = deleteShotColor[i] + deleteShotNum;
				deleteShotChip[i] = deleteShotChip[i] + (deleteShotNum * shotPoint[j]) / 2;
			}
		}

		//弾の消去
		local{
			let deleteType = GetCommonDataDefault("PermitAllShotDelete", deleteAllShot);
			DeleteEnemyShotImmediatelyInCircle([SHOT,ALL][deleteType],dx,dy,rad);
		}
		yield;

		if(num < 2){
			rad = rad/2;
			ascent(i in 0..4){
				deleteBullets(dx +rad *[-1,1][trunc(i/2)],dy + rad*[-1,1][i%2], rad, num+1);
			}
		}


	}//end task

    //---------------

	//周囲のエフェクト
	task shootEffect(let num){

		let vertical;
		let horizonal;

		yield;

		let objEffect = initEffect(imgShoot,4,2);
		ObjEffect_SetRenderState(objEffect, SUBTRACT);

		ascent(i in 0..4){
			let alpha = 255;
			ObjEffect_SetVertexColor(objEffect,i,alpha,alpha-30,alpha,alpha);
		}

		alternative(num)
		case(0,1){
			Obj_SetPosition(objEffect,0,0);
		}
		case(2,3){
			ObjEffect_SetAngle(objEffect,0,0,180);
			Obj_SetPosition(objEffect,430,480);
		}

		while(finderFlag){
			alternative(num)
			case(0,1){
				vertical= Obj_GetY(objShot)-finV/2;
				horizonal= Obj_GetX(objShot)-finH/2;
			}
			case(2,3){
				vertical= 480-(Obj_GetY(objShot)+finV/2);
				horizonal= 430-(Obj_GetX(objShot)+finH/2);
			}

			ascent(i in 0..4){
				let efX= [430, horizonal][num%2];
				let efY= [vertical,480][num%2];
				ObjEffect_SetVertexUV(objEffect,i,[0,efX][trunc(i/2)],[0,efY][i%2]);
				ObjEffect_SetVertexXY(objEffect,i,[0,efX][trunc(i/2)],[0,efY][i%2]);
			}

			yield;
		}

		Obj_Delete(objEffect);

	}//end task

    //------------------------

	//制限時間円のエフェクト
	task timeEffect(){

		let objTimeCircle = initEffect(imgAya, 52, 3);

		ascent(i in 0..52){
			let angle = trunc(i/2) * 360/25;
			let radius = [100,90][i%2];
			ObjEffect_SetVertexXY(objTimeCircle, i, radius * cos(angle), radius * sin(angle));
			ObjEffect_SetVertexUV(objTimeCircle, i, [51,118][trunc((i%4)/2)], [13,16][i%2]);
		}

		function vertexAlpha(let display){
			ascent(i in 0..52){
				ObjEffect_SetVertexColor(objTimeCircle, i, [0,100][display], 250, 100, 100);
			}
		}//end function

		loop{
			//不可視化
			vertexAlpha(false);
			while(!finderFlag){yield;}

			//可視化
			vertexAlpha(true);

			ascent(i in 0..finderCounterMax){
				let scale = 1 - (i / finderCounterMax);
				ObjEffect_SetScale(objTimeCircle, scale, scale);
				Obj_SetPosition(objTimeCircle ,Obj_GetX(objShot),Obj_GetY(objShot));
				if(!finderFlag){break;}
				yield;
			}
		}

	}//end task

    //-----------------------------
	//撮影

	sub takeSnapShot(){

		let posX = Obj_GetX(objShot);
		let posY = Obj_GetY(objShot);

		let x1 = posX-(finH/2)-1;
		if(x1 < minX){x1 = minX;}
		let x2 = posX+(finH/2)+1;
		if(x2 > maxX){x2 = maxX;}
		let y1 = posY-(finV/2)-1;
		if(y1 < minY){y1 = minY;}
		let y2 = posY+(finV/2)+1;
		if(y2 > maxY){y2 = maxY;}

		SaveSnapShot("snapshot\"~snapFile[photoNo]~".bmp",x1,y1,x2,y2);
		SetCommonData(snapFile[photoNo]~"XY", trunc(x2-x1)*10^3 + trunc(y2-y1));
	}

    //-------------------------------
	//写真の評価
	sub judgeSnapShot(){

		let point = 0;

		//消した弾が0でないとき
		if(deleteShotTotal != 0){

			//粒の色(RED,GREEN,BLUE,YELLOW,PURPLE,AQUA,ORANGE,WHITE)
			let red = [255,50,50,255,255,50,200,255];
			let green = [50,255,50,255,50,255,200,255];
			let blue = [50,50,255,50,255,255,100,255];

			let other = 0;

			local{
				//種類に応じた得点加算
				let number = 0;
					ascent(i in 0..12){
					point += deleteShotKind[i] * shotPoint[i] * shotDeletePoint;
					number += deleteShotKind[i];
					deleteShotKind[i] = 0;
				}
				other = deleteShotTotal- number;
				point += other * shotPoint[12]* shotDeletePoint;
			}

			local{
				//色
				let colerBullet = deleteShotTotal - other;
				let number = 0;
				ascent(i in 0..8){
					if(deleteShotColor[i] > 0){number++;}
				}

				let color=[0,0,0];
				color[0] = deleteShotColor[0] + deleteShotColor[6];
				color[1] = deleteShotColor[1];
				color[2] = deleteShotColor[2] + deleteShotColor[5];

				if(number >= 4){
					shotBonus[6] = [2,1][number >= 6];
					shotBonus[5] = number * [200,400][number >= 6];
				}else{
					ascent(i in 0..3){
						if((color[i] / deleteShotTotal) >= 0.6){
							shotBonus[6] = 3 + i;
							let bonus = trunc(color[i]/20 + 1);
							shotBonus[5] = [300 * bonus, 2000][bonus > 6];
							break;
						}
					}
				}
				ascent(i in 0..8){
					deleteShotColor[i] = 0;
				}

				point += shotBonus[5];
			}

			//フィルム回復する光の粒発生
			ascent(i in 0..8){
				let colorChip = trunc(deleteShotChip[i]);
				loop(colorChip){
					let x = Obj_GetX(objShot) + rand(-finH/2,finH/2);
					let y = Obj_GetY(objShot) + rand(-finV/2,finV/2);
					recoveryChip([red[i],green[i],blue[i]], x, y);
				}
				deleteShotChip[i] = 0;
			}
			loop(trunc(other*0.75)){
				let x = Obj_GetX(objShot) + rand(-finH/2,finH/2);
				let y = Obj_GetY(objShot) + rand(-finV/2,finV/2);
				recoveryChip([120,120,120], x, y);
			}

			deleteShotTotal = 0;
		}

		//リスクショット
		if(riskShotBonus>0){
			let risk = 400 * (6 - riskShotBonus);
			point += risk;
			shotBonus[4] = risk;
		}

		//被写体を撮影しているとき
		if(length(bossShotBonus) > 0){
			let bonus = 0;
			ascent(i in 0..length(bossShotBonus)){
				if(bossShotBonus[i] > 1){bossShotBonus[i] = 1;}
				bonus += bossShotBonus[i];
			}
			bonus = 1 + bonus/length(bossShotBonus);
			shotBonus[0] = trunc(bonus*10);
			point *= bonus;
		}

		//自分が写っているとき
		if(selfShotBonus){
			point *= 1.2;
			shotBonus[1] = 12;
		}

		//どちらも写っている時
		if(length(bossShotBonus) > 0 && selfShotBonus){
			point *= 1.5;
			if(length(bossShotBonus) == 1){
				shotBonus[2] = 15;
			}else{
				shotBonus[3] = 15;
			}
		}

		//100以下の端数切捨
		point -= point % 100;

		//写真を収める
		if(length(bossShotBonus) > 0 && takePhoto){

			SetCommonData(snapFile[photoNo],-1);
			yield;

			SetCommonData(snapFile[photoNo], point);
			photoAdmin(photoNo,Obj_GetX(objShot),Obj_GetY(objShot));
			photoNo++;
			if(photoNo > 9){photoNo = 0;}

		}else if(length(bossShotBonus) == 0){
			point *= 0.1;
		}

		resultPoint = point;
		AddScore(point);

	}//end sub

}//end task



//写真管理タスク
task photoAdmin(let photoNo, let posX, let posY){

	let objScore=[];
	let photoX = [450,520,590][photoNo%3];
	let photoY = [266,324,382,440][trunc(photoNo/3)];
	let imgPhoto = "snapshot\"~snapFile[photoNo]~".bmp";

	LoadGraphic(imgPhoto);

	let objPhoto = initEffect(imgPhoto,4,8);
	ObjEffect_SetRenderState(objPhoto,ALPHA);

	local{
		let xy = GetCommonDataDefault(snapFile[photoNo]~"XY",1001);
		let x = trunc(xy / 10^3);
		let y = xy % (10^3);

		ascent(i in 0..4){
			ObjEffect_SetVertexXY(objPhoto,i,[-x/2,x/2][trunc(i/2)],[-y/2,y/2][i%2]);
			ObjEffect_SetVertexUV(objPhoto,i,[0,x][trunc(i/2)],[0,y][i%2]);
			ObjEffect_SetVertexColor(objPhoto,i,250,250,250,250);
		}
	}


	local{

		Obj_SetPosition(objPhoto, posX, posY);

		let angle = chAngle(atan2(photoY-posY, photoX-posX));
		let distant = ((photoY - posY)^2 + (photoX - posX)^2)^0.5;
		let time = trunc(rand(29,32));

		ascent(i in 0..time+1){
			let scale = 1 - 0.75 * (i/time);
			posX += (distant/time) * cos(angle);
			posY += (distant/time) * sin(angle);
			Obj_SetPosition(objPhoto, posX, posY);
			ObjEffect_SetScale(objPhoto, scale, scale);
			ObjEffect_SetAngle(objPhoto, 0, 0, i * (360/30));
			yield;
		}
	}

	//得点表示
	local{
		let score =  GetCommonDataDefault(snapFile[photoNo], 0);

		let string = [];
		let leng = trunc(log10(score)) + 1;
		if(leng<2){leng = 2;}
		string = string ~ toInt(score, leng);

		ascent(i in 0..leng){

			objScore = objScore ~ [initEffect(imgChar,4,8)];
			ObjEffect_SetRenderState(objScore[i],ALPHA);
			ObjEffect_SetScale(objScore[i],0.5,0.5);

			local{
				let number = string[i];
				let x = 5 + 15 * (number % 5);
				let y = 7 +  24 * trunc(number / 5);
				ascent(j in 0..4){
					ObjEffect_SetVertexXY(objScore[i],j,[-6,6][trunc(j/2)],[-8,9][j%2]);
					ObjEffect_SetVertexUV(objScore[i],j,[x,x+13][trunc(j/2)],[y,y+18][j%2]);
					ObjEffect_SetVertexColor(objScore[i],j,250,250,250,250);
				}
			}

			let x = photoX + 40 + (i - leng) * 8;
			let y = photoY + 33;
			Obj_SetPosition(objScore[i], x, y);
		}

	}//end local

	loop{
		yield;
		if(filmStop || GetCommonDataDefault(snapFile[photoNo],-1) == -1){break;}
	}

	ascent(i in 0..length(objScore)){
		Obj_Delete(objScore[i]);
	}

	Obj_Delete(objPhoto);
	DeleteGraphic(imgPhoto);
	
}//end task


//フィルム装填回復
task recoveryChip(let color,let chipX, let chipY){

	let speed = 0;
	let objChip = initEffect(imgAya,4,3);
	ObjEffect_SetRenderState(objChip, ADD);
	ascent(i in 1..4){
		ObjEffect_SetVertexXY(objChip, i, [-7,7][trunc(i/2)],[-7,7][i%2]);
		ObjEffect_SetVertexUV(objChip, i, [98,109][trunc(i/2)],[27,38][i%2]);
		ObjEffect_SetVertexColor(objChip, i, rand(50,200),color[0],color[1],color[2]);
	}

	//自機に向かって移動
	loop{
		let distX = GetPlayerX() - chipX;
		let distY = GetPlayerY() - chipY;
		let angle = chAngle(atan2(distY,distX));

		chipX = chipX + speed * cos(angle);
		chipY = chipY + speed * sin(angle);

		if(speed < 10){speed += 10/60;}
		Obj_SetPosition(objChip,chipX,chipY);
		if(((|distX|)+(|distY|))<30){break;}
		yield;
	}

	Obj_Delete(objChip);
	PlaySE("seScore.wav");

	if(filmCharge < 100){
		filmCharge += chargeRate;
		if(filmCharge>100){filmCharge = 99;}
	}

}//end task
