let MZC_PITEM_GETSOUND = "";
let MZC_PITEM_IMAGE = "";

// Pアイテムをドロップするメソッド
// 引数：アイテムをドロップするX座標
//		 アイテムをドロップするY座標
//		 アイテムをドロップする範囲
//		 アイテム数
//		 EXステージか否か
function DropPItem( let srcX, let srcY, let spread, let num, let isEx ) {
	let bigNum = floor( num / 8 );
	loop ( bigNum ) {
		PItem( true, srcX + rand( -spread, spread ), srcY + rand( -spread, spread ),
			   OnBomb(), OnPlayerMissed(), isEx );
	}
	loop ( num - bigNum * 8 ) {
		PItem( false, srcX + rand( -spread, spread ), srcY + rand( -spread, spread ),
			   OnBomb(), OnPlayerMissed(), isEx );
	}
}

// ミスしたときにPアイテムをばらまくタスク
// @Initializedで一度だけ呼ぶ
// 引数：EXステージか否か
task DropPItemOnMissedTask( let isEx ) {
	let isMissed = false;
	loop {
		if ( OnPlayerMissed() ) {
			if ( isMissed == false ) {
				isMissed = true;
				DropPItem( GetPlayerX(), GetPlayerY(), 64, 13, isEx );
			}
		}
		else {
			isMissed = false;
		}
		yield;
	}
}

