#TouhouDanmakufu
#Title[Spell]
#Text[Spell]
#Player[REIMU]
#PlayLevel[Normal]
#ScriptVersion[2]
#PlayLevel[Normal]

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\background.png";
	let slashimage = D ~ "system\slash2.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 slide = 0;
	let slide2 = 0;
	let slashtime = 0;
	let thing = 0;
	let thing2 = 0;

	let f = 0;
	let f2 = 0;

	CutIn(YOUMU,"slash u - mhm -",cut,0,0,256,512);


	@Initialize{
		SetLife(3000);
		SetTimer(40);
		SetScore(100000);
		LoadGraphic(bg);
    		LoadUserShotData(shot); 
		LoadGraphic(imgBoss);
		SetInvincibility(140);
		SetMovePosition03(GetCenterX,GetCenterY-190,8,4);
		LoadSE(p1);
		LoadSE(slashse);
		LoadGraphic(slashimage);
		LoadSE(d1);
		LoadSE(d2);
		LoadSE(t1);
		LoadSE(t2);
		LoadSE(s1);
		LoadSE(s2);
		LoadSE(s3);
		mainTask;
		damage;
		timeout;
		move;
	}

	@MainLoop{
 		SetCollisionA(GetX,GetY,32);
		SetCollisionB(GetX,GetY,16);
		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);
	}


	@BackGround{

		SetTexture(bg);

		SetRenderState(ALPHA);

		SetAlpha(255);
	
		SetGraphicRect(0,0,256,256);

		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);
		DeleteGraphic(imgBoss);
		DeleteGraphic(bg);
		loop(30){CreateItem(ITEM_SCORE,GetX+rand(-130,130),GetY+rand(0,140));}
	}
task slasheffect{

  let obj=Obj_Create(OBJ_EFFECT);


  
let counter = 0;

    
ObjEffect_SetRenderState(obj, ALPHA);

    ObjEffect_SetTexture(obj, slashimage);


    Obj_SetPosition(obj, GetX, GetCenterY);


    ObjEffect_SetScale(obj, 1, 1.5);

    ObjEffect_SetLayer(obj, 5);


    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);

    ObjEffect_CreateVertex(obj, 4);

    
ObjEffect_SetVertexXY(obj, 0, -10, -130);

    ObjEffect_SetVertexXY(obj, 1, 10, -130);

    ObjEffect_SetVertexXY(obj, 2, -10, 130);

    ObjEffect_SetVertexXY(obj, 3, 10, 130);


    ObjEffect_SetVertexUV(obj, 0, 0, 0);

    ObjEffect_SetVertexUV(obj, 1, 20, 0);

    ObjEffect_SetVertexUV(obj, 2, 0, 260);

    ObjEffect_SetVertexUV(obj, 3, 20, 260);


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

      counter++;

    yield;

  }

}

	task slash{
		PlaySE(p1);
		wait(120);
		PlaySE(s2);
		Slow(1);
		MotionBlur(ALL,70,100);
		wait(45);
		Slow(0);
		slashtime = 1;
		slasheffect;
		PlaySE(slashse);
		wait(3);
	}
	task move{

	loop(380){
		if(slashtime == 1){
			SetMovePosition02(GetCenterX,GetCenterY+205,1);
		}
	yield;}

		loop{
			SetMovePosition02(GetCenterX+rand(90,-90),GetCenterY+rand(110,180),60);
		wait(180);
		}

	}
	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;
		wait(120);
			slash;
	}
	task fire{
		let thing = 0;
		let thingy = 0;
		wait(60);
		loop{
	loop(39){
		b(GetClipMinX-thing,GetClipMinY,0,atan2(GetPlayerY-(GetY-100),GetPlayerX-(GetX)),153,40);
	thing-=10;
	PlaySE(s1);
	wait(3);
	}
	loop(45){
		b(GetClipMaxX,GetClipMinY-thingy,0,atan2(GetPlayerY-(GetY-100),GetPlayerX-(GetX)),153,40);
	thingy-=10;
	PlaySE(s1);
	wait(3);
	}
	loop(39){
		b(GetClipMinX-thing,GetClipMaxY,0,atan2(GetPlayerY-(GetY-100),GetPlayerX-(GetX)),153,40);
	thing+=10;
	PlaySE(s1);
	wait(3);
	}
	loop(45){
		b(GetClipMinX,GetClipMinY-thingy,0,atan2(GetPlayerY-(GetY-100),GetPlayerX-(GetX)),153,40);
	thingy+=10;
	PlaySE(s1);
	wait(3);
	}

		thing = 0;
		thingy = 0;
		}
	}
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(70);
              Obj_SetAngle(obj,atan2(Obj_GetY(obj) - GetPlayerY, Obj_GetX(obj) - GetPlayerX)-180);
		Obj_SetSpeed(obj,3); 
		ObjShot_SetGraphic(obj,152);


		ObjShot_SetBombResist(obj, false);

		loop{
		if(slashtime == 1){
			let bgraphic = rand(85,87);

	//middle-bottom
if(Obj_GetX(obj) < GetCenterX+7 && Obj_GetX(obj) > GetCenterX-7 && Obj_GetY(obj) > GetCenterY+200 && 
!Obj_BeDeleted(obj)){
			loop(16){b2(Obj_GetX(obj),Obj_GetY(obj),rand(2,3),dir-45,bgraphic,0); dir+=360/16;}
				PlaySE(s3);
				ObjShot_SetGraphic(obj,150);
				//Obj_Delete(obj);
				yield;
			break;}

	//middle-top
if(Obj_GetX(obj) < GetCenterX+7 && Obj_GetX(obj) > GetCenterX-7 && Obj_GetY(obj) <  GetCenterY-200 && 
!Obj_BeDeleted(obj)){
			loop(16){b2(Obj_GetX(obj),Obj_GetY(obj),rand(2,3),dir-45,bgraphic,0); dir+=360/16;}
				PlaySE(s3);
				ObjShot_SetGraphic(obj,150);
				//Obj_Delete(obj);
				yield;
			break;}

	//middle
if(Obj_GetX(obj) < GetCenterX+7 && Obj_GetX(obj) > GetCenterX-7 && Obj_GetY(obj) < GetCenterY+200 && 
Obj_GetY(obj) > GetCenterY-200 && !Obj_BeDeleted(obj)){
			loop(2){b2(Obj_GetX(obj),Obj_GetY(obj),rand(2,3),dir-45,bgraphic,0); dir+=360/2;}
				PlaySE(s3);
				ObjShot_SetGraphic(obj,150);
				//Obj_Delete(obj);
				yield;
			break;}

	}
		yield;}
	yield; wait(1000);}
	}
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(20);
        	loop(rand(35,45)){Obj_SetAngle(obj,dir); dir-=rand(7,9); yield;}
		loop(10){Obj_SetSpeed(obj,v); v-=0.1; yield;} 
	wait(16000);
	yield;}
	}
	function wait(w){loop(w){yield;}}
}