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

script_enemy_main{
	
	@Initialize{
		imgBoss = imgBoss_Cirno;
		InitializeData();
		InitializeAction();
		
		SetLife( 1000 );
		SetX( CenterX );
		SetY( MinY + 110 );
		SetDamageRate( 22, 20 );
		SetCollisionEx( 32, 16, false, 240 );
		SetEnemyMarker( true );
		
		TMain();
	}
	
	@MainLoop{
		yield;
	}
	
	@DrawLoop{
		DrawBoss( imgBoss );
	}
	
	@Finalize{
		FinalizeData();
	}
	
	task TMain(){
		FWait( 60 );
		
		DeclareSpell();
		
		Atack();
	}
	
	function DeclareSpell(){
		SetScore( 26000000 );
		SetTimer( 65 );
		CutIn( KOUMA, "XuACVNC[v" ,NULL, 0, 0, 0, 0 );
		SetAction( ACT_SPELL, 180 );
		FWait( 180 );
	}
	
	task Atack(){
		let angle = 0;
		
		loop{
			SetAction( ACT_SHOT_A, 120 );
			
			Atack1();
			FWait( 160 );
			
			SetAction( ACT_SHOT_B, 100 );
			ColorConcentration( 100, 64, 64, 255 );
			PlaySE( se_charge );
			FWait( 100 );
			
			Atack2();
			FWait( 280 );
			
			SetAction( ACT_SHOT_A, 320 );
			Atack3();
			FWait( 260 );
		}
	}
	
	task Atack1(){
		let way = 12;
		let span = 360 / way;
		let angle = GetAngleToPlayer() - span / 2;
		
		ascent( i in 0..way ){
			Shot1( GetX(), GetY(), 6.4, angle + i * span, 160, 10, US_SCALE_A_BLUE, 0, 0, 20, 2, 3, 60 );
		}
	}
	
	task Atack2(){
		let way = 46;
		let span = 360 / way;
		let angle = GetAngleToPlayer() - span / 2;
		let x = 0;
		let y = 0;
		let speed = 4.2;
		
		loop( 5 ){
			loop( way ){
				x = GetGapX( GetX(), 22, angle );
				y = GetGapY( GetY(), 22, angle );
				Shot2( x, y, speed, angle, US_BALL_M_BLUE, 30, 70 );
				angle += span;
			}
			
			speed -= 0.4;
			FWait( 12 );
		}
	}
	
	task Atack3(){
		let angle = GetAngleToPlayer() - 90;
		let count = 0;
		let x = 0;
		let y = 0;
		
		loop( 80 ){
			x = GetGapX( GetX(), 28, angle );
			y = GetGapY( GetY(), 28, angle );
			loop( 3 ){
				Shot2( x, y, 6.6 + count * 0.012, angle, US_BALL_M_BLUE, 20, 18 + 20 );
				angle += 120;
			}
			count += 3;
			angle += absolute( sin( count ) ) * 17;
			FWait( 3 );
		}
	}
	
	task Shot1( x, y, speed, angle, lng, wid, graph, delay, roll, time, num, way, span ){
		let obj = CreateObjLaser( x, y, 0, angle, 0, wid, graph, delay, false );
		ObjShot_SetBombResist( obj, false );
		let count = 0;
		let vLng = 0;
		
		let mx = speed * cos( angle );
		let my = speed * sin( angle );
		let ex = 0;
		let ey = 0;
		
		while( !Obj_BeDeleted( obj ) ){
			if( vLng < lng ){
				vLng += speed;
				ObjLaser_SetLength( obj, vLng );
			}else{
				vLng = lng;
				x += mx;
				y += my;
			}
			
			Obj_SetPosition( obj, x, y );
			
			if( count > time ){
				ex = ObjLaser_GetEndX( obj );
				ey = ObjLaser_GetEndY( obj );
				angle += 180 - ( way - 1 ) / 2 * span;
				speed *= 0.68;
				
				ascent( i in 0..way ){
					if( num > 0 ){
						Shot1( ex, ey, speed, angle, lng, wid, graph, 0, roll, time, num - 1, way, span );
					}else{
						let obj2 = CreateObjMoveLaser( ex, ey, speed, angle, lng, wid, graph, delay, false );
						ObjShot_SetBombResist( obj2, false );
					}
					
					angle += span;
				}
				
				break;
			}
			
			count += 1;
			yield;
		}
		
		if( !Obj_BeDeleted( obj ) ){
			while( !Obj_BeDeleted( obj ) ){
				vLng -= speed;
				x += mx;
				y += my;
				if( vLng < 0 ){
					break;
				}
				Obj_SetPosition( obj, x, y );
				ObjLaser_SetLength( obj, vLng );
				
				yield;
			}
			
			if( !Obj_BeDeleted( obj ) ){
				Obj_Delete( obj );
			}
		}
	}
	
	task Shot2( x, y, speed, angle, graph, delay, time ){
		let obj = CreateObjShot( x, y, speed, angle, graph, delay );
		let count = 0;
		let graphs = [ US_BALL_SS_BLUE, US_BALL_S_BLUE, US_ICE_BLUE ];
		let gnum = length( graphs );
		let snum = rand_int( 3, 6 );
		
		while( !Obj_BeDeleted( obj ) ){
			count += 1;
			if( count > time ){
				x = Obj_GetX( obj );
				y = Obj_GetY( obj );
				loop( snum ){
					CreateShot01( x, y, speed * rand( 0.2, 0.34 ), angle + rand( -12, 12 ), graphs[ rand_int( 0, gnum - 1 ) ], 20 );
				}
				Obj_Delete( obj );
			}
			yield;
		}
	}
	
	#include_function ".\initialize.txt"
	
	#include_function ".\..\lib\lib_anime_cirno.txt"
	
	#include_function ".\..\lib\lib_shot.txt"
	#include_function ".\..\lib\lib_effect.txt"
}