task PlayerDrawCount
{
loop
{
	if(DSdot)
	{
		CharaDrawCount+=1;
		if(CharaDrawCount>=DrawFrame*7)
		{
			CharaDrawCount=0;
		}

		CharaLeftMoveDrawCount+=1;

		if(CharaLeftMoveDrawCount>=DrawFrame*7)
		{
			CharaLeftMoveDrawCount=DrawFrame*4;
		}
		if(KeyStateLeft=="FREE")
		{
			CharaLeftMoveDrawCount=0;
		}

		CharaRightMoveDrawCount+=1;

		if(CharaRightMoveDrawCount>=DrawFrame*7)
		{
			CharaRightMoveDrawCount=DrawFrame*4;
		}
		if(KeyStateRight=="FREE")
		{
			CharaRightMoveDrawCount=0;
		}
	}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	else
	{	
			if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD" || KeyStateRight=="PUSH" || KeyStateRight=="HOLD"){
				if((KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD") && CharaDrawCount>=-18){
					CharaDrawCount--;
				}else if((KeyStateRight=="PUSH" || KeyStateRight=="HOLD") && CharaDrawCount<=18){
					CharaDrawCount++;
				}
			}else{
				if(CharaDrawCount<0){
					CharaDrawCount++;
				}else if(CharaDrawCount>0){
					CharaDrawCount--;
				}
			}
	}
//////////////////////////////////////////////////////////////////////////////////////////////////
yield;
}
}

sub PlayerDraw
{
	if(DSdot)
	{
		SetTexture(imgDS);
		if(FinderMode==false && GetCommonDataDefault("DefeatBossAction",false)==false)
		{
			if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD")
			{
				if(GetCommonDataDefault("TimeStop",false)==false && GetCommonDataDefault("DefeatBoss",false)==false)
				{
				SetGraphicRect(floor(CharaLeftMoveDrawCount/DrawFrame)*32, 49, floor(CharaLeftMoveDrawCount/DrawFrame+1)*32, 97);
				}
			}
			else if(KeyStateRight=="PUSH" || KeyStateRight=="HOLD")
			{
				if(GetCommonDataDefault("TimeStop",false)==false && GetCommonDataDefault("DefeatBoss",false)==false)
				{
				SetGraphicRect(floor(CharaRightMoveDrawCount/DrawFrame)*32, 97, floor(CharaRightMoveDrawCount/DrawFrame+1)*32, 144);
				}
			}
			else
			{
				if(GetCommonDataDefault("TimeStop",false)==false && GetCommonDataDefault("DefeatBoss",false)==false)
				{
				SetGraphicRect(floor(CharaDrawCount/DrawFrame)*32, 1, floor(CharaDrawCount/DrawFrame+1)*32, 49);
				}
			}
		}
		else
		{
			let angle=360+atan2(Obj_GetY(CameraCoordinate)-GetPlayerY,Obj_GetX(CameraCoordinate)-GetPlayerX);
			if(angle%360>=337.5)
			{
				SetGraphicRect(0, 177, 32, 225);
			}
			else
			{
				SetGraphicRect(floor((angle%360+22.5)/45)*32, 177, floor((angle%360+22.5)/45+1)*32, 225);
			}
		}
		SetRenderState(ALPHA);
		if(GetCommonDataDefault("BunSpell",false)==true && GetCommonDataDefault("LoseBoss",false)==true)
		{
			SetAlpha(0);
			SetAutoEffect(false);
		}
		DrawGraphic(GetPlayerX(), GetPlayerY());//自機の描画
	}
	else
	{
		SetTexture(imgPlayer);
		let dir = 0;
		if(FinderMode || GetCommonDataDefault("DefeatBossAction",false)==true)
		{
			let angle=atan2(Obj_GetY(CameraCoordinate)-GetPlayerY,Obj_GetX(CameraCoordinate)-GetPlayerX);
			dir = trunc((angle + 11.25) / 22.5);
			if(dir < 0){dir += 16;}
			else if(dir >= 16){dir -= 16;}
			let rX = 48 * dir;
			SetGraphicRect(rX, 144, rX + 48, 208);
		}
		else if(GetCommonDataDefault("TimeStop",false)==false)
		{
			SetGraphicRect(432+(48*trunc(CharaDrawCount/2)),0,48+432+(48*trunc(CharaDrawCount/2)),48);
		}
		SetRenderState(ALPHA);
		SetGraphicAngle(0,0,0);
		if(GetCommonDataDefault("BunSpell",false)==true && GetCommonDataDefault("LoseBoss",false)==true)
		{
			SetAlpha(0);
			SetAutoEffect(false);
		}
		DrawGraphic(GetPlayerX, GetPlayerY+6);
	}
}

