#include "data/scripts/library/spawn.h"

void keyshoot(void Shot, float dx, float dy, float Vx, float Vy, int Time)
{// Shoot projectile only when attack button is held
    void self = getlocalvar("self");
    int iPIndex = getentityproperty(self,"playerindex"); //Get player index
    int TPhase = getentityvar(self, 0);

    if (playerkeys(iPIndex, 0, "attack") && !(playerkeys(iPIndex, 0, "attack2") ) ){
      if(TPhase != NULL() && TPhase <= openborvariant("elapsed_time") - 2*Time){
        setentityvar(self, 0, NULL());
        TPhase = NULL();
      }
      if (TPhase == NULL()){
        shoot(Shot, dx, dy, 0);
        setentityvar(self, 0, openborvariant("elapsed_time"));
      }
    }
}

void keymissile(void Shot, float dx, float dy, float Vx, float Vy, int Time)
{// Shoot projectile only when attack button is held
    void self = getlocalvar("self");
    int iPIndex = getentityproperty(self,"playerindex"); //Get player index
    int TPhase = getentityvar(self, 1);

    if (playerkeys(iPIndex, 0, "attack2") && !(playerkeys(iPIndex, 0, "attack") ) ){
      if(TPhase != NULL() && TPhase <= openborvariant("elapsed_time") - 2*Time){
        setentityvar(self, 1, NULL());
        TPhase = NULL();
      }
      if (TPhase == NULL()){
        shooter2(Shot, dx, dy, 0, Vx, Vy, 0);
        setentityvar(self, 1, openborvariant("elapsed_time"));
      }
    }
}

void keyshoot2(void Shot, float dx, float dy, float Vx, float Vy, int Time)
{// Shoot projectile only when attack button is held
    void self = getlocalvar("self");
    int iPIndex = getentityproperty(self,"playerindex"); //Get player index
    int TPhase = getentityvar(self, 0);

    if (playerkeys(iPIndex, 0, "attack") ){
      if(TPhase != NULL() && TPhase <= openborvariant("elapsed_time") - 2*Time){
        setentityvar(self, 0, NULL());
        TPhase = NULL();
      }
      if (TPhase == NULL()){
        if(playerkeys(iPIndex, 0, "moveup")){// Up is pressed?
          Vy = Vy;
        } else if(playerkeys(iPIndex, 0, "movedown")){// Down is pressed?
          Vy = -Vy;
        } else {
          Vy = 0;
        }
        shooter2(Shot, dx, dy, 0, Vx, Vy, 0);
        setentityvar(self, 0, openborvariant("elapsed_time"));
      }
    }
}

void keybomb(void Shot, float dx, float dy, float Vx, float Vy, int Time)
{// Shoot bombs only when attack2 button is held and there's enough bomb
    void self = getlocalvar("self");
    int iPIndex = getentityproperty(self,"playerindex"); //Get player index
    int TPhase = getentityvar(self, 1);
    int Bombs = getglobalvar("Bomb");

    if (Bombs == NULL()){
      Bombs=30;
      setglobalvar("Bomb", Bombs);
    }

    if (playerkeys(iPIndex, 0, "attack2") && Bombs>0){
      if(TPhase != NULL() && TPhase <= openborvariant("elapsed_time") - 2*Time){
        setentityvar(self, 1, NULL());
        TPhase = NULL();
      }
      if (TPhase == NULL()){
        shooter2(Shot, dx, dy, 0, Vx, Vy, 0);
        setentityvar(self, 1, openborvariant("elapsed_time"));
        setglobalvar("Bomb", Bombs-1);
      }
    }
}

void keyspeed(float Vx, float MinVx, float MaxVx)
{// Increases x speed if forward button is held until maximum speed is reached
// Also decreases x speed if forward button is not held until minimum speed is reached
// Vx : Speed increment/decrement
// MinVx: Minimum speed
// MaxVx: Maximum speed
    void self = getlocalvar("self");
    int iPIndex = getentityproperty(self,"playerindex");
    int dir = getentityproperty(self,"direction");
    int Sx = getentityproperty(self,"xdir"); // Get current x velocity

    if(dir==0){
      if(Sx > -MaxVx && playerkeys(iPIndex, 0, "moveleft")){
        changeentityproperty(self, "velocity", Sx-Vx);
      } else if(Sx < -MinVx && !playerkeys(iPIndex, 0, "moveleft")){
        changeentityproperty(self, "velocity", Sx+Vx);
      }
    } else {
      if(Sx < MaxVx && playerkeys(iPIndex, 0, "moveright")){
        changeentityproperty(self, "velocity", Sx+Vx);
      } else if(Sx > MinVx && !playerkeys(iPIndex, 0, "moveright")){
        changeentityproperty(self, "velocity", Sx-Vx);
      }
    }
}

void beidle()
{// Go to IDLE animation!
    void self = getlocalvar("self");

    setidle(self, openborconstant("ANI_IDLE"));
}

void dasher( float Vx, float Vy, float Vz )
{// Dash with desired speed!
    void self = getlocalvar("self");
    int dir = getentityproperty(self,"direction");

    if(dir==0){ // Facing left?
      Vx = -Vx ;
    }

    changeentityproperty(self, "velocity", Vx, Vz, Vy); //Move!
}

void flip()
{// Flip to opposite direction
    void self = getlocalvar("self");
    int dir = getentityproperty(self,"direction");

    if(dir==0){ // Facing left?
      changeentityproperty(self, "direction", 1);
    } else {
      changeentityproperty(self, "direction", 0);
    }
}

void degravity(int Ratio)
{// Changes antigravity effect
    void self = getlocalvar("self");
    changeentityproperty(self, "antigravity", Ratio);
}

void attack0(void Ani)
{// Attack interruption
    void self = getlocalvar("self");

    performattack(self, openborconstant(Ani)); //Attack!
}

void shooterDmg(void vName, float fX, float fY, float fZ, float Vx, float Vy, float Vz, float Factor)
{//Spawns projectile next to caller
 //Only spawns if health is below limit (factor x max HP)
 //vName: Model name of entity to be spawned in
 //fX: X location adjustment
 //fZ: Y location adjustment
 //fY: Z location adjustment
 //Vx: X speed
 //Vy: Y speed
 //Vz: Z speed
 //Factor: Health Ratio

   void self = getlocalvar("self"); //Get calling entity
   int MHealth = getentityproperty(self, "maxhealth");
   int Health = getentityproperty(self, "health");
   void vSpawn; //Spawn object.

   if(Health < Factor*MHealth){ 
     vSpawn = shooter2(vName, fX, fY, fZ, Vx, Vy, Vz);
   }
}
