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

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


let m3_nMarisaA_RStar = 24;
let m3_nMarisaA_angleStar = 0;
let m3_nMarisaA_xStar = m3_nMarisaA_RStar;
let m3_nMarisaA_yStar = 0;

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

// 3 魔理沙　魔符

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

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

	task TMainShot() {
		let limit = 30;
		let shotPowerFastMaxBase = 1.75;
		let shotPowerFastMax = shotPowerFastMaxBase;
		//let shotPowerFastMaxAry = [ 0.5, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 1 ];
		let shotPowerSlowMaxBase = 1.75;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		//let shotPowerSlowMaxAry = [ 0.5, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 1 ];
		let wFast = 4;
		let wFastAry = [ 4, 4, 4, 4, 4, 4, 4, 3, 3 ];
		let wSlow = 4;
		let wSlowAry = [ 4, 4, 4, 4, 4, 4, 4, 3, 3 ];
		let v = 15;
		let yGap = 5;
		
		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 ) ) {
					//shotPowerSlowMax = updatePower( shotPowerSlowMaxAry );
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 96 ) {
						angleGapAry = [ -4, 4 ];
						xGapAry = [ 0, 0 ];
						num = 2;
					}
					else if ( m_nPower >= 64 ) {
						angleGapAry = [ 0, 0 ];
						xGapAry = [ -8, 8 ];
						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 );
					}
					PlaySE( m_sSePlayerShotSound );
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerFastMax = updatePower( shotPowerFastMaxAry );
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 96 ) {
						angleGapAry = [ -4, 4 ];
						xGapAry = [ 0, 0 ];
						num = 2;
					}
					else if ( m_nPower >= 64 ) {
						angleGapAry = [ 0, 0 ];
						xGapAry = [ -8, 8 ];
						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 );
					}
					PlaySE( m_sSePlayerShotSound );
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	task TMainSubShot() {
		let limit = 30;
		let shotPowerFastMaxBase = 1.75;
		let shotPowerFastMax = shotPowerFastMaxBase;
		//let shotPowerFastMaxAry = [ 0.5, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 1 ];
		let shotPowerSlowMaxBase = 1.75;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		//let shotPowerSlowMaxAry = [ 0.5, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 1 ];
		let wFast = 4;
		let wSlow = 4;
		let v = 15;
		let yGap = 5;
		
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) { return; }
			while ( OnMissed() == true || OnEvent() == true ){
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			
			if ( m_bIsSlowMove ) {	//低速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerSlowMax = updatePower( shotPowerSlowMaxAry );
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 96 ) {
						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 ) ) {
					//shotPowerFastMax = updatePower( shotPowerFastMaxAry );
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 96 ) {
						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;
		let shotPowerFastMax = shotPowerFastMaxBase;
		//let shotPowerFastMaxAry = [ 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1 ];
		let shotPowerSlowMaxBase = 2;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		//let shotPowerSlowMaxAry = [ 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2 ];
		let wFast = 25;
		let wFastAry = [ 25, 25, 15, 15, 15, 10, 5, 5, 5 ];
		let wSlow = 25;
		let wSlowAry = [ 25, 25, 15, 15, 15, 10, 5, 5, 5 ];
		let type = 2;
		let typeFastAry = [ 2, 2, 2, 3, 3, 4, 4, 4, 5 ];
		let typeSlowAry = [ 6, 6, 6, 7, 7, 8, 8, 8, 9 ];
		let v = 15;
		let ddir = 1;
		
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) { return; }
			while ( OnMissed() == true || OnEvent() == true ){
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			
			let xL = GetPlayerX() - m3_nMarisaA_xStar;
			let xR = GetPlayerX() + m3_nMarisaA_xStar;
			let y = GetPlayerY() - m3_nMarisaA_yStar;
			wFast = updatePower( wFastAry );
			wSlow = updatePower( wSlowAry );
			
			if ( m_bIsSlowMove == true ) {	//低速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerSlowMax = updatePower( shotPowerSlowMaxAry );
					type = updatePower( typeSlowAry );
					TMagicNapalm( GetPlayerX(), GetPlayerY(), shotPowerSlowMax, type );
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerFastMax = updatePower( shotPowerFastMaxAry );
					type = updatePower( typeFastAry );
					let xGapAry = [ 0, 0 ];
					let xAry = [ xL, xR ];
					let dirAry = [ 1, -1 ];
					let num = 2;
					ascent ( let i in 0..num ) {
						let xGap = xGapAry[ i ];
						let x = xAry[ i ];
						let dir = dirAry[ i ];
						TMagicMissile( x + xGap, y, shotPowerFastMax, type, dir * ddir );
					}
					ddir *= -1;
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	task TSubSubShot() {
		let limit = 30;
		let shotPowerFastMaxBase = 1;
		let shotPowerFastMax = shotPowerFastMaxBase;
		//let shotPowerFastMaxAry = [ 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1 ];
		let wFast = 25;
		let wFastAry = [ 25, 25, 15, 15, 15, 15, 10, 10, 5 ];
		let type = 2;
		let typeFastAry = [ 2, 2, 2, 3, 3, 4, 4, 4, 5 ];
		let v = 15;
		let ddir = 1;
		
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) { return; }
			while ( OnMissed() == true || OnEvent() == true ){
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			
			let xL = GetPlayerX() - m3_nMarisaA_xStar;
			let xR = GetPlayerX() + m3_nMarisaA_xStar;
			let y = GetPlayerY() - m3_nMarisaA_yStar;
			wFast = updatePower( wFastAry );

			if ( m_bIsSlowMove == true ) {	//低速移動時の自弾
				yield;
			}
			else {	//高速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerFastMax = updatePower( shotPowerFastMaxAry );
					type = updatePower( typeFastAry );
					let xGapAry = [];
					let xAry = [];
					let dirAry = [];
					let num = 0;
					if ( m_nPower >= 64 && m_nPower < 80 ) {
						xGapAry = [ 0, 0 ];
						xAry = [ xL, xR ];
						dirAry = [ 1, -1 ];
						num = 2;
					}
					ascent ( let i in 0..num ) {
						let xGap = xGapAry[ i ];
						let x = xAry[ i ];
						let dir = dirAry[ i ];
						TMagicMissile( x + xGap, y, shotPowerFastMax, type, dir * ddir );
					}
					ddir *= -1;
				}
				loop ( wFast ) { yield; }
			}
		}
	}


	//マジックミサイル
	task TMagicMissile( let x, let y, let dmg, let type, let nDir ) {
		let acc = 0.6;
		let vmax = 12;
		let count = 0;
		let shotPowerMaxBase = 0.55;
		let shotPowerMax = shotPowerMaxBase;
		//let shotPowerMaxAry = [ 0.13, 0.145, 0.16, 0.175, 0.19, 0.205, 0.22, 0.235, 0.25 ];
		//shotPowerMax = updatePower( shotPowerMaxAry );

		let obj = Obj_Create( OBJ_SHOT );
		Obj_SetX( obj, x );
		Obj_SetY( obj, y );
		Obj_SetSpeed( obj, 0 );
		Obj_SetAngle( obj, 270 + 1.5 * nDir );
		Obj_SetAlpha( obj, 192 );
		ObjShot_SetBombResist( obj, true );
		ObjShot_SetGraphic( obj, type );
		ObjShot_SetDamage( obj, dmg );
		ObjShot_SetPenetration( obj, 2 );
		ObjShot_ToItem( obj, false );

		while ( Obj_BeDeleted( obj ) == false ) {
			Obj_SetAlpha( obj, Accelerate( 0, 255, count, 5 ) );
			if ( Obj_GetSpeed( obj ) < vmax ) {
				Obj_SetSpeed( obj, Min( vmax, Obj_GetSpeed( obj ) + acc ) );
			}
			if ( Obj_IsIntersected( obj ) == true ) {
				TExplode( Obj_GetX( obj ), Obj_GetY( obj ), shotPowerMax, 16, 64, 0 );
				Obj_Delete( obj );
			}
			count++;
			yield;
		}
	}

	//マジックナパーム
	task TMagicNapalm( let x, let y, let dmg, let type ) {
		let acc = 0.25;
		let vmax = 12;
		let count = 0;
		let shotPowerMaxBase = 0.7;
		let shotPowerMax = shotPowerMaxBase;
		//let shotPowerMaxAry = [ 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7 ];
		//shotPowerMax = updatePower( shotPowerMaxAry );
		
		let obj = Obj_Create( OBJ_SHOT );
		Obj_SetX( obj, x );
		Obj_SetY( obj, y );
		Obj_SetSpeed( obj, 0 );
		Obj_SetAngle( obj, 270 );
		Obj_SetAlpha( obj, 0 );
		ObjShot_SetBombResist( obj, true );
		ObjShot_SetGraphic( obj, type );
		ObjShot_SetDamage( obj, dmg );
		ObjShot_SetPenetration( obj, 2 );
		ObjShot_ToItem( obj, false );

		while ( Obj_BeDeleted( obj ) == false ) {
			Obj_SetAlpha( obj, Accelerate( 0, 255, count - 5, 20 ) );
			if ( Obj_GetSpeed( obj ) < vmax ) {
				Obj_SetSpeed( obj, Min( vmax, Obj_GetSpeed( obj ) + acc ) );
			}
			if ( Obj_IsIntersected( obj ) == true ) {
				TExplode( Obj_GetX( obj ), Obj_GetY( obj ), shotPowerMax, 20, 96, 1 );
				Obj_Delete( obj );
			}
			count++;
			yield;
		}
	}

	//	遅延範囲攻撃(エフェクトのみ)
	task TExplode( let nX, let nY, let nDamage, let nTime, let nMaxSize, let nType ) {
		let img = players ~ "MarisaA\img\shot_marisa.png";
		let nGraphic = [ 10, 11 ][ nType ];
		let nSpeed = [ 2, 4 ][ nType ];
		let nAngle = 270 + rand( -45, 45 );
		let nR = [ 6, 12 ][ nType ];
		nX += nR * cos( nAngle );
		nY += nR * sin( nAngle );
		TExplodeDamage( nX, nY, nDamage, nTime, nGraphic, nSpeed, nAngle );

		let nAlphaMax = [ 128, 128 ][ nType ];
		let nSrcX = [ 21, 137 ][ nType ];
		let nSrcY = [ 277, 268 ][ nType ];
		let nSrcWidth = [ 84, 110 ][ nType ];
		let nSrcHeight = [ 84, 110 ][ nType ];
		let nDstWidth = nMaxSize * 1.5;
		let nDstHeight = nMaxSize * 1.5;
		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 );
		ObjEffect_SetAngle( obj, 0, 0, rand( 0, 360 ) );
		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 ] );
		}
		Obj_SetPosition( obj, nX, nY );
		ascent( let i in 0..nTime ){
			if ( Obj_BeDeleted( obj ) == true ) {
				break;
			}
			let nScale = SlowDown( 0, 1, i, nTime );
			let nAlpha = Accelerate( nAlphaMax, 0, i, nTime );
			ascent ( let j in 0..4 ) {
				ObjEffect_SetVertexColor( obj, j, nAlpha, 255, 255, 255 );
			}
			ObjEffect_SetScale( obj, nScale, nScale );
			Obj_SetPosition( obj, Obj_GetX( obj ) + nSpeed * cos( nAngle ),
							 Obj_GetY( obj ) + nSpeed * sin( nAngle ) );
			yield;
		}
		if ( Obj_BeDeleted( obj ) == false ) {
			Obj_Delete( obj );
		}
	}

	//	遅延範囲攻撃(ダメージのみ)
	task TExplodeDamage ( let nX, let nY, let nDamage, let nTime,
						  let nType, let nSpeed, let nAngle ) {
		let nObjCount = 0;
		let obj = Obj_Create( OBJ_SHOT );
		Obj_SetPosition( obj, nX, nY );
		Obj_SetAlpha( obj, 0 );
		ObjShot_SetGraphic( obj, nType );
		ObjShot_SetDamage( obj, nDamage );
		ObjShot_SetPenetration( obj, nTime );
		while ( Obj_BeDeleted( obj ) == false && nObjCount <= nTime ) {
			yield;
			Obj_SetPosition( obj, Obj_GetX( obj ) + nSpeed * cos( nAngle ),
							 Obj_GetY( obj ) + nSpeed * sin( nAngle ) );
			nObjCount++;
		}
		Obj_Delete( obj );
	}

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

			if ( m_bIsSlowMove == true ) {
				if ( m3_nMarisaA_angleStar < 75) {
					m3_nMarisaA_angleStar += 9.375;
					m3_nMarisaA_xStar = m3_nMarisaA_RStar * cos( m3_nMarisaA_angleStar );
					m3_nMarisaA_yStar = m3_nMarisaA_RStar * sin( m3_nMarisaA_angleStar );
				}
			}
			else {
				if ( m3_nMarisaA_angleStar > 0 ) {
					m3_nMarisaA_angleStar -= 9.375;
					m3_nMarisaA_xStar = m3_nMarisaA_RStar * cos( m3_nMarisaA_angleStar );
					m3_nMarisaA_yStar = m3_nMarisaA_RStar * sin( m3_nMarisaA_angleStar );
				}
			}
			yield;
		}
	}
}

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

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

