#TouhouDanmakufu
#Title[Morinku2]
#Text[Nonspell 2]
#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;
	let time = 180;
		
	@Initialize{
		SetLife(2500);
		//SetScore(10000000);
		SetDamageRate(80,50);
		SetTimer(65);		
		SetInvincibility(100);		
		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","New Impossible Request 'Precious Hourai Jewel'",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_enep00_2.wav");
	}


	task mainTask{
		SetMovePosition03(GetCenterX,GetCenterY-110,10,2.5);
		wait(120);		
		yield;
		fire;		
		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(4);
			SetMovePositionRandom01(40,20,2,GetCenterX-40,GetCenterY-130,GetCenterX+40,GetCenterY-90);
			wait(time);
			yield;
		}
	}	
		
	task fire{

		let x = 0;
		let dir = 0;
		let frame = 0;
		
		
		loop{
			dir = rand(0,360);
			
			Concentration01(60);
			PlaySE(CSD ~ "sfx\se_ch02.wav");			
			wait(10);
			//SetPlayerInvincibility(100000);	
			
			loop(660){
			PlaySE(CSD ~ "sfx\se_tan00_2.wav");
			while(x<6){
				reflect(GetEnemyX+30*cos(dir),GetEnemyY+30*sin(dir),1.55,dir,91,20);
				dir+=360/9;
				x++;
			}
			x=0;
			dir+= rand(8,15);
			wait(3);}
			wait(time);	
			time+= -6;
		}
		yield;
	}		
	
	//object bullet task
	task reflect(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);
						
			while(!Obj_BeDeleted(obj)){
			
				if(Obj_GetY(obj) < GetClipMinY){
					PlaySE(CSD ~ "sfx\se_kira00.wav");
					CreateShot01(Obj_GetX(obj),Obj_GetY(obj),1.55,-dir,93,20);
                                                              
                                                                               
					Obj_Delete(obj);
				
				
				
				}
				
				yield;
			}
			
			
	}
	
	function wait(w){
		loop(w){yield;}
	}
}