//////////////////////////////////////////////////////////////////////
//	プレイヤースクリプト
//////////////////////////////////////////////////////////////////////

let SakuyaA_RStar = 24;
let SakuyaA_angleStar = 0;
let SakuyaA_xStar = SakuyaA_RStar;
let SakuyaA_yStar = 0;

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

//メインタスク
task MainTask05() {
	let myPlayerIndex = 5;
	let homingAngle = 270;
	let homingId = 65535;

	TCheckEnemy();
	TSlowMove();
	TMainShot();
	TMainSubShot();
	TSubShot();
	TSubSubShot();

	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 TCheckEnemy() {
		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 );

		while ( Obj_BeDeleted( obj ) == false ) {
			if ( m_nPlayerIndex != myPlayerIndex ) {
				Obj_Delete( obj );
				return;
			}
			
			homingId = 65535;
			ascent( let i in EnumEnemyBegin .. EnumEnemyEnd ) {
				let id = EnumEnemyGetID( i );
				let x  = GetEnemyInfo( id, ENEMY_X );
				let y  = GetEnemyInfo( id, ENEMY_Y );
				ObjShot_SetPenetration( obj, 2 );
				Obj_SetPosition( obj, x, y );
				if ( Obj_IsIntersected( obj ) == true
					 && id < homingId ) {
					homingId = id;
				}
			}
			yield;
		}
		TCheckEnemy();
	}
