#TouhouDanmakufu
#Title[Spell 3 - Lunatic]
#Text[Spell 3]
#Player[REIMU]
#PlayLevel[Lunatic]
#ScriptVersion[2]

script_enemy_main{

	let D	= GetCurrentScriptDirectory;

	let imgBoss = D ~ "system\Youmu.png";
	let shot = D ~ "ZUN's shotdata.txt";
	let cut = D ~ "system\face_05_01.png";
	let bg = D ~ "system\background3.png";
	let slashimage = D ~ "system\slash3.png";
	let slashimage2 = D ~ "system\slash4.png";
	let s1 = D ~ "sfx\Shot.wav";
	let s2 = D ~ "sfx\Wave.wav";
	let s3 = D ~ "sfx\Shot 2.wav";
	let p1 = D ~ "sfx\Ch00.wav";
	let slashse = D ~ "sfx\Slash.wav";
	let d1 = D ~ "sfx\Damage00.wav";
	let d2 = D ~ "sfx\Damage01.wav";
	let t1 = D ~ "sfx\t1.wav";
	let t2 = D ~ "sfx\t2.wav";
	let c1 = D ~ "sfx\Power0.wav";
	let slide = 0;
	let slide2 = 0;
	let slashtime = 0;
	let slashtime2 = 1;
	let alph1 = 0;
	let alph2 = 255;
	let xpos = GetCenterX+180;
	let e1 = D ~ "sfx\Enep01.wav";
	let thing = 120;
	CutIn(YOUMU,"slash u - mhm -",cut,0,0,256,512);
	let f = 0;
	let f2 = 0;


	@Initialize{
		SetLife(3800);
		SetTimer(50);
		SetScore(100000);
		SetDurableSpellCard;
		SetPlayerX(GetCenterX);
		SetPlayerY(GetCenterY+30);
		SetMovePosition03(GetCenterX-90,GetCenterY-110,8,4);
                	SetEffectForZeroLife(90, 100, 1);
    		LoadUserShotData(shot);
		LoadGraphic(bg);
		LoadGraphic(imgBoss);
		LoadGraphic(slashimage);
		LoadGraphic(slashimage2);
		LoadSE(slashse);
		LoadSE(p1);
		LoadSE(d1);
		LoadSE(d2);
		LoadSE(t1);
		LoadSE(t2);
		LoadSE(s1);
		LoadSE(s2);
		LoadSE(s3);
		LoadSE(e1);
		LoadSE(c1);
		PlaySE(c1);
		mainTask;
		damage;
		timeout;
		move;
		fadein;
		movein;
		CreateEnemyFromFile(GetCurrentScriptDirectory~"yuyuyuko.txt", 0, 0, 0, 0, 0);
	}

	@MainLoop{
		yield;
	}

	@DrawLoop{
		SetTexture(imgBoss);
		SetRenderState(ALPHA);
		SetAlpha(255);
		SetGraphicScale(1,1);
		SetGraphicAngle(0,0,0);
		if(int(GetSpeedX())==0){
			if(f<10){ SetGraphicRect(256,100,304,160); }
			if(f>=10 && f<20){ SetGraphicRect(304,100,352,160); }
			if(f>=20 && f<30){ SetGraphicRect(352,100,400,160);  }
			if(f>=30 && f<40){ SetGraphicRect(400,100,448,160);}
			f2 = 0;
		}
		if(GetSpeedX()>0){
			SetGraphicAngle(180,0,0);
			if(f2<7){ SetGraphicRect(256,160,304,224); }
			if(f2>=7 && f2<14){ SetGraphicRect(304,160,352,224); }
			if(f2>=14 && f2<21){ SetGraphicRect(352,160,400,224); }
			if(f2>=21){ SetGraphicRect(400,160,448,224); }
			f2++;
		}
		if(GetSpeedX()<0){
			if(f2<7){ SetGraphicRect(256,160,304,224); }
			if(f2>=7 && f2<14){ SetGraphicRect(304,160,352,224); }
			if(f2>=14 && f2<21){ SetGraphicRect(352,160,400,224); }
			if(f2>=21){ SetGraphicRect(400,160,448,224); }
			f2++;
		}
		DrawGraphic(GetX,GetY);
		f++;
		f2++;
		if(f==40){f=0;}
		SetFontColor(255,255,255, 255,255,255);
		DrawText("Youmu Konpaku",GetCenterX-123,GetCenterY-205,14,255);
	DrawText("BGM: Voyage 1970",xpos,GetCenterY+205,14,alph2);
	}


	@BackGround{

		SetTexture(bg);

		SetRenderState(ALPHA);

		SetAlpha(120);
	
		SetGraphicRect(0,0,10000,10000);

		SetGraphicScale(2,2);

		SetGraphicAngle(0,0,0);

		DrawGraphic(GetCenterX-slide2,GetCenterY+slide);
	slide+=2;	
	slide2-=1; 
	}



	@Finalize{
		DeleteSE(d1);
		DeleteSE(d2);
		DeleteSE(t1);
		DeleteSE(t2);
		DeleteSE(s1);
		DeleteSE(s2);
		DeleteSE(s3);
		PlaySE(e1);
		DeleteGraphic(imgBoss);
		DeleteGraphic(bg);
		DeleteGraphic(slashimage);
		DeleteGraphic(slashimage2);
		loop(150){CreateItem(ITEM_SCORE,GetX+rand(-200,200),GetY+rand(-50,190));}
		loop(2){CreateItem(ITEM_BOMB,GetX+rand(-130,130),GetY+rand(0,140));}
		CreateItem(ITEM_1UP,GetX,GetY);
	}
task slasheffect{

  let obj=Obj_Create(OBJ_EFFECT);


  
let counter = 0;

    
ObjEffect_SetRenderState(obj, ALPHA);

    ObjEffect_SetTexture(obj, slashimage2);


    	Obj_SetPosition(obj, GetCenterX+60, GetCenterY-65);


    ObjEffect_SetScale(obj, 0.8, 0.8);
    ObjEffect_SetLayer(obj, 5);


    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);

    ObjEffect_CreateVertex(obj, 4);

    
ObjEffect_SetVertexXY(obj, 0, -91, -91);

    ObjEffect_SetVertexXY(obj, 1, 91, -91);

    ObjEffect_SetVertexXY(obj, 2, -91, 91);

    ObjEffect_SetVertexXY(obj, 3, 91, 91);


    ObjEffect_SetVertexUV(obj, 0, 0, 0);

    ObjEffect_SetVertexUV(obj, 1, 183, 0);

    ObjEffect_SetVertexUV(obj, 2, 0, 183);

    ObjEffect_SetVertexUV(obj, 3, 183, 183);


      while(!Obj_BeDeleted(obj)) {
	
      if(counter==6000){Obj_Delete(obj);}

      counter++;

    yield;

  }

}

