#e[Single]
#ScriptVersion[2]
#Title[uUdEv]
#Text[]
#BGM[.\..\bgm\bgm.wav]
#BackGround[User]
#Player[FREE]

script_enemy_main{
	
	@Initialize{
		imgBoss = imgBoss_Reimu;
		InitializeData();
		InitializeAction();
		
		SetLife( 1000 );
		SetX( CenterX );
		SetY( MinY + 120 );
		SetDamageRate( 22, 20 );
		SetCollisionEx( 32, 16, false, 220 );
		SetEnemyMarker( true );
		
		TMain();
	}
	
	@MainLoop{
		yield;
	}
	
	@DrawLoop{
		DrawBoss( imgBoss );
	}
	
	@Finalize{
		FinalizeData();
	}
	
	task TMain(){
		FWait( 60 );
		
		DeclareSpell();
		
		Atack();
	}
	
	function DeclareSpell(){
		SetScore( 26000000 );
		SetTimer( 52 );
		CutIn( KOUMA, "uUdEv" ,NULL, 0, 0, 0, 0 );
		SetAction( ACT_SPELL, 180 );
		FWait( 180 );
	}
	
	task Atack(){
		let graphs = [ US_BILL_RED, US_BILL_ORANGE, US_BILL_YELLOW, US_BILL_GREEN, US_BILL_AQUA, US_BILL_BLUE, US_BILL_PURPLE ];
		let angle = rand( 0, 360 );
		let graph = 0;
		
		loop{
			if( graph % 5 == 4 ){
				Move();
			}else{
				SetAction( ACT_PRAY, 60 );
			}
			
			loop( 6 ){
				ascent( i in 0..6 ){
					Shot1( GetX(), GetY(), 7.4, angle + i * 360 / 6, graphs[ graph % 7 ], 0, 14, 2, graph % 2 + 1 );
				}
				FWait( 5 );
			}
			angle += 28;
			graph += 1;
			
			Atack2( angle );
			FWait( 20 );
		}
	}
	
	task Shot1( x, y, speed, angle, graph, delay, time, num, type ){
		let pangle = 40;
		let obj = CreateObjShot( x, y, speed, angle, graph, delay );
		
		FWait( time );
		
		if( !Obj_BeDeleted( obj ) ){
			x = Obj_GetX( obj );
			y = Obj_GetY( obj );
			angle = Obj_GetAngle( obj );
			num -= 1;
			if( num >= 0 ){
				Shot1( x, y, speed * 0.56, angle + pangle, graph, delay, time, num, type );
				Shot1( x, y, speed * 0.56, angle - pangle, graph, delay, time, num, type );
			}else{
				CreateShot01( x, y, speed * 0.62, angle + pangle, graph, delay );
				CreateShot01( x, y, speed * 0.62, angle - pangle, graph, delay );
			}
			
			Obj_Delete( obj );
		}
	}
	
	task Atack2( angle ){
		angle += 30;
		
		ascent( i in 0..6 ){
			Shot2( CenterX, MinY + 120, 6, angle + i * 360 / 6, US_ONMYOU_L_RED, 0, 0.1, 90 );
		}
	}
	
	task Shot2( x, y, speed, angle, praph, delay, gspeed, time ){
		let obj = CreateObjShot( x, y, speed, angle, praph, delay );
		let count = 0;
		FWait( delay );
		
		while( !Obj_BeDeleted( obj ) ){
			if( speed > 0 ){
				speed -= gspeed;
			}
			Obj_SetSpeed( obj, speed );
			count += 1;
			if( count > time ){
				break;
			}
			yield;
		}
		
		if( !Obj_BeDeleted( obj ) ){
			x = Obj_GetX( obj );
			y = Obj_GetY( obj );
			angle -= 6 * 3.5;
			
			ascent( i in 0..8 ){
				CreateShotA( 1, x, y, 0 );
				SetShotDataA( 1,  0, 7.2, angle + 180 + i * 6, 0, 0, 99, US_BILL_WHITE );
				SetShotDataA( 1, 45,   1, NULL, 0, 0, NULL, NULL );
				FireShot( 1 );
			}
			
			Obj_Delete( obj );
		}
	}
	
	task Move(){
		let limminx = MinX + 32;
		let limmaxx = MaxX - 32;
		let limminy = MinY + 120 + 16;
		let limmaxy = MinY + 120 - 16;
		let x = GetPlayerX();
		let y = rand( limminy, limmaxy );
		if( x < limminx ){
			x = limminx;
		}else if(  x > limmaxx  ){
			x = limmaxx;
		}
		
		SetAction( ACT_MOVE, 120 );
		SetMovePosition03( x, y, 20, 6 );
	}
	
	#include_function ".\initialize.txt"
	
	#include_function ".\..\lib\lib_anime_reimu.txt"

}