task ChargeFilmSE//高速巻き時のSEを出すタスク。Deleteして無理やり中断している
{
loop
{
if(GetCommonDataDefault("DoubleSpoilerStageMenu",false)==false && !OnEvent)
{
	if(Charge<100)
	{
		if(KeyStateSlowMove=="HOLD" && KeyStateShot=="HOLD")
		{
			PlaySE(SE[5]);
		}
		while(KeyStateSlowMove=="HOLD" && KeyStateShot=="HOLD" && Charge<100){wait(1);}
	}
	DeleteSE(SE[5]);
	if(FinderMode==true)
	{
		while(FinderMode==true){wait(1);}
		wait(70);
		if(GetCommonDataDefault("BunSpell",false)==true)
		{
			wait(30);
		}
	}
}
wait(1);
}
}

task ChargeFilmFinishSE//100%になったときに鳴るSEのタスク
{
loop
{
if(GetCommonDataDefault("DoubleSpoilerStageMenu",false)==false && !OnEvent)
{
	if(Charge==100)
	{
		PlaySE(SE[2]);
		while(Charge==100){wait(1);}
	}
}
wait(1);
}
}

task ChargeFilm//通常時と高速巻き時の装填速度。高速巻き時の装填速度は若干初めが遅い
{
let PlusCharge=0;
let PlusChargeCount=0;
let RapidPlusCharge=0;
let RapidPlusChargeCount=0;
let amp=1;
loop
{
if(GetCommonDataDefault("DoubleSpoilerStageMenu",false)==false && !OnEvent)
{
	if(GetCommonDataDefault("BunSpell",false)==false && IsBossExisting==false)
	{
		amp=3;
	}
	else
	{
		amp=1;
	}

	if(Charge<100)
	{
		if((KeyStateSlowMove=="PUSH" || KeyStateSlowMove=="HOLD") && (KeyStateShot=="PUSH" || KeyStateShot=="HOLD"))
		{
			ChargeEffect;
			Charge+=0.125*amp;
			Charge+=RapidPlusCharge;
			if(RapidPlusChargeCount<5)
			{
				Charge-=0.0625*amp;
				Charge+=PlusCharge*amp;
				RapidPlusCharge=0;
			}
			else if(RapidPlusChargeCount<70)
			{
				RapidPlusCharge=3*RapidPlusChargeCount/560;
			}
			else
			{
				RapidPlusCharge=0.375*amp;
			}
			RapidPlusChargeCount+=1;
		}
		else
		{
			Charge+=0.0625*amp;
			Charge+=PlusCharge*amp;
			if(PlusChargeCount<60)
			{
				PlusCharge=PlusChargeCount/480;
			}
			else
			{
				PlusCharge=0.125*amp;
			}
			RapidPlusChargeCount=0;
		}
		PlusChargeCount+=1;
	}
	else
	{
		Charge=100;
	}
	if(FinderMode==true)
	{
		while(FinderMode==true){wait(1);}
		PlusChargeCount=0;
		RapidPlusChargeCount=0;
		wait(70);
		if(GetCommonDataDefault("BunSpell",false)==true)
		{
			wait(30);
		}
	}
}
wait(1);
}
}
let SpeedChange=false;
task SetSpeedChange//自機のスピード制御。ファインダーモードではスピード０
{
loop
{
	if(SpeedChange==true || GetCommonDataDefault("TimeStop",false)==true || GetCommonDataDefault("DefeatBossAction",false)==true)
	{
		loop
		{
		SetSpeed(0, 0);
		if(SpeedChange==false){
		wait(1);
		break;}
		if(FinderMode==false){
		wait(1);
		break;}
		wait(1);
		}

	}
	else if((KeyStateSlowMove=="PUSH" || KeyStateSlowMove=="HOLD") && (KeyStateShot=="PUSH" || KeyStateShot=="HOLD"))
	{
		SetSpeed(5.0, 0.5);
	}
	else
	{
		SetSpeed(5.0, 2.3);
	}
wait(1);
}
}

task CameraDirChange//カメラを回転させる
{
loop
{
if(GetCommonDataDefault("DoubleSpoilerStageMenu",false)==false)
{
	if(KeyStateUser=="PUSH")
	{
		PlaySE(SE[7]);
		CameraDir+=90;
	}
}
wait(1);
}
}

