#TouhouDanmakufu
#Title[Reimu's Danmaku Sandwich]
#Text[MMM SAMMICH]
#BackGround[User(.\img\BG.png,0,0)]
#PlayLevel[Normal]
#Player[FREE]
#ScriptVersion[2]



script_enemy_main
{
let csd = GetCurrentScriptDirectory;
#include_function ".\Boss.dnh"
let ReimuSprite = csd~"img/ReimuSprite.png";
let ReimuCutin = csd~"img/ReimuCutin.png";

let Shot2 = csd~"se/Shot1.wav";
let Laser1 = csd~"se/Laser1.wav";
let Charge = csd~"se/Charge.wav";
let time01 = csd~"se\timeout01.wav";
let time02 = csd~"se\timeout02.wav";
let SBG = csd~"./sbg.png";
let BALL = csd~"./haku.png";
let bga=0;

@Initialize
{
LoadGraphic(BALL);
LoadGraphic(ReimuSprite);
LoadGraphic(SBG);
LoadSE(Shot2);
LoadSE(Charge);
LoadSE(time01);
LoadSE(time02);

SetLife(1000);
SetDamageRate(10,5);
SetTimer(60);

SetScore(100000);

CutIn(YOUMU,"Wild Exorcism Dance",ReimuCutin,0,0,256,512);
SetEffectForZeroLife(60,0,0);	
SetMovePosition02(GetCenterX,GetCenterY-120,60);
LoadUserShotData(GetCurrentScriptDirectory~"Shot.txt");

MainTask;

Vulnerable(10,5,1200,45,15);

}


task Amulets{
	loop{loop(20){yield;}
	ascent(i in -3..3){CreateShot01(GetX,GetY,4,90+15*i+7.5,95,5)}}
}

task Needles{
	let a=0;
	loop{loop(10){yield;}
	ascent(i in 0..4){CreateShot01(GetX+100*cos(a+90*i)-4,GetY+100*sin(a+90*i),6,90,57,20);
	CreateShot01(GetX+100*cos(a+90*i)+4,GetY+100*sin(a+90*i),6,90,57,20)}
	a=a+10;}
}

task Circle{
loop{
	loop(240){yield;}
	ascent(j in 1..4){let r=-1;ascent(i in -5..40){if(i>0 && rand(0,1)<0.15){r=1};CreateShot02(GetX,GetY+36*i,0,60*r,0.05*j,j/2,57,60)}
		r=-1;ascent(i in -5..40){if(i>0 && rand(0,1)<0.15){r=1};CreateShot02(GetX,GetY+36*i,0,180+60*r,0.05*j,j/2,57,60)}
		r=-1;ascent(i in -40..40){if(i>0 && rand(0,1)<0.15){r=1};CreateShot02(GetX+36*i,GetY,0,90-60*r,0.05*j,j/2,57,60)}
		r=-1;ascent(i in -40..40){if(i>0 && rand(0,1)<0.15){r=1};CreateShot02(GetX+36*i,GetY,0,-90-60*r,0.05*j,j/2,57,60)}}
	ascent(j in 1..4){let r=-1;ascent(i in -5..40){if(i>0 && rand(0,1)<0.15){r=1};CreateShot02(GetX,GetY+36*i,0,-60*r,0.05*j,j/2,57,60)}
		r=-1;ascent(i in -5..40){if(i>0 && rand(0,1)<0.15){r=1};CreateShot02(GetX,GetY+36*i,0,180-60*r,0.05*j,j/2,57,60)}
		r=-1;ascent(i in -40..40){if(i>0 && rand(0,1)<0.15){r=1};CreateShot02(GetX+36*i,GetY,0,90+60*r,0.05*j,j/2,57,60)}
		r=-1;ascent(i in -40..40){if(i>0 && rand(0,1)<0.15){r=1};CreateShot02(GetX+36*i,GetY,0,-90+60*r,0.05*j,j/2,57,60)}}
	}
}
task Mega{
loop{
ascent(i in 0..70){CreateShot01(GetX,GetY,rand(0.3,5),rand(0,360),202,180)}
ascent(i in 0..140){CreateShot01(GetX,GetY,rand(0.3,5),rand(0,360),96,180)}
ascent(i in 0..90){CreateShot01(GetX,GetY,rand(0.3,5),rand(0,360),25,180)}
loop(180){yield;}
SetX(rand(70,380));SetY(420);Concentration01(60);
loop(60){yield;}
ascent(i in 0..20){CreateShot01(GetX,GetY,rand(0.2,7),GetAngleToPlayer+rand(-10,10),203,20)}
ascent(i in 0..50){CreateShot01(GetX,GetY,rand(0.2,7),GetAngleToPlayer+rand(-10,10),97,20)}
ascent(i in 0..30){CreateShot01(GetX,GetY,rand(0.2,7),GetAngleToPlayer+rand(-10,10),27,20)}
loop(60){yield;}
SetMovePosition02(225,90,60);
loop(60){yield;}
}
}

task SuikaShot(x,y,s,a,k,t,d){
let obj=Obj_Create(OBJ_SHOT);
	
	
	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetSpeed(obj,s);
	Obj_SetAngle(obj,a);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,d);
	let ti=12+d;
	let u=-1;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		
		yield;
		ti--;
		u--;
		if(ti==0){u=20;Obj_SetSpeed(obj,s);}
		if(u>0){if(-sin(atan2(Obj_GetY(obj)-GetPlayerY,Obj_GetX(obj)-GetPlayerX)-Obj_GetAngle(obj))>0){Obj_SetAngle(obj,Obj_GetAngle(obj)+k)}
		else{Obj_SetAngle(obj,Obj_GetAngle(obj)-k)}}
		
	}
}
task Suika{
loop{
	loop(30){yield;}
	SuikaShot(GetX+50,GetY+20,5,90,2,246,30);
	SuikaShot(GetX-50,GetY+20,5,90,2,246,30);
	SuikaShot(GetX+100,GetY-20,5,90,2,246,30);
	SuikaShot(GetX-100,GetY-20,5,2,90,246,30)
	}
}

