#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["2spellcard1"]
#Text["2ndboss[r]"]
#include "script/default_system/Default_ShotConst.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/hersheet.png";
let animFrame = 0; 
let animFrame2 = 0;
let spellcardSFX = GetCurrentScriptDirectory ~ "SFX/SPELLCARD.wav";
let bossdeathSFX = GetCurrentScriptDirectory ~ "SFX/ATTACK3.wav";
let objText = ObjText_Create();
let shootSFX = GetCurrentScriptDirectory ~ "SFX/TWINKLE.wav";
let shoot2SFX = GetCurrentScriptDirectory ~ "SFX/ATTACK5.wav";
let shootSFXball = GetCurrentScriptDirectory ~ "SFX/TWINKLE3.wav";
let mirrors = GetCurrentScriptDirectory ~ "sprite/herspellbg.png";
let overlay1 = GetCurrentScriptDirectory ~ "sprite/herspelloverlay.png";
let count = 0;
let circleObj = ObjPrim_Create(OBJ_SPRITE_2D);

let textoverlay = GetCurrentScriptDirectory ~ "sprite/spelltext.png";

let CSD = GetCurrentScriptDirectory;



@Initialize {
  
	SpellImage;
	SpellText;

   SetAutoDeleteObject(true); //automatically deletes object when THIS script ends

        bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
        ObjEnemy_Regist(bossObj); 

        ObjMove_SetPosition(bossObj,182,200); //warps the boss

        ObjMove_SetDestAtSpeed(bossObj,180,120,10); //movement function

 ObjText_SetText(objText, "consequence ~ near death experience");
    ObjText_SetFontSize(objText, 12);
    ObjText_SetFontType(objText, "Russell Square Regular");
    ObjText_SetFontBold(objText, true);
    ObjText_SetFontColorTop(objText, 255, 255, 255);
    ObjText_SetFontColorBottom(objText, 255, 255, 255);
    ObjText_SetFontBorderType(objText, BORDER_FULL);
    ObjText_SetFontBorderColor(objText, 224, 64, 192);
    ObjText_SetFontBorderWidth(objText, 1);
    ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
    ObjText_SetMaxWidth(objText, 300);
   ObjRender_SetX(objText, 100 - 50);
	ObjRender_SetY(objText, 20);
   
              LoadSound (spellcardSFX);
        LoadSound(bossdeathSFX);
		LoadSound(shoot2SFX);
		LoadSound(shootSFXball);
     PlaySE(spellcardSFX);
	 LoadSound(shootSFX);

     mainTask;

}




@MainLoop {
     bossX = ObjMove_GetX(bossObj);
     bossY = ObjMove_GetY(bossObj);
     
//enemy collision
    ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
    ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
	
   

    yield;
}

task renderBG {

 
        let scrollTex = 0;

        //background
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20); 
	ObjPrim_SetTexture(obj,mirrors); 
	ObjSprite2D_SetSourceRect(obj,0,0,1068,1426);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);

        //spell card overlay
        let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
        Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,overlay1); 
        ObjRender_SetBlendType(overlayObj,BLEND_ADD_ARGB); //blend type (effect)
        ObjRender_SetAlpha(overlayObj,100); //set opacity, 200 is maximum
	ObjSprite2D_SetSourceRect(overlayObj,0,0,1024,768);
	ObjSprite2D_SetDestRect(overlayObj,0,0,GetStgFrameWidth,GetStgFrameHeight);
 
        while(!Obj_IsDeleted(bossObj)) {
          ObjSprite2D_SetSourceRect(overlayObj,0-scrollTex,0-scrollTex,1024-scrollTex,768-scrollTex);
		
          scrollTex += 2; //controll how fast its scrolling
          yield;
      }
        Obj_Delete(overlayObj);
} 


 

task mainTask { 
	renderBoss;
	MagicCircle;
    renderBG;
	movement;
        TEnd;


} 

task MagicCircle
{    let alpha = 0;
      let spin = 0;
	  let path = GetCurrentScriptDirectory ~ "sprite/hercircle.png";
	  

     ObjPrim_SetTexture(circleObj,path);
     ObjRender_SetBlendType(circleObj,BLEND_ADD_ARGB);
     Obj_SetRenderPriorityI(circleObj,100);
     ObjRender_SetAlpha(circleObj,120);
     ObjRender_SetScaleXYZ(circleObj,0.6,0.6,0);
     ObjSprite2D_SetSourceRect(circleObj,0,0,490,490);
     ObjSprite2D_SetDestCenter(circleObj);
     
   while(!Obj_IsDeleted(bossObj)){

       ObjRender_SetPosition(circleObj,bossX+32,bossY+12,0);
       ObjRender_SetAngleZ(circleObj,spin);
       ObjRender_SetAlpha(circleObj,alpha);

         alpha = 120;
		 spin+=1.8;
		yield;
        }
    
       yield;
  
 }
 