*/
	task TMainShot() {
		let limit = 30;
		let gap = 6;
		let shotPowerFastMaxBase = 1.4;
		let shotPowerFastMax = shotPowerFastMaxBase;
		//let shotPowerFastMaxAry = [ 1.2, 1.2, 1.4, 1.4, 1.4, 1.4, 1.4, 1.4, 1.6 ];
		let shotPowerSlowMaxBase = 1.8;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		//let shotPowerSlowMaxAry = [ 1.6, 1.6, 1.8, 1.8, 2.0, 2.0, 2.2, 2.2, 2.4 ];
		let wFast = 5;
		let wFastAry = [ 5, 5, 5, 5, 5, 5, 5, 4, 4 ];
		let wSlow = 5;
		let wSlowAry = [ 5, 5, 5, 5, 5, 5, 5, 5, 5 ];
		let v = 15;
		let yGap = 8;
		
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) {
				return;
			}
			while ( OnEvent() == true ) {
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			
			if( m_bIsSlowMove == true ) {	//低速移動時の自弾
				wSlow = updatePower( wSlowAry );
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerSlowMax = updatePower( shotPowerSlowMaxAry );
					let angleGapAry = [];
					let num = 0;
					homingAngle = getHormingAngle();
					if ( m_nPower >= 96 ) {
						angleGapAry = [ -5, 5 ];
						num = 2;
					}
					else if ( m_nPower >= 48 ) {
						angleGapAry = [ -4, 4 ];
						num = 2;
					}
					else if ( m_nPower >= 32 ) {
						angleGapAry = [ -3, 0, 3 ];
						num = 3;
					}
					else if ( m_nPower >= 8 ) {
						angleGapAry = [ -1.5, 1.5 ];
						num = 2;
					}
					else {
						angleGapAry = [ 0 ];
						num = 1;
					}
					ascent ( let i in 0..num ) {
						let angleGap = angleGapAry[ i ];
						CreatePlayerShot01( GetPlayerX(),
											GetPlayerY() - yGap,
											v, homingAngle + angleGap,
											shotPowerSlowMax, 1, 1 );
					}
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時の自弾
				wFast = updatePower( wFastAry );
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerFastMax = updatePower( shotPowerFastMaxAry );
					let angleGapAry = [];
					let num = 0;
					if ( m_nPower >= 96 ) {
						angleGapAry = [ -10, 0, 10 ];
						num = 3;
					}
					else if ( m_nPower >= 48 ) {
						angleGapAry = [ -10, 10 ];
						num = 2;
					}
					else if ( m_nPower >= 32 ) {
						angleGapAry = [ -6, 0, 6 ];
						num = 3;
					}
					else if ( m_nPower >= 8 ) {
						angleGapAry = [ -3, 3 ];
						num = 2;
					}
					else {
						angleGapAry = [ 0 ];
						num = 1;
					}
					ascent ( let i in 0..num ) {
						let angleGap = angleGapAry[ i ];
						CreatePlayerShot01( GetPlayerX(),
											GetPlayerY() - yGap,
											v, 270 + angleGap, shotPowerFastMax,
											1, 1 );
					}
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	task TSubShot() {
		let limit = 30;
		let gap = 6;
		let shotPowerFastMaxBase = 1.6;
		let shotPowerFastMax = shotPowerFastMaxBase;
		//let shotPowerFastMaxAry = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.2 ];
		let shotPowerSlowMaxBase = 2.0;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		//let shotPowerSlowMaxAry = [ 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.8 ];
		let wFast = 30;
		let wSlow = 10;
		let wFastAry = [ 30, 20, 15, 15, 8, 8, 8, 8, 8 ];
		let wSlowAry = [ 10, 10, 5, 5, 5, 5, 5, 4, 3 ];
		let v = 15;
		
		//loop ( 4 ) { yield; }
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) {
				return;
			}
			while ( OnEvent() == true ) {
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			
			let xL = GetPlayerX() - SakuyaA_xStar;
			let xR = GetPlayerX() + SakuyaA_xStar;
			let y = GetPlayerY() - SakuyaA_yStar;
			if( m_bIsSlowMove == true ) {	//低速移動時のサブ弾
				wSlow = updatePower( wSlowAry );
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerSlowMax = updatePower( shotPowerSlowMaxAry );
					let angleGapAry = [];
					let xAry = [];
					let num = 0;
					if ( m_nPower >= 128 ) {
						angleGapAry = [ 0, 0 ];
						xAry = [ xL, xR ];
						num = 2;
					}
					else if ( m_nPower >= 96 ) {
						angleGapAry = [ -2, -0.5, 0.5, 2 ];
						xAry = [ xL, xL, xR, xR ];
						num = 4;
					}
					else if ( m_nPower >= 64 ) {
						angleGapAry = [ -0.25, 0.25 ];
						xAry = [ xL, xR ];
						num = 2;
					}
					else if ( m_nPower >= 48 ) {
						angleGapAry = [ -0.25, 0, 0.25 ];
						xAry = [ xL, GetPlayerX(), xR ];
						num = 3;
					}
					else {
						angleGapAry = [ -0.25, 0.25 ];
						xAry = [ xL, xR ];
						num = 2;
					}
					ascent ( let i in 0..num ) {
						let angleGap = angleGapAry[ i ];
						let x = xAry[ i ];
						CreatePlayerShot01( x,
											y,
											v, homingAngle + angleGap, shotPowerSlowMax, 1,
											2 );
					}
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時のサブ弾
				wFast = updatePower( wFastAry );
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerFastMax = updatePower( shotPowerFastMaxAry );
					let angleAry = [];
					let xAry = [];
					let num = 0;
					if ( m_nPower >= 128 ) {
						angleAry = [ 230, 240, 250, 260, 280, 290, 300,310 ];
						xAry = [ xL, xL, xL, xL, xR, xR, xR, xR ];
						num = 8;
					}
					else if ( m_nPower >= 80 ) {
						angleAry = [ 235, 245, 255, 285, 295, 305 ];
						xAry = [ xL, xL, xL, xR, xR, xR ];
						num = 6;
					}
					else if ( m_nPower >= 48 ) {
						angleAry = [ 236.5, 246.5, 293.5, 302.5 ];
						xAry = [ xL, xL, xR, xR ];
						num = 4;
					}
					else if ( m_nPower >= 32 ) {
						angleAry = [ 236.5, 246.5, 256.5, 283.5, 293.5, 302.5 ];
						xAry = [ xL, xL, xL, xR, xR, xR ];
						num = 6;
					}
					else if ( m_nPower >= 16 ) {
						angleAry = [ 237.5, 252.5, 287.5, 302.5 ];
						xAry = [ xL, xL, xR, xR ];
						num = 4;
					}
					else {
						angleAry = [ 245, 295 ];
						xAry = [ xL, xR ];
						num = 2;
					}
					ascent ( let i in 0..num ) {
						let angle = angleAry[ i ];
						let x = xAry[ i ];
						CreatePlayerShot01( x, y,
											v, angle, shotPowerFastMax, 1, 2 );
					}
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	task TMainSubShot() {
		let limit = 30;
		let gap = 6;
		let shotPowerFastMaxBase = 1.4;
		let shotPowerFastMax = shotPowerFastMaxBase;
		//let shotPowerFastMaxAry = [ 1.2, 1.2, 1.4, 1.4, 1.4, 1.4, 1.4, 1.4, 1.6 ];
		let shotPowerSlowMaxBase = 1.8;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		//let shotPowerSlowMaxAry = [ 1.6, 1.6, 1.8, 1.8, 2.0, 2.0, 2.2, 2.2, 2.4 ];
		let wFast = 5;
		let wFastAry = [ 5, 5, 5, 5, 4, 4, 4, 8, 8 ];
		let wSlow = 5;
		let wSlowAry = [ 5, 5, 5, 5, 4, 4, 4, 4, 4 ];
		let v = 15;
		let yGap = 8;
		
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) {
				return;
			}
			while ( OnEvent() == true ) {
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			
			if( m_bIsSlowMove == true ) {	//低速移動時の自サブ弾
				wSlow = updatePower( wSlowAry );
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerSlowMax = updatePower( shotPowerSlowMaxAry );
					let angleGapAry = [];
					let num = 0;
					if ( m_nPower >= 48 ) {
						angleGapAry = [ 0 ];
						num = 1;
					}
					ascent ( let i in 0..num ) {
						let angleGap = angleGapAry[ i ];
						CreatePlayerShot01( GetPlayerX(),
											GetPlayerY() - yGap,
											v, homingAngle + angleGap, shotPowerSlowMax,
											1, 1 );
					}
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時の自サブ弾
				wFast = updatePower( wFastAry );
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerFastMax = updatePower( shotPowerFastMaxAry );
					let angleGapAry = [];
					let num = 0;
					if ( m_nPower >= 96 ) {
						angleGapAry = [ -20, 20 ];
						num = 2;
					}
					else if ( m_nPower >= 48 ) {
						angleGapAry = [ 0 ];
						num = 1;
					}
					ascent ( let i in 0..num ) {
						let angleGap = angleGapAry[ i ];
						CreatePlayerShot01( GetPlayerX(),
											GetPlayerY() - yGap,
											v, 270 + angleGap, shotPowerFastMax, 1, 1 );
					}
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	task TSubSubShot() {
		let limit = 30;
		let gap = 6;
		let shotPowerFastMaxBase = 1.6;
		let shotPowerFastMax = shotPowerFastMaxBase;
		//let shotPowerFastMaxAry = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.2 ];
		let shotPowerSlowMaxBase = 2.0;
		let shotPowerSlowMax = shotPowerSlowMaxBase;
		//let shotPowerSlowMaxAry = [ 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.8 ];
		let wFast = 30;
		let wSlow = 10;
		let wFastAry = [ 30, 20, 15, 15, 15, 10, 4, 4, 4 ];
		let wSlowAry = [ 10, 10, 10, 10, 10, 10, 10, 10, 4 ];
		let v = 15;
		
		//loop ( 4 ) { yield; }
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) {
				return;
			}
			while ( OnEvent() == true ) {
				if ( m_nPlayerIndex != myPlayerIndex ) { return; }
				yield;
			}
			
			let xL = GetPlayerX() - SakuyaA_xStar;
			let xR = GetPlayerX() + SakuyaA_xStar;
			let y = GetPlayerY() - SakuyaA_yStar;
			if( m_bIsSlowMove == true ) {	//低速移動時のサブサブ弾
				wSlow = updatePower( wSlowAry );
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerSlowMax = updatePower( shotPowerSlowMaxAry );
					let angleGapAry = [];
					let xAry = [];
					let num = 0;
					if ( m_nPower >= 128 ) {
						angleGapAry = [ -4, 4 ];
						xAry = [ xL, xR ];
						num = 2;
					}
					else if ( m_nPower >= 96 ) {
						angleGapAry = [];
						xAry = [];
						num = 0;
					}
					else if ( m_nPower >= 64 ) {
						angleGapAry = [ -4, 4 ];
						xAry = [ xL, xR ];
						num = 2;
					}
					ascent ( let i in 0..num ) {
						let angleGap = angleGapAry[ i ];
						let x = xAry[ i ];
						CreatePlayerShot01( x,
											y,
											v, homingAngle + angleGap, shotPowerSlowMax,
											1, 2 );
					}
				}
				loop ( wSlow ) { yield; }
			}
			else {	//高速移動時のサブサブ弾
				wFast = updatePower( wFastAry );
				if ( m_bIsShooting == true
					 || ( 0 < m_nShotKeyPassedTime && m_nShotKeyPassedTime <= limit ) ) {
					//shotPowerFastMax = updatePower( shotPowerFastMaxAry );
					let angleAry = [];
					let xAry = [];
					let num = 0;
					if ( m_nPower >= 128 ) {
						angleAry = [ 272.5, 267.5 ];
						xAry = [ xL, xR ];
						num = 2;
					}
					else if ( m_nPower >= 80 ) {
						angleAry = [ 265, 275 ];
						xAry = [ xL, xR ];
						num = 2;
					}
					else if ( m_nPower >= 48 ) {
						angleAry = [ 256.5, 283.5 ];
						xAry = [ xL, xR ];
						num = 2;
					}
					ascent ( let i in 0..num ) {
						let angle = angleAry[ i ];
						let x = xAry[ i ];
						CreatePlayerShot01( x,
											y,
											v, angle, shotPowerFastMax, 1, 2 );
					}
				}
				loop ( wFast ) { yield; }
			}
		}
	}

	function getHormingAngle() {
		//let minLength = 65535;
		//let minX = GetPlayerX();
		//let minY = 0;
		let angle = 270;
		let gap = 30;//22.5;
		/*ascent( let i in EnumEnemyBegin .. EnumEnemyEnd ) {
			let id = EnumEnemyGetID( i );
			let x  = GetEnemyInfo( id, ENEMY_X );
			let y  = GetEnemyInfo( id, ENEMY_Y );
			let len = GetGapLength( GetPlayerX(), GetPlayerY(), x, y );
			if ( len < minLength ) {
				minLength = len;
				minX = x;
				minY = y;
			}
		
		}*/
		let minX = GetEnemyInfoDefault( homingId, ENEMY_X, GetPlayerX() );
		let minY = GetEnemyInfoDefault( homingId, ENEMY_Y, 0 );
		angle = GetGapAngle( GetPlayerX(), GetPlayerY(), minX, minY );
		while ( angle < 0 ) {
			angle += 360;
		}
		while ( angle > 360 ) {
			angle -= 360;
		}
		if ( 270 - gap <= angle && angle <= 270 + gap ) {
			return angle;
		}
		return 270;
	}

	task TSlowMove {
		loop {
			if ( m_nPlayerIndex != myPlayerIndex ) {
				return;
			}
			if ( m_bIsSlowMove == true ) {
				if ( SakuyaA_angleStar < 75) {
					SakuyaA_angleStar += 9.375;
					SakuyaA_xStar = SakuyaA_RStar * cos( SakuyaA_angleStar );
					SakuyaA_yStar = SakuyaA_RStar * sin( SakuyaA_angleStar );
				}
			}
			else {
				if ( SakuyaA_angleStar > 0 ) {
					SakuyaA_angleStar -= 9.375;
					SakuyaA_xStar = SakuyaA_RStar * cos( SakuyaA_angleStar );
					SakuyaA_yStar = SakuyaA_RStar * sin( SakuyaA_angleStar );
				}
			}
			yield;
		}
	}
}

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

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

//スペカ宣言時
function onSpellCard05() {
	let imgSakuyaCutIn = players ~ "SakuyaA\img\cutin_sakuya.png";
	//カットイン
	if ( m_bIsSlowMove == true ) {
		CutIn( YOUMU,"幻符「殺人ドール」", imgSakuyaCutIn );
		UseSpellCard( "KillingDoll", NULL );
	}
	else {
		CutIn( YOUMU,"幻符「インディスクリミネイト」", imgSakuyaCutIn );
		UseSpellCard( "Indiscriminate", NULL );
	}
}

//描画ループ
function onDrawLoop05() {
	let imgSakuya = players ~ "SakuyaA\img\player_sakuya.png";
	SetColor( 255, 255, 255 );
	SetRenderState( ALPHA );
	SetAlpha( 255 );
	SetTexture( imgSakuya );
	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() );
	
	SetGraphicRect( 0, 160, 19, 179 );
	DrawGraphic( GetPlayerX() - SakuyaA_xStar, GetPlayerY() - SakuyaA_yStar );
	SetGraphicRect( 0, 180, 19, 199 );
	DrawGraphic( GetPlayerX() + SakuyaA_xStar, GetPlayerY() - SakuyaA_yStar );
}

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

