#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spellcard 6"]
#Text[""]
#System[".\system\Default_System.txt"]

#include"./lib/AllStarShot_Const.dnh"
#include"./lib/Functions.txt"
#include"./BGTask.txt"
#include"./Animation_lib.txt"
let Enemy = ID_INVALID;
let EnemyX = 0;
let EnemyY = 0;
let yuyuX = 0;
let yuyuY = 0;
let yuyuko = 0;
//those Min and Max x y are taken from Shadow's Raimoo, Edited for full screen by Me(i am too honest :))
//if(ObjEnemy_GetInfo(Enemy, INFO_LIFE) == 0){break;}  my god "function"
let MinX = 0;
let MaxX = 640;
let MinY = 0;
let MaxY = GetStgFrameHeight*1.8;
let eimg = CSD~"Letra.png";
@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){SetScriptResult(1900);}
	case(EV_REQUEST_TIMER){SetScriptResult(70);}
case(EV_REQUEST_SPELL_SCORE){SetScriptResult(90000);}
}

@Initialize{
	Enemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(Enemy);
//SetShotAutoDeleteClip(50,50,50,50);
SetAutoDeleteObject(true);
ObjEnemy_SetDamageRate(Enemy,0,0);
DrawTimer03(1);
InitializeAction(Enemy,GetCurrentScriptDirectory ~"img/dot_mogel.png");
DrawBoss_Task(Enemy);
name("Mogel",0,30);
ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	
	BossActions;
}

task DrawPig{
ObjPrim_SetTexture(Enemy,GetCurrentScriptDirectory~"img/pig_sheet.png");
ObjSprite2D_SetSourceRect(Enemy,405,0,540,145);
ObjSprite2D_SetDestCenter(Enemy);
}

@MainLoop{
	yield;
}

task BossActions{
ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2, 140, 20,6);
//SetAction(Enemy,ACT_MOVE_R,100);
//wait(120);
	ShootBullets;
//Texto;
	while(ObjEnemy_GetInfo(Enemy, INFO_LIFE) > 0){
		EnemyX = ObjMove_GetX(Enemy);
		EnemyY = ObjMove_GetY(Enemy);
		ObjEnemy_SetIntersectionCircleToShot(Enemy, EnemyX, EnemyY, 20);
		ObjEnemy_SetIntersectionCircleToPlayer(Enemy, EnemyX, EnemyY, 20);
		yield;
	}
DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(Enemy);
	//loop(60){yield;}
	CloseScript(GetOwnScriptID);
}
function wait(t){loop(t){yield;}}

task ShootBullets{
//Preparation
MagicCircleEx2(Enemy,false); //Magic Circle, false doesnt contain opening, but loads the last script's pos and angle
LifeCircleBar_A(Enemy,0.5,0.0,false);//Active Lifebar part(enemy,usable part,unusable part(empity),opening)
LifeCircleBar_B(Enemy,0.0,0.0,false);//Inactive LB part
LifeCircleBar_Marker(Enemy,0.5);//Marker
Aura1(Enemy,8,128,128,128);	//Aura(enemy,spawn rate,r,g,b)			
wait(60);
//Spellcard
SE(CSD~"sfx/spell.wav");
Cutin01(3,0,0,CSD ~ "img/mogel.png",0,0,700,1200);
Cutin03(2,0,0,"Abecedario -A,B,C-");
SpellBGoverlay;
SpellBG3;
wait(90);
ObjEnemy_SetDamageRate(Enemy,60,5);
//Shots
let angul = 0;
let angul2 = 180;
let time = 180;
let radius = 30;
shoot2;
wait(120);
loop{
if(ObjEnemy_GetInfo(Enemy, INFO_LIFE) == 0){break;}
SE(CSD~"sfx/tan04.wav");
Explod(EnemyX,EnemyY,0);
Explod(EnemyX,EnemyY,180);
wait(180);
  }
}
function angleToPlayer{
let angle=atan2(GetPlayerY-EnemyY,GetPlayerX-EnemyX);
return angle;
}

task Explod(x,y,angle){
let e = CreateShotFA2(x,y,4,angle,-0.1,0,ORB_GRAY,10,true);
ObjShot_SetSpellResist(e,true);
wait(60);
let spdadd = 2.5;
let angleadd= 0;
let angleadd2= 0;
ascent(i in 0..8){
 CreateShotFA1(ObjMove_GetX(e),ObjMove_GetY(e),2+spdadd,getAtanAngle(e,Player)+angleadd,OVAL_GRAY,10,false);
 CreateShotFA1(ObjMove_GetX(e),ObjMove_GetY(e),2+spdadd,getAtanAngle(e,Player)-angleadd,OVAL_GRAY,10,false);
 angleadd+= 3;
 spdadd-= 0.3;
 }
 SE(CSD~"sfx/nuclear.wav");
 NuclearShot_Negative(3,ObjMove_GetX(e),ObjMove_GetY(e),0,0,0.7,0,0,"WHITE",120,true);
 Obj_Delete(e);
}