task EffectItemCreate//アイテム取得時のエフェクト
{
		let scax=1;
		let scay=1;
		let obj=Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgBorder);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -4, -4);
		ObjEffect_SetVertexXY(obj, 1, 4,  -4);
		ObjEffect_SetVertexXY(obj, 2, 4, 4);
		ObjEffect_SetVertexXY(obj, 3,  -4,  4);
		
		ObjEffect_SetVertexUV(obj, 0,  173,  119);
		ObjEffect_SetVertexUV(obj, 1,  180, 119);
		ObjEffect_SetVertexUV(obj, 2, 180,  126);
		ObjEffect_SetVertexUV(obj, 3, 173, 126);
		ObjEffect_SetLayer(obj,3);
		Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,255,100,100,255);
			}
		let StandardSpeed=rand(3,5);	
		let speed=StandardSpeed;
		let angle=rand(0,360);
		let lightangle=rand(0,360);
		loop(50)
		{
			lightangle+=12;
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,205+50*cos(lightangle),100,100,255);
			}
			Obj_SetPosition(obj,Obj_GetX(obj)+speed*cos(angle),Obj_GetY(obj)+speed*sin(angle));
			speed-=StandardSpeed/50;
			ObjEffect_SetScale(obj, scax,scay);
			scax-=0.005;
			scay-=0.005;
			wait(1);
		}

		loop(50)
		{
			lightangle+=12;
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,205+50*cos(lightangle),100,100,255);
			}
			let sangle=atan2(GetPlayerY-Obj_GetY(obj),GetPlayerX-Obj_GetX(obj));
			Obj_SetPosition(obj,Obj_GetX(obj)+speed*cos(sangle),Obj_GetY(obj)+speed*sin(sangle));
			speed+=0.3;
			ObjEffect_SetScale(obj, scax,scay);
			scax-=0.015;
			scay-=0.015;
			wait(1);
		}
		Obj_Delete(obj);
}

task dummyshot(let enemyID,let dx,let dy)//写真内に敵がいた場合敵座標に弾を発生させる
{
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj,dx,dy);
		Obj_SetSpeed(obj,0);
		Obj_SetAngle(obj,0);
		if(GetCommonDataDefault("BunSpell",false)==true)
		{
			ObjShot_SetDamage(obj,501);
		}
		else if(OnEnemySpell)
		{
			ObjShot_SetDamage(obj,1001);
		}
		else
		{
			ObjShot_SetDamage(obj,751);
		}
		ObjShot_SetPenetration(obj,1);
		ObjShot_SetGraphic(obj,121);
		Obj_SetAlpha(obj,0);
		//ObjShot_SetBombResist(obj,true);
		while(!Obj_BeDeleted(obj))
		{
		if(GetCommonDataDefault("BunSpell",false)==true)
		{
			if(Obj_IsIntersected(obj) && enemyID==1 && GetCommonDataDefault("Playermissed",false)==false)
			{
				BossCapture=true;
			}
		}
		else
		{
			if(Obj_IsIntersected(obj) && enemyID==0 && GetCommonDataDefault("Playermissed",false)==false)
			{
				BossCapture=true;
			}
		}
		wait(1);
		}
		Obj_Delete(obj);
}

function dxdy(i,j,dis2,finderangle,CAngle,size)//カメラの中心からカメラの端までのX座標
{
	let a=dis2*cos(finderangle+CAngle-270)+size*i*cos(CAngle+CameraDir)+size*j*sin(-CAngle-CameraDir);
	return(a);
}
function dxdy2(i,j,dis2,finderangle,CAngle,size)//カメラの中心からカメラの端までのY座標
{
	let a=dis2*sin(finderangle+CAngle-270)+size*i*sin(CAngle+CameraDir)+size*j*cos(-CAngle-CameraDir);
	return(a);
}

task ShootTimeOut//ファインダーモードをタイムアウトしたときに呼び出される
{
//	wait(5);
	wait(1);
	Charge=50;
	if(GetCommonDataDefault("BunSpell",false)==false)
	{
	TimeSlowEffect;
	}
	else
	{
		SetCommonData("PlayerPhotoTimeStop",true);
		wait(5);
		if(GetPlayerScriptName=="HATATE.txt")
		{
			Charge=50;
		}	
		wait(25);
	}
}

task TimeSlowEffect//写真を撮ったときやタイムアウトになったときに呼び出される。本家では時間停止するが、無理なのでスローに
{
wait(1);
if(GetCommonDataDefault("BunSpell",false)==false)
{
Slow(2);
}
wait(10);
Slow(0);
}


