/**
 * サニー用ボム
 */
script_spell Bomb_Sunny{
	
	/**
	 * イニシャライズ処理
	 */
	@Initialize{
		//スペルメイン処理開始
		TMain();
	}
	
	/**
	 * メインループ処理
	 */
	@MainLoop{
		yield;
	}
	
	/**
	 * ファイナライズ処理
	 */
	@Finalize{
	}
	
	/**
	 * スペルメイン処理
	 */
	task TMain(){
		let x = GetPlayerX();
		let y = GetPlayerY();
		
		//450F(7.5秒)無敵
		SetPlayerInvincibility( 450 );
		
		//48F(カットイン時間)待つ
		FWait( 48 );
		
		//6方向に光球を発射
		ascent( i in 0..6 ){
			Effect1( x, y, i * 60, 240 );
		}
		
		//240F(4秒)待つ
		FWait( 240 );
		
		//6方向に光球を発射
		ascent( i in 0..6 ){
			Effect2( x, y, i * 60, 70 );
		}
		
		//全体攻撃を行うダミー作成
		Effect3( 70 );
		
		//72F(合計6秒)待つ
		FWait( 72 );
		
		//スペルカード終了
		End();
	}
	
	/**
	 * 光球エフェクト1
	 * 
	 * @param {Integer} x     x座標
	 * @param {Integer} y     y座標
	 * @param {Integer} angle 発射角度
	 * @param {Integer} time  持続時間
	 */
	task Effect1( x, y, angle, time ){
		let objx = 0;
		let objy = 0;
		let rad = 0;
		let count = 0;
		
		let obj = CreateObjEffectBall( x, y, imgShot );
		
		while( !Obj_BeDeleted( obj ) ){
			objx = Obj_GetX( obj );
			objy = Obj_GetY( obj );
			
			//当たり判定
			ObjSpell_SetIntersecrionCircle( obj, objx, objy, 120, 2, true );
			count += 1;
			angle += 2;
			rad += sin( count * 360 / time ) * 3;
			objx = x + rad * cos( angle );
			objy = y + rad * sin( angle );
			Obj_SetPosition( obj, objx, objy );
			
			//残像エフェクト
			if( count % 2 == 0 ){
				Effect4( objx, objy, 40 );
			}
			
			//時間削除
			if( count > time ){
				Obj_Delete( obj );
			}
			
			yield;
		}
	}
	
	/**
	 * 光球エフェクト2
	 * 
	 * @param {Integer} x     x座標
	 * @param {Integer} y     y座標
	 * @param {Integer} angle 発射角度
	 * @param {Integer} time  持続時間
	 */
	task Effect2( x, y, angle, time ){
		let objx = x;
		let objy = y;
		let speed = 800 / time;
		let mx = speed * cos( angle );
		let my = speed * sin( angle );
		let count = 0;
		
		let obj = CreateObjEffectBall( x, y, imgShot );
		
		while( !Obj_BeDeleted( obj ) ){
			objx = Obj_GetX( obj );
			objy = Obj_GetY( obj );
			
			count += 1;
			objx += mx;
			objy += my;
			Obj_SetPosition( obj, objx, objy );
			
			//星弾発射
			if( count < 12 ){
				CreateObjSpellEffectShot( objx, objy, rand( 6, 14 ), angle - 180 + rand( -30, 30 ), 0, 0, time - count, rand( 2, 3 ), imgShot, 32, 338, 32, 32 );
			}
			
			//時間削除
			if( count > time ){
				Obj_Delete( obj );
			}
			
			yield;
		}
	}
	
	/**
	 * 全体攻撃用ダミーエフェクト1
	 * 
	 * @param {Integer} time  持続時間
	 */
	task Effect3( time ){
		let count = 0;
		let x = GetCenterX();
		let y = GetCenterY();
		
		let obj = CreateObjEffectBall( x, y, imgShot );
		ObjEffect_SetVertexColor( obj, 0, 0, 255, 255, 255);
		ObjEffect_SetVertexColor( obj, 1, 0, 255, 255, 255);
		ObjEffect_SetVertexColor( obj, 2, 0, 255, 255, 255);
		ObjEffect_SetVertexColor( obj, 3, 0, 255, 255, 255);
		
		while( !Obj_BeDeleted( obj ) ){
			//当たり判定
			ObjSpell_SetIntersecrionCircle( obj, x, y, 800, 5, true );
			
			count += 1;
			
			//時間削除
			if( count > time ){
				Obj_Delete( obj );
			}
			
			yield;
		}
	}
	
	/**
	 * 光球エフェクト3 演出専用
	 * 
	 * @param {Integer} x     x座標
	 * @param {Integer} y     y座標
	 * @param {Integer} time  持続時間
	 */
	task Effect4( x, y, time ){
		let obj = CreateObjEffectBall( x, y, imgShot );
		let scale = 4;
		let count = 0;
		
		while( !Obj_BeDeleted( obj ) ){
			ObjEffect_SetScale( obj, scale, scale );
			scale -= 4 * 2.6 / time;
			count += 3;
			if( count > time ){
				Obj_Delete( obj );
			}
			FWait( 3 );
		}
	}
	
	/**
	 * 光球オブジェクトを作成して返す
	 * 
	 * @param {Integer} x      x座標
	 * @param {Integer} y      y座標
	 * @param {String} imgPath 画像パス
	 * @return オブジェクトID
	 */
	function CreateObjEffectBall( x, y, imgPath ){
		let obj = Obj_Create( OBJ_SPELL );
		ObjEffect_SetTexture( obj, imgPath );
		ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
		ObjEffect_CreateVertex( obj, 4 );
		
		ObjEffect_SetVertexXY( obj, 0, -15, -15 );
		ObjEffect_SetVertexXY( obj, 1, -15,  15 );
		ObjEffect_SetVertexXY( obj, 2,  15, -15 );
		ObjEffect_SetVertexXY( obj, 3,  15,  15 );
		
		ObjEffect_SetVertexUV( obj, 0, 286 +  0, 302 +  0 );
		ObjEffect_SetVertexUV( obj, 1, 286 +  0, 302 + 30 );
		ObjEffect_SetVertexUV( obj, 2, 286 + 30, 302 +  0 );
		ObjEffect_SetVertexUV( obj, 3, 286 + 30, 302 + 30 );
		
		ObjEffect_SetVertexColor( obj, 0, 255, 255, 255, 255);
		ObjEffect_SetVertexColor( obj, 1, 255, 255, 255, 255);
		ObjEffect_SetVertexColor( obj, 2, 255, 255, 255, 255);
		ObjEffect_SetVertexColor( obj, 3, 255, 255, 255, 255);
		
		ObjEffect_SetRenderState( obj, ADD );
		ObjEffect_SetScale( obj, 4, 4 );
		Obj_SetPosition( obj, x, y );
		
		return obj;
	}
	
	//スクリプトのインクルード
	#include_function ".\System\SystemConstant.txt"
	#include_function ".\System\MaterialConstant.txt"
	
	#include_function ".\Library\Lib_Bomb.txt"
}