#include_function "lib\Ikareimu\System\Polarity.dnh"
/******************************************
* Variables                               *
******************************************/

let ACTIVE = 1;
let INACTIVE = 2;
let CHARGING = 3;

let imgShield = "lib\Ikareimu\img\shield.png";
let ShieldAlpha = 255;
let ShieldAlphaChange = -1;
let ShieldState = ACTIVE;
let ShieldTimer = 0;
let ShieldColor;

let ShieldTriggerKey = "c";
let ShieldShotCounter = 0;
let ShieldSlowCounter = 0;

/******************************************
* Public Functions                        *
******************************************/

sub ShieldManager_Initialize {
  let sstate = GetCommonDataDefault("ShieldState", 0);
  if(sstate == 0){
    SetCommonData("ShieldState", ACTIVE);
  }
  LoadGraphic(imgShield);
  ShieldColor = GetPolarity;
}

function Shield_SetState(state){
  SetCommonData("ShieldState", state);
}

function Shield_GetState(){
  return GetCommonDataDefault("ShieldState", ACTIVE);
}

sub ShieldManager_Run {
  if(GetKeyState(VK_SHOT)==KEY_PUSH && ShieldTriggerKey=="shotslow"){
    ShieldShotCounter = 4;
  }
  if(ShieldTriggerKey=="shotslow" && ShieldShotCounter>0){
    ShieldShotCounter--;
  }
  if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH && ShieldTriggerKey=="shotslow"){
    ShieldSlowCounter = 4;
  }
  if(ShieldTriggerKey=="shotslow" && ShieldSlowCounter>0){
    ShieldSlowCounter--;
  }
  
  if(
    (GetKeyState(VK_USER)==KEY_PUSH && ShieldTriggerKey == "c") ||
    (GetKeyState(VK_SKIP)==KEY_PUSH && ShieldTriggerKey == "ctrl") ||
    (GetKeyState(VK_SHOT)==KEY_PUSH && ShieldSlowCounter>0 && ShieldTriggerKey == "shotslow") ||
    (GetKeyState(VK_SLOWMOVE)==KEY_PUSH && ShieldShotCounter>0 && ShieldTriggerKey == "shotslow")
  ){
    SetCommonData("ShieldState", INACTIVE);
    ShieldTimer = 0;
    ShieldColor = GetPolarity;
    SwitchPolarity;
    ShieldShotCounter = 0;
    ShieldSlowCounter = 0;
  }
  Shield_setAlpha;
}

sub Shield_Draw {
  SetRenderState(ADD);
  SetTexture(imgShield);
  if(ShieldColor == RED){
    SetGraphicRect(0, 0, 64, 64);
  }
  if(ShieldColor == BLUE){
    SetGraphicRect(0, 64, 64, 128);
  }
  SetColor(ShieldAlpha, ShieldAlpha, ShieldAlpha);
  DrawGraphic(GetPlayerX, GetPlayerY);
  SetGraphicScale(1, 1);
}

sub ShieldManager_Finalize {
  DeleteGraphic(imgShield);
}

/******************************************
* Private Functions                       *
******************************************/

sub Shield_setAlpha {
  if(GetCommonDataDefault("ShieldState", ACTIVE) == ACTIVE){
    if(ShieldAlpha >= 255){
      ShieldAlphaChange = -1;
    }
    if(ShieldAlpha <= 128){
      ShieldAlphaChange = 1;
    }
    ShieldAlpha += ShieldAlphaChange;
  }
  if(GetCommonDataDefault("ShieldState", ACTIVE) == CHARGING){
    ShieldColor = GetPolarity;
    ShieldAlpha += 13;
    if(ShieldAlpha >=255){
      ShieldAlpha = 255;
      SetCommonData("ShieldState", ACTIVE);
    }
  }
  if(GetCommonDataDefault("ShieldState", ACTIVE) == INACTIVE){
    ShieldTimer += 1;
    ShieldAlpha -= 50;
    SetGraphicScale(ShieldTimer*0.1+1, ShieldTimer*0.1+1);
    if(ShieldAlpha < 0){
      ShieldAlpha = 0;
    }
    if(ShieldTimer == 10){
      SetCommonData("ShieldState", CHARGING);
    }
  }
}