#TouhouDanmakufu
#Title["Dream Palace Great Mausoleum"]
#Text[spell5]
#Image[.\img\spellcard0103.jpg]
#PlayLevel[Hard]
#ScriptVersion[2]

script_enemy_main
{
#include_function ".\system\Effects.txt";
#include_function ".\cutin.txt";
#include_function ".\drawall.txt";
#include_function ".\maple.txt" 
#include_function ".\hitbox.txt";
#include_function".\SnowFunc.txt";
let ImgBoss = GetCurrentScriptDirectory~"system\enm5m.png";
let boat = GetCurrentScriptDirectory~"system\enm5.png";
let imgExRumiaBack=GetCurrentScriptDirectory~"img\ExRumiaBack.png";
let CSD = GetCurrentScriptDirectory;
let ci = CSD ~ "system\NMM.png";
let cx=GetCenterX();
let frame = 0;
let framedelay = 0;
let gp;
let x;
let switch = 1;
let randx = 0;
let arr = [60,50,40,30,20,10];
let Cut = GetCurrentScriptDirectory~"img\Toziko.png";
let ThisShot = GetCurrentScriptDirectory~"ZUN's shotdata.txt";
let l = CSD ~ "system\extras.png";
let test = CSD ~ "system\Nuclear.png";

@Initialize
{
CreateHitbox;
SetLife(800);
SetDamageRate(10,0);
SetTimer(70);
mainTask;

SetInvincibility(360);
SetMovePosition03(GetCenterX, GetCenterY-100, 5, 2.5);
//ConcentrationEx0(120,255,0,0,true);
//PlaySE(GetCurrentScriptDirectory~"stE4Boss2.wav");
//PlaySE(GetCurrentScriptDirectory~"cardcall.wav");
LoadGraphic(ImgBoss);
LoadUserShotData(ThisShot);
LoadGraphic(Cut);
LoadGraphic(boat);
LoadGraphic(ci);
LoadGraphic(l);
LoadGraphic(test);
SetTexture(ImgBoss);
SetGraphicRect(0, 0, 64, 64);
MagicCircle(true);
Aura;
}

//---------------------------------------------------------------

@MainLoop 
{

SetCollisionA(GetX, GetY, 32);
SetCollisionB(GetX, GetY, 16);
if(frame>=300){
SetCollisionA(GetX, GetY, 32*2);
SetCollisionB(GetX, GetY, 16*2);
}

// Start Attack
frame++;
yield;

// End Attack


// Start Boss Move


// End Boss Move



}

//---------------------------------------------------------------

@DrawLoop
{
SetTexture(ImgBoss);
SetRenderState(ALPHA);
SetAlpha(255);
SetGraphicScale(1.1, 1.1);
if(angle>180 && angle <360){
rotate = 0;
}
else {
rotate = 180;
}
drawboss;
DrawHitbox;


}
//---------------------------------------------------------------

@BackGround
{
	drawbg;
}	

//---------------------------------------------------------------

@Finalize
{
PlaySE(CSD~"sound\enemoff.wav");
DeleteGraphic(ImgBoss);
}

// main task
task mainTask{
	wait(90);
	SetColor(0, 255, 255); 
	Concentration01(60);
	SetColor(255, 255, 255); 
	wait(60);
	setboss;
	PlayDelaySE(CSD~"sound\fam.wav",0);
	wait(60);
	drawboat;
	wait(90);
	drawcircle(GetX,GetY+120,0,0);
	PlaySE(GetCurrentScriptDirectory~"cardcall.wav");
	SetScore(500000);
	cutin("NAZRIN", "Dream Palace Great Mausoleum", Cut, 0, 0, 288, 511);
	movement;
	yield;
}

task fire{
	loop{
	PlayDelaySE(CSD~"sound\shot5.wav",0);
	Bullet(GetCenterX+rand_int(-180,180),GetClipMinY+rand_int(0,10),rand(1.5,2.5),rand_int(70,110),93,10,0);
	wait(4);
	yield;
	PlayDelaySE(CSD~"sound\shot5.wav",0);
	Bullet(GetCenterX+rand_int(-180,180),GetClipMaxY+rand_int(0,10),rand(1,2),rand_int(70,110),93,10,0);
	wait(4);
	yield;
	PlayDelaySE(CSD~"sound\shot5.wav",0);
	Bullet(GetClipMinX,GetCenterY+rand_int(-200,200),rand(1.5,2.5),rand_int(70,110),93,10,0);
	wait(4);
	yield;
	PlayDelaySE(CSD~"sound\shot5.wav",0);
	Bullet(GetClipMaxX,GetCenterY+rand_int(-200,200),rand(1.5,2.5),rand_int(70,110),93,10,0);
	wait(4);
	yield;
	}
	}

task fire2{
	wait(300);
	loop{
	PlayDelaySE(CSD~"sound\bulletwave.wav",0);
	ascent(i in 0..5){
	Bullet(i*84,GetClipMinY+rand_int(0,10),rand(1.5,2.5),rand_int(70,110),94,10,0);
	Bullet(i*84,GetClipMaxY+rand_int(0,10),rand(1,2),rand_int(70,110),94,10,0);
	Bullet(GetClipMinX,i*94,rand(1.5,2.5),rand_int(70,110),94,10,0);
	Bullet(GetClipMaxX,i*94,rand(1.5,2.5),rand_int(70,110),94,10,0);
	}
	yield;
	wait(179-frame/60);
	}
	}

task fire3{
	wait(1320);
	loop{
	PlayDelaySE(CSD~"sound\bulletwave.wav",0);
	ascent(i in 0..3){
	Bullet(i*140,GetClipMinY+rand_int(0,10),rand(1,2),rand_int(70,110),90,10,1);
	Bullet(i*140,GetClipMaxY+rand_int(0,10),rand(1,2),rand_int(70,110),90,10,1);
	Bullet(GetClipMinX,i*165,rand(1,2),rand_int(70,110),90,10,1);
	Bullet(GetClipMaxX,i*165,rand(1,2),rand_int(70,110),90,10,1);
	}
	yield;
	wait(179-frame/60);
	}
	}

task fire4{
	wait(870);
	loop{
	PlayDelaySE(CSD~"sound\bulletwave.wav",0);
	ascent(i in 0..3){
	Bullet(i*140+rand_int(-5,5),GetClipMinY+rand_int(0,10),rand(1.5,2),rand_int(70,110),89,10,2);
	Bullet(i*140+rand_int(-5,5),GetClipMaxY+rand_int(0,10),rand(1.5,2),rand_int(70,110),89,10,2);
	Bullet(GetClipMinX,i*165+rand_int(-5,5),rand(1.5,2),rand_int(70,110),89,10,2);
	Bullet(GetClipMaxX,i*165+rand_int(-5,5),rand(1.5,2),rand_int(70,110),89,10,2);
	}
	yield;
	wait(179-frame/60);
	}
	}

task Bullet(x, y, v, dir, graphic, delay,check) 
	{
    	 let obj = Obj_Create(OBJ_SHOT);
	 let c = 0;
    	 Obj_SetPosition   (obj, x, y);
    	 Obj_SetSpeed      (obj, v);
    	 Obj_SetAngle      (obj, Obj_GetAngleToEnemy(obj));
	 if(check==1){
	Obj_SetAngle      (obj, Obj_GetAngleToPlayer(obj));
	}
	 if(check==2){
	Obj_SetAngle      (obj, Obj_GetAngleToCenter(obj));
	}
    	 ObjShot_SetGraphic(obj, graphic);
    	 ObjShot_SetDelay  (obj, delay);
	 ObjShot_SetBombResist(obj, true);
		 while(! Obj_BeDeleted(obj)) 
		{
		 yield;
		}
	}

task setboss{
	ascent(i in 0..125){
	SetY(GetY-1*sin(i+135));
	wait(1);
	}
	}

task movement{
	SetMovePosition03(GetCenterX+90, GetCenterY-100, 10, 1);
	wait(60);
	SetColor(0, 255, 255); 
	Concentration01(120);
	SetColor(155, 255, 0); 
	Concentration01(120);
	SetColor(255, 255, 255); 
	PlayDelaySE(CSD~"sound\charge3.wav",0);
	wait(120);
	corner;
	fire;
	fire2;
	fire3;
	fire4;
	loop{
	SetX(GetCenterX+90*cos(angle));
	SetY((GetCenterY-100)+30*sin(angle));
	angle = angle%360+0.5;
	yield;
	}
	}

task corner{
	loop{
	CreateShotA(0,GetClipMinX,rand_int(0,464),30);
	SetShotDataA(0,0,0,0,0,0,0,1);
	SetShotKillTime(0,0);
	FireShot(0);
	CreateShotA(0,GetClipMaxX,rand_int(0,464),30);
	SetShotDataA(0,0,0,0,0,0,0,1);
	SetShotKillTime(0,0);
	FireShot(0);
	CreateShotA(0,rand_int(0,416),GetClipMinY,30);
	SetShotDataA(0,0,0,0,0,0,0,1);
	SetShotKillTime(0,0);
	FireShot(0);
	CreateShotA(0,rand_int(0,416),GetClipMaxY,30);
	SetShotDataA(0,0,0,0,0,0,0,1);
	SetShotKillTime(0,0);
	FireShot(0);
	wait(1);
	yield;
	}
	}

task drawboat
	{
    	 let e = Obj_Create(OBJ_EFFECT);
	 let c = 0;
	 let cx = 0;
	 let cy = 0;
	 let change = 0;
    	 Obj_SetPosition   (e, GetClipMaxX, GetCenterY-100);
	 Obj_SetSpeed(e,6.5);
	 Obj_SetAngle(e,160);
	 ObjEffect_SetTexture(e,boat);
	ObjEffect_SetScale(e,1,1);
	 ObjEffect_SetRenderState(e,ALPHA);
	ObjEffect_SetLayer(e,1);
	 ObjEffect_SetPrimitiveType(e,PRIMITIVE_TRIANGLESTRIP);
	 ObjEffect_CreateVertex(e, 4);
	Obj_SetAlpha(e,255);
	ObjEffect_SetVertexXY(e,0,-94,-46);
	ObjEffect_SetVertexXY(e,1,94,-46);
	ObjEffect_SetVertexXY(e,2,-94,46);
	ObjEffect_SetVertexXY(e,3,94,46);
	ObjEffect_SetVertexUV(e,0,0,0);
	ObjEffect_SetVertexUV(e,1,188,0);
	ObjEffect_SetVertexUV(e,2,0,92);
	ObjEffect_SetVertexUV(e,3,188,92);
		 while(! Obj_BeDeleted(e)) 
		{
		c++;
		if(Obj_GetSpeed(e)>0.1){
		Obj_SetSpeed(e,Obj_GetSpeed(e)-0.1);
		}
		ObjEffect_SetAngle(e,0,0,5+3*sin(c*2));
		if(c==100){
		cx = Obj_GetX(e);
		cy = Obj_GetY(e);
		}
		if(c>90){
		Obj_SetPosition   (e, GetX-3, GetY+41);
		}
		 yield;
		}
	}

	function Obj_GetAngleToPlayer(obj) {
	return atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj));
	}
	function Obj_GetAngleToEnemy(obj) {
	return atan2(GetY - Obj_GetY(obj), GetX - Obj_GetX(obj));
	}
	function Obj_GetAngleToCenter(obj) {
	return atan2(GetCenterY - Obj_GetY(obj), GetCenterX - Obj_GetX(obj));
	}

task PlayDelaySE(se,delay){
        loop(delay){yield;}
        PlaySE(se);
}
	function wait(w){
		loop(w){yield;}
			}

}