let playerAngle=GetAngleToPlayer(GetPlayerObjectID);
let Player = GetPlayerObjectID;
let defchar = 1;
/*#include"./Obj_Charge.txt"
#include"./nuclear.txt"
#include"./Obj_LifeBar.txt"
#include"./Obj_MagicCircle.txt"
#include"./Obj_Aura.txt"
#include"./Obj_ShotEffect.txt"
#include"./Obj_Cutin.txt"
#include"./Obj_Timer.txt"*/
#include"./Effect_func.txt"
#include"./Obj_Cutin.txt"
// Draws the name of the character
task name(text,x,y){
InstallFont(GetCurrentScriptDirectory ~ "system/Russell Square Regular.ttf");
let objText = ObjText_Create();
ObjText_SetText(objText,text);
ObjText_SetFontType(objText,"Russell Square Regular");
ObjText_SetFontSize(objText, 12);
ObjText_SetFontBold(objText, true); //Negrita
ObjText_SetFontColorTop(objText, 0, 254, 0); //Color parte arriba
ObjText_SetFontColorBottom(objText, 0, 254, 0); // = parte abajo
ObjText_SetFontBorderType(objText, BORDER_FULL); //Borde, los otros son BORDER_FULL y BORDER_SHADOW
ObjText_SetFontBorderColor(objText,0, 0, 0); // color del borde
ObjText_SetFontBorderWidth(objText, 1); // ancho del borde
ObjText_SetMaxWidth(objText,348); //ancho max
ObjText_SetMaxHeight(objText,50); // alto max
Obj_SetRenderPriorityI(objText, 79); //prioridad
ObjRender_SetX(objText, x); //x
ObjRender_SetY(objText, y); //y
while(!Obj_IsDeleted(Enemy)){
yield; 
 }
Obj_Delete(objText);
}


function getAtanAngle(obj,obj2){
	let angle=atan2(ObjMove_GetY(obj2)-ObjMove_GetY(obj),ObjMove_GetX(obj2)-ObjMove_GetX(obj));
	return angle;
}


function SE(file){
LoadSound(file);
PlaySE(file);
}

//Ultima's Delay Shot Functions
       function CreateShotFA1(x,y,s,a,g,d,holdspeed){
              let obj = CreateShotA1(x,y,s,a,g,0);
            if(holdspeed){
                ObjMove_SetSpeed(obj,0);
            }
        ObjShot_SetIntersectionEnable(obj,false);
             fadeinshot(d,obj,s,holdspeed);
              return obj
              }
        task fadeinshot(d,obj,s,holdspeed){
         let alpha = 00;
         let scale = 2.4;
         let a = 255/d;
         let b = 1/d;
         let done = false;
         while(!done){
         alpha+=a;
         scale-=b;
         ObjRender_SetScaleXYZ(obj,scale,scale,1);
         if(scale <1){
         ObjRender_SetScaleXYZ(obj,1,1,1);
         scale = 1;
         }
         if(scale <=1&&alpha>=255){
         done = true;
         }

        // ObjRender_SetScaleXYZ(obj,1,1,1);
         ObjRender_SetAlpha(obj,alpha);
         yield;
         }
                        if(holdspeed){
                ObjMove_SetSpeed(obj,s);

            }
          ObjShot_SetIntersectionEnable(obj,true);

         }


       function CreateShotFA2(x,y,s,a,acc,ms,g,d,holdspeed){
              let obj = CreateShotA2(x,y,s,a,acc,ms,g,0);
            if(holdspeed){
                ObjMove_SetSpeed(obj,0);
                ObjMove_SetAcceleration(obj,0);
                ObjMove_SetMaxSpeed(obj,0);
            }
        ObjShot_SetIntersectionEnable(obj,false);
             fadeinshot2(d,obj,s,acc,ms,holdspeed);
              return obj
              }

         task fadeinshot2(d,obj,s,acc,ms,holdspeed){
         let alpha = 00;
         let scale = 2.4;
         let a = 255/d;
         let b = 1/d;
         let done = false;
         while(!done){
         alpha+=a;
         scale-=b;
         ObjRender_SetScaleXYZ(obj,scale,scale,1);
         if(scale <1){
         ObjRender_SetScaleXYZ(obj,1,1,1);
         scale = 1;
         }
         if(scale <=1&&alpha>=255){
         done = true;
         }

        // ObjRender_SetScaleXYZ(obj,1,1,1);
         ObjRender_SetAlpha(obj,alpha);
         yield;
         }
                        if(holdspeed){
                ObjMove_SetSpeed(obj,s);
                ObjMove_SetAcceleration(obj,acc);
                ObjMove_SetMaxSpeed(obj,ms);
            }
          ObjShot_SetIntersectionEnable(obj,true);

         }

         function GetX{
         return ObjMove_GetX(Enemy)
         }
         function GetY{
         return ObjMove_GetY(Enemy)
         }
         function GetCenterX{
         return GetStgFrameWidth/2
         }
         function GetCenterY{
         return GetStgFrameHeight/2
         }

