#TouhouDanmakufu
#Title["Ghost Spirit 'Gagouji's Judgment'"]
#Text[spell4]
#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 imgExRumiaBack=GetCurrentScriptDirectory~"img\ExRumiaBack.png";
let CSD = GetCurrentScriptDirectory;
let ci = CSD ~ "system\NMM.png";
let cx=GetCenterX();
let frame = 0;
let framedelay = 0;
let angle = 0;
let gpx;
let gpy;
let switch = 1;
let Cut = GetCurrentScriptDirectory~"img\Toziko.png";
let ThisShot = GetCurrentScriptDirectory~"ZUN's shotdata.txt";
let l = CSD ~ "system\extras.png";

@Initialize
{
drawcircle(GetX,GetY+120,0,0);
CreateHitbox;
SetLife(850);
SetDamageRate(20,10);
SetTimer(60);
mainTask;

SetInvincibility(120);
SetMovePosition03(GetCenterX, GetCenterY-120, 0.1, 1.5);
//PlaySE(GetCurrentScriptDirectory~"stE4Boss2.wav");
PlaySE(GetCurrentScriptDirectory~"cardcall.wav");
LoadGraphic(ImgBoss);
LoadUserShotData(ThisShot);
LoadGraphic(Cut);
LoadGraphic(ci);
LoadGraphic(l);
SetScore(500000);
cutin("NAZRIN", "Ghost Spirit 'Gagouji's Judgment'", Cut, 0, 0, 288, 511);
SetTexture(ImgBoss);
SetGraphicRect(0, 0, 64, 64);
MagicCircle(true);
Aura;
//Border2(255,0,0);

}

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

@MainLoop 
{

SetCollisionA(GetX, GetY, 32);
SetCollisionB(GetX, GetY, 16);


frame++;

// Start Attack

yield;

// End Attack


// Start Boss Move


// End Boss Move



}

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

@DrawLoop
{
SetTexture(ImgBoss);
SetRenderState(ALPHA);
SetAlpha(255);
SetGraphicScale(1.1, 1.1);
drawboss;
DrawHitbox;


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

@BackGround
{
	drawbg;
}	

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

@Finalize
{
DeleteGraphic(ImgBoss);
		loop(25){
		CreateItem(ITEM_SCORE,GetX()+rand(-50,50),GetY()+rand(-20,20));
		}
        if(GotSpellCardBonus){
	PlaySE(CSD~"sound\GotBonus.wav");
	}
	else{
	PlaySE(CSD~"sound\SpellOff.wav");
	}
}

// main task
task mainTask{
	wait(90);
	fire;
	yield;
}

task fire{
	gpx = GetPlayerX;
	gpy = GetPlayerY;
	LocateEnemy;
	StartMapleConcentrate;
	PlayDelaySE(CSD~"sound\charge2.wav",0);
	wait(60);
	ascent(n in 0..120){
	if(n==60){
	SetMovePositionRandom01(rand(20,50),0,2,GetClipMinX+100,rand(150,160),GetClipMaxX-100,rand(100,150));
	}
	if(n%2==0){
	PlayDelaySE(CSD~"sound\shot1.wav",0);
	}
	angle = rand_int(0,59);
	ascent(i in 0..5){
	Bullet(gpx+(60+n*3)*cos(i*72+angle),gpy+(60+n*3)*sin(i*72+angle),0.1,rand_int(0,359),75,20,1);
	}
	wait(1);
	}
	wait(20);
	rapidfire;
	}

task fire2{
	gpx = GetPlayerX;
	gpy = GetPlayerY;
	LocateEnemy;
	StartMapleConcentrate;
	PlayDelaySE(CSD~"sound\charge2.wav",0);
	wait(60);
	descent(n in 0..150){
	if(n==90){
	SetMovePositionRandom01(rand(20,50),0,2,GetClipMinX+100,rand(150,160),GetClipMaxX-100,rand(100,150));
	}
	if(n%2==0){
	PlayDelaySE(CSD~"sound\shot1.wav",0);
	}
	angle = rand_int(0,59);
	ascent(i in 0..5){
	Bullet(gpx+(70+n*3)*cos(i*72+angle),gpy+(70+n*3)*sin(i*72+angle),0.1,rand_int(0,359),78,20,1);
	}
	wait(1);
	}
	wait(30);
	rapidfire2;
	}

task rapidfire{
	//angle = GetAngleToPlayer;
	ascent(n in 0..15){
	PlayDelaySE(CSD~"sound\shot4.wav",0);
	if(n%3==0){
	ascent(i in 0..10){
	ascent(j in 0..2){
	CreateShotA(0,GetX,GetY,10);
	SetShotDataA(0,0,2,i*36+j*2+n*5,0,0,0,94);
	FireShot(0);
	}
	}
	}
	ascent(a in 0..1){
	ascent(i in 0..9){
	CreateShotA(0,GetX-10,GetY-14,10);
	SetShotDataA(0,0,4,i*20+n*switch*0.8+5,0,-0.02,3.5,11);
	FireShot(0);
	}
	}
	wait(5);
	}
	wait(20);
	ascent(n in 0..12){
	PlayDelaySE(CSD~"sound\shot2.wav",0);
	gpx = rand_int(-100,100);
	gpy = rand_int(-50,50);
	ascent(a in 0..4){
	ascent(i in 0..6){
	CreateShot01(GetX+gpx,GetY+gpy,1+a/2,i*60+n*20,102,15);
	}
	}
	wait(5);
	}
	fire2;
	}

task rapidfire2{
	ascent(n in 0..6){
	ascent(i in 0..15){
	ascent(a in 0..6){
	CreateShot01(GetX,GetY,1+a/2,GetAngleToPlayer+i*24,95,10);
	}
	}
	PlayDelaySE(CSD~"sound\laser2.wav",0);
	ascent(i in 0..3){
	if(n%2==0){
	lightlaser(GetX-10, GetY-17, 0, GetAngleToPlayer-15+i*15,500,15, 14, 60,i);
	}
	else {
	lightlaser(GetX+10, GetY-17, 0, GetAngleToPlayer-15+i*15,500,15, 14, 60,i);
	}
	}
	wait(20);
	}
	switch = -switch;
	wait(30);
	fire;
	}

	   task lightlaser(x, y, v, dir,l,w, graphic, delay,check) {
	   let obj = Obj_Create(OBJ_LASER);
 	   let c = 0;
 	   Obj_SetPosition   (obj, x, y);
  	  Obj_SetSpeed      (obj, v);
 	   ObjShot_SetGraphic(obj, graphic);
  	  ObjShot_SetDelay  (obj, delay);
 	   Obj_SetAngle      (obj, dir);
 	   ObjLaser_SetLength(obj,l);
    	ObjLaser_SetWidth (obj,w);
    	ObjLaser_SetSource(obj,false);
	 Obj_SetAngle(obj,Obj_GetAngleToPlayer(obj)-15+check*15);
  	  while(! Obj_BeDeleted(obj)) {
		c++;
		ObjLaser_SetWidth (obj,rand_int(12,14));
		if(c>150){ObjShot_FadeDelete(obj);}
   	     yield;
	    }
	}

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, dir);
    	 ObjShot_SetGraphic(obj, graphic);
    	 ObjShot_SetDelay  (obj, delay);
	 ObjShot_SetBombResist(obj, true);
		 while(! Obj_BeDeleted(obj)) 
		{
		c++;
		if(c>=360){
		CreateShotA(0,Obj_GetX(obj),Obj_GetY(obj),10);
		SetShotDataA(0,0,0,0,0,0,0,graphic);
		SetShotKillTime(0,0);
		FireShot(0);
		Obj_Delete(obj);
		}
		 yield;
		}
	}