task movement {
	wait(120);
	loop { 
	wait(60);
	fire;
	    wait(20);
		
		fire2;
		wait(10);
		fire2;
		wait(20);
			fire;
			fire2;
		wait(20);
		fire2;
			fire;
		wait(20);
		fire2;
		
		
	
	}
}

@Event {
//boss health and timer
       alternative(GetEventType())
       case(EV_REQUEST_LIFE) {
          SetScriptResult(1800);
       }
       case(EV_REQUEST_TIMER) {
          SetScriptResult(70);
     }

}

task SpellImage{
        let obj = ObjPrim_Create(OBJ_SPRITE_2D);
        let imgpath = GetCurrentScriptDirectory() ~ "sprite/spellgirl1.png";
        ObjPrim_SetTexture(obj, imgpath);
		Obj_SetRenderPriorityI(obj,60);
        ObjSprite2D_SetSourceRect(obj, 0, 1, 900,766); 
        ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetScaleXYZ(obj,0.5,0.5,0);
	
		// Moves the object's coordinates 
	        	let a = 0;
             ascent(i in 0..20){
             ObjRender_SetPosition(obj, a, a, 0);
              a+= 10;
			  
yield;
           
		}
		wait(30);
		ObjRender_SetAlpha(obj,200);
		wait(5);
		ObjRender_SetAlpha(obj,100);
		wait(5);
		ObjRender_SetAlpha(obj,50);
		wait(5);
		Obj_Delete(obj);
		
		

    
	}
	
		task SpellText{
 let scrollTex = 0;
      
	  let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
        Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,textoverlay); 
        ObjRender_SetBlendType(overlayObj,BLEND_ADD_ARGB); //blend type (effect)
        ObjRender_SetAlpha(overlayObj,100); //set opacity, 200 is maximum
	ObjSprite2D_SetSourceRect(overlayObj,0,0,400,504);
	ObjRender_SetScaleXYZ(overlayObj,1,1,0);
	ObjSprite2D_SetDestRect(overlayObj,0,0,GetStgFrameWidth,GetStgFrameHeight);
 
        loop(70) {
          ObjSprite2D_SetSourceRect(overlayObj,400-scrollTex,504-scrollTex,0-scrollTex,0-scrollTex);
		
          scrollTex += 5; //controll how fast its scrolling
          yield;
		 
		 
      }
	  
		ObjRender_SetAlpha(overlayObj,200);
		wait(5);
		ObjRender_SetAlpha(overlayObj,100);
		wait(5);
		ObjRender_SetAlpha(overlayObj,50);
		wait(5);
		Obj_Delete(overlayObj);
        
	
}


@Finalize {

}



function wait(w) { loop(w) { yield; } }


task renderBoss {
	let dir;
        let speed;

        //texture the boss and set the center
        ObjPrim_SetTexture(bossObj,imgBoss);
        ObjSprite2D_SetSourceRect(bossObj,0,0,77,100);
        ObjSprite2D_SetDestCenter(bossObj);
        ObjRender_SetScaleXYZ(bossObj,0.6,0.6,0);
    
	while(!Obj_IsDeleted(bossObj)) {
		//when the boss is NOT deleted

		
		dir = ObjMove_GetAngle(bossObj);
		speed = ObjMove_GetSpeed(bossObj);
		
 //animation handling
		if(speed == 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);
			if(animFrame < 10) { ObjSprite2D_SetSourceRect(bossObj,12,0,65,68); }
			if(animFrame >= 20 && animFrame < 30) { ObjSprite2D_SetSourceRect(bossObj,68,0,115,68); }
			if(animFrame >= 30 && animFrame < 40) { ObjSprite2D_SetSourceRect(bossObj,115,0,160,68); }
			if(animFrame >= 50) { ObjSprite2D_SetSourceRect(bossObj,160,0,212,68); }

			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,210,0,263,68); }
			if(animFrame2 >= 15 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,259,0,313,68); }
			if(animFrame2 >= 30 && animFrame2 < 40){ ObjSprite2D_SetSourceRect(bossObj,210,0,263,68); }
			if(animFrame2 >= 40) { ObjSprite2D_SetSourceRect(bossObj,259,0,313,68); }
			
		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,210,0,263,68); }
			if(animFrame2 >= 15 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,259,0,313,68); }
			if(animFrame2 >= 30 && animFrame2 < 40){ ObjSprite2D_SetSourceRect(bossObj,210,0,263,68); }
			if(animFrame2 >= 40) { ObjSprite2D_SetSourceRect(bossObj,259,0,313,68); }
		}
		animFrame++;		// count animFrame. (++ is +1)
		animFrame2+=2;		// count animFrame2.
		if(animFrame > 60) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}