//Shoot Shape Port!
task ShootShape(x, y, speed, angle, graphic, delay, corners, bullets){
  let Corners = [];
  let SideBullets = [];
  ascent(i in 0..corners){
    let obj = ObjShot_Create(OBJ_SHOT);
ObjShot_Regist(obj);
    Corners = Corners ~ [obj];
    
    ObjMove_SetPosition(obj, x, y);
    ObjMove_SetSpeed(obj, speed);
    ObjMove_SetAngle(obj, angle+i*(360/corners));
    ObjShot_SetGraphic(obj, graphic);
    ObjShot_SetDelay(obj, delay);
    
    ObjRender_SetAlpha(obj, 0);
    ObjShot_SetIntersectionEnable(obj, false);
    ObjShot_SetAutoDelete(obj, false);
  }
  
  ascent(i in 0..corners){
    if(i==corners-1){
      let Side = BulletLine(Corners[i], Corners[0]);
      SideBullets = SideBullets ~ [Side];
    }
    else {
      let Side = BulletLine(Corners[i], Corners[i+1]);
      SideBullets = SideBullets ~ [Side];
    }
  }
  
  while(length(SideBullets) > 0){
    descent(i in 0..length(SideBullets)){
      if(SideBullets[i] == [0]){
        SideBullets = erase(SideBullets, i);
      }
      else {
        let tempArray = SideBullets[i];
        descent(j in 1..length(tempArray)){
          if(Obj_IsDeleted(tempArray[j])){
            tempArray = erase(tempArray, j);
          }
        }
        SideBullets[i] = tempArray;
      }
    }
    yield;
  }
  
  ascent(i in 0..corners){
    Obj_Delete(Corners[i]);
  }
  
  function BulletLine(obj1, obj2){
    let SideBullets = [0];
    let DummyArray = [0];
    ascent(i in 0..bullets){
      let obj = ObjShot_Create(OBJ_SHOT);
ObjShot_Regist(obj);
      SideBullets = SideBullets ~ [obj];
      DummyArray = DummyArray ~ [0];
      
      ObjShot_SetGraphic(obj, graphic);
      ObjShot_SetDelay(obj, delay);
    }
    RealTask;
    return SideBullets;
    
    task RealTask {
      while(SideBullets != DummyArray){
        let x1 = ObjMove_GetX(obj1);
        let y1 = ObjMove_GetY(obj1);
        let x2 = ObjMove_GetX(obj2);
        let y2 = ObjMove_GetY(obj2);
        let distance = ((x1-x2)^2 + (y1-y2)^2)^0.5;
        let interval = distance/bullets;
        let angle = atan2(y2-y1, x2-x1);
        descent(i in 1..length(SideBullets)){
          if(Obj_IsDeleted(SideBullets[i])){
            SideBullets[i] = 0;
          }
          else {
            let xo = ObjMove_GetX(SideBullets[i]);
            let yo = ObjMove_GetY(SideBullets[i]);
            
            ObjMove_SetPosition(SideBullets[i], x1+cos(angle)*interval*i, y1+sin(angle)*interval*i);
            
            let xn = ObjMove_GetX(SideBullets[i]);
            let yn = ObjMove_GetY(SideBullets[i]);
            let newangle = atan2(yn-yo, xn-xo);
            
            ObjMove_SetAngle(SideBullets[i], newangle);
          }
        }
        yield;
      }
    }
  }
}

