#TouhouDanmakufu
#Title[Elemental Sign : Wicked Light]
#Text[Elemental Sign : Wicked Light]
#BackGround[User(.\system\bg0.png, 0, 0)]
#Player[FREE]
#ScriptVersion[2]
//#Image[.\system\image.jpg]
#BGM[.\bgm\lunaticprincess.mp3]

script_enemy_main{
		
		#include_function".\function_cutin.txt";
		#include_function".\Halo.txt";
		
	let CSD = GetCurrentScriptDirectory;

	let imgBoss = CSD ~ "system\kaguyasprite.png";
	let cut = GetCurrentScriptDirectory ~ "system\kaguyacutin.png";
	let bg1 = CSD ~ "system\bg1.png";
	let bg2 = CSD ~ "system\bg2.png";
	let f = 0;
	let f2 = 0;
	let f3 = 0;
	let slide = 0;
	let hover = 0;
	let hovertrue = 0;
		
	@Initialize{
		SetLife(2000);
		SetScore(10000000);
		SetDamageRate(22,10);
		SetTimer(90);		
		SetInvincibility(150);		
		LoadGraphic(imgBoss);
		LoadGraphic(cut);
		LoadGraphic(bg1);
		LoadGraphic(bg2);
		LoadUserShotData(CSD ~ "ZUN's shotdata.txt");
		CreateAura03("ROSE",0,0,1.3,1.3,1.3);
		cutin("NAZRIN","Elemental Sign 'Wicked Light'",cut,0,5,600,439);
		
		mainTask;

	}

	@MainLoop{
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 32);
		
		yield;
	}

	@DrawLoop{
		
		SetTexture(imgBoss);
		SetRenderState(ALPHA);
		SetAlpha(255);
		SetGraphicScale(1,1);
		SetGraphicAngle(0,0,0);		
		
		if(int(GetSpeedX())==0){
			if(f<3){SetGraphicRect(0,0,80,80);}
			f2=0;
			hovertrue=1;
		}
		
		if(GetSpeedX()>0){
			if(f2<3){SetGraphicRect(0,80,80,160);}
			if(f2>=3 && f2<6){SetGraphicRect(80,80,160,160);}
			if(f2>=6){SetGraphicRect(160,80,240,160);}
			f2++;		
			hovertrue=0;
		}
		if(GetSpeedX()<0){
			SetGraphicAngle(180,0,0);
			if(f2<3){SetGraphicRect(0,80,80,160);}
			if(f2>=3 && f2<6){SetGraphicRect(80,80,160,160);}
			if(f2>=6){SetGraphicRect(160,80,240,160);}
			f2++;		
			hovertrue=0;
		}
		if(hovertrue==1){hover++;}
		if(hovertrue==0){hover=0;}
		DrawGraphic(GetX,GetY+(8*sin(hover*3)));
	}

	@BackGround{
		SetTexture(bg2);
		SetGraphicRect(0,0,256,5000+slide);
		SetGraphicScale(1.75,1.75);
		SetGraphicAngle(0,0,0);
		DrawGraphic(GetCenterX,GetCenterY-(slide*2));
		
		slide+=0.5;
		SetTexture(bg1);
		SetGraphicRect(0,0,384,448);
		SetGraphicScale(1.5,1.01);
		SetGraphicAngle(0,0,0);
		DrawGraphic(GetCenterX,GetCenterY);
		
		
	}

	@Finalize{
		DeleteGraphic(imgBoss);
		DeleteGraphic(cut);
		DeleteGraphic(bg1);
		DeleteGraphic(bg2);
		PlaySE(CSD ~ "sfx\se_bonus2.wav");
		CreateItem(ITEM_BOMB,GetEnemyX,GetEnemyY);
		PlaySE(CSD ~ "sfx\se_enep00_2.wav");
	}


	task mainTask{
		SetMovePosition03(GetCenterX,GetCenterY-110,10,2.5);
		wait(120);		
		yield;
		fire;
		balls;
		//movement;
		wait(5220);
		loop(6){PlaySE(CSD ~ "sfx\se_timeout.wav");
		wait(60);}
		loop(5){PlaySE(CSD ~ "sfx\se_timeout2.wav");
		wait(60);}
	}

	task movement{
		loop{
			wait(40);
			SetMovePosition02(GetCenterX+rand(-40,30),GetCenterY-40,50);
			wait(20);
			yield;
		}
	}
	
		
	task balls{
		let y = 0;
		let dir2 = 0;
		let color = 17;
	
		wait(10);
		loop{
	
		while(y<24){
				
                                          
				dir2+=360/90;
     
				color++;
				if(color>1){color=187;}
				y++;
			}
			y=0;
			color = 17;
			dir2+= rand(0,360);
			wait(95);
		}
		yield;
	}
	
	task fire{

		let x = 0;
		let dir = 0;
		let frame = 0;
		
		
		loop{
			let angle = rand(0,360);
			
			Concentration01(60);
			PlaySE(CSD ~ "sfx\se_ch00.wav");
			
			wait(40);
			let color = 52;
			//SetPlayerInvincibility(100000);				
			loop{				
				f3++;
				let turn = 3*sin(f3*2);
				
				PlaySE(CSD ~ "sfx\se_tan00_2.wav");
				while(x<28){
				CreateShotA(1,GetEnemyX+15*cos(dir),GetEnemyY+15*sin(dir),10);

				// (id,time,speed,dir,dir-velocity,accel,maxspeed,graphic)
				SetShotDataA(1,0,1,dir,turn,-2.01,2.55,color);
				SetShotDataA(1,1,NULL,NULL,0,1,2.55,color);
                                                                SetShotDataA(1,0,2,dir,turn,-2.01,2.55,color);
				SetShotDataA(1,0,NULL,NULL,1,1,2.55,color);
                                                                SetShotDataA(1,0,1,dir,turn,-3.01,2.55,color);
				SetShotDataA(1,0,NULL,NULL,3,1,2.55,color);
                                                                SetShotDataA(1,0,5,dir,turn,-4.12,2.55,color);
				SetShotDataA(1,0,NULL,NULL,4,1,2.55,color);
				 SetShotDataA(1,0,5,dir,turn,-5.12,2.55,color);
				SetShotDataA(1,0,NULL,NULL,4,1,2.55,color);
                                                                 SetShotDataA(1,0,5,dir,turn,-7.12,2.55,color);
				SetShotDataA(1,0,NULL,NULL,1,1,2.55,color);
				FireShot(1);
				
				// adjust angle of next bullet to complete a circle
				dir+=360/6;
				x++;
				
			}
			// reset x value
			x = 0;
			frame+=1;
			dir += rand(6,8)*sin(frame*1.5);
			color+=1;
			if(color>175){color=132;}
			wait(1.85);
			}
			yield;
		}
	}		
	
	//object bullet task
	task invincible(x,y,v,dir,graphic,delay){
			let obj = Obj_Create(OBJ_SHOT);
			
			Obj_SetPosition(obj,x,y);
			Obj_SetSpeed(obj,v);
			Obj_SetAngle(obj,dir);
			ObjShot_SetGraphic(obj,graphic);
			ObjShot_SetDelay(obj,delay);
			ObjShot_SetBombResist(obj,true);
			
			yield;			
	}
	
	function wait(w){
		loop(w){yield;}
	}
}