function PhotoSelect(obj,obj9,No,DX,DY, scax,scay)//変数（PhotoSelectCorsol）にしたがってスペル終了時拡大写真を選択する。Adjust使って見た目よくしてる
{
let CX=GetCenterX;
let CY=GetCenterY-55;
let AdJust=0;
let AdJustsize=0;
let MoveFrame=10;
let size=0.9;
	if(PhotoSelectCorsol==No)
	{
		loop
		{
			if(AdJust<MoveFrame)
			{
				AdJust+=1;
			}
			Obj_SetPosition(obj,DX+(CX-DX)*AdJust/MoveFrame,DY+(CY-DY)*AdJust/MoveFrame);
			ObjEffect_SetScale(obj, scax+(size-scax)*AdJust/MoveFrame,scay+(size-scay)*AdJust/MoveFrame);
			Obj_SetPosition(obj9,DX+(CX-DX)*AdJust/MoveFrame,DY+(CY-DY)*AdJust/MoveFrame);
			ObjEffect_SetScale(obj9, scax+(size-scax)*AdJust/MoveFrame,scay+(size-scay)*AdJust/MoveFrame);
			if(PhotoSelectCorsol!=No)
			{
				break;
			}
			if(GetCommonDataDefault("PhotoEraze",false)==true)
			{
				Obj_Delete(obj);
				Obj_Delete(obj9);
				break;
			}

			if(AdJust==MoveFrame)
			{
				SetCommonData("PhotoSaveReady",true);
			}
			else
			{
				SetCommonData("PhotoSaveReady",false);
			}
			yield;	
		}
	}
	if(PhotoSelectCorsol!=No)
	{
		loop
		{
			if(AdJust>0)
			{
				AdJust-=1;
			}
			Obj_SetPosition(obj,DX+(CX-DX)*AdJust/MoveFrame,DY+(CY-DY)*AdJust/MoveFrame);
			ObjEffect_SetScale(obj, scax+(1-scax)*AdJust/MoveFrame,scay+(1-scay)*AdJust/MoveFrame);
			Obj_SetPosition(obj9,DX+(CX-DX)*AdJust/MoveFrame,DY+(CY-DY)*AdJust/MoveFrame);
			ObjEffect_SetScale(obj9, scax+(1-scax)*AdJust/MoveFrame,scay+(1-scay)*AdJust/MoveFrame);
			if(PhotoSelectCorsol==No)
			{
				break;
			}
			if(GetCommonDataDefault("PhotoEraze",false)==true)
			{
				Obj_Delete(obj);
				Obj_Delete(obj9);
				break;
			}
			//wait(1);
yield;	
		}
	}
}


task PhotoSelectChange//スペル終了時写真選択するための変数を変えるタスク
{
while(GetCommonDataDefault("ShootingEnd",false)==false){yield;}
let num=photoNo;
PhotoSelectCorsol=GetCommonDataDefault("BestShootNo",0);
yield;
loop
{
	if(KeyStateLeft == "PUSH")
	{
		PhotoSelectCorsol-=1;
		if(PhotoSelectCorsol<=-1)
		{
			PhotoSelectCorsol=num-1;
		}
	}
	if(KeyStateRight == "PUSH")
	{
		PhotoSelectCorsol+=1;
		if(PhotoSelectCorsol>=num)
		{
			PhotoSelectCorsol=0;
		}
	}
	yield;
}
}

task LoadDoubleSpoilerData
{//スペル側で設定したスコアポイントを反映
	wait(60);
	shotPoint = GetCommonDataDefault("ShotBasePoint",[100, 150, 400, 110, 40, 70, 80, 130, 200, 70, 60, 200, 150]);
}

	function NumToStringNoComplete(let num)
	{
		if(num==0)
		{
			num=0.1;
		}
		let leng = trunc(log10(num))+1;//整数部分の桁数を求める
		let string = ToString(num);//数字を文字列に変える
		return string[0..leng];//文字列をスライシングして返す
	}

function CentralSetEffectUVzndXY(obj,left,up,right,down)
{
	ObjEffect_SetVertexXY(obj, 0, -(right-left)/2, -(down-up)/2);
	ObjEffect_SetVertexXY(obj, 1, (right-left)/2,  -(down-up)/2);
	ObjEffect_SetVertexXY(obj, 2, (right-left)/2, (down-up)/2);
	ObjEffect_SetVertexXY(obj, 3,  -(right-left)/2,  (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);
}