#TouhouDanmakufu
#Title[Divine Spear "Spear the Gungnir". Lunatic Mode.]
#Text[Remilia SC2.]
#Player[FREE]
#BGM[.\BGM1.wav]
#ScriptVersion[2]

script_enemy_main{
let CSD = GetCurrentScriptDirectory;
let niceshot = CSD~"\supershot\supershot.txt";
let laserSE = CSD~"Laser3.wav";
let gungnirSE = CSD~"laser.wav";
let shot = CSD~"Shot1.wav";
let charge = CSD~"se_ch02.wav";
let death = CSD~"BossDeath.wav";
let time01 = CSD~"timeout01.wav";
let time02 = CSD~"timeout02.wav";
let remiliasheet = CSD~"boss15.png";
let remiliacutin = CSD~"remilia.png";
let circle = CSD~"circle.png";
let spellbg = CSD~"cdbg15.png";
let frame = 0;
#include_function ".\AnimationLib.dnh"
#include_function ".\FunctionLib.dnh"

 @Initialize{
                LoadUserShotData(niceshot);
                LoadGraphic(remiliasheet);
                LoadGraphic(spellbg);
                LoadGraphic(circle);
		SetLife(1000);
		SetTimer(60);
                SetScore(100000);
		SetInvincibility(100);
		SetMovePosition03(GetCenterX, GetCenterY-160, 12, 9);
                SetDamageRate(10,5);
                SetEffectForZeroLife(70,200,1);
                CutIn(YOUMU,"Divine Spear"\""Spear the Gungnir"\",remiliacutin,0,0,256,512);
                mainTask;
                Timer;

    CreateAnimation("remilia", "idle", 1);
    SetAnimationFrame("remilia", "idle", 0, remiliasheet, 12, 0, 0, 64, 82);

    CreateAnimation("remilia", "move", 3);
    SetAnimationFrame("remilia", "move", 0, remiliasheet, 12, 0, 0, 64, 82);
    SetAnimationFrame("remilia", "move", 1, remiliasheet, 12, 64, 0, 128, 82);
    SetAnimationFrame("remilia", "move", 2, remiliasheet, 12, 128, 0, 192, 82);

  }


  @MainLoop{
  yield;
  if(frame<900){frame++;}
  if(frame==800){SetDamageRate(50,50);}
  SetCollisionA(GetX,GetY,30);
  SetCollisionB(GetX,GetY,25);
 }

 let counter1=0;
 let counter2=0;
 let counter3=0;
 let graphicangle=0;
 let circlespin=0;
 let wave=0;
 let float=0;

 @DrawLoop{

  SetTexture(circle);
  SetGraphicRect(64,64,192,192);
  SetGraphicScale(1+sin(wave)*0.2,1+sin(wave)*0.2);
  SetGraphicAngle(0,0,circlespin);
  SetAlpha(100);
  DrawGraphic(GetX,GetY);
  circlespin+=4;
  wave+=2;

  SetTexture(remiliasheet);
  SetGraphicScale(1,1);
  SetAlpha(255);


  if(GetSpeedX>0 && counter1==0){
  graphicangle=0;
  Animate("remilia","move",false);
  counter1++;
  counter2=0;
  counter3=0;}

  if(GetSpeedX<0 && counter2==0){
  graphicangle=180;
  Animate("remilia","move",false);
  counter2++;
  counter1=0;
  counter3=0;}

  if(GetSpeedX==0 && counter3==0){
  Animate("remilia","idle",true);
  counter3++;
  counter1=0;
  counter2=0;}

  //true makes it loop, false makes it continue on the last sprite.

  SetGraphicAngle(graphicangle,0,0);
  DrawAnimatedSprite("remilia", GetX, GetY+sin(float)*4);
  float += (1/((|GetSpeedX|)+1))*3;
 }

 @Finalize{
 }
 
 let bgalpha=0;
 let bgspin=0;

 @BackGround{
  if(bgalpha<150){bgalpha+=2;}
  SetTexture(spellbg);
  SetGraphicRect(0,0,256,256);
  SetGraphicScale(2.2,2.2);
  SetGraphicAngle(0,0,bgspin);
  SetAlpha(bgalpha);
  DrawGraphic(GetCenterX,GetCenterY);
  bgspin+=1;

 }

     task mainTask{
     wait(100);
     let wave=0;
     loop{
     PlaySE(gungnirSE);
     Laser(GetX,GetY-30,180);
     wait(120);
     PlaySE(charge);
     SetColor(200,0,0);
     Concentration01(30);
     SetColor(255,255,255);
     wait(30);
     PlaySE(laserSE);
     wait(200);
     }
     }



     task Laser(x, y, angle){
     let laser=Obj_Create(OBJ_LASER);
     let frame=0;
     let frame2=0;
     let addangle=0;
     let wave=0;

     Obj_SetPosition(laser,x,y);
     Obj_SetAngle(laser,angle);
     ObjLaser_SetLength(laser,250);
     ObjLaser_SetWidth(laser,10);
     ObjLaser_SetSource(laser,false);
     ObjShot_SetGraphic(laser,2);
     Obj_SetSpeed(laser,20);
     ObjShot_SetDelay(laser,60);

     wait(60);
     while(!Obj_BeDeleted(laser)){
     frame++;
     if(frame>=90){frame2++;}

     if(frame<60){
     TurnToPoint(laser,3,GetPlayerX,GetPlayerY);}
  
     if(frame==60){
     PlaySE(laserSE);
     CreateLaserA(1,Obj_GetX(laser),Obj_GetY(laser),800,20,2,30);
     SetLaserDataA(1,0,Obj_GetAngle(laser),0,0,0,0);
     SetShotKillTime(1,30);
     FireShot(1);}
     
     if(frame>=90){
     Obj_SetPosition(laser, Obj_GetX(laser)+cos(Obj_GetAngle(laser))*Obj_GetSpeed(laser), Obj_GetY(laser)+sin(Obj_GetAngle(laser))*Obj_GetSpeed(laser));}

     if(frame>90 && frame2==2){
     ascent(i in 0..4){
     CreateLaserA(1,Obj_GetX(laser),Obj_GetY(laser),800,20,2,30+i*10);
     SetLaserDataA(1,0,Obj_GetAngle(laser)+i*360/5,0,0,0,0);
     SetShotKillTime(1,150+i*10);
     FireShot(1);
     CreateLaserA(1,Obj_GetX(laser),Obj_GetY(laser),800,20,2,30+i*10);
     SetLaserDataA(1,0,Obj_GetAngle(laser)-i*360/5,0,0,0,0);
     SetShotKillTime(1,150+i*10);
     FireShot(1);
     wave+=7.3;}
     frame2=0;}

     if(frame>90){
     PlaySE(shot);
     ascent(l in 0..3){
     ascent(i in 0..8){
     CreateShotA(1,Obj_GetX(laser),Obj_GetY(laser),10);
     SetShotDataA(1,0,-5,addangle,0,0.1,0,26);
     SetShotDataA(1,70,0,NULL,0,0.05,1+l/2,NULL);
     FireShot(1);
     addangle+=31.5;}}}

    yield;
    }
    }     

     task Timer{
     loop{
	if (GetTimer > 5 && GetTimer <= 11){PlaySE(time01);}
	if (GetTimer >= 1 && GetTimer <= 5){PlaySE(time02);}
	wait(60);
	}
        }
     
}
