#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["2spellcard2"]
#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/DEAD.wav";
let objText = ObjText_Create();
let shootSFX = GetCurrentScriptDirectory ~ "SFX/TWINKLE2.wav";
let shootSFX2 = GetCurrentScriptDirectory ~ "SFX/TWINKLE.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;

//textures

let texFog = CSD ~ "sprite/fog.png";

let texTree = CSD ~ "sprite/tree.png"; 

let texFloor = CSD ~ "sprite/stage2wall.png";

//--------------------------------------------
//function to spawn a floor piece
function makeFloor(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFloor);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,90,0,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}
//function to spawn wall
function makeWall(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFloor);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,180,90,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}


function makeTree(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texTree);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,300,300);
        ObjRender_SetScaleXYZ(obj,0.5,0.5,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,0,180,180);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}
function makeFog(locX,locY,locZ,red,green,blue) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFog);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,4096,4096);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

      
       ObjRender_SetPosition(obj,locX,locY,locZ);


    ObjSprite3D_SetBillboard(obj, true); //makes this object face camera no matter where the camera is facing 
}

function makeWall2(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFloor);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,180,90,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}
//--------------------------------


@Initialize {
    
	SpellText;
	SpellImage;
	

   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, "borrowing empitts's magic ~ heart attack");
    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);
     PlaySE(spellcardSFX);
	 LoadSound(shootSFX);
	 LoadSound(shootSFX2);

     mainTask;

}




@MainLoop {
     bossX = ObjMove_GetX(bossObj);
     bossY = ObjMove_GetY(bossObj);
     
//enemy collision
    ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
    ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
	
	   if(count == 0){
            CreateOrb2(1, 64);
            CreateOrb2(-1, 64);
        }
        count++;
		

    yield;
}

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);
        
	
}

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 CreateOrb2(dir, dist) {
        wait(100);
		let objcount = 0; //counter to control firing of bullets and movement
        //obtain object ID
	let graphic = GetCurrentScriptDirectory() ~ "sprite/familiar.png"; //Define path
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, graphic);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 60, 53); //use whatever dimensions necessary
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
        
		
		while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) { //linked to the parent enemy's life
  ObjRender_SetPosition(obj, ObjMove_GetX(bossObj) + dir * dist, ObjMove_GetY(bossObj), 1);
	while(!Obj_IsDeleted(obj)){
	

    if(animFrame < 20){
      ObjSprite2D_SetSourceRect(obj, 0, 0, 60, 53);
    }
    if(animFrame >= 20 && animFrame < 30){
      ObjSprite2D_SetSourceRect(obj, 60, 0, 120, 53);
    }
    if(animFrame >= 30 && animFrame < 40){
      ObjSprite2D_SetSourceRect(obj, 120, 0, 180, 53);
    }
    if(animFrame >= 40){
      ObjSprite2D_SetSourceRect(obj, 180, 0, 240, 53);
    }
    if(animFrame > 45){
      // Loop back to the start after 4 frames of animation
      animFrame = 0;
    }
    animFrame ++;
    yield;
             if (GetCommonData("Difficulty", 3) == 3){
            if (objcount % 12 == 0) { //control firing rate
                let curx = ObjRender_GetX(obj);
                let cury = ObjRender_GetY(obj);
		ascent(i in 0..36) {
		PlaySE(shootSFX2);
                    CreateShotA1(curx, cury, 1 + 1*sin(i * 8), 90 + 60*sin(i * 3) + objcount * 4 * dir, DS_RICE_S_RED, 0);
                    CreateShotA1(curx, cury, 1 + 1*sin(i * 8), 90 - 60*sin(i * 3) + objcount * 4 * dir, DS_RICE_S_RED, 0);
		}
		
            }
            objcount++;
            yield;
        }
		if (GetCommonData("Difficulty", 2) == 2){
            if (objcount % 15 == 0) { //control firing rate
                let curx = ObjRender_GetX(obj);
                let cury = ObjRender_GetY(obj);
		ascent(i in 0..36) {
		PlaySE(shootSFX2);
                    CreateShotA1(curx, cury, 1 + 1*sin(i * 8), 90 + 60*sin(i * 3) + objcount * 4 * dir, DS_RICE_S_RED, 0);
                    CreateShotA1(curx, cury, 1 + 1*sin(i * 8), 90 - 60*sin(i * 3) + objcount * 4 * dir, DS_RICE_S_RED, 0);
		}
		
            }
            objcount++;
            yield;
        }
		if (GetCommonData("Difficulty", 1) == 1){
            if (objcount % 40 == 0) { //control firing rate
                let curx = ObjRender_GetX(obj);
                let cury = ObjRender_GetY(obj);
		ascent(i in 0..36) {
		PlaySE(shootSFX2);
                    CreateShotA1(curx, cury, 1 + 1*sin(i * 8), 90 + 60*sin(i * 3) + objcount * 4 * dir, DS_RICE_S_RED, 0);
                    CreateShotA1(curx, cury, 1 + 1*sin(i * 8), 90 - 60*sin(i * 3) + objcount * 4 * dir, DS_RICE_S_RED, 0);
		}
		
            }
			}
			
			if (GetCommonData("Difficulty", 0) == 0){
            if (objcount % 65 == 0) { //control firing rate
                let curx = ObjRender_GetX(obj);
                let cury = ObjRender_GetY(obj);
		ascent(i in 0..36) {
		PlaySE(shootSFX2);
                    CreateShotA1(curx, cury, 1 + 1*sin(i * 8), 90 + 60*sin(i * 3) + objcount * 4 * dir, DS_RICE_S_RED, 0);
                    CreateShotA1(curx, cury, 1 + 1*sin(i * 8), 90 - 60*sin(i * 3) + objcount * 4 * dir, DS_RICE_S_RED, 0);
		}
		
            }
			}
            objcount++;
            yield;
        }
		}
        Obj_Delete(obj);
    }
	

