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

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


let m4_nMarisaB_RStar = 24;
let m4_nMarisaB_angleStar = 0;
let m4_nMarisaB_xStar = m4_nMarisaB_RStar;
let m4_nMarisaB_yStar = 0;

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

// 4 魔理沙　恋符

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

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

	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, 4, 4 ];
		let wSlow = 4;
		let wSlowAry = [ 4, 4, 4, 4, 4, 4, 4, 4, 4 ];
		let v = 15;
		let yGap = 5;
		
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) { return; }
			while ( OnBomb() == true || OnMissed() == true || OnEvent() == true ) {
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}

			wFast = updatePower( wFastAry );
			wSlow = updatePower( wSlowAry );
			
			if( m_bIsSlowMove == true ) {	//低速移動時の自弾
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerSlowMax = updatePower( shotPowerSlowMaxAry );
					let angleGapAry = [];
					let xGapAry = [];
					let num = 0;
					if ( m_nPower >= 80 ) {
						angleGapAry = [ -5, 0, 5 ];
						xGapAry = [ 0, 0, 0 ];
						num = 3;
					}
					else if ( m_nPower >= 32 ) {
						angleGapAry = [ -2, 2 ];
						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 );
					}
				}
				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 >= 128 ) {
						angleGapAry = [ -10, -5, 0, 5, 10 ];
						xGapAry = [ 0, 0, 0, 0, 0 ];
						num = 5;
					}
					else if ( m_nPower >= 80 ) {
						angleGapAry = [ -5, 0, 5 ];
						xGapAry = [ 0, 0, 0 ];
						num = 3;
					}
					else if ( m_nPower >= 32 ) {
						angleGapAry = [ -2, 2 ];
						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 );
					}
				}
				loop ( wFast ) { yield; }
			}
		}
	}


	task TSubShot {
		let myPlayerIndex = 4;
		let limit = 30;
		let shotPowerFastMaxBase = 1;
		let shotPowerFastMax = shotPowerFastMaxBase;
		//let shotPowerFastMaxAry = [ 0.75, 0.75, 0.8, 0.8, 0.85, 0.85, 0.9, 0.95, 1 ];
		let shotPowerSlowMaxBase = 1.75;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		//let shotPowerSlowMaxAry = [ 1.5, 1.5, 1.6, 1.6, 1.7, 1.7, 1.8, 1.9, 2 ];
		let laserWidthAry = [ 4.5, 4.5, 4.8, 4.8, 5.1, 5.1, 5.4, 5.7, 6 ];
		let laserWidth = 4.5;
		let shotBlink = 60;
		let shotBlinkAry = [ 60, 90, 120, 150, 180, 210, 240, 270, 300 ];
		let objs = [];
		let effectObjs = [];
		let maxAlpha = 255;
		let oldAlpha = maxAlpha;
		let blinkCount = 0;
		let count = 0;
		let alpha = 0;
		let oldXs = [ 0, 0, 0, 0, 0, 0, 0, 0 ];

		ascent ( let i in 0..2 ) {
			let obj = Obj_Create( OBJ_LASER );
			Obj_SetAngle( obj, 270 );
			Obj_SetAutoDelete( obj, false );
			Obj_SetCollisionToPlayer( obj, false );
			ObjShot_SetBombResist( obj, true );
			ObjShot_SetGraphic( obj, [ 2, 3 ][ m_bIsSlowMove == true ] );
			ObjShot_SetDamage( obj, 0 );
			ObjShot_ToItem( obj, false );
			ObjShot_SetPenetration( obj, 600 );
			ObjLaser_SetLength( obj, 448 );
			ObjLaser_SetSource( obj, false );
			objs = objs ~ [ obj ];
		}
		
		ascent ( let i in 0..8 ) {
			let obj = Obj_Create( OBJ_LASER );
			Obj_SetAngle( obj, 270 );
			Obj_SetAutoDelete( obj, false );
			Obj_SetCollisionToPlayer( obj, false );
			ObjShot_SetBombResist( obj, true );
			ObjShot_SetGraphic( obj, 2 );
			ObjShot_SetDamage( obj, 0 );
			ObjShot_ToItem( obj, false );
			ObjShot_SetPenetration( obj, 600 );
			ObjLaser_SetLength( obj, 448 );
			ObjLaser_SetSource( obj, false );
			effectObjs = effectObjs ~ [ obj ];
		}
		
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) {
				ascent ( let i in 0..length( effectObjs ) ) {
					if ( Obj_BeDeleted( effectObjs[ i ] ) == false ) {
						Obj_Delete( effectObjs[ i ] );
					}
				}
				ascent ( let i in 0..length( objs ) ) {
					if ( Obj_BeDeleted( objs[ i ] ) == false ) {
						Obj_Delete( objs[ i ] );
					}
				}
				return;
			}
			while ( OnBomb() == true || OnMissed() == true || OnEvent() == true ) {
				ascent ( let i in 0..length( effectObjs ) ) {
					ObjLaser_SetWidth( effectObjs[ i ], 0 );
					Obj_SetAlpha( effectObjs[ i ], 0 );
					Obj_SetX( effectObjs[ i ], GetPlayerX() );
					Obj_SetY( effectObjs[ i ], GetPlayerY() );
				}
				ascent ( let i in 0..length( objs ) ) {
					ObjLaser_SetWidth( objs[ i ], 0 );
					Obj_SetAlpha( objs[ i ], 0 );
					Obj_SetX( objs[ i ], GetPlayerX() );
					Obj_SetY( objs[ i ], GetPlayerY() );
				}
				oldXs = [ GetCenterX(), GetCenterX(),
						  GetCenterX(), GetCenterX(),
						  GetCenterX(), GetCenterX(),
						  GetCenterX(), GetCenterX() ];
				if ( m_nPlayerIndex != myPlayerIndex ) {
					ascent ( let i in 0..length( effectObjs ) ) {
						if ( Obj_BeDeleted( effectObjs[ i ] ) == false ) {
							Obj_Delete( effectObjs[ i ] );
						}
					}
					ascent ( let i in 0..length( objs ) ) {
						if ( Obj_BeDeleted( objs[ i ] ) == false ) {
							Obj_Delete( objs[ i ] );
						}
					}
					return;
				}
				yield;
			}
			
			ascent ( let i in 1..length( oldXs ) ) {
				oldXs[ i - 1 ] = oldXs[ i ];
			}
				oldXs[ length( oldXs ) - 1 ] = GetPlayerX();

			let xGap = 0;
			if ( m_bIsSlowMove == false ) {
				if ( m_bIsMoveRight == true ) {
					xGap = 5;
				}
				else if ( m_bIsMoveLeft == true ) {
					xGap = -5;
				}
			}
			let xL = GetPlayerX() - m4_nMarisaB_xStar + xGap;
			let xR = GetPlayerX() + m4_nMarisaB_xStar + xGap;
			let y = GetPlayerY() - m4_nMarisaB_yStar;
			let laserSlowYGap = 0;
			if ( m_bIsShooting == true
				 || ( 0 <= m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
				let shotPowerMax;
				if( m_bIsSlowMove == true ) {
					//shotPowerSlowMax = updatePower( shotPowerSlowMaxAry );
					shotPowerMax = shotPowerSlowMax;
				}
				else {
					//shotPowerFastMax = updatePower( shotPowerFastMaxAry );
					shotPowerMax = shotPowerFastMax;
				}
					
				laserWidth = updatePower( laserWidthAry );
				shotBlink = updatePower( shotBlinkAry );
				if ( Obj_BeDeleted( objs[ 0 ] ) == true ) {
					ascent ( let i in 0..2 ) {
						let obj = Obj_Create( OBJ_LASER );
						Obj_SetAngle( obj, 270 );
						Obj_SetAutoDelete( obj, false );
						Obj_SetCollisionToPlayer( obj, false );
						ObjShot_SetBombResist( obj, true );
						ObjShot_SetGraphic( obj, [ 2, 3 ][ m_bIsSlowMove == true ] );
						ObjShot_SetDamage( obj, 0 );
						ObjShot_ToItem( obj, false );
						ObjShot_SetPenetration( obj, 600 );
						ObjLaser_SetLength( obj, 448 );
						ObjLaser_SetSource( obj, false );
						objs[ i ] = obj;
					}
					count = 0;
				}

				if ( m_bIsShooting == true && m_nShotKeyPassedTime >= limit ) {
					alpha = maxAlpha;
					oldAlpha = alpha;
				}
				else if ( m_bIsShooting == true && m_nShotKeyPassedTime <= 0 ) {
					alpha = 0;
					oldAlpha = alpha;
				}
				else if ( m_bIsShooting == true && m_bIsSlowMove == true ) {
					alpha = maxAlpha * m_nShotKeyPassedTime / limit;
					oldAlpha = alpha;
				}
				else if ( m_bIsShooting == false && m_nShotKeyPassedTime >= limit ) {
					alpha = 0;
				}
				else if ( m_bIsShooting == false && m_nShotKeyPassedTime <= 0 ) {
					alpha = oldAlpha;
				}
				else if ( m_bIsShooting == false ) {
					alpha = oldAlpha - oldAlpha * m_nShotKeyPassedTime / limit;
				}

				if ( m_bIsSlowMove == false ) {
					if ( limit <= blinkCount && blinkCount < shotBlink + limit ) {
						alpha = maxAlpha;
					}
					else if ( blinkCount >= shotBlink + 2 * limit ) {
						alpha = 0;
					}
					else if ( blinkCount >= shotBlink + limit ) {
						alpha = maxAlpha - maxAlpha * ( blinkCount - shotBlink - limit ) / limit;
					}
					else if ( blinkCount >= 0 && blinkCount < limit ) {
						alpha = maxAlpha * blinkCount / limit;
					}
				}
				else if ( m_bIsSlowMove == true
						  && 8 <= m_nSlowKeyPassedTime ) {
					xL = GetPlayerX();
					xR = GetPlayerX();
					y = GetPlayerY();
					laserSlowYGap = 5;
					ascent ( let i in 0..length( effectObjs ) ) {
						ObjLaser_SetWidth( effectObjs[ i ], laserWidth );
						Obj_SetAlpha( effectObjs[ i ], alpha * ( 0.2 + 0.1 * i ) );
						Obj_SetX( effectObjs[ i ], oldXs[ i ] );
						Obj_SetY( effectObjs[ i ], GetPlayerY() - laserSlowYGap );
					}
				}
				
				if ( m_bIsSlowMove == false
					 || ( m_bIsSlowMove == true && 8 > m_nSlowKeyPassedTime ) ) {
					ascent ( let i in 0..length( effectObjs ) ) {
						ObjLaser_SetWidth( effectObjs[ i ], 0 );
						Obj_SetAlpha( effectObjs[ i ], 0 );
						Obj_SetX( effectObjs[ i ], GetPlayerX() );
						Obj_SetY( effectObjs[ i ], GetPlayerY() );
					}
				}
				ascent ( let i in 0..2 ) {
					ObjLaser_SetWidth( objs[ i ], laserWidth );
					Obj_SetAlpha( objs[ i ], alpha );
					Obj_SetX( objs[ i ], [ xL, xR ][ i ] );
					Obj_SetY( objs[ i ], y - laserSlowYGap );
				}
				
				// limitフレームショット継続でDelayが終わったら攻撃力付与
				if ( m_bIsShooting == true && limit <= m_nShotKeyPassedTime ) {
					if ( alpha == maxAlpha ) {
						ascent ( let i in 0..2 ) {
							Obj_SetCollisionToPlayer( objs[ i ], true );
							ObjShot_SetDamage( objs[ i ], shotPowerMax );
						}
					}
					else {
						ascent ( let i in 0..2 ) {
							Obj_SetCollisionToPlayer( objs[ i ], false );
							ObjShot_SetDamage( objs[ i ], 0 );
						}
					}
				}
				if( m_bIsSlowMove == true ) {	//低速移動時のサブ弾
					blinkCount = 0;
				}
				else {	//高速移動時のサブ弾
					blinkCount = [ blinkCount + 1, 0 ][ blinkCount >= shotBlink + 3 * limit ];
				}
			}
			else {
				if ( Obj_BeDeleted( objs[ 0 ] ) == false ) {
					if ( count <= limit ) {
						ascent ( let i in 0..2 ) {
							Obj_SetAlpha( objs[ i ], alpha - alpha * count / limit );
							Obj_SetX( objs[ i ], [ xL, xR ][ i ] );
							Obj_SetY( objs[ i ], y );
						}
					}
					else {
						ascent ( let i in 0..2 ) {
							Obj_Delete( objs[ i ] );
						}
					}
					count++;
				}
				ascent ( let i in 0..length( effectObjs ) ) {
					ObjLaser_SetWidth( effectObjs[ i ], 0 );
					Obj_SetAlpha( effectObjs[ i ], 0 );
					Obj_SetX( effectObjs[ i ], GetPlayerX() );
					Obj_SetY( effectObjs[ i ], GetPlayerY() );
				}
				blinkCount = 0;
			}
			
			yield;
		}
	}

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

			if ( m_bIsSlowMove == true ) {
				if ( m4_nMarisaB_angleStar < 75) {
					m4_nMarisaB_angleStar += 9.375;
					m4_nMarisaB_xStar = m4_nMarisaB_RStar * cos( m4_nMarisaB_angleStar );
					m4_nMarisaB_yStar = m4_nMarisaB_RStar * sin( m4_nMarisaB_angleStar );
				}
			}
			else {
				if ( m4_nMarisaB_angleStar > 0 ) {
					m4_nMarisaB_angleStar -= 9.375;
					m4_nMarisaB_xStar = m4_nMarisaB_RStar * cos( m4_nMarisaB_angleStar );
					m4_nMarisaB_yStar = m4_nMarisaB_RStar * sin( m4_nMarisaB_angleStar );
				}
			}
			yield;
		}
	}
}

