#include_function "lib\Ikareimu\Common\Math.dnh"
#include_function "lib\Ikareimu\System\Polarity.dnh"
#include_function "lib\Ikareimu\System\Shield.dnh"
#include_function "lib\Ikareimu\System\Power.dnh"
#include_function "lib\Ikareimu\System\Shot.dnh"
#include_function "lib\Ikareimu\System\Laser.dnh"
#include_function "lib\Ikareimu\System\Chaining.dnh"
#include_function "lib\Ikareimu\System\Collision.dnh"

#include_function "lib\Ikareimu\Common\ShotReplace.dnh"

let EnemyPolarity = NONE;
let FinalizeRun = false;
let autoDeathCounter = 0;

/******************************************
* Enemy Script Functions                  *
******************************************/

// Run this in @Initialize
sub IkareimuEnemy_Initialize {
  PolarityManager_Initialize;
  ShieldManager_Initialize;
  PowerManager_Initialize;
  CollisionManager_Initialize;
  
  LoadUserShotData("lib\Ikareimu\Common\ShotDefinition.dnh");
}

// Run this every frame in @MainLoop
sub IkareimuEnemy_Run {
  CollisionManager_Run;
  ShotManager_Run;
  LaserManager_Run;
}

// Run this in @Finalize
sub IkareimuEnemy_Finalize {
  CollisionManager_Finalize;
}

// Run this to set an enemy's polarity.
// Valid parameters are RED, BLUE, and NONE.
// RED enemies take double damage from BLUE bullets.
// BLUE enemies take double damage from RED bullets.
// NONE enemies take normal damage from all bullets.
// Defaults to NONE.
function IkareimuEnemy_SetPolarity(polarity){
  if(polarity!=RED && polarity!=BLUE && polarity!=NONE){
    RaiseError("You did not set a valid polarity with IkareimuEnemy_SetPolarity.", "Ikareimu System Error");
  }
  EnemyPolarity = polarity;
}

// Run this instead of the standard SetCollisionA to
// allow double damage on opposite polarity.
function IkareimuEnemy_SetCollisionA(x, y, radius){
  SetCommonData("SetCollisionA", GetCommonDataDefault("SetCollisionA", [])~[[x, y, radius, EnemyPolarity]]);
  SetCollisionA(x, y, 1);
}

// Adds to the chain count for the chaining scoring system.
// Basically make this run for a RED or BLUE enemy when it
// dies.
sub IkareimuEnemy_AddChainCount {
  if(EnemyPolarity != NONE){
    Chain_Add(EnemyPolarity, GetX, GetY);
  }
}

// Run this instead of the standard VanishEnemy to remove
// normal enemies.
sub IkareimuEnemy_VanishEnemy {
  if(GetLife==GetEnemyLife && GetX==GetEnemyX && GetY==GetEnemyY){
    RaiseError("IkareimuEnemy_VanishEnemy cannot be called on a boss.", "Ikareimu System Error");
  }
  SetLife(9999);
  FinalizeRun = true;
}