task slasheffect2{

  let obj=Obj_Create(OBJ_EFFECT);


  
let counter = 0;

    
ObjEffect_SetRenderState(obj, ALPHA);

    ObjEffect_SetTexture(obj, slashimage);


    	Obj_SetPosition(obj, GetCenterX+60, GetCenterY+65);


    ObjEffect_SetScale(obj, 0.8, 0.8);
    ObjEffect_SetLayer(obj, 5);


    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);

    ObjEffect_CreateVertex(obj, 4);

    
ObjEffect_SetVertexXY(obj, 0, -91, -91);

    ObjEffect_SetVertexXY(obj, 1, 91, -91);

    ObjEffect_SetVertexXY(obj, 2, -91, 91);

    ObjEffect_SetVertexXY(obj, 3, 91, 91);


    ObjEffect_SetVertexUV(obj, 0, 0, 0);

    ObjEffect_SetVertexUV(obj, 1, 183, 0);

    ObjEffect_SetVertexUV(obj, 2, 0, 183);

    ObjEffect_SetVertexUV(obj, 3, 183, 183);


      while(!Obj_BeDeleted(obj)) {
	
      if(counter==6000){Obj_Delete(obj);}

      counter++;

    yield;

  }

}

task slasheffect3{

  let obj=Obj_Create(OBJ_EFFECT);


  
let counter = 0;

    
ObjEffect_SetRenderState(obj, ALPHA);

    ObjEffect_SetTexture(obj, slashimage2);


    	Obj_SetPosition(obj, GetCenterX-60, GetCenterY+65);


    ObjEffect_SetScale(obj, 0.8, 0.8);
    ObjEffect_SetLayer(obj, 5);


    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);

    ObjEffect_CreateVertex(obj, 4);

    
