#include_function ".\stage.dnh"

let imgStage1a = GetCurrentScriptDirectory ~ "stage\stage01a.png";
let imgStage1b = GetCurrentScriptDirectory ~ "stage\stage01b.png";

let Stage1Enemy1 = GetCurrentScriptDirectory ~ "enemy\stage1fairy1.dnh";
let Stage1Enemy2 = GetCurrentScriptDirectory ~ "enemy\stage1fairy2.dnh";
let Stage1Enemy3 = GetCurrentScriptDirectory ~ "enemy\stage1fairy3.dnh";
let Stage1Utsuho = GetCurrentScriptDirectory ~ "enemy\boss\utsuho.dnh";
let DeathDummy = GetCurrentScriptDirectory ~ "enemy\boss\death_dummy.dnh";

let bgmStage1 = GetCurrentScriptDirectory ~ "bgm\bgm01.mp3";
let bgmBoss1 = GetCurrentScriptDirectory ~ "bgm\bgm02.mp3";

task Stage1 {
  CurrentStage = 1;
  LoadGraphic(imgStage1a);
  LoadGraphic(imgStage1b);
  LoadGraphic(imgBackdrop);
  Stage1BG;
  PlayMusic(bgmStage1);
  
  Wait(60);
  CreateEnemyFromFile(Stage1Enemy1, 0, -100, 0, 0, 0);
  
  WaitForZeroEnemy;
  Wait(120);
  loop(15){
    let x = rand(-200, 200);
    while(absolute(x) < 32){ x = rand(-200, 200); }
    let y = rand(-200, 200);
    while(absolute(y) < 32){ y = rand(-200, 200); }
    CreateEnemyFromFile(Stage1Enemy1, x, y, 0, 0, 0);
    Wait(30);
  }
  
  WaitForZeroEnemy;
  Wait(60);
  CreateEnemyFromFile(Stage1Enemy2, 0, -150, 0, 0, 0);
  
  WaitForZeroEnemy;
  Wait(120);
  loop(5){
    let x = rand(-256, 256);
    while(absolute(x) < 32){ x = rand(-256, 256); }
    let y = rand(-256, 256);
    while(absolute(y) < 32){ y = rand(-256, 256); }
    CreateEnemyFromFile(Stage1Enemy2, x, y, 0, 0, 0);
    Wait(20);
  }
  
  WaitForZeroEnemy;
  Wait(120);
  ascent(i in 0..3){
    CreateEnemyFromFile(Stage1Enemy2, cos(i*120-90)*150, sin(i*120-90)*150, 0, 0, 0);
  }
  Wait(180);
  ascent(i in 0..5){
    CreateEnemyFromFile(Stage1Enemy1, cos(i*72+90)*180, sin(i*72+90)*180, 0, 0, 0);
  }
  
  WaitForZeroEnemy;
  Wait(120);
  CreateEnemyFromFile(Stage1Enemy3, 0, -120, 0, 0, 0);
  
  WaitForZeroEnemy;
  Wait(120);
  DeleteEnemyShotToItem(ALL);
  CreateEventFromScript("Stage1_1");
  
  while(GetCommonDataDefaultEx("temp", "event step", 1) < 2){ yield; }
  CreateEnemyBossFromFile(Stage1Utsuho, 0, -120, 0, 0, 0);
  
  while(GetCommonDataDefaultEx("temp", "event step", 1) < 3){ yield; }
  DeleteMusic(bgmStage1);
  Wait(30);
  PlayMusic(bgmBoss1);
  
  WaitForZeroEnemy;
  let pos = GetCommonDataEx("temp", "death point");
  CreateEnemyFromFile(DeathDummy, pos[0], pos[1], 0, 0, 0);
  Wait(120);
  FadeOutMusic(bgmBoss1, 60);
  Wait(60);
  DeleteMusic(bgmBoss1);
  Clear;
  
}