task SetDamageRateAtFrame(obj, shot,bomb,frame){//Talos
   ObjEnemy_SetDamageRate(obj, 0,0);
   let time=frame;
   while(IsPlayerSpellActive){ //Prevents Bomb Damage from previous attack
     time--;
     yield;
   }
   while(time>0){
     time--;
     yield;
   }
   ObjEnemy_SetDamageRate(obj,shot,bomb);
 }
 
task Alert(long){ //ph3 try to port
	let def		= ObjPrim_Create(OBJ_SPRITE_2D);
	let def2	= [ObjPrim_Create(OBJ_SPRITE_2D),
					ObjPrim_Create(OBJ_SPRITE_2D)];
	let img1	= GetCurrentScriptDirectory~"al00.png";
	let img2	= GetCurrentScriptDirectory~"al01.png";
	let alpha1	= 0;
	let alpha2	= 0;
	let move1	= 0;
	let moveA	= 0;
	let kill	= 0;
	let i		= 0;
	
	//LoadGraphic(img1);
	//LoadGraphic(img2);
	
	ObjRender_SetPosition(def,GetStgFrameWidth/2,250,0);
	ObjPrim_SetTexture(def,img1);
	ObjRender_SetBlendType(def,BLEND_ALPHA);
	ObjPrim_SetPrimitiveType(def,PRIMITIVE_TRIANGLESTRIP);
	Obj_SetRenderPriorityI(def,23);
	ObjRender_SetScaleXYZ(def,1,1,1);
	ObjPrim_SetVertexCount(def,4);
	ObjPrim_SetVertexPosition(def,0,128/-2,128/-2,0);
	ObjPrim_SetVertexPosition(def,1,128/-2,128/2,0);
	ObjPrim_SetVertexPosition(def,2,128/2,128/-2,0);
	ObjPrim_SetVertexPosition(def,3,128/2,128/2,0);
	ObjPrim_SetVertexUVT(def,0,0,0);
	ObjPrim_SetVertexUVT(def,1,0,128);
	ObjPrim_SetVertexUVT(def,2,128,0);
	ObjPrim_SetVertexUVT(def,3,128,128);
	ascent(i in 0..4){
	  ObjPrim_SetVertexColor(def,i,255,255,255);
	  ObjPrim_SetVertexAlpha(def,i,alpha1);	
	  }
	//ObjEffect_SetVertexColor(def,0,alpha1,255,255,255);
	//ObjEffect_SetVertexColor(def,1,alpha1,255,255,255);
	//ObjEffect_SetVertexColor(def,2,alpha1,255,255,255);
	//ObjEffect_SetVertexColor(def,3,alpha1,255,255,255);
	
	loop(2){
		ObjRender_SetPosition(def2[i],GetStgFrameWidth/2,250,0);
		ObjPrim_SetTexture(def2[i],img2);
		ObjRender_SetBlendType(def2[i],BLEND_ALPHA);
		ObjPrim_SetPrimitiveType(def2[i],PRIMITIVE_TRIANGLESTRIP);
		Obj_SetRenderPriorityI(def2[i],23);
		ObjRender_SetScaleXYZ(def2[i],1,1,1);
		ObjPrim_SetVertexCount(def2[i],4);
		ObjPrim_SetVertexPosition(def2[i],0,600/-2,244/-2,0);
		ObjPrim_SetVertexPosition(def2[i],1,600/-2,244/2,0);
		ObjPrim_SetVertexPosition(def2[i],2,600/2,244/-2,0);
		ObjPrim_SetVertexPosition(def2[i],3,600/2,244/2,0);
		ObjPrim_SetVertexUVT(def2[i],0,0,0);
		ObjPrim_SetVertexUVT(def2[i],1,0,244);
		ObjPrim_SetVertexUVT(def2[i],2,600,0);
		ObjPrim_SetVertexUVT(def2[i],3,600,244);
		//ObjEffect_SetVertexColor(def2[i],0,alpha2,255,255,255);
		//ObjEffect_SetVertexColor(def2[i],1,alpha2,255,255,255);
		//ObjEffect_SetVertexColor(def2[i],2,alpha2,255,255,255);
		//ObjEffect_SetVertexColor(def2[i],3,alpha2,255,255,255);
		ascent(j in 0..4){
	  ObjPrim_SetVertexColor(def2[i],j,255,255,255);
	  ObjPrim_SetVertexAlpha(def2[i],j,alpha2);	  
	}
		i++;
	}i=0;
	ObjRender_SetAngleXYZ(def2[0],0,0,0);
	ObjRender_SetAngleXYZ(def2[1],0,0,0);
	
	loop{
		kill++;
		loop(2){
			ObjPrim_SetVertexUVT(def2[i],0,0+kill,0);
			ObjPrim_SetVertexUVT(def2[i],1,0+kill,244);
			ObjPrim_SetVertexUVT(def2[i],2,600+kill,0);
			ObjPrim_SetVertexUVT(def2[i],3,600+kill,244);
			i++;
		}i=0;
		
		if(alpha1<255&&long>kill){alpha1+=15;}
		if(alpha2<255&&long>kill){alpha2+=15;}
		if(alpha1>0&&long<kill){alpha1-=15;}
		if(alpha2>0&&long<kill){alpha2-=15;}
		//ObjEffect_SetVertexColor(def,0,alpha1,255,255,255);
		//ObjEffect_SetVertexColor(def,1,alpha1,255,255,255);
		//ObjEffect_SetVertexColor(def,2,alpha1,255,255,255);
		//ObjEffect_SetVertexColor(def,3,alpha1,255,255,255);
		ascent(a in 0..4){
	  ObjPrim_SetVertexColor(def,a,255,255,255);
	  ObjPrim_SetVertexAlpha(def,a,alpha1);	  
	}
		loop(2){
			//ObjEffect_SetVertexColor(def2[i],0,alpha2,255,255,255);
			//ObjEffect_SetVertexColor(def2[i],1,alpha2,255,255,255);
			//ObjEffect_SetVertexColor(def2[i],2,alpha2,255,255,255);
			//ObjEffect_SetVertexColor(def2[i],3,alpha2,255,255,255);
			ascent(b in 0..4){
	  ObjPrim_SetVertexColor(def2[i],b,255,255,255);
	  ObjPrim_SetVertexAlpha(def2[i],b,alpha2);	  
	}
			i++;
		}i=0;
		
		if(alpha1<=0&&alpha2<=0&&long<kill){
			Obj_Delete(def);
			Obj_Delete(def2[0]);
			Obj_Delete(def2[1]);
			break;
		}
		
		yield;
	}
}
function CreateEnemyA1(x,y,type){
let pig = ObjEnemy_Create(OBJ_ENEMY);
let acto;
ObjEnemy_Regist(pig);
ObjMove_SetPosition(pig,x,y);

return pig;
}

