#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spellcard 1"]
#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;
@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){SetScriptResult(800);}
	case(EV_REQUEST_TIMER){SetScriptResult(50);}
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_edorev.png");
DrawBoss_Task(Enemy);
name("Edorev Milton",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,0);	//Aura(enemy,spawn rate,r,g,b)			
wait(60);
//Spellcard
SE(CSD~"sfx/spell.wav");
Cutin01(3,0,0,CSD ~ "img/endo.png",0,0,700,1200);
Cutin03(2,0,0,"Ley del trueno -Explosion Electrica-");
SpellBGoverlay;
SpellBG1;
SpellBG2;
wait(90);
ObjEnemy_SetDamageRate(Enemy,20,5);
//Shots
let angul = 0;
let angul2 = 0;
let time = 180;
let radius = 30;
wait(80);
shot2;
loop{
if(ObjEnemy_GetInfo(Enemy, INFO_LIFE) == 0){break;}
angul = getAtanAngle(Enemy,Player);
 ascent(i in 0..40){
  SE(CSD~"sfx/tan00.wav");
  Sshot(EnemyX+radius*cos(angul+i*360/40),EnemyY+radius*sin(angul+i*360/40),2,angul+i*360/40,OVAL_YELLOW,0.5,time);
  wait(1);
  time-= 1;
  }
  time = 180;
  wait(180-40);
  
  wait(60);
  ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2+rand(80,-80), 140+rand(50,-50), 20,6);
wait(80);
  }
}
function angleToPlayer{
let angle=atan2(GetPlayerY-EnemyY,GetPlayerX-EnemyX);
return angle;
}

task Sshot(x,y,speed,angle,graphic,speedadd,frame){
ascent(i in 0..3){
 let a = CreateShotFA1(x,y,0,angle,graphic,10,true);
 ObjMove_AddPatternA1(a,frame,speed+i*speedadd,NO_CHANGE);
 }
}

task shot2{
loop{
SE(CSD~"sfx/Laser.wav");
 laser(rand(0,GetStgFrameWidth),0);
 wait(30);
 }
}

task laser(x,y){
let a = CreateCurveLaserA1(x,y,2,45,50,15,LIGHTNING,0);
ObjCrLaser_SetTipDecrement(a,0);
while(!Obj_IsDeleted(a)){
 ObjMove_AddPatternA2(a,0,5,90+45,-0.2,0,0);
 wait(30);
 ObjMove_AddPatternA2(a,0,5,45,-0.2,0,0);
 wait(30);
 }

}