task mainTask { 
    MagicCircle;
	renderBoss;
    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,40);
     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,bossY,0);
       ObjRender_SetAngleZ(circleObj,spin);
       ObjRender_SetAlpha(circleObj,alpha);

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

task movement {
wait(100);
loop{
wait(150);
	fire;
	}

}

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

}

@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 {

   let angleT = angleToPlayer;
   
    loop(13){
	 CreateShotA1(bossX,bossY,1,angleT,DS_BUTTERFLY_R_PURPLE,10);
		CreateShotA1(bossX,bossY,1,angleT-10,DS_BUTTERFLY_R_PURPLE,10);
		 CreateShotA1(bossX,bossY,1,angleT+10,DS_BUTTERFLY_R_PURPLE,10);
		}
    }


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 TDrawLoopFamiliar(obj){
  let objFami = ObjPrim_Create(OBJ_SPRITE_2D);
  // things for getting it set up (SetTexture, SetRenderPriority, etc)
  let familiar = GetCurrentScriptDirectory() ~ "sprite/familiar.png";
   ObjPrim_SetTexture(objFami, familiar);

    ObjSprite2D_SetSourceRect(objFami, 64, 1, 127, 64);
    ObjSprite2D_SetDestCenter(objFami);
	Obj_SetRenderPriorityI(objFami,70);
	
  let animFrame = 0;
  let interval = 5;
  let x = 64;
  let y = 64;


  while(!Obj_IsDeleted(obj)){
    // Set the position of the sprite to the position of the object you want to have a texture.
	
    ObjRender_SetPosition(obj, ObjMove_GetX(bossObj) + 1 * 64, ObjMove_GetY(bossObj), 1);

    if(animFrame < 15){
      ObjSprite2D_SetSourceRect(objFami, 0, 0, 60, 53);
    }
    if(animFrame >= 20 && animFrame < 25){
      ObjSprite2D_SetSourceRect(objFami, 60, 0, 120, 53);
    }
    if(animFrame >= 25  && animFrame < 30){
      ObjSprite2D_SetSourceRect(objFami, 120, 0, 180, 53);
    }
    if(animFrame >= 30){
      ObjSprite2D_SetSourceRect(objFami, 180, 0, 240, 53);
    }
    if(animFrame > 35){
      // Loop back to the start after 4 frames of animation
      animFrame = 0;
    }
    animFrame ++;
    yield;
  }
 
}


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

}