script_enemy_main {
  #include_function ".\lib\enemy.dnh"
  #include_function ".\lib\utusho_animation.dnh"
  Spawned = true;
  
  let imgSun = GCD ~ "img\utsuho_effect1.png";
  let imgCutIn = GCD ~ "img\utsuho_cutin.png";
  let imgWarning = GCD ~ "img\utsuho_warning.png";
  let imgDeathParticle = GCD ~ "lib\img\maple.png";
  
  let imgBG1 = GCD ~ "img\utsuho_cardbg1.png";
  let imgBG2 = GCD ~ "img\utsuho_cardbg2.png";
  let bgCount = 0;
  let bgX = 0;
  let bgY = 0;
  let bgRectX = 0;
  let bgRectY = 0;
  @Initialize {
    LoadGraphic(imgBG1);
    LoadGraphic(imgBG2);
    LoadGraphic(imgSun);
    LoadGraphic(imgCutIn);
    LoadGraphic(imgWarning);
    LoadGraphic(imgDeathParticle);
    MagicCircle(false);
    InitializeEnemy;
    InitializeUtsuhoAnimation;
    SetLife(400);
    SetDamageRate(0, 0);    
    SetEffectForZeroLife(76, 0, 0);
    mainTask;
    ManageDeath;
  }
  
  @MainLoop {
    bgCount++;
    bgX -= GetCommonDataEx("temp", "xDisplace");
    bgY -= GetCommonDataEx("temp", "yDisplace");
    bgRectX -= GetCommonDataEx("temp", "xDisplace");
    bgRectY -= GetCommonDataEx("temp", "yDisplace");
    if(bgX>320){
      bgX -= 640;
    }
    if(bgX<-320){
      bgX += 640;
    }
    if(bgY>320){
      bgY -= 640;
    }
    if(bgY<-320){
      bgY += 640;
    }
    if(bgRectX>512){
      bgRectX -= 1024;
    }
    if(bgRectX<-512){
      bgRectX += 1024;
    }
    if(bgRectY>512){
      bgRectY -= 1024;
    }
    if(bgRectY<-512){
      bgRectY += 1024;
    }
    
    if(GetLife > 0){
      SetCollisionA(GetX, GetY, 48);
      SetCollisionB(GetX, GetY, 32);
    }
    if(GetLife <= 0){
      DeleteEnemyShotToItem(ALL);
    }
    yield;
  }
  
  @DrawLoop {
    DrawUtsuho;
  }
  
  @BackGround {
    SetTexture(imgBG1);
    SetGraphicRect(bgCount/2-bgRectX, -bgCount/2-bgRectY, bgCount/2+512-bgRectX, -bgCount/2+512-bgRectY);
    DrawGraphic(GetCenterX, GetCenterY);
    
    SetTexture(imgBG2);
    SetGraphicRect(0, 0, 256, 256);
    SetGraphicAngle(0, 0, bgCount/2);
    SetGraphicScale(1.5, 1.5);
    DrawGraphic(GetCenterX+bgX-640, GetCenterY+bgY-640);
    DrawGraphic(GetCenterX+bgX-640, GetCenterY+bgY);
    DrawGraphic(GetCenterX+bgX-640, GetCenterY+bgY+640);
    DrawGraphic(GetCenterX+bgX    , GetCenterY+bgY-640);
    DrawGraphic(GetCenterX+bgX    , GetCenterY+bgY);
    DrawGraphic(GetCenterX+bgX    , GetCenterY+bgY+640);
    DrawGraphic(GetCenterX+bgX+640, GetCenterY+bgY-640);
    DrawGraphic(GetCenterX+bgX+640, GetCenterY+bgY);
    DrawGraphic(GetCenterX+bgX+640, GetCenterY+bgY+640);
    
    SetGraphicAngle(0, 0, 0);
    SetGraphicScale(1, 1);
  }
  
  @Finalize {
    DeleteGraphic(imgUtsuho);
    FinalizeEnemy;
    
    if(GotSpellCardBonus){
      PlaySE(sfxCardGet);
    }
  }
  
  task Warning {
    PlaySE(sfxWarning);
    let symbol = ObjEffect_InitializeRect(imgWarning, 7);
    ObjEffect_SetXYRect(symbol, -256, -64, 256, 64);
    Obj_SetPosition(symbol, GetCenterX, GetCenterY);
    
    let warning1 = ObjEffect_InitializeRect(imgWarning, 7);
    ObjEffect_SetXYRect(warning1, -256, -20, 256, 20);
    Obj_SetPosition(warning1, GetCenterX, GetCenterY+80);
    
    let warning2 = ObjEffect_InitializeRect(imgWarning, 7);
    ObjEffect_SetXYRect(warning2, -256, -20, 256, 20);
    Obj_SetPosition(warning2, GetCenterX, GetCenterY-80);
    
    let counter = 0;
    ascent(i in 0..31){
      ObjEffect_SetUVRect(symbol, counter, 0, counter+512, 128);
      ObjEffect_SetUVRect(warning1, -counter, 216, -counter+512, 256);
      ObjEffect_SetUVRect(warning2, -counter, 216, -counter+512, 256);
      ObjEffect_SetColor(symbol, 4, 255/30*i, 255, 255, 255);
      ObjEffect_SetColor(warning1, 4, 255/30*i, 255, 255, 255);
      ObjEffect_SetColor(warning2, 4, 255/30*i, 255, 255, 255);
      counter++;
      yield;
    }
    loop(88){
      ObjEffect_SetUVRect(symbol, counter, 0, counter+512, 128);
      ObjEffect_SetUVRect(warning1, -counter, 216, -counter+512, 256);
      ObjEffect_SetUVRect(warning2, -counter, 216, -counter+512, 256);
      counter++;
      yield;
    }
    descent(i in 0..31){
      ObjEffect_SetUVRect(symbol, counter, 0, counter+512, 128);
      ObjEffect_SetUVRect(warning1, -counter, 216, -counter+512, 256);
      ObjEffect_SetUVRect(warning2, -counter, 216, -counter+512, 256);
      ObjEffect_SetColor(symbol, 4, 255/30*i, 255, 255, 255);
      ObjEffect_SetColor(warning1, 4, 255/30*i, 255, 255, 255);
      ObjEffect_SetColor(warning2, 4, 255/30*i, 255, 255, 255);
      counter++;
      yield;
    }
  }
  
  task mainTask {
    Wait(60);
    SetScore(2000000);
    PlaySE(sfxSpellcard);
    cutin("NAZRIN", "Nuclear Fission "\"" Critical Mass"\", imgCutIn, 0, 0, 425, 512);
    Warning;
    Wait(120);
    loop {
      PlaySE(sfxCharge);
      Concentration01(90);
      Wait(85);
      SunBullet(1.5, 300);
      FollowPlayer(300);
      ascent(j in 0..4){
        PlaySE(sfxSun);
        let angle = rand(0, 360);
        ascent(i in 0..3){
          MiniSun(GetX+cos(angle+i*120)*(0.75+j*0.1875)*100, GetY+sin(angle+i*120)*(0.75+j*0.1875)*100, 3, i*120+angle, 0.8, 150);
        }
        Wait(90);
      }
      SetDamageRate(15, 15);
    }
  }
  
  task SunBullet(size, duration){
    if(GetLife <= 0){ return; }
    let obj = [
      WrapObj_Create(OBJ_EFFECT),
      Obj_Create(OBJ_EFFECT),
      Obj_Create(OBJ_EFFECT),
      Obj_Create(OBJ_EFFECT),
      Obj_Create(OBJ_EFFECT),
      Obj_Create(OBJ_EFFECT),
      Obj_Create(OBJ_EFFECT),
      Obj_Create(OBJ_EFFECT),
      Obj_Create(OBJ_EFFECT)
    ];
    
    ascent(i in 0..8){
      ObjEffect_SetTexture(obj[i], imgSun);
      ObjEffect_SetRenderState(obj[i], ADD);
      ObjEffect_SetLayer(obj[i], 3);
      ObjEffect_SetPrimitiveType(obj[i], PRIMITIVE_TRIANGLEFAN);
      ObjEffect_CreateVertex(obj[i], 4);
      ObjEffect_SetVertexUV(obj[i], 0, 0, 0);
      ObjEffect_SetVertexUV(obj[i], 1, 256, 0);
      ObjEffect_SetVertexUV(obj[i], 2, 256, 256);
      ObjEffect_SetVertexUV(obj[i], 3, 0, 256);
      ObjEffect_SetVertexXY(obj[i], 0, -128, -128);
      ObjEffect_SetVertexXY(obj[i], 1, 128, -128);
      ObjEffect_SetVertexXY(obj[i], 2, 128, 128);
      ObjEffect_SetVertexXY(obj[i], 3, -128, 128);
    }
    
    ascent(i in 0..10){
      if(GetLife <= 0){ break; }
      ascent(j in 0..9){
        ObjEffect_SetScale(obj[j], size/20*i, size/20*i);
      }
      SetCollisionB(Obj_GetX(obj[0]), Obj_GetY(obj[0]), 128*size/20*i*0.63);
      Obj_SetPosition(obj[0], GetX, GetY);
      Obj_SetPosition(obj[1], GetX-640, GetY-640);
      Obj_SetPosition(obj[2], GetX-640, GetY);
      Obj_SetPosition(obj[3], GetX-640, GetY+640);
      Obj_SetPosition(obj[4], GetX, GetY-640);
      Obj_SetPosition(obj[5], GetX, GetY+640);
      Obj_SetPosition(obj[6], GetX+640, GetY-640);
      Obj_SetPosition(obj[7], GetX+640, GetY);
      Obj_SetPosition(obj[8], GetX+640, GetY+640);
      yield;
    }
    
    let count = 0;
    while(count < duration){
      if(GetLife <= 0){ break; }
      ascent(j in 0..9){
        ObjEffect_SetScale(obj[j], size*(0.5+count/duration/2), size*(0.5+count/duration/2));
      }
      SetCollisionB(Obj_GetX(obj[0]), Obj_GetY(obj[0]), 128*size*(0.5+count/duration/2)*0.63);
      Obj_SetPosition(obj[0], GetX, GetY);
      Obj_SetPosition(obj[1], GetX-640, GetY-640);
      Obj_SetPosition(obj[2], GetX-640, GetY);
      Obj_SetPosition(obj[3], GetX-640, GetY+640);
      Obj_SetPosition(obj[4], GetX, GetY-640);
      Obj_SetPosition(obj[5], GetX, GetY+640);
      Obj_SetPosition(obj[6], GetX+640, GetY-640);
      Obj_SetPosition(obj[7], GetX+640, GetY);
      Obj_SetPosition(obj[8], GetX+640, GetY+640);
      count++;
      yield;
    }
    
    Burst(Obj_GetX(obj[0]), Obj_GetY(obj[0]), size);
    ascent(i in 0..10){
      if(GetLife <= 0){ break; }
      ascent(j in 0..9){
        ObjEffect_SetScale(obj[j], size*(1+i*0.025), size*(1+i*0.025));
        ObjEffect_SetColor(obj[j], 4, 255-i*25, 255, 255, 255);
      }
      yield;
    }
    ascent(i in 0..9){
      Obj_Delete(obj[i]);
    }
  }
 
  task MiniSun(x, y, speed, angle, size, duration){
    if(GetLife <= 0){ return; }
    let obj = WrapObj_Create(OBJ_EFFECT);
    Obj_SetPosition(obj, x, y);
    
    ObjEffect_SetTexture(obj, imgSun);
    ObjEffect_SetRenderState(obj, ADD);
    ObjEffect_SetLayer(obj, 3);
    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
    ObjEffect_CreateVertex(obj, 4);
    ObjEffect_SetVertexUV(obj, 0, 0, 0);
    ObjEffect_SetVertexUV(obj, 1, 256, 0);
    ObjEffect_SetVertexUV(obj, 2, 256, 256);
    ObjEffect_SetVertexUV(obj, 3, 0, 256);
    ObjEffect_SetVertexXY(obj, 0, -128, -128);
    ObjEffect_SetVertexXY(obj, 1, 128, -128);
    ObjEffect_SetVertexXY(obj, 2, 128, 128);
    ObjEffect_SetVertexXY(obj, 3, -128, 128);
    
    ascent(i in 0..10){
      if(GetLife <= 0){ break; }
      ObjEffect_SetScale(obj, size/10*i, size/10*i);
      SetCollisionB(Obj_GetX(obj), Obj_GetY(obj), 128*size/10*i*0.63);
      yield;
    }
    ObjEffect_SetScale(obj, size, size);
    Obj_SetSpeed(obj, speed);
    Obj_SetAngle(obj, angle);
    
    descent(i in 0..ceil(duration)){
      if(GetLife <= 0){ break; }
      ObjEffect_SetScale(obj, size/ceil(duration)*i, size/ceil(duration)*i);
      SetCollisionB(Obj_GetX(obj), Obj_GetY(obj), 128*size/ceil(duration)*i*0.63);
      yield;
    }
    
    Obj_Delete(obj);
  }
 
  task Burst(x, y, size){
    if(GetLife <= 0){ return; }
    let radius = size*120;
    loop(30){
      if(GetLife <= 0){ break; }
      PlaySE(sfxShot);
      loop(4){
        let dist = rand(0, radius);
        let angle = rand(0, 360);
        WrapObjShot_Initialize(x+cos(angle)*dist, y+sin(angle)*dist, rand(0.5, 1), rand(0, 360), 45, 10, 480);
      }
      yield;
    }
  }
  
  task FollowPlayer(duration){
    if(GetLife <= 0){ return; }
    SetWrapSpeed(2);
    loop(duration){
      SetWrapAngle(GetAngleToPlayer);
      if(GetLife <= 0){ break; }
      yield;
    }
    SetWrapSpeed(0);
  }
}