/*Ver 0.8
!!This Charge effect is still slightly buggy and isn't a the finished product!!
Feel free to poke around and change things if you wish!

	This -> nTestCharge(x-position,y-position,frames,red-value,green-value,blue-value); <- This

This effect is just the same image stacked on top of each other spinning and shrinking.
This effect and image were created by neito, but you are free to use these as you please.

notes: long charge times make it look abit silly because the spinning speed is random and if it hits a high number
itll be spinning noticeably fast 
*/
/////////////////////////////////////////////////////////////////////////////

task nTestCharge(x,y,flame,r1,g1,b1) //This is the Task youll be using for the effect!!
{
	ascent(i in 0..5){
		lotemasLite(x+rand(-5,5),y+rand(-15,15),1*i,flame,r1,g1,b1,(1*i)/1); //Creates the Color Charge effect
		lotemas(x+rand(-5,5),y+rand(-15,15),1*i,flame,250,250,250,(1*i)); //Creates the Black charge effect
	}
	loop(flame-30){ //No dot effects will be spawned if the charge times is less then or equal to 30 frames.
		loop(rand(1,3)){
			oltahmas(x+rand(-5,5),y+rand(-5,5),30,r1,g1,b1); //Creates Dots of black and the designated colors randomly that fly in at the given position.
		}
		yield;
	}
}