//スペカ宣言時
function onSpellCard03() {
	let imgMarisaCutIn = players ~ "MarisaA\img\cutin_marisa.png";
	//カットイン
	if ( m_bIsSlowMove == true ) {
		CutIn( YOUMU,"魔符「ミルキーウェイ」", imgMarisaCutIn );
		UseSpellCard( "MilkyWay", NULL );
	}
	else {
		CutIn( YOUMU,"魔符「スターダストレヴァリエ」", imgMarisaCutIn );
		UseSpellCard( "StardustReverie", NULL );
	}
}

//描画ループ
function onDrawLoop03() {
	let imgMarisa = players ~ "MarisaA\img\player_marisa.png";
	SetColor( 255, 255, 255 );
	SetRenderState( ALPHA );
	SetAlpha( 255 );
	SetTexture( imgMarisa );
	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( 0, 160, 20, 177 );
	DrawGraphic( GetPlayerX() - m3_nMarisaA_xStar, GetPlayerY() - m3_nMarisaA_yStar );
	SetGraphicRect( 0, 160, 20, 177 );
	DrawGraphic( GetPlayerX() + m3_nMarisaA_xStar, GetPlayerY() - m3_nMarisaA_yStar );
	SetGraphicAngle( 0, 0, 0 );
}

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

