#TouhouDanmakufu
#Title["Mononobe Scrolls 'Force Lightning'"]
#Text[spell3]
#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 angle2 = 0;
let gp;
let x;
let switch = 1;
let acc = 0.004;
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
{
drawcircle(GetX,GetY+120,0,0);
CreateHitbox;
SetLife(850);
SetDamageRate(20,10);
SetTimer(60);
mainTask;

SetInvincibility(180);
SetMovePosition03(GetCenterX, GetCenterY-150, 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);
LoadGraphic(test);
SetScore(500000);
cutin("NAZRIN", "Mononobe Scrolls 'Force Lightning'", 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(18){
		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{
	SetShotAutoDeleteClip(18,18,18,18);
	wait(90);
	fire;
	yield;
}

task fire{
	loop{
	if(GetLife>450){
	PlayDelaySE(CSD~"sound\charge2.wav",0);
	Concentration01(60);
	wait(60);
	if(switch==1){
	PlayDelaySE(CSD~"sound\laser4.wav",0);
	ascent(i in 0..12){
	if(i<5){
	PlayDelaySE(CSD~"sound\shot5.wav",30+i*5);
	}
	lightlaser(GetX+30*cos(i*30), GetY+30*sin(i*30), 0, i*30,50,10, 19, 0,0);
	}
	}
	else if(switch==-1){
	PlayDelaySE(CSD~"sound\laser4.wav",0);
	ascent(i in 0..12){
	if(i<5){
	PlayDelaySE(CSD~"sound\shot5.wav",30+i*5);
	}
	lightlaser(GetPlayerX+40*cos(-i*30), GetPlayerY+40*sin(-i*30), 0, -i*30,50,10, 18, 0,0);
	}
	}
	switch = -switch;
	yield;
	wait(120-frame/60);
	SetMovePositionRandom01(rand(20,50),0,2,GetClipMinX+100,rand(100,110),GetClipMaxX-100,rand(100,150));
	wait(120-frame/60);
	}
	else{
	DeleteEnemyShot(ALL);
	fire2;
	break;
	}
	}
	}

task fire2{
	acc = 0.007;
	SetMovePosition03(GetCenterX, GetCenterY-140, 5, 2);
	PlayDelaySE(CSD~"sound\Weird3.wav",0);
	Concentration01(120);
	SetInvincibility(120);
	wait(120);
	loop{
	ascent(i in 0..2){
	lightlaser(GetX+30*cos(angle2+i*180), GetY+30*sin(angle2+i*180), 0, angle2+i*180,50,10, 19, 0,0);
	}
	PlayDelaySE(CSD~"sound\laser4.wav",0);
	ascent(i in 0..5){
	PlayDelaySE(CSD~"sound\shot5.wav",30+i*5);
	}
	angle2 += 37;
	if(angle%185==0){
	SetMovePositionRandom01(rand(20,50),0,2,GetClipMinX+100,rand(100,110),GetClipMaxX-100,rand(100,150));
	}
	yield;
	wait(14);
	}
	}

	   task lightlaser(x, y, v, dir,l,w, graphic, delay,check) {
	   let obj = Obj_Create(OBJ_LASER);
 	   let c = 0;
	   let color = 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);
	if(graphic==19){color = 69;}
	if(graphic==18){color = 68;}
  	  while(! Obj_BeDeleted(obj)) {
		c++;
		if(c==5 && check<5){
		angle =rand_int(-15,15);
		lightlaser(ObjLaser_GetEndX(obj), ObjLaser_GetEndY(obj), 0, dir+angle,50,10, graphic, 0,check+1);
		Explosion01(ObjLaser_GetEndX(obj),ObjLaser_GetEndY(obj),5,0.02,60);
		}
		if(c>30){
		ascent(i in 0..4){
		CreateShotA(0,ObjLaser_GetEndX(obj),ObjLaser_GetEndY(obj),20);
		SetShotDataA(0,0,i/25,Obj_GetAngle(obj)+i*90+dir+45,0,acc,1.2,80);
		FireShot(0);
		}
		CreateShotA(0,ObjLaser_GetEndX(obj),ObjLaser_GetEndY(obj),20);
		SetShotDataA(0,0,0,Obj_GetAngleToEnemy(obj),0,acc,2,80);
		FireShot(0);
		ObjShot_FadeDelete(obj);
		}
   	     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;}
			}

}