//平常時ループ
function onMainLoop04() {
	//ショット音
	if ( m_bIsShooting == true
		 && OnBomb() == false
		 && OnEvent() == false
		 && OnMissed() == false
		 && m_nCount % 5 == 0 ) {
		PlaySE( m_sSePlayerShotSound );
	}
}

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

//スペカ宣言時
function onSpellCard04() {
	let imgMarisaCutIn = players ~ "MarisaB\img\cutin_marisa.png";
	//カットイン
	if ( m_bIsSlowMove == true ) {
		CutIn( YOUMU,"恋符「マスタースパーク」", imgMarisaCutIn );
		UseSpellCard( "MasterSpark", NULL );
	}
	else {
		CutIn( YOUMU,"恋符「ノンディレクショナルレーザー」", imgMarisaCutIn );
		UseSpellCard( "NonDirectionalLaser", NULL );
	}
}

//描画ループ
function onDrawLoop04() {
	let imgMarisa = players ~ "MarisaB\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() - m4_nMarisaB_xStar, GetPlayerY() - m4_nMarisaB_yStar );
	SetGraphicRect( 0, 160, 20, 177 );
	DrawGraphic( GetPlayerX() + m4_nMarisaB_xStar, GetPlayerY() - m4_nMarisaB_yStar );
	SetGraphicAngle( 0, 0, 0 );
}

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