task shoot2{

 ShootFamiliars;
ShootFamiliars2;
 wait(180);
 
}

task ShootFamiliars{
let enemies = [CreateEnemyA1(EnemyX,EnemyY,0),CreateEnemyA1(EnemyX,EnemyY,0),CreateEnemyA1(EnemyX,EnemyY,0)];
let radius = 70; //radio en el cual cada slave girará
let angle = 0; //angulo inicial

ObjPrim_SetTexture(enemies[0],eimg);
ObjSprite2D_SetSourceRect(enemies[0], 0, 0, 23, 33);  // A Graphic
ObjSprite2D_SetDestCenter(enemies[0]);

ObjPrim_SetTexture(enemies[1],eimg);
ObjSprite2D_SetSourceRect(enemies[1], 25, 0, 50, 33);  // B Graphic
ObjSprite2D_SetDestCenter(enemies[1]);

ObjPrim_SetTexture(enemies[2],eimg);
ObjSprite2D_SetSourceRect(enemies[2], 50, 0, 75, 33);  // C Graphic
ObjSprite2D_SetDestCenter(enemies[2]);

ascent(i in 0..3){
 ObjMove_SetDestAtWeight(enemies[i],EnemyX+radius*cos(angle+i*120),EnemyY+radius*sin(angle+i*120),3,10);
 }
 wait(60);
 ascent(e in 0..3){
  Bshot(enemies[e],angle+e*120);
  }
  
 loop{
  ascent(o in 0..3){
  ObjMove_SetPosition(enemies[o],EnemyX+radius*cos(angle+o*120),EnemyY+radius*sin(angle+o*120))
   }
   //radius =70+sin(angle/2)*70; 
   angle+= 3.123;
   wait(1);
  }
}

task ShootFamiliars2{
let enemies = [CreateEnemyA1(EnemyX,EnemyY,0),CreateEnemyA1(EnemyX,EnemyY,0),CreateEnemyA1(EnemyX,EnemyY,0)];
let radius = 70; //radio en el cual cada slave girará
let angle = 60; //angulo inicial

ObjPrim_SetTexture(enemies[0],eimg);
ObjSprite2D_SetSourceRect(enemies[0], 0, 0, 23, 33);  // A Graphic
ObjSprite2D_SetDestCenter(enemies[0]);

ObjPrim_SetTexture(enemies[1],eimg);
ObjSprite2D_SetSourceRect(enemies[1], 25, 0, 50, 33);  // B Graphic
ObjSprite2D_SetDestCenter(enemies[1]);

ObjPrim_SetTexture(enemies[2],eimg);
ObjSprite2D_SetSourceRect(enemies[2], 50, 0, 75, 33);  // C Graphic
ObjSprite2D_SetDestCenter(enemies[2]);

ascent(i in 0..3){
 ObjMove_SetDestAtWeight(enemies[i],EnemyX+radius*cos(angle+i*120),EnemyY+radius*sin(angle+i*120),3,10);
 }
 wait(60);
 ascent(e in 0..3){
  Bshot2(enemies[e],angle+e*120);
  }
  
 loop{
  ascent(o in 0..3){
  ObjMove_SetPosition(enemies[o],EnemyX+radius*cos(angle+o*120),EnemyY+radius*sin(angle+o*120))
   }
   //radius =70-sin(angle/2)*70; 
   angle-= 3.123;
   wait(1);
  }
}

task Bshot(obj,angle){
let angadd = 0;
loop{
let SpdAdd = 0;
 loop(6){
  let a=CreateShotFA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,angle+angadd,0.05,2+SpdAdd,PELLET_GRAY,10,true);
  angadd+=3.123;
  SpdAdd+=0.2;
  SE(CSD~"sfx/tan01.wav");
  wait(1);
  }
  angadd+= (3*16);
 wait(16);
 }
}

task Bshot2(obj,angle){
let angadd = 0;
loop{
let SpdAdd = 0;
 loop(6){
  let a=CreateShotFA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,angle+angadd,0.05,2+SpdAdd,PELLET_GRAY,10,true);
  ObjRender_SetColorHSV(a,0,0,0);
  
  angadd-=3.123;
  SpdAdd+=0.2;
  wait(1);
  }
  angadd-= (3*16);
 wait(16);
 }
}