task Suika2{
loop{
loop(15){yield}
Reverse(10,rand(30,440),4,233,60);
loop(15){yield}
Reverse(440,rand(30,440),4,233,60);
loop(15){yield}
Reverse(rand(30,420),470,4,233,60);
}
}

task Reverse(x,y,s,t,d){
let obj=Obj_Create(OBJ_SHOT);
	
	
	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetSpeed(obj,s);
	Obj_SetAngle(obj,180+atan2(Obj_GetY(obj)-GetEnemyY,Obj_GetX(obj)-GetEnemyX));
	let a=Obj_GetAngle(obj);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,d);
	let ti=12+d;
	let u=-1;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		if(((Obj_GetY(obj)-GetEnemyY)^2+(Obj_GetX(obj)-GetEnemyX)^2)^0.5<100){
		SuikaShot(Obj_GetX(obj),Obj_GetY(obj),3,-a,3,238,20);
		SuikaShot(Obj_GetX(obj),Obj_GetY(obj),3,60-a,3,238,20);
		SuikaShot(Obj_GetX(obj),Obj_GetY(obj),3,-60-a,3,238,20);
		Obj_Delete(obj);}
		yield;
		
	}
}

task Delay(R,a,t,d){
let obj=Obj_Create(OBJ_SHOT);
	
	
	Obj_SetX(obj,GetX+R*cos(a));
	Obj_SetY(obj,GetY+R*sin(a));
	Obj_SetSpeed(obj,0);
	Obj_SetAngle(obj,180+atan2(Obj_GetY(obj)-GetEnemyY,Obj_GetX(obj)-GetEnemyX));
	let a=Obj_GetAngle(obj);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,d);
	let ti=d-5;
	let u=-1;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(ti>0){ti--;yield;}
		
		Obj_Delete(obj);
		
	
}

task Aura(R,t,d,n){
let a=0;
loop{yield;a=a+2;
ascent(i in 0..n){Delay(R,a+360*i/n,t,d);}
}
}

task Aya(d){
let obj=Obj_Create(OBJ_SHOT);
	
	
	Obj_SetX(obj,GetX);
	Obj_SetY(obj,GetY);
	Obj_SetSpeed(obj,4);
	Obj_SetAngle(obj,rand(0,360));
	ObjShot_SetGraphic(obj,rand_int(10,12));
	ObjShot_SetDelay(obj,0);
	let ti=20;
	let u=-1;
	let v=(-1)^rand_int(1,2);
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		ti--;
		yield;
		if(ti==0){u=0;Obj_SetAngle(obj,Obj_GetAngle(obj)+90*v)};
		if(u==0){Obj_SetAngle(obj,Obj_GetAngle(obj)+v*360*4/(2*3.142*80))}
		if(ti==d){u=1;}
	}
}

task Dance{
loop(60){yield}
loop{
loop(10){yield}
CreateShot01(GetX,GetY-50,5,90,107,5);
loop(10){yield}
CreateShot01(GetX-20,GetY-50,5,90,107,5);
CreateShot01(GetX+20,GetY-50,5,90,107,5);
loop(10){yield}
CreateShot01(GetX-40,GetY-50,5,90,107,5);
CreateShot01(GetX+40,GetY-50,5,90,107,5);
loop(10){yield}
CreateShot01(GetX-20,GetY-50,5,90,107,5);
CreateShot01(GetX+20,GetY-50,5,90,107,5);
}
}


