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 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);
    MagicCircle(false);
    InitializeEnemy;
    InitializeUtsuhoAnimation;
    SetLife(500);
    SetDamageRate(0, 0);
    mainTask;
  }
  
  @MainLoop {
    SetCollisionA(GetX, GetY, 48);
    SetCollisionB(GetX, GetY, 32);
    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;
    }
    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", "Atom Sign "\""Nuclear Flare"\", imgCutIn, 0, 0, 425, 512);
    Warning;
    Wait(120);
    loop {
      PlaySE(sfxCharge);
      Concentration01(90);
      Wait(75);
      Animate("utsuho", "start cast", false);
      Wait(15);
      PlaySE(sfxSun);
      ascent(i in 0..6){
        SunBullet(GetX-4, GetY-32, 2.5, GetAngleToPlayer+i*60, 1, 360);
      }
      Wait(75);
      PlaySE(sfxSun);
      ascent(i in 0..6){
        SunBullet(GetX-4, GetY-32, 2.5, GetAngleToPlayer+i*60, 1, 360);
      }
      Wait(30);
      SetDamageRate(15, 15);
      Animate("utsuho", "stop cast", false);
      Wait(GetAnimationTotalTime("utsuho", "stop cast"));
      Animate("utsuho", "idle", true);
      Wait(60);
      Move02(rand(0, 360), rand(75, 100));
      
      let angle = rand(0, 360);
      loop(45){
        PlaySE(sfxShot);
        ascent(i in 0..3){
          WrapObjShot_Initialize(GetX+rand(-8, 8), GetY+rand(-8, 8), 1.5, angle+i*120, 45, 30, 420);
        }
        angle += 14;
        Wait(2);
      }
    }
  }
  
  task SunBullet(x, y, speed, angle, size, duration){
    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){
      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)){
      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 RandomMove {
    let x = GetX+rand(-120, 120);
    let y = GetY+rand(-120, 120);
    while(Distance(GetX, GetY, x, y) < 50){
      x = GetX+rand(-120, 120);
      y = GetY+rand(-120, 120);
    }
    let angle = AngleFrom(GetX, GetY, x, y);
    let dist = Distance(GetX, GetY, x, y);
    SetWrapAngle(angle);
    while(dist > 1){
      SetWrapSpeed(dist/20);
      dist -= GetWrapSpeed;
      yield;
    }
    SetWrapSpeed(0);
  }
  
  task Move01(x, y){
    let angle = AngleFrom(GetX, GetY, x, y);
    let dist = Distance(GetX, GetY, x, y);
    SetWrapAngle(angle);
    while(dist > 1){
      SetWrapSpeed(dist/20);
      dist -= GetWrapSpeed;
      yield;
    }
    SetWrapSpeed(0);
  }
  
  task Move02(angle, dist){
    SetWrapAngle(angle);
    while(dist > 1){
      SetWrapSpeed(dist/20);
      dist -= GetWrapSpeed;
      yield;
    }
    SetWrapSpeed(0);
  }
}