////////////////////////////////////////////////////////////
//	プレイヤー固有処理部分
//	プレイヤーを追加する場合は以下の各メソッドに固有処理を追加してください。
////////////////////////////////////////////////////////////

//グローバル変数はここ
//
//※グローバル変数は、非選択の自機も含めて全ての自機スクリプトから参照されます
//　同名の変数は上書きされますので、命名にご注意下さい


let m2_nReimuB_RStar = 24;
let m2_nReimuB_angleStar = 0;
let m2_nReimuB_xStar = m2_nReimuB_RStar;
let m2_nReimuB_yStar = 0;

//////////////////////////////////////////////////////////////////////
//	プレイヤースクリプト
//	各メソッド名の末尾のXXを、プレイヤースクリプトのインデックスに
//	変更してください
//
//	例：onInitializeXX() → onInitialize09()
//////////////////////////////////////////////////////////////////////

// 2 霊夢　夢符

//スクリプト初期化
function onInitialize02() {
}

//メインタスク
task MainTask02() {
	let myPlayerIndex = 2;
	/*
	//ザッピングに対応する場合、ザッピング時にすべての実行中のループを
	//終了させてください。
	//例：現在のプレイヤーインデックスが自分のインデックスと異なれば終了する
	if ( m_nPlayerIndex != 04 ) {
		return;
	}
	*/
	TSlowMove();
	TMainShot();
	TSubShot();
	TSubSubShot();

	task TMainShot() {
		let limit = 30;
		let shotPowerFastMaxBase = 1.75;
		let shotPowerFastMax = shotPowerFastMaxBase;
		let shotPowerSlowMaxBase = 1.75;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		let wFast = 5;
		let wFastAry = [ 5, 5, 5, 5, 5, 5, 5, 5, 5 ];
		let wSlow = 5;
		let wSlowAry = [ 5, 5, 5, 5, 5, 5, 5, 5, 5 ];
		let v = 12;
		let yGap = 2;
		
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) { return; }
			while ( OnMissed() == true || OnEvent() == true ){
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			wFast = updatePower( wFastAry );
			wSlow = updatePower( wSlowAry );
			
			if( m_bIsSlowMove ) {	//低速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 128 ) {
						angleGapAry = [ -7, 0, 7 ];
						xGapAry = [ 0, 0, 0 ];
						num = 3;
					}
					else if ( m_nPower >= 96 ) {
						angleGapAry = [ -6, 0, 6 ];
						xGapAry = [ 0, 0, 0 ];
						num = 3;
					}
					else if ( m_nPower >= 16 ) {
						angleGapAry = [ -1.5, 1.5 ];
						xGapAry = [ 0, 0 ];
						num = 2;
					}
					else {
						angleGapAry = [ 0 ];
						xGapAry = [ 0 ];
						num = 1;
					}
					ascent ( let i in 0..num ) {
						let angleGap = angleGapAry[ i ];
						let xGap = xGapAry[ i ];
						CreatePlayerShot01( GetPlayerX() + xGap, GetPlayerY() - yGap,
											v, 270 + angleGap,
											shotPowerSlowMax, 1, 1 );
					}
					if ( m_nPower < 48 ) {
						PlaySE( m_sSePlayerShotSound );
					}
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 96 ) {
						angleGapAry = [ -16, -8, 0, 8, 16 ];
						xGapAry = [ 0, 0, 0, 0, 0 ];
						num = 5;
					}
					else if ( m_nPower >= 80 ) {
						angleGapAry = [ -8, 0, 8 ];
						xGapAry = [ 0, 0, 0 ];
						num = 3;
					}
					else if ( m_nPower >= 64 ) {
						angleGapAry = [ -7, 0, 7 ];
						xGapAry = [ 0, 0, 0 ];
						num = 3;
					}
					else if ( m_nPower >= 48 ) {
						angleGapAry = [ -6.5, 0, 6.5 ];
						xGapAry = [ 0, 0, 0 ];
						num = 3;
					}
					else if ( m_nPower >= 32 ) {
						angleGapAry = [ -6, 0, 6 ];
						xGapAry = [ 0, 0, 0 ];
						num = 3;
					}
					else if ( m_nPower >= 16 ) {
						angleGapAry = [ -1.5, 1.5 ];
						xGapAry = [ 0, 0 ];
						num = 2;
					}
					else {
						angleGapAry = [ 0 ];
						xGapAry = [ 0 ];
						num = 1;
					}
					ascent ( let i in 0..num ) {
						let xGap = xGapAry[ i ];
						let angleGap = angleGapAry[ i ];
						CreatePlayerShot01( GetPlayerX() + xGap, GetPlayerY() - yGap,
											v, 270 + angleGap,
											shotPowerFastMax, 1, 1 );
					}
					if ( m_nPower < 80 ) {
						PlaySE( m_sSePlayerShotSound );
					}
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	task TSubShot() {
		let limit = 30;
		let shotPowerFastMaxBase = 1.85;
		let shotPowerFastMax = shotPowerFastMaxBase;
		let shotPowerSlowMaxBase = 1.8;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		let wFast = 3;
		let wFastAry = [ 30, 15, 10, 8, 8, 5, 3, 3, 3 ];
		let wSlow = 2;
		let wSlowAry = [ 15, 10, 6, 5, 3, 3, 3, 2, 2 ];
		let type = 3;
		let typeSlowAry = [ 6, 6, 6, 5, 5, 4, 4, 4, 3 ];
		let vFast = 22;//27.5;
		let vSlow = 22;

		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) { return; }
			while ( OnMissed() == true || OnEvent() == true ){
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			wFast = updatePower( wFastAry );
			wSlow = updatePower( wSlowAry );

			let xL = GetPlayerX() - m2_nReimuB_xStar;
			let xR = GetPlayerX() + m2_nReimuB_xStar;
			let y = GetPlayerY() - m2_nReimuB_yStar / 2;
			
			if( m_bIsSlowMove ) {	//低速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					let xGapAry = [];
					let num = 0;
					type = updatePower( typeSlowAry );
					if ( m_nPower >= 128 ) {
						xGapAry = [ GetPlayerX(), GetPlayerX(), GetPlayerX(), GetPlayerX() ];
						num = 4;
					}
					else if ( m_nPower >= 64 ) {
						xGapAry = [ GetPlayerX(), GetPlayerX() ];
						num = 2;
					}
					else {
						xGapAry = [ GetPlayerX() ];
						num = 1;
					}
					ascent ( let i in 0..num ) {
						let xGap = xGapAry[ i ];
						TNeedleShot( xGap, GetPlayerY(), vSlow, shotPowerSlowMax, type );
					}
					if ( m_nPower >= 48 ) {
						PlaySE( m_sSePlayerShotSound );
					}
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 128 ) {
						let gap = 12;
						xGapAry = [ xL - gap, xL + gap, xR - gap, xR + gap ];
						num = 4;
					}
					else if ( m_nPower >= 96 ) {
						let gap = 12;
						xGapAry = [ xL - gap, xR + gap ];
						num = 2;
					}
					else if ( m_nPower >= 80 ) {
						let gap = 12;
						xGapAry = [ xL + gap, xR + gap ];
						num = 2;
					}
					else if ( m_nPower >= 64 ) {
						let gap = 7;
						xGapAry = [ xL + gap, xR + gap ];
						num = 2;
					}
					else {
						xGapAry = [ xL, xR ];
						num = 2;
					}
					ascent ( let i in 0..num ) {
						let xGap = xGapAry[ i ];
						TNeedleShot( xGap, y, vFast, shotPowerFastMax, 2 );
					}
					if ( m_nPower >= 80 ) {
						PlaySE( m_sSePlayerShotSound );
					}
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	task TSubSubShot() {
		let limit = 30;
		let shotPowerFastMaxBase = 1.85;
		let shotPowerFastMax = shotPowerFastMaxBase;
		let wFast = 3;
		let wFastAry = [ 30, 15, 10, 8, 8, 7, 5, 5, 5 ];
		let vFast = 22;
		let count = 0;

		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) { return; }
			while ( OnMissed() == true || OnEvent() == true ){
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			wFast = updatePower( wFastAry );

			let xL = GetPlayerX() - m2_nReimuB_xStar;
			let xR = GetPlayerX() + m2_nReimuB_xStar;
			let y = GetPlayerY() - m2_nReimuB_yStar / 2;
			
			if( m_bIsSlowMove ) {	//低速移動時の自弾
				count = 0;
				yield;
			}
			else {	//高速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 128 ) {
						let gap = 12;
						xGapAry = [ xL, xL + gap, xL + 2 * gap,
									xR, xR - gap, xR - 2 * gap  ];
						num = 6;
					}
					else if ( m_nPower >= 96 ) {
						if ( count % 2 == 0 ) {
							let gap = 12;
							xGapAry = [ xL + gap, xL, xR, xR - gap ];
							num = 4;
						}
						else {
							let gap = 12;
							xGapAry = [ xL + gap, xR - gap ];
							num = 2;
						}
					}
					else if ( m_nPower >= 80 ) {
						if ( count % 2 == 0 ) {
							let gap = 12;
							xGapAry = [ xL - gap, xL, xR, xR - gap ];
							num = 4;
						}
						else {
							let gap = 12;
							xGapAry = [ xL - gap, xR - gap ];
							num = 2;
						}
					}
					else if ( m_nPower >= 64 ) {
						let gap = 7;
						xGapAry = [ xL - gap, xR - gap ];
						num = 2;
					}
					ascent ( let i in 0..num ) {
						let xGap = xGapAry[ i ];
						TNeedleShot( xGap, y, vFast, shotPowerFastMax, 2 );
					}
					count++;
				}
				else {
					count = 0;
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	task TNeedleShot( let x, let y, let v, let dmg, let type ) {
		let isSlowAttack = [ false, true ][ type > 2 ];
		let angle = 270;
		let alphaMax = [ 255, 80, 100, 192, 224 ][ type - 2 ];
		let countMax = [ 0, 1 ][ isSlowAttack == true ];
		let obj = Obj_Create( OBJ_SHOT );
		let count = 0;
		Obj_SetX( obj, x );
		Obj_SetY( obj, y );
		Obj_SetSpeed( obj, v );
		Obj_SetAngle( obj, 270 );
		Obj_SetAlpha( obj, 0 );
		ObjShot_SetBombResist( obj, true );
		ObjShot_SetGraphic( obj, type );
		ObjShot_SetDamage( obj, dmg );
		ObjShot_SetPenetration( obj, 1 );
		ObjShot_ToItem( obj, false );

		while ( Obj_BeDeleted( obj ) == false ) {
			let alpha = alphaMax * count / countMax;
			if ( alpha > alphaMax ) {
				alpha = alphaMax;
			}
			Obj_SetAlpha( obj, alpha );
			if ( Obj_IsIntersected( obj ) == true ) {
				THitEffect( Obj_GetX( obj ), Obj_GetY( obj ), type - 2 );
				Obj_Delete( obj );
			}
			count++;
			yield;
		}
	}

	task THitEffect( let nX, let nY, let nType ) {
		let img = players ~ "ReimuB\img\shot_reimu.png";
		let nAlphaMax = [ 160, 48, 88, 160, 192 ][ nType ];
		let nScaleXMax = [ 1.0, 1.0, 1.0, 1.0, 1.0 ][ nType ];
		let nScaleYMax = [ 4.0, 4.0, 4.0, 4.0, 4.0 ][ nType ];
		let nTime = 15;
		
		let nSrcX = [ 323, 345, 349, 355, 359 ][ nType ];
		let nSrcY = [ 5, 4, 94, 183, 273 ][ nType ];
		let nSrcWidth = [ 10, 58, 49, 38, 30 ][ nType ];
		let nSrcHeight = [ 48, 85, 79, 73, 67 ][ nType ];
		let nDstWidth = nSrcWidth;
		let nDstHeight = nSrcHeight;
		let nDstWidthHalf = nDstWidth / 2;
		let nDstHeightHalf = nDstHeight / 2;

		let obj = Obj_Create( OBJ_EFFECT );
		Obj_SetAutoDelete( obj, true );
		ObjEffect_SetTexture( obj, img );
		ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
		ObjEffect_CreateVertex( obj, 4 );
		ObjEffect_SetRenderState( obj, ADD );
		ascent( let i in 0..4 ){
			let nXIndex = truncate( i / 2 );
			let nYIndex = i % 2;
			ObjEffect_SetVertexColor( obj, i, nAlphaMax, 255, 255, 255 );
			ObjEffect_SetVertexXY( obj, i,
								   nDstWidthHalf * [ -1, 1 ][ nXIndex ],
								   nDstHeightHalf * [ -1, 1 ][ nYIndex ] );
			ObjEffect_SetVertexUV( obj, i,
								   nSrcX + [ 0, nSrcWidth ][ nXIndex ],
								   nSrcY + [ 0, nSrcHeight ][ nYIndex ] );
		}
		ascent( let t in 0..nTime ){
			if ( Obj_BeDeleted( obj ) == true ) {
				break;
			}
			let nScaleX = nScaleXMax - nScaleXMax * t / nTime;
			let nScaleY = 1.0 + ( nScaleYMax - 1.0 ) * t / nTime;
			let nAlpha = nAlphaMax - nAlphaMax * t / nTime;
			let yGap = nDstHeightHalf * nScaleY / 1.5;
			ascent ( let j in 0..4 ) {
				ObjEffect_SetVertexColor( obj, j, nAlpha, 255, 255, 255 );
			}
			ObjEffect_SetScale( obj, nScaleX, nScaleY );
			Obj_SetPosition( obj, nX, nY - yGap );
			yield;
		}
		if ( Obj_BeDeleted( obj ) == false ) {
			Obj_Delete( obj );
		}
	}

	task TSlowMove {
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) { return; }

			if ( m_bIsSlowMove == true ) {
				if ( m2_nReimuB_angleStar < 75) {
					m2_nReimuB_angleStar += 9.375;
					m2_nReimuB_xStar = m2_nReimuB_RStar * cos( m2_nReimuB_angleStar );
					m2_nReimuB_yStar = m2_nReimuB_RStar * sin( m2_nReimuB_angleStar );
				}
			}
			else {
				if ( m2_nReimuB_angleStar > 0 ) {
					m2_nReimuB_angleStar -= 9.375;
					m2_nReimuB_xStar = m2_nReimuB_RStar * cos( m2_nReimuB_angleStar );
					m2_nReimuB_yStar = m2_nReimuB_RStar * sin( m2_nReimuB_angleStar );
				}
			}
			yield;
		}
	}
}