ObjEffect_SetVertexXY(obj, 0, -91, -91);

    ObjEffect_SetVertexXY(obj, 1, 91, -91);

    ObjEffect_SetVertexXY(obj, 2, -91, 91);

    ObjEffect_SetVertexXY(obj, 3, 91, 91);


    ObjEffect_SetVertexUV(obj, 0, 0, 0);

    ObjEffect_SetVertexUV(obj, 1, 183, 0);

    ObjEffect_SetVertexUV(obj, 2, 0, 183);

    ObjEffect_SetVertexUV(obj, 3, 183, 183);


      while(!Obj_BeDeleted(obj)) {
	
      if(counter==6000){Obj_Delete(obj);}

      counter++;

    yield;

  }

}

task slasheffect4{

  let obj=Obj_Create(OBJ_EFFECT);


  
let counter = 0;

    
ObjEffect_SetRenderState(obj, ALPHA);

    ObjEffect_SetTexture(obj, slashimage);

	Obj_SetPosition(obj, GetCenterX-60, GetCenterY-65);


    ObjEffect_SetScale(obj, 0.8, 0.8);
    ObjEffect_SetLayer(obj, 5);


    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);

    ObjEffect_CreateVertex(obj, 4);

    
ObjEffect_SetVertexXY(obj, 0, -91, -91);

    ObjEffect_SetVertexXY(obj, 1, 91, -91);

    ObjEffect_SetVertexXY(obj, 2, -91, 91);

    ObjEffect_SetVertexXY(obj, 3, 91, 91);


    ObjEffect_SetVertexUV(obj, 0, 0, 0);

    ObjEffect_SetVertexUV(obj, 1, 183, 0);

    ObjEffect_SetVertexUV(obj, 2, 0, 183);

    ObjEffect_SetVertexUV(obj, 3, 183, 183);


      while(!Obj_BeDeleted(obj)) {
	
      if(counter==6000){Obj_Delete(obj);}

      counter++;

    yield;

  }

}

	task fadein{
		loop(255){
			alph1++;
			yield;
		}
	}
	task movein{
		loop(40){
			xpos-=3;
		wait(2);
		}
		wait(120);
		loop(51){
			alph2-=5;
			yield;
		}
	}
	task slash{
		PlaySE(p1);
		wait(120);
		PlaySE(s2);
		Slow(1);
		MotionBlur(ALL,70,100);
		wait(45);
		if(slashtime2==2){slasheffect;}
		if(slashtime2==3){slasheffect2;}
		if(slashtime2==4){slasheffect3;}
		if(slashtime2==5){slasheffect4;}
		Slow(0);
		slashtime = 1;
		PlaySE(slashse);
		wait(1);
		slashtime = 0;
		slashtime2++;
	}
	task move{
	loop{
		if(slashtime == 1){
			if(slashtime2 == 1){
				SetMovePosition02(GetCenterX,GetCenterY-120,1);
			}
			if(slashtime2 == 2){
				SetMovePosition02(GetCenterX+120,GetCenterY,1);
			}
			if(slashtime2 == 3){
				SetMovePosition02(GetCenterX,GetCenterY+120,1);
			}
			if(slashtime2 == 4){
				SetMovePosition02(GetCenterX-120,GetCenterY,1);
			}
			if(slashtime2 == 5){
				SetMovePosition02(GetCenterX,GetCenterY-120,1);
			}
		}
	yield;}
	}
	task damage{
		loop{
		let bosslife = GetLife;
		wait(3);
			if(GetLife < 600 && GetLife > 300 && bosslife > GetLife){PlaySE(d1);}
			if(GetLife < 300 && GetLife > 0 && bosslife > GetLife){PlaySE(d2);}
		}
	}
	task timeout{
		loop{
	if(GetTimer == 10 || GetTimer == 9 || GetTimer == 8 || GetTimer == 7 || GetTimer == 6 || GetTimer == 5 || GetTimer == 4){PlaySE(t1);}
	if(GetTimer == 3 || GetTimer == 2 || GetTimer == 1){PlaySE(t2);}
		wait(60);}
	}
	task mainTask{
			fire;
			fireb;
		wait(120);
			slash;
		wait(240);
			firec;
			fired;
		wait(260);
		loop(4){
			slash;
		wait(500);
		}
	}
	task fire{
		wait(60);
		loop{
		let dir = 270;
		let thingy = 0.5;
		loop{
				loop(6){
				b(GetCenterX+thing*cos(dir),GetCenterY+thing*sin(dir), thingy, dir, 28, 30);
				thingy+=0.25;}
			PlaySE(s1);
			dir+=10;
			wait(5);
			thingy = 0.5;
			}
	wait(1);
	}
       }

	task fireb{
		wait(60);
		loop{
		let dir = 270;
			loop{
				b2(GetCenterX+120*cos(dir),GetCenterY+120*sin(dir), 1, dir-180, 154, 40);
			PlaySE(s1);
			dir+=2;
			wait(1);
			}
		wait(1);
		}
	}

	task firec{
		wait(60);
		loop{
		let dir = 270;
			loop{
				b3(GetCenterX+120*cos(dir),GetCenterY+120*sin(dir), 1, dir-rand(150,210), 85, 40);
			PlaySE(s3);
			if(slashtime2 == 2){dir=rand(-90,-90);}
			if(slashtime2 == 3){dir=rand(-90,0);}
			if(slashtime2 == 4){dir=rand(-90,90);}
			if(slashtime2 == 5){dir=rand(-90,180);}
			if(slashtime2 == 6){dir=rand(-90,269);}
			wait(11);
			}
		wait(60);
	}
	}
	task fired{
		wait(60);
		loop{
		let dir = 270;
		let sides = 3;
			loop{
		loop(sides){b3(GetCenterX+0*cos(dir),GetCenterY+0*sin(dir), 0.6, dir, 100, 60); dir+=360/sides;}
			PlaySE(s3);
			dir+=17;
			if(GetTimer < 11){sides=6; dir+=17;}
			if(GetTimer > 10){sides += 0.25;}
			wait(20);
			if(GetTimer > 10){wait(50);}
			}
		wait(60);
	}
	}
	task b(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)){
			wait(1);
	if(
	Obj_GetX(obj) > GetClipMinX &&
	Obj_GetY(obj) > GetClipMinY &&
	Obj_GetY(obj) < GetClipMaxY &&
	Obj_GetX(obj) < GetClipMaxX
	){
		if(slashtime == 1){
let sides = 0;
if(slashtime2 == 1){sides = 12;}
if(slashtime2 == 2){sides = 3;}
if(slashtime2 == 3){sides = 3;}
if(slashtime2 == 4){sides = 3;}
if(slashtime2 == 5){sides = 5;}
		loop(sides){b3(Obj_GetX(obj),Obj_GetY(obj),1,dir,7,60); dir+=360/sides;}
if(slashtime2 > 1){
		loop(sides){b3(Obj_GetX(obj),Obj_GetY(obj),1.1,dir-2,9,60); dir+=360/sides;}
if(slashtime2 > 2){
		loop(sides){b3(Obj_GetX(obj),Obj_GetY(obj),1.2,dir-4,11,60); dir+=360/sides;}
if(slashtime2 > 3){
		loop(sides){b3(Obj_GetX(obj),Obj_GetY(obj),1.3,dir-6,13,60); dir+=360/sides;}
if(slashtime2 > 4){
		loop(sides){b3(Obj_GetX(obj),Obj_GetY(obj),1.4,dir-8,15,60); dir+=360/sides;}
}}}}
			Obj_Delete(obj);
			yield;
		}
	}
	}
}
	task b2(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)){
				wait(39);
				Obj_Delete(obj);
				yield;
			}
	}
	task b3(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)){
				wait(90);
				wait(800);
				yield;
			}
	}
	function wait(w){loop(w){yield;}}
}