#TouhouDanmakufu
#Title["Thunder Arrow  'Gagouji's Storm'"]
#Text[spell1]
#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 gp;
let x;
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(700);
SetDamageRate(20,10);
SetTimer(60);
mainTask;

SetInvincibility(120);
SetMovePosition03(GetCenterX, GetCenterY-120, 0.1, 1.5);
//ConcentrationEx0(120,255,0,0,true);
//PlaySE(GetCurrentScriptDirectory~"stE4Boss2.wav");
PlaySE(GetCurrentScriptDirectory~"cardcall.wav");
LoadGraphic(ImgBoss);
LoadUserShotData(ThisShot);
LoadGraphic(Cut);
LoadGraphic(ci);
LoadGraphic(l);
SetScore(500000);
cutin("NAZRIN", "Thunder Arrow  'Gagouji's Storm'", 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(12){
		CreateItem(ITEM_SCORE,GetX()+rand(-40,40),GetY()+rand(-20,20));
		}
        if(GotSpellCardBonus){
	PlaySE(CSD~"sound\GotBonus.wav");
	}
	else{
	PlaySE(CSD~"sound\SpellOff.wav");
	}
}

// main task
task mainTask{
	wait(90);
	PlayDelaySE(CSD~"sound\charge2.wav",0);
	LocateEnemy;
	StartMapleConcentrate;
	wait(60);
	loop(70){
	MapleExplode(8, -0.04);
	}
	PlayDelaySE(CSD~"sound\stars.wav",5);
	fire;
	loopfire;
	yield;
}

task fire{
	wait(5);
	loop(4){
	ascent(i in 0..12){
	Bullet(GetX,GetY,4.5,i*30+angle,94,10,0);
	}
	angle += 13;
	if(angle%26==0){
	SetMovePositionRandom01(rand(20,50),0,2,GetClipMinX+100,rand(150,160),GetClipMaxX-100,rand(100,150));
	}
	framedelay++;
	wait(120);
	}
	wait(30);
	fireend;
	}

task loopfire{
	let angle2 = 0;
	wait(10);
	loop(60){
	PlayDelaySE(CSD~"sound\shot1.wav",0);
	ascent(i in 0..3){
	CreateShotA(0,GetX+100,GetY,10);
	SetShotDataA(0,0,2.8,i*2+angle2,0,0,0,93);
	FireShot(0);
	CreateShotA(0,GetX+100,GetY,10);
	SetShotDataA(0,0,2.8,i*2+angle2+180,0,0,0,93);
	FireShot(0);
	}
	angle2 += 35;
	wait(4);
	PlayDelaySE(CSD~"sound\shot1.wav",0);
	ascent(i in 0..3){
	CreateShotA(0,GetX-100,GetY,10);
	SetShotDataA(0,0,2.8,i*2-angle2+135,0,0,0,93);
	FireShot(0);
	CreateShotA(0,GetX-100,GetY,10);
	SetShotDataA(0,0,2.8,i*2-angle2+135+180,0,0,0,93);
	FireShot(0);
	}
	wait(4);
	}
	}

task fireend{
	ascent(n in 0..3){ 
	PlayDelaySE(CSD~"sound\shot2.wav",0);
	ascent(i in 0..15){
	CreateShotA(0,GetX,GetY,10);
	SetShotDataA(0,0,2.5,i*24+n*12,0,0,0,27);
	FireShot(0);
	}
	ascent(i in 0..8){
	CreateShotA(0,GetX,GetY,10);
	SetShotDataA(0,0,3.5,i*45+n*22.5,0,0,0,26);
	FireShot(0);
	}
	wait(30);
	}
	PlayDelaySE(CSD~"sound\charge2.wav",0);
	LocateEnemy;
	StartMapleConcentrate;
	wait(60);
	loop(70){
	MapleExplode(8, -0.04);
	}
	PlayDelaySE(CSD~"sound\stars.wav",5);
	fire;
	loopfire;
	}

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)) 
		{
		/*if(Obj_GetY(obj)>=GetClipMaxY || Obj_GetY(obj) <= GetClipMinY){
		ascent(i in 0..7){
		follower(Obj_GetX(obj),Obj_GetY(obj),4,-Obj_GetAngle(obj),95,5+i*10,0);
		}
		Obj_Delete(obj);
		}
		else{*/
		ascent(i in 0..8){
		//PlayDelaySE(CSD~"sound\shot1.wav",5+i*5);
		follower(Obj_GetX(obj),Obj_GetY(obj),4,180-Obj_GetAngle(obj),95,5+i*8,0);
		}
		Obj_Delete(obj);
		 yield;
		}
	}

task follower(x, y, v, dir, graphic, delay,check) 
	{
    	 let obj = Obj_Create(OBJ_SHOT);
	 let c = 0;
	 let time = 0;
	 let count = 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);
	 wait(delay);
		 while(! Obj_BeDeleted(obj)) 
		{
		c++;
		if(c<30){
		Obj_SetAngle(obj,dir-45);
		Obj_SetSpeed      (obj, Obj_GetSpeed(obj)-0.13);
		}
		if(c==30){
		Obj_SetSpeed      (obj, v);
		}
		else if(c>=30 && c<60){
		Obj_SetAngle(obj,dir+45);
		Obj_SetSpeed      (obj, Obj_GetSpeed(obj)-0.13);
		}
		else if(c==60){
		time++;
		Obj_SetSpeed      (obj, v);
		c=0;
		}
		 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;}
			}

}