//平常時ループ
function onMainLoop02() {
}

//ミス時ループ
function onMissedLoop02() {
	MainLoop;
}

//スペカ宣言時
function onSpellCard02() {
	let imgReimuCutIn = players ~ "ReimuB\img\cutin_reimu.png";
	//カットイン
	if ( m_bIsSlowMove == true ) {
		CutIn( YOUMU,"夢符「二重結界」", imgReimuCutIn );
		UseSpellCard( "NijuKekkai", NULL );
	}
	else {
		CutIn( YOUMU,"夢符「封魔陣」", imgReimuCutIn );
		UseSpellCard( "Fumajin", NULL );
	}
}

//描画ループ
function onDrawLoop02() {
	let imgReimu = players ~ "ReimuB\img\player_reimu.png";
	SetColor( 255, 255, 255 );
	SetRenderState( ALPHA );
	SetAlpha( 255 );
	SetTexture( imgReimu );
	if ( m_bIsMoveLeft == true ) {
		SetGraphicRect( 0, 48, 47, 95 );
	}
	else if( m_bIsMoveRight == true ) {
		SetGraphicRect( 0, 96, 47, 143 );
	}
	else if ( m_bIsMoveStraight == true ) {
		SetGraphicRect( 0, 0, 47, 47 );
	}
	DrawGraphic( GetPlayerX(), GetPlayerY() );
	SetGraphicAngle( 0, 0, 6 * m_nCount );
	SetGraphicRect( 1, 160, 19, 177 );
	DrawGraphic( GetPlayerX() - m2_nReimuB_xStar, GetPlayerY() - m2_nReimuB_yStar );
	SetGraphicAngle( 0, 0, -6 * m_nCount );
	SetGraphicRect( 1, 160, 19, 177 );
	DrawGraphic( GetPlayerX() + m2_nReimuB_xStar, GetPlayerY() - m2_nReimuB_yStar );
	SetGraphicAngle( 0, 0, 0 );
}

//スクリプト終了
function onFinalize02() {
}

