//////////////////////////////////////////////////////////////////////
//	スペルカードスクリプト
//////////////////////////////////////////////////////////////////////

//咲夜　幻符　高速ボム　幻符「インディスクリミネイト」
script_spell Indiscriminate
{
	let ShotBlueL   = 3;
	let ShotPurpleL = 4;

	let Time    = 180;
	let XCenter = GetPlayerX() - 32;
	let YCenter = GetPlayerY() - 16;

	@Initialize {
		SetPlayerInvincibility( Time );
		TMain();
	}

	@MainLoop {
		yield;
	}

	task TMain {
		let w1 = 60;
		let num     = 60;
		let bPurple = false;

		CollectItems();
		loop ( 48 ) { yield; }
		
		loop ( num ) {
			loop ( 4 ) {
				TShot( bPurple );
				bPurple = ! bPurple;
			}
			yield;
		}

		loop ( Time - 48 - num ) { yield; }
		End;
	}

	function initShot( let angle, let bPurple ) {
		let obj = Obj_Create( OBJ_SHOT );
		let r = rand( 32, 48 );
		let dmg = 6.15;
		
		Obj_SetPosition( obj, XCenter + r * cos( angle ), YCenter + r * sin( angle ) );
		Obj_SetSpeed( obj, 10 );
		Obj_SetAngle( obj, angle );
		Obj_SetAlpha( obj, 224 );
		ObjShot_SetDelay( obj, 0 );
		ObjShot_SetGraphic( obj, [ ShotBlueL, ShotPurpleL ][ bPurple == true ] );
		ObjShot_SetDamage( obj, dmg );
		ObjShot_SetPenetration( obj, 1 );
		//ObjSpell_SetIntersecrionCircle( obj, 0, 0, 32, dmg, true );
		
		return obj;
	}

	task TShot( let bPurple ) {
		let angle = rand( -180, 180 );
		let w = rand( -2, 2 );
		let obj = initShot( angle, bPurple );

		while ( Obj_BeDeleted( obj ) == false ) {
			Obj_SetAngle( obj, angle );
			yield;
			angle += w;
		}
	}
}