function angleToPlayer {
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}
task fire {

if (GetCommonData("Difficulty", 3) == 3){

   let angleT = angleToPlayer;
   
    loop(13){
	PlaySE(shoot2SFX);
	angleT += 360/10;
	ascent(i in 0..16){
	    let obj2 = CreateShotA2(bossX, bossY, 1, angleT, 3, 2, DS_BUTTERFLY_PURPLE, 20);
		angleT += 360/52;
		ObjMove_AddPatternA3(obj2, 30, 1.5, angleT, 2, 0.6, 3,DS_KNIFE_YOUMU_WHITE);
		}
    }
	}
	if (GetCommonData("Difficulty", 2) == 2){

   let angleT = angleToPlayer;
   
    loop(10){
	PlaySE(shoot2SFX);
	angleT += 360/10;
	ascent(i in 0..12){
	    let obj2 = CreateShotA2(bossX, bossY, 1, angleT, 3, 2, DS_BUTTERFLY_PURPLE, 20);
		angleT += 360/24;
		ObjMove_AddPatternA3(obj2, 30, 1.5, angleT, 2, 0.4, 3,DS_KNIFE_YOUMU_WHITE);
		}
    }
	}
	if (GetCommonData("Difficulty", 1) == 1){

   let angleT = angleToPlayer;
   
    loop(10){
	PlaySE(shoot2SFX);
	angleT += 360/10;
	ascent(i in 0..5){
	    let obj2 = CreateShotA2(bossX, bossY, 1, angleT, 3, 2, DS_BUTTERFLY_PURPLE, 20);
		angleT += 360/16;
		ObjMove_AddPatternA3(obj2, 30, 1.5, NO_CHANGE, 2, 0, 2,DS_KNIFE_YOUMU_WHITE);
		}
    }
	}
	
		if (GetCommonData("Difficulty", 0) == 0){

   let angleT = angleToPlayer;
   
    loop(10){
	PlaySE(shoot2SFX);
	angleT += 360/10;
	ascent(i in 0..10){
	    let obj2 = CreateShotA2(bossX, bossY, 1, angleT, 3, 2, DS_BUTTERFLY_PURPLE, 20);
		angleT += 360/10;
		ObjMove_AddPatternA3(obj2, 30, 1.5, NO_CHANGE, 2, 0, 2,DS_KNIFE_YOUMU_WHITE);
		}
    }
	}
}
task fire2 {
if (GetCommonData("Difficulty", 3) == 3){

   let angleT = angleToPlayer;
   
    loop(13){
	angleT += 360/10;
	ascent(i in 0..14){
	    let obj2 = CreateShotA2(bossX, bossY, 1, angleT, 3, 3, DS_BUTTERFLY_PURPLE, 20);
		angleT += 360/40;
		ObjMove_AddPatternA3(obj2, 50, 5, NO_CHANGE, 3, -0.5, 3,DS_KNIFE_YOUMU_RED);
		}
    }
	}
	if (GetCommonData("Difficulty", 2) == 2){

   let angleT = angleToPlayer;
   
    loop(13){
	angleT += 360/10;
	ascent(i in 0..14){
	    let obj2 = CreateShotA2(bossX, bossY, 1, angleT, 3, 3, DS_BUTTERFLY_PURPLE, 20);
		angleT += 360/30;
		ObjMove_AddPatternA3(obj2, 50, 5, NO_CHANGE, 3, -0.5, 3,DS_KNIFE_YOUMU_RED);
		}
    }
	}
		if (GetCommonData("Difficulty", 1) == 1){

   let angleT = angleToPlayer;
   
    loop(13){
	angleT += 360/10;
	ascent(i in 0..14){
	    let obj2 = CreateShotA2(bossX, bossY, 1, angleT, 3, 3, DS_BUTTERFLY_PURPLE, 20);
		angleT += 360/30;
		ObjMove_AddPatternA3(obj2, 50, 5, NO_CHANGE, 3, -0.5, 3,DS_KNIFE_YOUMU_RED);
		}
    }
	}
		if (GetCommonData("Difficulty", 0) == 0){

   let angleT = angleToPlayer;
   
    loop(13){
	angleT += 360/10;
	ascent(i in 0..10){
	    let obj2 = CreateShotA2(bossX, bossY, 1, angleT, 3, 3, DS_BUTTERFLY_PURPLE, 20);
		angleT += 360/20;
		ObjMove_AddPatternA3(obj2, 50, 5, NO_CHANGE, 3, -0.5, 3,DS_KNIFE_YOUMU_RED);
		}
    }
	}
}

task SnakePattern(obj, angleT){
	let wvel = 1;
	ascent(i in 0..12){
		ObjMove_AddPatternA2(obj, i * 30, ObjMove_GetSpeed(obj), angleT, 0.01, wvel, 3);
		wvel *= -1;
	}
}

task TDrawLoop {
    let imgBoss = GetCurrentScriptDirectory() ~ "sprite/testsheet.png";
    ObjPrim_SetTexture(bossObj, imgBoss);
    ObjSprite2D_SetSourceRect(bossObj, 64, 1, 127, 64);
    ObjSprite2D_SetDestCenter(bossObj);
}


task TEnd 
{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
        
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(20);	
	CloseScript(GetOwnScriptID());

}