// Pアイテム本体
task PItem( let isBig, let srcX, let srcY, let isAlreadyBomb, let isAlreadyMissed, let isEx ) {
	//アイテム画像
	let img = MZC_PITEM_IMAGE;
	//アイテム取得時効果音
	let se = "seScore.wav";
	
	let wid = 16;
	let nAngle = 270;
	let nSpeed = 2.8;
	let isHoming = false;
	let count = 0;
	let obj = Obj_Create( OBJ_EFFECT );
	let markerObj;
	let FLAN_CDNS = "__FLAN_FRAMEWORK";
	let FLAN_CD_PLAYER_POWER = "PLAYER_POWER";
	let MZC_CDNS_PLAYER = "MZC_CDNS_PLAYER";
	let MZC_CD_PLAYER_ITEM_COLLECT_LINE = "MZC_CD_PLAYER_ITEM_COLLECT_LINE";
	let isAlreadyOnTop = false;
	let isAlreadyOutTop = true;
	
	if ( IsCommonDataAreaExists( FLAN_CDNS ) == false ) {
		CreateCommonDataArea( FLAN_CDNS );
	}
	if ( IsCommonDataAreaExists( MZC_CDNS_PLAYER ) == false ) {
		CreateCommonDataArea( MZC_CDNS_PLAYER );
	}

	Obj_SetPosition( obj, srcX, srcY );
	Obj_SetAlpha( obj, 255 );
	Obj_SetCollisionToPlayer( obj, false );
	ObjEffect_SetTexture( obj, img );
	ObjEffect_SetRenderState( obj, ALPHA );
	ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
	ObjEffect_CreateVertex( obj, 4 );
	ascent ( let i in 0..4 ) {
		let x = truncate( i / 2 );
		let y = i % 2;
		ObjEffect_SetVertexColor( obj, i, 255, 255, 255, 255 );
		ObjEffect_SetVertexXY( obj, i,
							   wid / 2 * [ -1, 1 ][ x ],
							   wid / 2 * [ -1, 1 ][ y ] );
		ObjEffect_SetVertexUV( obj, i,
							   [ 17, 0 ][ isBig == true ] + [ 0, wid ][ x ],
							   [ 0, wid ][ y ] );
	}
	ObjEffect_SetLayer( obj, 2 );
	Obj_SetAutoDelete( obj, false );
	Obj_SetAngle( obj, nAngle );
	while ( Obj_BeDeleted( obj ) == false ) {
		let nX = Obj_GetX( obj );
		let nY = Obj_GetY( obj );
		let nPower = GetCommonDataDefaultEx( FLAN_CDNS, FLAN_CD_PLAYER_POWER, 0 );		
		if ( nY > GetClipMaxY() + 32 ) {	//画面下に出たので消滅
			Obj_Delete( obj );
		}		
		else if ( nY < GetClipMinY() ) {	//画面上に出たのでマーカーに変化
			//ascent ( let i in 0..4 ) {
			//	ObjEffect_SetVertexColor( obj, i, 0, 255, 255, 255 );
			//}
			if ( isAlreadyOnTop == false ) {
				markerObj = Obj_Create( OBJ_EFFECT );
				ObjEffect_SetLayer( markerObj, 4 );
				ObjEffect_SetTexture( markerObj, img );
				ObjEffect_SetRenderState( markerObj, ALPHA );
				ObjEffect_SetPrimitiveType( markerObj, PRIMITIVE_TRIANGLELIST );
				ObjEffect_CreateVertex( markerObj, 3 );
				ascent( let i in 0..3 ){
					ObjEffect_SetVertexXY( markerObj, i,
										   [ 0, -5, 5 ][ i ],
										   [ -3, 3, 3 ][ i ] );
					ObjEffect_SetVertexUV( markerObj, i,
										   56 + [ 0, -5, 5 ][ i ],
										   56 + [ -3, 3, 3 ][ i ] );
					ObjEffect_SetVertexColor( markerObj, i, 255, 255, 0, 0 );
				}
				isAlreadyOnTop = true;
				isAlreadyOutTop = false;
				Obj_SetPosition( markerObj, nX, GetClipMinY() + 6 );
			}
			else {
				Obj_SetPosition( markerObj, nX, GetClipMinY() + 6 );
			}
		}
		else if ( isAlreadyOutTop == false ) {
			//ascent ( let i in 0..4 ) {
			//	ObjEffect_SetVertexColor( obj, 0, 255, 255, 255, 255 );
			//}
			Obj_Delete( markerObj );
			isAlreadyOutTop = true;
		}
		//取得
		if ( ( ( ( GetPlayerX() - nX ) ^ 2 + ( GetPlayerY() - nY ) ^ 2 ) ^ 0.5 ) < 22
			 && OnPlayerMissed() == false ) {
			let nextPower = nPower + [ 1, 8 ][ isBig == true ];
			SetCommonDataEx( FLAN_CDNS, FLAN_CD_PLAYER_POWER,
							 [ nextPower, 128 ][ nextPower >= 128 ] );
			PlaySE( se );
			Obj_Delete( obj );
		}
		//取得ラインより上にいる
		if ( GetPlayerY() <= GetCommonDataDefaultEx( MZC_CDNS_PLAYER, MZC_CD_PLAYER_ITEM_COLLECT_LINE, -1 )
			 && OnPlayerMissed() == false
			 && ( nPower >= 128 || isEx ) ) {
			isHoming = true;
		}

		//ボムを使用した瞬間
		if ( OnBomb() == false ) {
			isAlreadyBomb = false;
		}
		else if ( OnBomb() == true && isAlreadyBomb == false && OnPlayerMissed() == false ) {
			isHoming = true;
			isAlreadyBomb = true;
		}

		//ミスした
		if ( OnPlayerMissed() == true && isAlreadyMissed == false ) {
			isAlreadyMissed = true;
			if ( isHoming == true ) {
				isHoming = false;
				Obj_SetAngle( obj, 270 );
				Obj_SetSpeed( obj, 2.8 );
				count = 0;
			}
		}
		
		if ( isHoming == true && OnPlayerMissed() == false ) {
			Obj_SetSpeed( obj, 10 );
			Obj_SetAngle( obj,
						  atan2( GetPlayerY() - Obj_GetY( obj ),
								 GetPlayerX() - Obj_GetX( obj ) )
						  );
		}
		else if ( count <= 120 ) {
			if ( count <= 0 ) {
				nSpeed = 2.8;
			}
			else if ( count >= 120 ) {
				nSpeed = -2.5;
			}
			else {
				nSpeed = 2.8 - 5.3 * count / 120;
			}
			Obj_SetSpeed( obj, nSpeed );
		}
		count++;
		yield;
	}
}
