#include_function ".\Camera.dnh"

let RED = -1000;
let PURPLE = -1001;
let BLUE = -1002;
let CYAN = -1003;
let GREEN = -1004;
let YELLOW = -1005;
let ORANGE = -1006;

let Placeholder1 = [];
let Placeholder2 = [];
let PlaceholderGlow = [];

let imgPlaceholders = GCD ~ "img\placeholder.png";
let imgCharge = GCD ~ "img\charge.png";
let imgRecovery = GCD ~ "img\recovery.png";
let imgDeathWave = GCD ~ "img\deathwave.png";
let imgDeathParticle = GCD ~ "img\deathparticle.png";

task ManageDeathEffect {
  loop {
    while(!OnMissed){ yield; }
    CreateDeathEffect;
    while(OnMissed){ yield; }
  }
  
  sub CreateDeathEffect {
    CreateDeathWave;
    loop(32){
      CreateDeathParticle;
    }
    
    task CreateDeathWave {
      let obj = ObjEffect_InitializeRect(imgDeathWave, 4);
      ObjEffect_SetUVRect(obj, 0, 0, 64, 64);
      ObjEffect_SetXYRect(obj, -32, -32, 32, 32);
      Obj_SetPosition(obj, GetPlayerX, GetPlayerY);
      
      let scale = 0;
      descent(i in 0..45){
        ObjEffect_SetScale(obj, scale, scale);
        ObjEffect_SetColor(obj, 4, i*200/45, 255, 255, 255);
        scale += i/200;
        yield;
      }
      Obj_Delete(obj);
    }
    
    task CreateDeathParticle {
      let scale = rand(0.2, 1);
      let maxspeed = rand(2, 10);
      let angle = rand(0, 360);
      let type = rand_int(0, 4)*16;
      
      let obj = ObjEffect_InitializeRect(imgDeathParticle, 4);
      ObjEffect_SetUVRect(obj, type, 0, type+16, 16);
      ObjEffect_SetXYRect(obj, -8, -8, 8, 8);
      Obj_SetPosition(obj, GetPlayerX, GetPlayerY);
      
      let speed = maxspeed;
      descent(i in 0..45){
        Obj_SetPosition(obj, Obj_GetX(obj)+cos(angle)*speed, Obj_GetY(obj)+sin(angle)*speed);
        if(i < 20){
          ObjEffect_SetScale(obj, scale*i/20, scale*i/20);
          ObjEffect_SetColor(obj, 4, 10*i, 255, 255, 255);
        }
        else {
          ObjEffect_SetScale(obj, scale, scale);
          ObjEffect_SetColor(obj, 4, 200, 255, 255, 255);
        }
        speed -= i/1000*maxspeed;
        yield;
      }
      Obj_Delete(obj);
    }
  }
}

task ManagePlaceholders {
  yield;
  loop(10){
    let obj = ObjEffect_InitializeRect(imgPlaceholders, 8);
    ObjEffect_SetUVRect(obj, 32, 0, 64, 32);
    ObjEffect_SetXYRect(obj, -16, -16, 16, 16);
    Obj_SetPosition(obj, -1000, -1000);
    Placeholder1 = Placeholder1 ~ [obj];
    
    obj = ObjEffect_InitializeRect(imgPlaceholders, 8);
    ObjEffect_SetUVRect(obj, 0, 0, 32, 32);
    ObjEffect_SetXYRect(obj, -16, -16, 16, 16);
    Obj_SetPosition(obj, -1000, -1000);
    Placeholder2 = Placeholder2 ~ [obj];
    
    obj = ObjEffect_InitializeRect(imgPlaceholders, 8);
    ObjEffect_SetUVRect(obj, 64, 0, 96, 32);
    ObjEffect_SetXYRect(obj, -16, -16, 16, 16);
    Obj_SetPosition(obj, -1000, -1000);
    PlaceholderGlow = PlaceholderGlow ~ [obj];
  }
  loop {
    while(GetTargetShotCount==0){
      yield;
    }
    ascent(i in 0..GetTargetShotCount){
      let x = GetClipMaxX+56+floor(i/5)*112;
      let y = 420 - 90*(i%5);
      CreateNormalPlaceholder(i, x, y);
    }
    while(GetTargetShotCount != 0){
      yield;
    }
  }
  
  task CreateNormalPlaceholder(num, x, y){
    let obj = Placeholder1[num];
    Obj_SetPosition(obj, x, y);
    
    while(CurrentPicture <= num){
      yield;
    }
    CreateGlowingPlaceholder(num, x, y-30);
    descent(i in 0..30){
      ObjEffect_SetColor(obj, 4, i*255/30, 255, 255, 255);
      yield;
    }
    Obj_SetPosition(obj, -1000, -1000);
    ObjEffect_SetColor(obj, 4, 255, 255, 255, 255);
  }
  
  task CreateGlowingPlaceholder(num, x, y){
    let obj = Placeholder2[num];
    Obj_SetPosition(obj, x, y);
    
    let glow = PlaceholderGlow[num];
    Obj_SetPosition(glow, x+2, y-3);
    
    ascent(i in 0..31){
      ObjEffect_SetColor(obj, 4, i*255/30, 255, 255, 255);
      ObjEffect_SetColor(glow, 4, i*255/30, 255, 255, 255);
      yield;
    }
    let counter = 0;
    while(GetTargetShotCount != 0){
      ObjEffect_SetColor(glow, 4, 191+cos(counter*10)*32, 255, 255, 128);
      counter++;
      yield;
    }
    Obj_SetPosition(obj, -1000, -1000);
    Obj_SetPosition(glow, -1000, -1000);
  }
}

