#include_function "lib\Ikareimu\Common\Math.dnh"
#include_function "lib\Ikareimu\Common\Effects.dnh"
#include_function "lib\Ikareimu\System\Polarity.dnh"
#include_function "lib\Ikareimu\System\Power.dnh"
#include_function "lib\Ikareimu\System\Collision.dnh"

/******************************************
* Variables                               *
******************************************/

let RedShots         = [];
let BlueShots        = [];

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

// Used in the same way as Obj_Create(OBJ_SHOT) and
// Obj_Create(OBJ_LASER)but adds the id into an array.
function ColorShot_Create(color, x, y, speed, angle, graphic, delay){
  let obj = Obj_Create(OBJ_SHOT);
  Obj_SetPosition(obj, x, y);
  Obj_SetSpeed(obj, speed);
  Obj_SetAngle(obj, angle);
  ObjShot_SetGraphic(obj, graphic);
  ObjShot_SetDelay(obj, delay);
  if(color == RED){
    RedShots = RedShots ~ [obj];
  }
  else if(color == BLUE){
    BlueShots = BlueShots ~ [obj];
  }
  return obj;
}



// Manages the absorption and collision settings of bullets.
sub ShotManager_Run {
  ObjShot_Red_manage;
  ObjShot_Blue_manage;
}

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

// Manage the red bullets.
sub ObjShot_Red_manage {
  let len = length(RedShots);
  descent(i in 0..len){
    let obj = RedShots[i];
    if(Obj_BeDeleted(obj)){
      RedShots = erase(RedShots, i);
    }
    else {
      if(ObjCollision_GetState() == ON){
        if(GetPolarity == RED){
          Obj_SetCollisionToPlayer(obj, false);
          let x = Obj_GetX(obj);
          let y = Obj_GetY(obj);
          if(ObjCollision_Happened(obj)){
            AbsorbEffect_A_create(x, y, RED01);
            Obj_Delete(obj);
            RedShots = erase(RedShots, i);
            AddPower(1);
            AddScore(100);
          }
        }
        else {
          Obj_SetCollisionToPlayer(obj, true);
        }
      }
      else {
        Obj_SetCollisionToPlayer(obj, true);
      }
    }
  }
}

// Manage the blue bullets.
sub ObjShot_Blue_manage {
  let len = length(BlueShots);
  descent(i in 0..len){
    let obj = BlueShots[i];
    if(Obj_BeDeleted(obj)){
      BlueShots = erase(BlueShots, i);
    }
    else {
      if(ObjCollision_GetState() == ON){
        if(GetPolarity == BLUE){
          Obj_SetCollisionToPlayer(obj, false);
          let x = Obj_GetX(obj);
          let y = Obj_GetY(obj);
          if(ObjCollision_Happened(obj)){
            AbsorbEffect_A_create(x, y, BLUE01);
            Obj_Delete(obj);
            BlueShots = erase(BlueShots, i);
            AddPower(1);
            AddScore(100);
          }
        }
        else {
          Obj_SetCollisionToPlayer(obj, true);
        }
      }
      else {
        Obj_SetCollisionToPlayer(obj, true);
      }
    }
  }
}