#TouhouDanmakufu
#Title["Ghost Sign 'Soka Servants'"]
#Text[spell2]
#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\daiyousei11.png";
let ThisShot = GetCurrentScriptDirectory~"ZUN's shotdata.txt";
let l = CSD ~ "system\extras.png";

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

SetInvincibility(300);
SetMovePosition03(GetCenterX, GetCenterY-120, 0.1, 2);
//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", "Ghost Sign 'Soka Servants'", Cut, 0, 0, 325, 488);
SetTexture(ImgBoss);
SetGraphicRect(0, 0, 64, 64);
MagicCircle(true);
Aura;
//Border2(255,0,0);

}

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

@MainLoop 
{

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


frame++;

framedelay = 30*sin(frame)+30;

// 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(15){
		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);
	fire;
	wait(205);
	PlayDelaySE(CSD~"sound\charge2.wav",0);
	LocateEnemy;
	StartMapleConcentrate;
	wait(60);
	loop(70){
	MapleExplode(8, -0.04);
	}
	PlayDelaySE(CSD~"sound\stars.wav",5);
	wait(330);
	laser;
	yield;
}

task fire{
	ascent(n in 0..8){
	PlaySE(CSD~"sound\fam.wav");
	CreateEnemyFromScript("starFam",GetX,GetY,0,0,n);
	wait(10);
	}
	}

task laser{
	loop{
	wait(30);
	SetMovePositionRandom01(rand(20,50),0,2,GetClipMinX+100,rand(150,160),GetClipMaxX-100,rand(100,150));
	Concentration01(30);
	wait(30);
	ascent(i in 0..5){
	ascent(a in 0..6){
	CreateShotA(0,GetX,GetY,10);
	SetShotDataA(0,0,1+a/2,GetAngleToPlayer+i*72,0,0,0,84);
	FireShot(0);
	}
	}
	wait(360);
	}
	}

sub spawn{
	PlaySE(CSD~"sound\fam.wav");
	CreateEnemyFromScript("starFam",GetX,GetY,0,0,0);
	 }

	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_GetAngleToPoint(obj,x,y) {
	return atan2(y - Obj_GetY(obj), x - Obj_GetX(obj));
	}

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

}

script_enemy starFam{
	
	let ThisShot = GetCurrentScriptDirectory~"ZUN's shotdata.txt";
	let CSD = GetCurrentScriptDirectory;
	let imgFam = CSD ~ "system\purple.png";
	let s = 0;
	let switch = 1;
	let count = 0;
	let r = 120;
	let famangle = GetArgument;
	let spin = 0;
@Initialize{
	LoadUserShotData(ThisShot);
	SetLife(1000);
	LoadGraphic(imgFam);
	maintask;
}

@MainLoop{

//SetCollisionA(GetX, GetY, 24);
SetCollisionB(GetX, GetY, 18);

yield;


}
	
@DrawLoop{
	SetTexture(imgFam);
	SetRenderState(ADD);
	if(s<=1){s+= 0.02;}	
	SetGraphicScale(s/5.5,s/5.5);
	SetGraphicAngle(0,0,spin);
	SetGraphicRect(0,0,400,400);
	DrawGraphic(GetX,GetY);
	spin -= 1;

}

@Finalize{

	//DeleteGraphic(imgFam);

}

task maintask{
	movement;
	wait(300+famangle*7);
	fire;
	yield;
	}

task fire{
	loop{
	if(count<2){
	PlaySE(CSD~"sound\shot1.wav");
	ascent(i in 0..1){
	CreateShotA(0,GetX,GetY,20);
	SetShotDataA(0,0,0,	GetAngleToPlayer,0,0,0.6,91);
	SetShotDataA(0,60,0,NULL,0,0.01,0.6,91);
	FireShot(0);
	}
	}
	wait(135);
	}
	}

task tail{
	ascent(n in 0..80){
	if(famangle%1==0){
	tailbullet(GetX, GetY, 1*switch, 180, 4, 10,1);
	}
	/*else{
	tailbullet(GetX, GetY, 1*switch, 0, 4, 10,1);
	}*/
	yield;
	wait(2);
	}
	}

task movement{
	if(famangle%2!=0){switch=-1;}
	SetMovePosition03(GetCenterX-165*switch, GetCenterY+famangle*55-200, 10, 2+famangle/5);
	wait(270-famangle*10);
	let more =0;
	loop{
	tail;
	if(switch==1){
	SetMovePosition03(GetCenterX+165*switch, GetCenterY+famangle*50-160, 10, 2.2);
	}
	else if(switch==-1){
	SetMovePosition03(GetCenterX+165*switch, GetCenterY+famangle*55-200, 10, 2.2);
	}
	wait(180);
	count++;
	switch = -switch;
	if(count==2){
	if(famangle==0){PlaySE(CSD~"sound\shot1.wav");}
	Concentration01(30);
	wait(30);
	if(famangle==0){PlaySE(CSD~"sound\bright.wav");}
	ascent(i in 0..4){
	ascent(a in 0..5+more){
	CreateShotA(0,GetX,GetY,10);
	SetShotDataA(0,0,0.7+a/3,i*90+GetAngleToPlayer+famangle*10,0,0,0,4);
	FireShot(0);
	}
	}
	wait(30);
	count = 0;
	more++;
	}
	}
	}

task tailbullet(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)) 
		{
		ObjShot_FadeDelete(obj);
		 yield;
		}
	}

	function wait(w){
		loop(w){yield;}
			}

}