#TouhouDanmakufu
#Title[Spell 2]
#Text[Spell 2]
#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\slash.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,"Ghost Half - Myon -",cut,0,0,256,512);


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

	@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(50){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, GetCenterX, GetY);


    ObjEffect_SetScale(obj, 1.2, 1);

    ObjEffect_SetLayer(obj, 5);


    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);

    ObjEffect_CreateVertex(obj, 4);

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

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

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

    ObjEffect_SetVertexXY(obj, 3, 130, 10);


    ObjEffect_SetVertexUV(obj, 0, 0, 0);

    ObjEffect_SetVertexUV(obj, 1, 260, 0);

    ObjEffect_SetVertexUV(obj, 2, 0, 20);

    ObjEffect_SetVertexUV(obj, 3, 260, 20);


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

      counter++;

    yield;

  }

}

	task slash{
		PlaySE(p1);
		wait(120);
		PlaySE(s2);
		Slow(1);
		MotionBlur(ALL,70,100);
		wait(45);
		slasheffect;
		Slow(0);
		slashtime = 1;
		PlaySE(slashse);
		wait(3);
		slashtime = 0;
	}
	task move{
loop{
		if(slashtime == 1){
			if(GetEnemyX < GetCenterX){
				SetMovePosition02(GetCenterX+150,GetCenterY-110,1);
			}
			if(GetEnemyX > GetCenterX){
				SetMovePosition02(GetCenterX-150,GetCenterY-110,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);
		loop{
			slash;
		wait(280);
		}
	}
	task glowything{
	loop{
	loop(60){
		thing+=2;
		thing2+=1;
	yield;}
	loop(60){
		thing-=1;
		thing2-=2;
	yield;}
	loop(60){
		thing+=1;
		thing2+=2;
	yield;}
	loop(60){
		thing-=2;
		thing2-=1;
	yield;}
	}
	}
	task fire{
		wait(60);
		loop{
		let dir = 0;
		loop{
				b(GetX+thing*cos(dir),GetY+thing2*sin(dir), 0.5, dir, 152, 30);
				b(GetX+thing*cos(dir),GetY+thing2*sin(dir), 1, dir, 152, 30);
			PlaySE(s1);
			dir+=30;
			wait(15);
			}
	wait(60);
	}
       }
	task fireb{
		wait(60);
		loop{
		let dir = 0;
			loop{
				b2(GetX+thing*cos(dir),GetY+thing2*sin(dir), 0.5, dir, 154, 40);
				b2(GetX+thing*cos(dir),GetY+thing2*sin(dir), 1, dir, 154, 40);
			PlaySE(s1);
			dir+=2;
			wait(1);
			}
	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){
		loop(7){CreateShot01(Obj_GetX(obj),Obj_GetY(obj),1,dir,13,60); dir+=360/7;}
		loop(7){CreateShot01(Obj_GetX(obj),Obj_GetY(obj),1.2,dir-10,15,60); dir+=360/7;}
		loop(7){CreateShot01(Obj_GetX(obj),Obj_GetY(obj),1.4,dir-20,4,60); dir+=360/7;}
		loop(7){CreateShot01(Obj_GetX(obj),Obj_GetY(obj),1.6,dir-30,6,60); dir+=360/7;}
		//loop(7){CreateShot01(Obj_GetX(obj),Obj_GetY(obj),1.8,dir-40,8,60); dir+=360/7;}
			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;
			}
	}
	function wait(w){loop(w){yield;}}
}