task ManageRecoveryParticles {
  loop {
    let array = GetShotDeleteEffectPositions;
    ascent(i in 0..length(array)){
      let array2 = array[i];
      CreateRecoveryParticle(array2[0], array2[1], array2[2]);
      let size = array2[3];
      let num = ceil((size^2)/128) - 1;
      if(num > 0){
        loop(num){
          let angle = rand(0, 360);
          let dist = rand(0, size*0.8);
          CreateRecoveryParticle(array2[0]+cos(angle)*dist, array2[1]+sin(angle)*dist, array2[2]);
        }
      }
    }
    SetShotDeleteEffectPositions([]);
    yield;
  }
  
  task CreateRecoveryParticle(x, y, color){
    let colorsarray = [0, 0, 0];
    alternative(color)
    case(RED){colorsarray = [255, 64, 64];}
    case(PURPLE){colorsarray = [128, 64, 255];}
    case(BLUE){colorsarray = [64, 64, 255];}
    case(CYAN){colorsarray = [64, 255, 255];}
    case(GREEN){colorsarray = [64, 255, 64];}
    case(YELLOW){colorsarray = [255, 255, 64];}
    case(ORANGE){colorsarray = [255, 128, 64];}
    others {colorsarray = [255, 255, 255];}
    
    let obj = ObjEffect_InitializeRect(imgRecovery, 3);
    ObjEffect_SetUVRect(obj, 0, 0, 16, 16);
    ObjEffect_SetXYRect(obj, -8, -8, 8, 8);
    ObjEffect_SetColor(obj, 4, 128, colorsarray[0], colorsarray[1], colorsarray[2]);
    Obj_SetPosition(obj, x, y);
    
    let angle = rand(0, 360);
    Wait(5);
    descent(i in 0..30){
      Obj_SetPosition(obj, Obj_GetX(obj)+cos(angle)*i/30, Obj_GetY(obj)+sin(angle)*i/30);
      yield;
    }
    Wait(15);
    
    let speed = 0;
    let counter = 0;
    while(Distance(Obj_GetX(obj), Obj_GetY(obj), GetPlayerX, GetPlayerY) > speed){
      let angle = AngleFrom(Obj_GetX(obj), Obj_GetY(obj), GetPlayerX, GetPlayerY);
      Obj_SetPosition(obj, Obj_GetX(obj)+cos(angle)*speed, Obj_GetY(obj)+sin(angle)*speed);
      ObjEffect_SetColor(obj, 4, 96+cos(counter*9)*32, colorsarray[0], colorsarray[1], colorsarray[2]);
      speed +=0.12;
      counter++;
      yield;
    }
    
    let lim = 60;
    let incr = 0.3;
    if(WasSuccessful){
      lim = 30;
      incr = 0.15;
    }
    if(CameraCharge < lim){
      CameraCharge += incr;
    }
    Obj_Delete(obj);
  }
}

task ManageChargeParticles {
  let counter = 0;
  loop {
    if(Charging){
      if(counter%3 == 0){
        CreateChargeParticle(rand(80, 100), rand(0, 360), rand(0.3, 0.8), rand(-0.2, -0.25), rand_int(0, 3));
      }
      counter++;
    }
    else {
      counter = 0;
    }
    yield;
  }
  
  task CreateChargeParticle(distance, angle, size, accel, type){
    let obj = ObjEffect_InitializeRect(imgCharge, 3);
    ObjEffect_SetUVRect(obj, type*16, 0, 16+type*16, 16);
    ObjEffect_SetXYRect(obj, -8, -8, 8, 8);
    ObjEffect_SetScale(obj, size, size);
    
    let speed = 0;
    let newdistance = distance;
    let alpha = 0;
    while(newdistance > 10){
      ObjEffect_SetColor(obj, 4, alpha, 255, 200, 200);
      Obj_SetPosition(obj, GetPlayerX+cos(angle)*newdistance, GetPlayerY+sin(angle)*newdistance);
      
      newdistance += speed;
      speed += accel;
      if(speed > 1){ speed = 1; }
      alpha += 20;
      if(alpha > 222){ alpha = 222; }
      
      yield;
    }
    Obj_Delete(obj);
  }
}