//咲夜　幻符　低速ボム　幻符「殺人ドール」
script_spell KillingDoll
{
	let ShotBlueL   = 3;
	let ShotPurpleL = 4;
	let se = GetCurrentScriptDirectory() ~ "players\SakuyaA\sound\bomb0.wav";
	let objs = [];
	let Time = 300;
	let homingId = 65535;
	let homingObjs = [];

	@Initialize {
		SetPlayerInvincibility( Time );
		SetSpeed( 0.8, 0.8 );
		TMain;
	}

	@Finalize {
		ascent ( let i in 0..length( objs ) ) {
			if ( Obj_BeDeleted( objs[ i ] ) == false ) {
				Obj_Delete( objs[ i ] );
			}
		}
		ascent ( let i in 0 .. length( homingObjs ) ) {
			if ( Obj_BeDeleted( homingObjs[ i ] ) == false ) {
				Obj_Delete( homingObjs[ i ] );
			}
		}
	}

	@MainLoop {
		yield;
	}

	task TMain {
		let w2 = 2;
		let angle = 0;
		let num   = 50;
		let span  = 180 / num;
		let bPurple = false;

		CollectItems();
		loop ( 48 ) { yield; }
		PlaySE( se );
		TCheckEnemy();
		ascent ( let i in 0..num ) {
			TShot( angle, bPurple, [ 18, 20, 22, 20  ][ i % 3 ] );
			TShot( angle + 180, bPurple, [ 18, 20, 22, 20 ][ i % 3 ] );
			loop ( w2 ) { yield; }

			angle += span;
			bPurple = ! bPurple;
		}
		loop ( Time - 48 - w2 * num  - 60 ) { yield; }
		SetSpeed( 4, 2.2 );
		loop ( 60 ) { yield; }
		End;
	}

	function initShot( let angle, let bPurple ) {
		let obj = Obj_Create( OBJ_SHOT );
		let r = 16;
		let dmg = 13.8;
		
		Obj_SetPosition ( obj,
						  GetPlayerX() + r * cos( angle + rand( -45, 45 ) ),
						  GetPlayerY() + r * sin( angle + rand( -45, 45 ) ) );
		Obj_SetSpeed( obj, 4 );
		Obj_SetAngle( obj, angle );
		Obj_SetAlpha( obj, 128 );
		ObjShot_SetDelay( obj, 0 );
		ObjShot_SetGraphic( obj, [ ShotBlueL, ShotPurpleL ][ bPurple == true ] );
		ObjShot_SetDamage( obj, dmg );
		ObjShot_SetPenetration( obj, 1 );
		//ObjSpell_SetIntersecrionCircle( obj, 0, 0, 32, dmg, true );
		Obj_SetAutoDelete( obj, false );

		return obj;
	}

	task TShot( let angle, let bPurple, let w ) {
		let obj = initShot( angle, bPurple );
		objs = objs ~ [ obj ];
		let way  = 60;
		let span = -360 / way;

		loop ( w ) { yield; }

		Obj_SetSpeed( obj, 0 );
		loop ( way ) {
			Obj_SetAngle( obj, angle );
			angle += span;
			yield;
		}

		/*if ( GetEnemyNum() != 0 ) {
			angle = atan2( GetEnemyY() - Obj_GetY( obj ), GetEnemyX() - Obj_GetX( obj ) );
		}*/
		let minX = GetEnemyInfoDefault( homingId, ENEMY_X, 0 );
		let minY = GetEnemyInfoDefault( homingId, ENEMY_Y, 0 );
		if ( minX != 0 && minY != 0 ) {
			angle = atan2( minY - Obj_GetY( obj ), minX - Obj_GetX( obj ) );
		}

		Obj_SetSpeed( obj, 20 );
		Obj_SetAngle( obj, angle );

		while ( Obj_GetX( obj ) > GetClipMinX() - 160
				&& Obj_GetX( obj ) < GetClipMaxX() + 160
				&& Obj_GetY( obj ) > GetClipMinY() - 160
				&& Obj_GetY( obj ) < GetClipMaxY() + 160 ) {
			yield;
		}
		Obj_Delete( obj );
	}

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

		loop {
			let enemyNum = GetEnemyNum();
			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( homingObjs[ i ] ) == false ) {
						Obj_Delete( homingObjs[ i ] );
					}
				}
				objNum = enemyNum;
			}
			else if ( enemyNum < objNum && enemyNum <= objNumMin ) {
				ascent ( let i in objNumMin .. objNum ) {
					if ( Obj_BeDeleted( homingObjs[ i ] ) == false ) {
						Obj_Delete( homingObjs[ 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 = homingObjs[ 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( homingObjs );
			}
			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 );
				homingObjs[ i ] = obj;
			}
		}
	}
	
/*
	task TCheckEnemy() {
		homingObj = Obj_Create( OBJ_LASER );
		Obj_SetAlpha( homingObj, 0 );
		ObjLaser_SetLength( homingObj, 1 );
		ObjLaser_SetWidth( homingObj, 1 );
		ObjLaser_SetSource( homingObj, false );
		Obj_SetAngle( homingObj, 270 );
		Obj_SetAutoDelete( homingObj, false );
		ObjShot_SetGraphic( homingObj, WHITE01 );
		ObjShot_SetDamage( homingObj, 0 );
		ObjShot_SetPenetration( homingObj, 2000 );

		while ( Obj_BeDeleted( homingObj ) == false ) {
			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( homingObj, 2 );
				Obj_SetPosition( homingObj, x, y );
				if ( Obj_IsIntersected( homingObj ) == true
					 && id < homingId ) {
					homingId = id;
				}
			}
			yield;
		}
		TCheckEnemy();
	}*/
}
