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

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


let m1_nReimuA_RStar = 24;
let m1_nReimuA_angleStar = 0;
let m1_nReimuA_xStar = m1_nReimuA_RStar;
let m1_nReimuA_yStar = 0;

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

// 1 霊夢　霊符

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

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

	task TCheckEnemy() {
		let objs = [];
		let objNumMin = 32;
		let objNum = objNumMin;
		let addCount = 0;
		loop ( 256 ) {
			objs = objs ~ [ 0 ];
		}
		let addIndex = length( objs );
		makeSeeker( 0, objNum );
		addCount = 10;
		addIndex = 0;

		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) {
				ascent ( let i in 0 .. length( objs ) ) {
					if ( Obj_BeDeleted( objs[ i ] ) == false ) {
						Obj_Delete( objs[ i ] );
					}
				}
				return;
			}
			let enemyNum = GetEnemyNum();
			if ( OnMissed() == true ) {
				ascent ( let i in 0 .. length( objs ) ) {
					if ( Obj_BeDeleted( objs[ i ] ) == false ) {
						Obj_Delete( objs[ i ] );
					}
				}
				makeSeeker( 0, objNum );
			}
			if ( enemyNum > objNum ) {
				makeSeeker( objNum, enemyNum );
				addCount = 10;
				addIndex = objNum;
				objNum = enemyNum;
			}
			else if ( enemyNum < objNum && enemyNum > objNumMin ) {
				ascent ( let i in enemyNum .. objNum ) {
					if ( Obj_BeDeleted( objs[ i ] ) == false ) {
						Obj_Delete( objs[ i ] );
					}
				}
				objNum = enemyNum;
			}
			else if ( enemyNum < objNum && enemyNum <= objNumMin ) {
				ascent ( let i in objNumMin .. objNum ) {
					if ( Obj_BeDeleted( objs[ i ] ) == false ) {
						Obj_Delete( objs[ i ] );
					}
				}
				objNum = objNumMin;
			}
			homingId = 65535;
			ascent( let i in EnumEnemyBegin .. EnumEnemyEnd ) {
				let id = EnumEnemyGetID( i );
				let x  = GetEnemyInfo( id, ENEMY_X );
				let y  = GetEnemyInfo( id, ENEMY_Y );
				if ( x <= 416 && x >= 32 && y <= 464 && y >= 16
					 && i < addIndex ) {
					let obj = objs[ i ];
					ObjShot_SetPenetration( obj, 257 );
					Obj_SetPosition( obj, x, y );
					if ( Obj_IsIntersected( obj ) == true
						 && id < homingId ) {
						homingId = id;
						if ( homingId == 0 ) {
							break;
						}
					}
				}
			}
			if ( addCount > 0 ) {
				addCount--;
			}
			else {
				addIndex = length( objs );
			}
			yield;
		}

		function makeSeeker( let beginIndex, let endIndex ) {
			ascent ( let i in beginIndex .. endIndex ) {
				let obj = Obj_Create( OBJ_LASER );
				Obj_SetAlpha( obj, 0 );
				ObjLaser_SetLength( obj, 1 );
				ObjLaser_SetWidth( obj, 1 );
				ObjLaser_SetSource( obj, false );
				Obj_SetAngle( obj, 270 );
				Obj_SetAutoDelete( obj, false );
				ObjShot_SetGraphic( obj, WHITE01 );
				ObjShot_SetDamage( obj, 0 );
				ObjShot_SetPenetration( obj, 2000 );
				objs[ i ] = obj;
			}
		}
	}


	task TMainShot() {
		let limit = 30;
		let shotPowerFastMaxBase = 3.0;
		let shotPowerFastMax = shotPowerFastMaxBase;
		let shotPowerSlowMaxBase = 2.0;
		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 ( 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.25, 0, 7.25 ];
						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 );
					}
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 128 ) {
						angleGapAry = [ -7.25, 0, 0, 7.25 ];
						xGapAry = [ -8, -8, 8, 8 ];
						num = 4;
					}
					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 >= 8 ) {
						angleGapAry = [ 0, 0 ];
						xGapAry = [ -4, 4 ];
						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 );
					}
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	task TSubShot() {
		let limit = 30;
		let shotPowerFastMaxBase = 1.4;
		let shotPowerFastMax = shotPowerFastMaxBase;
		let shotPowerSlowMaxBase = 2.8;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		let wFast = 5;
		let wFastAry = [ 30, 30, 15, 15, 15, 15, 5, 5, 5 ];
		let wSlow = 5;
		let wSlowAry = [ 15, 15, 15, 10, 6, 6, 5, 5, 5 ];
		let fastAngleGapAry128 = [ 20, 40, 60, 80 ];
		let fastAngleGapAry80 = [ 60, 40, 20 ];
		let fastAngleGapAryIndex = 0;
		let count = 0;
		
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) { return; }
			while ( OnEvent() == true ){
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			
			wFast = updatePower( wFastAry );
			wSlow = updatePower( wSlowAry );

			let xL = GetPlayerX() - m1_nReimuA_xStar;
			let xR = GetPlayerX() + m1_nReimuA_xStar;
			let y = GetPlayerY() - m1_nReimuA_yStar / 2;
			
			if( m_bIsSlowMove ) {	//低速移動時の自弾
				fastAngleGapAryIndex = 0;
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 128 ) {
						angleGapAry = [ -30, -10, 10, 30 ];
						xGapAry = [ GetPlayerX(), GetPlayerX(), GetPlayerX(), GetPlayerX() ];
						num = 4;
					}
					else if ( m_nPower >= 96 ) {
						if ( count % 6 == 0 ) {
							angleGapAry = [ -30, -10, 10, 30 ];
							xGapAry = [ GetPlayerX(), GetPlayerX(), GetPlayerX(), GetPlayerX() ];
							num = 4;
						}
						else {
							angleGapAry = [ -10, 10 ];
							xGapAry = [ GetPlayerX(), GetPlayerX() ];
							num = 2;
						}
					}
					else if ( m_nPower >= 64 ) {
						angleGapAry = [ -10, 10 ];
						xGapAry = [ GetPlayerX(), GetPlayerX() ];
						num = 2;
					}
					else {
						angleGapAry = [ 0 ];
						xGapAry = [ GetPlayerX() ];
						num = 1;
					}
					ascent ( let i in 0..num ) {
						let angleGap = angleGapAry[ i ];
						let xGap = xGapAry[ i ];
						THakureiAmulet( xGap, y, 270 + angleGap, shotPowerSlowMax );
					}
					count++;
				}
				else {
					count = 0;
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					let fastAngleGap = 0;
					if ( m_nPower >= 128 ) {
						fastAngleGap = fastAngleGapAry128[ fastAngleGapAryIndex ];
						angleGapAry = [ -fastAngleGap, fastAngleGap ];
						xGapAry = [ xL, xR ];
						num = 2;
						fastAngleGapAryIndex
							= [ fastAngleGapAryIndex + 1, 0 ][ fastAngleGapAryIndex >= 3 ];
					}
					else if ( m_nPower >= 96 ) {
						fastAngleGap = fastAngleGapAry128[ fastAngleGapAryIndex ];
						angleGapAry = [ -fastAngleGap, fastAngleGap ];
						xGapAry = [ xL, xR ];
						num = 2;
						fastAngleGapAryIndex
							= [ fastAngleGapAryIndex + 1, 0 ][ fastAngleGapAryIndex >= 2 ];
					}
					else if ( m_nPower >= 80 ) {
						fastAngleGap = fastAngleGapAry80[ fastAngleGapAryIndex ];
						angleGapAry = [ -fastAngleGap, fastAngleGap ];
						xGapAry = [ xL, xR ];
						num = 2;
						fastAngleGapAryIndex
							= [ fastAngleGapAryIndex + 1, 0 ][ fastAngleGapAryIndex >= 2 ];
					}
					else if ( m_nPower >= 64 ) {
						angleGapAry = [ -60, -30, 30, 60 ];
						xGapAry = [ xL, xL, xR, xR ];
						num = 4;
					}
					else if ( m_nPower >= 48 ) {
						if ( count % 2 == 0 ) {
							angleGapAry = [ -60, -30, 30, 60 ];
							xGapAry = [ xL, xL, xR, xR ];
							num = 4;
						}
						else {
							angleGapAry = [ -30, 30 ];
							xGapAry = [ xL, xR ];
							num = 2;
						}
					}
					else {
						angleGapAry = [ -30, 30 ];
						xGapAry = [ xL, xR ];
						num = 2;
					}
					ascent ( let i in 0..num ) {
						let xGap = xGapAry[ i ];
						let angleGap = angleGapAry[ i ];
						THomingAmulet( xGap, y, 270 + angleGap, shotPowerFastMax );
					}
					count++;
				}
				else {
					fastAngleGapAryIndex = 0;
					count = 0;
				}
				loop ( wFast ) { yield; }
			}
		}
	}
	
	task THakureiAmulet( let x, let y, let angleBase, let dmg ) {
		let v = 0;
		let vmax = 15;
		let acc = 0.75;
		let obj = __Amulet( x, y, angleBase, v, 3, dmg );
		
		while ( Obj_BeDeleted( obj ) == false ) {
			let minX = GetEnemyInfoDefault( homingId, ENEMY_X, 0 );
			let minY = GetEnemyInfoDefault( homingId, ENEMY_Y, 0 );
			let angle = Obj_GetAngle( obj );
			let plen = GetGapLength( GetPlayerX(), GetPlayerY(),
									 Obj_GetX( obj ), Obj_GetY( obj ) );

			if ( minX != 0 && minY != 0 ) {
				angle = GetGapAngle( Obj_GetX( obj ), Obj_GetY( obj ), minX, minY );
			}
			while ( angle < 0 ) {
				angle += 360;
			}
			while ( angle > 360 ) {
				angle -= 360;
			}
			if ( plen < 64 ) {
				Obj_SetAlpha( obj, 72 * plen / 96 );
			}
			else {
				Obj_SetAlpha( obj, 72 );
			}
			Obj_SetAngle( obj, angle );
			Obj_SetSpeed( obj, Min( Obj_GetSpeed( obj ) + acc, vmax ) );
			yield;
		}
		Obj_Delete( obj );
	}

	task THomingAmulet( let x, let y, let angleBase, let dmg ) {
		let v = 11;
		let obj = __Amulet( x, y, angleBase, v, 2, dmg );
		//let dda = 5;
		let da = 0;
		let accMinus = v / 10;//1.0;
		let accPlus = v / 20;//0.5;
		let nDiffLimit = 120;
		let nAddAngleBase = 10;//15;
		
		while ( Obj_BeDeleted( obj ) == false ) {
			let nAddAngle = nAddAngleBase + da;
			let minX = GetEnemyInfoDefault( homingId, ENEMY_X, 0 );
			let minY = GetEnemyInfoDefault( homingId, ENEMY_Y, 0 );
			let nAngle = Obj_GetAngle( obj );
			let destAngle = nAngle;
			
			if ( minX != 0 && minY != 0 ) {
				destAngle = GetGapAngle( Obj_GetX( obj ), Obj_GetY( obj ), minX, minY );
			}
			while ( destAngle < 0 ) { destAngle += 360; }
			while ( destAngle > 360 ) { destAngle -= 360; }
			while ( nAngle < 0 ) { nAngle += 360; }
			while ( nAngle > 360 ) { nAngle -= 360; }
			if ( destAngle - 180 > nAngle ) { nAngle += 360; }
			if ( destAngle + 180 < nAngle ) { nAngle -= 360; }

			let nDiffAngle = destAngle - nAngle;
			let nSpeed = Obj_GetSpeed( obj );
			if ( absolute( nDiffAngle ) > nDiffLimit ) {
				nSpeed -= accMinus;
				nAddAngle /= 2;
			}
			else {
				nSpeed += accPlus;
			}
			
			if ( nSpeed < 0 ) {
				nSpeed *= -1;
				nAngle = destAngle;
				nDiffAngle = 0;
			}
			else if ( nSpeed > v ) {
				nSpeed = v;
			}

			let nAddAngle2 = [ nAddAngle, nAddAngle * 2 ][ absolute( nDiffAngle ) < 90 ];

			if( ( nDiffAngle > 0 && nDiffAngle <= 180 ) || nDiffAngle <= -180 ){
				if ( absolute( nDiffAngle ) < nAddAngle2 ) {
					nAngle += absolute( nDiffAngle );
				}
				else {
					nAngle += nAddAngle2;
				}
			}
			else if( nDiffAngle > 180 || ( nDiffAngle < 0 && nDiffAngle > -180 ) ){
				if ( absolute( nDiffAngle ) < nAddAngle2 ) {
					nAngle -= absolute( nDiffAngle );
				}
				else {
					nAngle -= nAddAngle2;
				}
			}
			Obj_SetSpeed( obj, nSpeed );
			Obj_SetAngle( obj, nAngle );
			//dda += da;
			yield;
		}
		Obj_Delete( obj );
	}

	function __Amulet( let x, let y, let angle, let v, let type, let dmg ) {
		let obj = Obj_Create( OBJ_SHOT );
		Obj_SetPosition( obj, x, y );
		Obj_SetAngle( obj, angle );
		Obj_SetSpeed( obj, v );
		Obj_SetAlpha( obj, 72 );
		ObjShot_SetBombResist( obj, true );
		ObjShot_SetGraphic( obj, type );
		ObjShot_SetDamage( obj, dmg );
		ObjShot_SetPenetration( obj, 1 );
		ObjShot_ToItem( obj, false );
		return obj;
	}

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

			if ( m_bIsSlowMove == true ) {
				if ( m1_nReimuA_angleStar < 75) {
					m1_nReimuA_angleStar += 9.375;
					m1_nReimuA_xStar = m1_nReimuA_RStar * cos( m1_nReimuA_angleStar );
					m1_nReimuA_yStar = m1_nReimuA_RStar * sin( m1_nReimuA_angleStar );
				}
			}
			else {
				if ( m1_nReimuA_angleStar > 0 ) {
					m1_nReimuA_angleStar -= 9.375;
					m1_nReimuA_xStar = m1_nReimuA_RStar * cos( m1_nReimuA_angleStar );
					m1_nReimuA_yStar = m1_nReimuA_RStar * sin( m1_nReimuA_angleStar );
				}
			}
			yield;
		}
	}
}

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

//ミス時ループ
function onMissedLoop01() {
}

//スペカ宣言時
function onSpellCard01() {
	let imgReimuCutIn = players ~ "ReimuA\img\cutin_reimu.png";
	//カットイン
	if ( m_bIsSlowMove == true ) {
		CutIn( YOUMU,"霊符「夢想封印　集」", imgReimuCutIn );
		UseSpellCard( "MusouFuin_Shu", NULL );
	}
	else {
		CutIn( YOUMU,"霊符「夢想封印　散」", imgReimuCutIn );
		UseSpellCard( "MusouFuin_San", NULL );
	}
}

//描画ループ
function onDrawLoop01() {
	let imgReimu = players ~ "ReimuA\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() - m1_nReimuA_xStar, GetPlayerY() - m1_nReimuA_yStar );
	SetGraphicAngle( 0, 0, -6 * m_nCount );
	SetGraphicRect( 1, 160, 19, 177 );
	DrawGraphic( GetPlayerX() + m1_nReimuA_xStar, GetPlayerY() - m1_nReimuA_yStar );
	SetGraphicAngle( 0, 0, 0 );
}

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