task DanceCircles{let l=0;
loop{l=360/40-l;
loop(60){yield;}
PlaySE(Shot2);
ascent(i in 0..20){ascent(j in -2..3){CreateShot01(GetX,GetY,2.5,l+360/20*i+1*j,96-l/9,5+3*j);CreateShot01(GetX,GetY,2.5,l+360/20*i-1*j,96-l/9,5+3*j)}}
}

}

task Heaven{let a=0;
loop{yield;
if(OnBomb){SetInvincibility(360);break;}
}
loop{yield;
loop(10){CreateShot01(GetX,GetY,rand(5,7),rand(0,360),rand_int(95,97),20);}}
}

task Hell{loop{yield;
if(OnPlayerMissed){loop(75){yield;};SetLife(0);}
}}


task MainTask{
let a = 0;
let x1;
Heaven;
Hell;
let x2;
let y1;
let y2;
Dance;
DanceCircles;
let i=0;
Wait(60);
SetColor(255,64,64);
Concentration01(120);
SetColor(255,255,255);
PlaySE(Charge);

loop{	
		loop(40){yield};
		SetMovePosition03(GetPlayerX,rand(70,100),40,4);
		
		i=0;
		loop(40){yield};
		}
	
	
	
		
		PlaySE(Shot2);
		loop(240){yield};
		

	
	
	PlaySE(Shot2);
	
	
	
	
	
}

task Seek(x,y,s,a,t,k,d){
	let obj=Obj_Create(OBJ_SINUATE_LASER);
	ObjSinuateLaser_SetLength(obj,80);
     ObjSinuateLaser_SetWidth(obj,25);
	
	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetSpeed(obj,s);
	Obj_SetAngle(obj,a);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,d);
	let ti=12+d;
	let u=-1;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		
		yield;
		ti--;
		u--;
		if(ti==0){u=120;Obj_SetSpeed(obj,6);}
		if(u>0){Obj_SetAngle(obj,Obj_GetAngle(obj)+9)}
		if(u==1){X=(1-k)*X+k*GetPlayerX;Y=Y+k*(GetPlayerY-Y);}
		if(u==0){Obj_SetAngle(obj,180+atan2(Obj_GetY(obj)-Y,Obj_GetX(obj)-X));
				Obj_SetSpeed(obj,s);}
	}
}

task MoveAbout{
loop
	{
	SetMovePositionRandom01(64,64,4,GetClipMinX+128,GetClipMinY+64,GetClipMaxX-128,GetClipMinY+128);
	
	Wait(90);
	}
}





@MainLoop
{
SetCollisionA(GetX,GetY,48);
SetCollisionB(GetX,GetY,32);
yield;
}



let BR = [0,0,64,80];
let bossflip = 0;
let ani = 0;

task MoveAnimation
{
ani++;
let currentani = ani;

if (GetSpeedX > 0)
	{
	bossflip = 0;
	loop(8){if(currentani==ani){BR = [0,80,64,160]; yield;}}
	loop(8){if(currentani==ani){BR = [64,80,128,160]; yield;}}
	loop(8){if(currentani==ani){BR = [128,80,192,160]; yield;}}
	while(GetSpeed>0 && currentani==ani){BR = [192,80,256,160]; yield;}
	}
else{if (GetSpeedX < 0)
	{
	bossflip = 1;
	loop(8){if(currentani==ani){BR = [0,80,64,160]; yield;}}
	loop(8){if(currentani==ani){BR = [64,80,128,160]; yield;}}
	loop(8){if(currentani==ani){BR = [128,80,192,160]; yield;}}
	while(GetSpeed>0 && currentani==ani){BR = [192,80,256,160]; yield;}
	}}
if(currentani==ani){BR = [0,0,64,80];}
}

@DrawLoop
{
SetAlpha(255);
if (bossflip==0){SetGraphicScale(1,1);}
if (bossflip==1){SetGraphicScale(-1,1);}
SetColor(255,255,255);
SetGraphicAngle(0,0,0);
SetTexture(ReimuSprite);
SetGraphicRect(BR[0],BR[1],BR[2],BR[3]);
DrawGraphic(GetX,GetY);
}

let bgalpha = 0;
let bgangle = 0;

@BackGround
{
bga++;
SetTexture(SBG);
SetGraphicRect(0,0,450,500);
SetGraphicAngle ( 0 , 0 , 0 );
DrawGraphic(224,240);
SetTexture(BALL);
SetGraphicRect(0,0,380,380);
SetGraphicAngle ( 0 , 0 , -bga );
DrawGraphic(224,240);
}


@Finalize
{
DeleteGraphic(ReimuSprite);

}

}