task light(x,y,v,dir,delay)
	{
    	 let e = Obj_Create(OBJ_EFFECT);
	 let c = rand_int(0,5);
	 let change = 0;
    	 Obj_SetPosition   (e, x, y);
	CreateShotA(0,x,y,delay);
	SetShotDataA(0,0,0,0,0,0,0,107);
	SetShotKillTime(0,0);
	FireShot(0);
	loop(delay){yield;}
	 Obj_SetCollisionToPlayer(e,true);
	 Obj_SetSpeed      (e, v);
    	 Obj_SetAngle      (e, dir);
	 ObjEffect_SetTexture(e,l);
	 ObjEffect_SetScale(e,0,0);
	 ObjEffect_SetRenderState(e,ADD);
	//ObjEffect_SetLayer(e,3);
	 ObjEffect_SetPrimitiveType(e,PRIMITIVE_TRIANGLESTRIP);
	 ObjEffect_CreateVertex(e, 4);
	ObjEffect_SetVertexXY(e,0,-16,-16);
	ObjEffect_SetVertexXY(e,1,16,-16);
	ObjEffect_SetVertexXY(e,2,-16,16);
	ObjEffect_SetVertexXY(e,3,16,16);
	//ObjEffect_SetVertexUV(e,0,0,56);
	//ObjEffect_SetVertexUV(e,1,32,56);
	//ObjEffect_SetVertexUV(e,2,0,88);
	//ObjEffect_SetVertexUV(e,3,32,88);
		 while(! Obj_BeDeleted(e)) 
		{
		SetCollisionB(Obj_GetX(e),Obj_GetY(e),10);
		c++;
		change+= 0.5;
		if(change<=1){
		ObjEffect_SetScale(e,change,change);
		}
		if(c>=0 && c<2){
			ObjEffect_SetVertexUV(e,0,0,56);
			ObjEffect_SetVertexUV(e,1,32,56);
			ObjEffect_SetVertexUV(e,2,0,88);
			ObjEffect_SetVertexUV(e,3,32,88);
			}
		else if(c>=2 && c<4){
			ObjEffect_SetVertexUV(e,0,32,56);
			ObjEffect_SetVertexUV(e,1,64,56);
			ObjEffect_SetVertexUV(e,2,32,88);
			ObjEffect_SetVertexUV(e,3,64,88);
			}
		else if(c>=4 && c<6){
			ObjEffect_SetVertexUV(e,0,64,56);
			ObjEffect_SetVertexUV(e,1,96,56);
			ObjEffect_SetVertexUV(e,2,64,88);
			ObjEffect_SetVertexUV(e,3,96,88);
			}
		else if (c>=6){c=0;}
		 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));
	}

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

}