let here = GetCurrentScriptDirectory();

task tsubaTime(){
  real np = 0;
  CreateRenderTarget("screenshot");

  let screen = ObjPrim_Create(OBJ_SPRITE_2D);
  ObjRender_SetBlendType(screen, BLEND_ALPHA);
  Obj_SetRenderPriorityI(screen, 100);
  ObjPrim_SetTexture(screen, "screenshot");
  setrx(screen, gx); setry(screen, gy); 

  ObjShader_SetShaderF(screen, here ~ "S02_HLSL.txt");
	ObjShader_SetTechnique(screen, "Render");

  async{
    PlaySFXV(SFX_BOOOOOOM, 70); 
    PlaySFXV(SFX_MAO00, 80); 
    loop(30){
      Concentration(bx, by, 60, 800, 5, 8, 9, BLEND_ADD_ARGB, 27, 126, 27, 1);
      wait(2);
    }
  }

  while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
    ObjSprite2D_SetSourceRect(screen, 0, 0, stgw, stgh); ObjSprite2D_SetDestCenter(screen);
    RenderToTextureA1("screenshot", 0, 100, true); 

    ObjRender_SetAlpha(screen, np); np += 255/(60*1); np = (np > 255) ? 255 : np;
    yield;
  }
}

task missiles(){
  if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let aux; int n = 1;
    
    real r = 77, s, space = 3.5; real[] details;
	//}
	
  tsubaTime();
  wait(60);
  ObjEnemy_SetDamageRate(objBoss, 30, 15);

  ascent(int i in 0..2){
    details = [25, r, 1, 2*n, 120, 30, 1];

    aux = CustomFlowerA1(bx, by, 4.55, r + 360/2*i, SHOT_BALL_RED, details, BLEND_ADD_ARGB, [1, 100], [0, 123]);
    AllQED(aux, 200, true, true); touch_it(aux, 160); my_oh_my(aux, 160); n *= -1;
  }

  async {
    wait(30); real r = 0;
    while(!Obj_IsDeleted(objBoss) && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){

      PlaySFXV(SFX_SHOT01, 40); 
      ascent(int i in 0..20){
        real a = r + 360/20*i;

        aux = CScaledShotA1(bx, by, 3, a, SHOT_DOT_PINK, 15, 3);
        ObjShot_SetSpellResist(aux, true);
      }

      r += 360/40 + rand(-4, 4);
      wait(60*1/2);
    }
  }
   
  r = 270; s = r;
  while(!Obj_IsDeleted(objBoss) && ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){

    PlaySFXV(SFX_SHOT04, 30); 
    ascent(int i in -19..20){
      real a = r + space*i;

      aux = CScaledShotA1(bx, by, 6.4, a, SHOT_BULLET_GREY, 15, 1);
      ObjMove_AddPatternB2(aux, 15, NO_CHANGE, NO_CHANGE, NO_CHANGE, 1/5, NO_CHANGE, 4.5);
    }

    r = s + space/rand(1,4)*n; n *= -1;
    wait(60*1/6);
  }
}

//---------------------------------------------------------------------------------------------------------------------
task my_oh_my(obj, range){
  let main = GetCurrentScriptDirectory() ~ "./img/sleep.png";
  real alpha = 0;

  let snu = ObjPrim_Create(OBJ_SPRITE_2D); 
  ObjPrim_SetTexture(snu, main); ObjRender_SetAlpha(snu, alpha);
	ObjSprite2D_SetSourceRect(snu, 0, 0, 49, 43); ObjSprite2D_SetDestCenter(snu);
  ObjRender_SetScaleXYZ(snu, 0.75, 0.75, 0);
  while(!Obj_IsDeleted(obj)){
    if(GetObjectDistance(obj, GetPlayerObjectID) < range){
      alpha += 255/21;
    }
    else{
      alpha -= 255/18;
    }

    alpha = clamp(alpha, 0, 255);

    ObjRender_SetX(snu, px + 30); ObjRender_SetY(snu, py - 30);
    ObjRender_SetAlpha(snu, alpha);
    yield;
  }
		
}

task touch_it(obj, range){
  real[] save = GetPlayerSpeed();
  while(!Obj_IsDeleted(obj)){
    if(GetObjectDistance(obj, GetPlayerObjectID) < range){
      SetPlayerSpeed(save[0]/2.5, save[1]/2.5);
    }
    else{
      SetPlayerSpeed(save[0], save[1]);
    }

    yield;
  }

  SetPlayerSpeed(save[0], save[1]);
}