task Stage1BG {
  BackdropColor = [255, 128, 0];
  FogColor = [255, 128, 0];
  FogMin = 850;
  FogMax = 1000;
  CameraX = 0;
  CameraY = 0;
  CameraZ = 0;
  CameraDist = 250;
  CameraAngleX = 90;
  CameraAngleY = -60;
  bgCounter = 0;
  while(CurrentStage == 1){
    bgCounter++;
    yield;
  }
}

sub DrawStage1 {
  if(CurrentStage == 1){
    SetCameraAndFog;
    
    bgPlayerOffsetX -= GetCommonDataEx("temp", "xDisplace")/2;
    bgPlayerOffsetY -= GetCommonDataEx("temp", "yDisplace")/2;
    
    let OffsetX = bgCounter/2+bgPlayerOffsetX;
    let OffsetY = bgPlayerOffsetY;
    
    ascent(i in 0-floor(OffsetX/56.7)..13-floor(OffsetX/56.7)){
      DrawWave(i*64+bgCounter/2, OffsetY, (i+1)*64+bgCounter/2, OffsetY+2048, -380+i*56.7+OffsetX, 0, 0);
    }
  }
  
  function DrawWave(left, top, right, bottom, x, y, z){
    let width = right-left;
    WriteZBuffer(true);
    UseZBuffer(true);
    SetAlpha(255);
    SetTexture(imgStage1a);
    SetGraphicRect(left, top, left+width/6, bottom);
    SetGraphicAngle(0, -10, 0);
    DrawGraphic3D(x, y, z);
    SetGraphicRect(left+width/6, top, left+width/3, bottom);
    SetGraphicAngle(0, -30, 0);
    DrawGraphic3D(x+9.25, y, z+3.37);
    SetGraphicRect(left+width/3, top, left+width/2, bottom);
    SetGraphicAngle(0, -10, 0);
    DrawGraphic3D(x+18.5, y, z+6.75);
    SetGraphicRect(left+width/2, top, left+width*2/3, bottom);
    SetGraphicAngle(0, 10, 0);
    DrawGraphic3D(x+28.35, y, z+6.75);
    SetGraphicRect(left+width*2/3, top, left+width*5/6, bottom);
    SetGraphicAngle(0, 30, 0);
    DrawGraphic3D(x+37.6, y, z+3.37);
    SetGraphicRect(left+width*5/6, top, left+width, bottom);
    SetGraphicAngle(0, 10, 0);
    DrawGraphic3D(x+46.85, y, z);
    
    WriteZBuffer(true);
    UseZBuffer(false);
    SetAlpha(128);
    SetTexture(imgStage1b);
    SetGraphicRect(left+bgCounter/2, top, left+width/6+bgCounter/2, bottom);
    SetGraphicAngle(0, -10, 0);
    DrawGraphic3D(x, y, z);
    SetGraphicRect(left+width/6+bgCounter/2, top, left+width/3+bgCounter/2, bottom);
    SetGraphicAngle(0, -30, 0);
    DrawGraphic3D(x+9.25, y, z+3.37);
    SetGraphicRect(left+width/3+bgCounter/2, top, left+width/2+bgCounter/2, bottom);
    SetGraphicAngle(0, -10, 0);
    DrawGraphic3D(x+18.5, y, z+6.75);
    SetGraphicRect(left+width/2+bgCounter/2, top, left+width*2/3+bgCounter/2, bottom);
    SetGraphicAngle(0, 10, 0);
    DrawGraphic3D(x+28.35, y, z+6.75);
    SetGraphicRect(left+width*2/3+bgCounter/2, top, left+width*5/6+bgCounter/2, bottom);
    SetGraphicAngle(0, 30, 0);
    DrawGraphic3D(x+37.6, y, z+3.37);
    SetGraphicRect(left+width*5/6+bgCounter/2, top, left+width+bgCounter/2, bottom);
    SetGraphicAngle(0, 10, 0);
    DrawGraphic3D(x+46.85, y, z);
  }
}