task lotemas(x,y,Edscale,flame,r1,g1,b1,fractal)
{
	let imgEff = GetCurrentScriptDirectory~"./img/eltmas1.png";
	let scale = Edscale;
	let alpha = 0;
	let angle = rand(0,360);
	let angleR = rand(-3,3); 

	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_SUBTRACT);
	ObjPrim_SetTexture(obj, imgEff);
	Obj_SetRenderPriority(obj, 0.23);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 256, 256);
	ObjSprite2D_SetDestRect(obj, -128, -128, 128, 128);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetPosition(obj, x, y, 0);
	ascent(i in 0 .. flame){
		//if(Obj_IsDeleted(bossObj)==true){Obj_Delete(obj);}
		ObjRender_SetColor(obj, alpha*r1, alpha*g1, alpha*b1);
		ObjRender_SetScaleXYZ(obj,scale,scale,1.0);
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		angle+=angleR;
		scale-= Edscale/flame;
		alpha+= (1/fractal)/(flame/Edscale);
		yield;
	}
	Obj_Delete(obj);
}
task lotemasLite(x,y,Edscale,flame,r1,g1,b1,fractal)
{
	let imgEff = GetCurrentScriptDirectory~"./img/eltmas1.png";
	let scale = Edscale;
	let alpha = 0;
	let angle = rand(0,360);
	let angleR = rand(-3,3); 

	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	ObjPrim_SetTexture(obj, imgEff);
	Obj_SetRenderPriority(obj, 0.23);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 256, 256);
	ObjSprite2D_SetDestRect(obj, -128, -128, 128, 128);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetPosition(obj, x, y, 0);
	ascent(i in 0 .. flame){
		//if(Obj_IsDeleted(bossObj)==true){Obj_Delete(obj);}
		ObjRender_SetColor(obj, alpha*r1, alpha*g1, alpha*b1);
		ObjRender_SetScaleXYZ(obj,scale,scale,1.0);
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		angle+=angleR;
		scale-= Edscale/flame;
		alpha+= (1/fractal)/(flame/Edscale);
		yield;
	}
	Obj_Delete(obj);
}
task oltahmas(x,y,flame,r1,g1,b1)
{
	let imgEff = GetCurrentScriptDirectory~"./img/dot.png";
	let scaleStandard = rand(0.25,1);
	let scale = scaleStandard;
	let alpha = 0;
	let alphaDun = rand(0.1,1);

	let accer = rand(0.3, 0.4);
	let angle = rand(0, 360);
	let r = accer*(flame^2)/2;
	x += r*cos(angle);
	y += r*sin(angle);

	let ToF=0;
	ToF=rand(-1,1);

	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	if(ToF>0){
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	}else if(ToF<0){
		ObjRender_SetBlendType(obj, BLEND_SUBTRACT);
	}
	ObjPrim_SetTexture(obj, imgEff);
	Obj_SetRenderPriority(obj, 0.23);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
	ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
	ObjRender_SetPosition(obj, x, y, 0);
	ascent(i in 0 .. flame){
		ObjRender_SetPosition(obj, x, y, 0);
		ObjRender_SetAlpha(obj, 255*alpha);
		//if(Obj_IsDeleted(bossObj)==true){Obj_Delete(obj);}
		ObjRender_SetColor(obj, alpha*r1, alpha*g1, alpha*b1);
		ObjRender_SetScaleXYZ(obj,scale,scale,1.0);
		x -= accer*i*cos(angle);
		y -= accer*i*sin(angle);
		if(alpha<alphaDun){
			alpha+= alphaDun/5;
		}
		scale-=scaleStandard/flame;
		yield;
	}
	Obj_Delete(obj);
}
/////////////////////////////////////////////////////////////////////////////
function GetDistance(x1,y1,x2,y2){
     return(((x2-x1)^2+(y2-y1)^2)^(1/2));
 }
 