#TouhouDanmakufu
#Title[Majestic Light]
#Text[Majestic Light"]
#BackGround[User(.\system\bg0.png, 0, 0)]
#Player[FREE]
#ScriptVersion[2]
//#Image[.\system\image.jpg]

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 disappear = 1;
	let bgm = CSD ~ "bgm\lastspell.mp3";
		
	@Initialize{
		SetLife(1);		
		SetDamageRate(0,0);
		SetTimer(45);		
		LoadGraphic(imgBoss);
		LoadGraphic(cut);
		LoadGraphic(bg1);
		LoadGraphic(bg2);
		LoadMusic(bgm);
		MagicCircle(false);
		LastSpell;
		ForbidShot(true);
		SetDurableSpellCard;
		SetEffectForZeroLife(60,200,1);
		
		LoadUserShotData(CSD ~ "ZUN's shotdata.txt");		
		
		mainTask;

	}

	@MainLoop{
		if(disappear==0){
		SetCollisionA(GetX, GetY, 42);
		SetCollisionB(GetX, GetY, 35);
		}
		yield;
	}

	@DrawLoop{
		if(disappear==0){
		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);
		FadeOutMusic(bgm,50);
		PlaySE(CSD ~ "sfx\enep01.wav");
	}


	task mainTask{
		SetMovePosition01(GetCenterX,GetCenterY-100,5);		
		wait(120);	
		Concentration01(120);
		PlaySE(CSD ~ "sfx\charge.wav");
		wait(120);
		Explosion01(GetCenterX,GetCenterY-10,1,1,190);
		Explosion01(GetCenterX,GetCenterY-10,1,1,190);
		Explosion01(GetCenterX,GetCenterY-10,1,1,190);
		disappear=0;
		PlaySE(CSD ~ "sfx\se_cat00.wav");
		PlaySE(CSD ~ "sfx\se_tan00.wav");
		SetScore(10000000);
		PlayMusic(bgm);
		CreateAura03("ROSE",0,1,1.3,1.3,1.3);
		cutin("NAZRIN","Majestic Light'",cut,0,5,600,439);
		wait(120);
		Concentration01(60);
		PlaySE(CSD ~ "sfx\se_ch02.wav");
		wait(60);
		yield;		
		eternal;
		wait(60);
		instant;
		wait(1160);
		loop(20){PlaySE(CSD ~ "sfx\se_timeout.wav");
		wait(1);}
		loop(20){PlaySE(CSD ~ "sfx\se_timeout2.wav");
		wait(60);}
	}

	//33 seconds to timeout
	

	task eternal{
		let y = 0;
		let dir = GetAngleToPlayer;
		MotionBlurEx(ALL,60,255,ADD);
		PlaySE(CSD ~ "sfx\se_boon01.wav");
		PlaySE(CSD ~ "sfx\se_boon01.wav");
		while(y<3){eternitybullet(GetEnemyX,GetEnemyY,3.95,dir,193,0);
		y++;
		dir+=360/3;}
		
		
		yield;
	}
	
	task instant{
		let x = 0;
		loop(6){
		PlaySE(CSD ~ "sfx\se_boon00.wav");
		while(x<60){		
		CreateShotA(1,GetCenterX+rand(-220,220),GetCenterY+rand(-270,270),80);
		// (id,time,speed,dir,dir-velocity,accel,maxspeed,graphic)
		SetShotDataA(1,0,0,rand(0,360),0,0,0,14);
		SetShotDataA(1,90,0,NULL,0,0.01,2,23);
		FireShot(1);
		x++;}
		x=0;
		wait(60);
		PlaySE(CSD ~ "sfx\se_kira00.wav");
		wait(120);
		}
		loop{
		PlaySE(CSD ~ "sfx\se_boon00.wav");
		while(x<60){		
		CreateShotA(1,GetCenterX+rand(-220,220),GetCenterY+rand(-270,270),80);
		// (id,time,speed,dir,dir-velocity,accel,maxspeed,graphic)
		SetShotDataA(1,0,0,rand(0,360),0,0,0,14);
		SetShotDataA(1,90,0,NULL,0,0.01,2,23);
		FireShot(1);
		
		x++;}
		x=0;
		wait(60);
		PlaySE(CSD ~ "sfx\se_kira00.wav");
		wait(60);
		}
		yield;
	}		
	
	//object bullet task
	task eternitybullet(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) > GetClipMaxY){
					Obj_SetAngle(obj,-Obj_GetAngle(obj));
				}
				
				if(Obj_GetY(obj) < GetClipMinY){
					Obj_SetAngle(obj,-Obj_GetAngle(obj));
				}
				
				if(Obj_GetX(obj) < GetClipMinX){
					Obj_SetAngle(obj,180-Obj_GetAngle(obj));
				}
				
				if(Obj_GetX(obj) > GetClipMaxX){
					Obj_SetAngle(obj,180-Obj_GetAngle(obj));
				}
				
				eternitybullet2(Obj_GetX(obj),Obj_GetY(obj),0,0,193,5);
				wait(2);
				yield;
			}
	}
	
	task eternitybullet2(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);
						
			wait(240);			
			Obj_Delete(obj);
			yield;			
	}
	
	
	
	function wait(w){
		loop(w){yield;}
	}
}