#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["2spellcard5"]
#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/DEFEATED.wav";
let objText = ObjText_Create();
let shootSFX = GetCurrentScriptDirectory ~ "SFX/TWINKLE.wav";
let shootSFX2 = GetCurrentScriptDirectory ~ "SFX/ATTACK.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,190,120,10); //movement function

 ObjText_SetText(objText, "to die of an infection after being bit by rotton teeth");
    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, 305);
   ObjRender_SetX(objText, 100 - 50);
	ObjRender_SetY(objText, 20);
   
              LoadSound (spellcardSFX);
			  LoadSound(shootSFX2);
        LoadSound(bossdeathSFX);
     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);
	
	 if(count == 0){
            ascent(i in 0..6) {CreateOrb(i, 2, 4, 100);}
        }
        count++;

    yield;
}

 task CreateOrb(ID, num, dir, rad) {
        let objcount = 0; //counter to control firing of bullets and movement
        let angleO = ID*360/num; //Initial angle (will be updated)
        //obtain object ID
	let graphic = GetCurrentScriptDirectory() ~ "sprite/familiar2.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);
		wait(100);
        while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) { //linked to the parent enemy's life
            //ID*360/num sets the angle relative to the other familiars. objcount*dir handles the rotation
            angleO = ID*360/num + objcount*dir; //Update angle
            ObjRender_SetPosition(obj, ObjMove_GetX(bossObj) + rad*cos(angleO), 
                                       ObjMove_GetY(bossObj) + rad*sin(angleO), 1); 
            ObjRender_SetAngleZ(obj, angleO); 
			
			  if(animFrame < 20){
      ObjSprite2D_SetSourceRect(obj, 0, 0, 60, 53);
    }
    if(animFrame >= 30 && animFrame < 50){
      ObjSprite2D_SetSourceRect(obj, 60, 0, 120, 53);
    }
    if(animFrame >= 50 && animFrame < 70){
      ObjSprite2D_SetSourceRect(obj, 120, 0, 180, 53);
    }
    if(animFrame >= 70){
      ObjSprite2D_SetSourceRect(obj, 180, 0, 240, 53);
    }
    if(animFrame > 80){
      // Loop back to the start after 4 frames of animation
      animFrame = 0;
    }
    animFrame ++;
    yield;
	
	if (GetCommonData("Difficulty", 3) == 3){
            if (objcount % 16 == 0) { //control firing rate
                let currx = ObjRender_GetX(obj);
                let curry = ObjRender_GetY(obj);
                ascent(i in 0..2) {
				PlaySE(shootSFX); 
                    CreateShotA1(currx, curry, 3, rand(0, 360), DS_ICE_ORANGE	, 0);
					
					CreateShotA1(currx, curry, 3, rand(0, 360), DS_ICE_ORANGE	, 0);
                
				}
				
            }
			}
				if (GetCommonData("Difficulty", 2) == 2){
            if (objcount % 20 == 0) { //control firing rate
                let currx = ObjRender_GetX(obj);
                let curry = ObjRender_GetY(obj);
                ascent(i in 0..2) {
				PlaySE(shootSFX); 
                    CreateShotA1(currx, curry, 3, rand(0, 360), DS_ICE_ORANGE	, 0);
					
					CreateShotA1(currx, curry, 3, rand(0, 360), DS_ICE_ORANGE	, 0);
                
				}
				
            }
			}
						if (GetCommonData("Difficulty", 1) == 1){
            if (objcount % 26 == 0) { //control firing rate
                let currx = ObjRender_GetX(obj);
                let curry = ObjRender_GetY(obj);
                ascent(i in 0..2) {
				PlaySE(shootSFX); 
                    CreateShotA1(currx, curry, 3, rand(0, 360), DS_ICE_ORANGE	, 0);
					
					CreateShotA1(currx, curry, 3, rand(0, 360), DS_ICE_ORANGE	, 0);
                
				}
				
            }
			}
						if (GetCommonData("Difficulty", 0) == 0){
            if (objcount % 32 == 0) { //control firing rate
                let currx = ObjRender_GetX(obj);
                let curry = ObjRender_GetY(obj);
                ascent(i in 0..2) {
				PlaySE(shootSFX); 
                    CreateShotA1(currx, curry, 3, rand(0, 360), DS_ICE_ORANGE	, 0);
					
					CreateShotA1(currx, curry, 3, rand(0, 360), DS_ICE_ORANGE	, 0);
                
				}
				
            }
			}
            objcount++;
            yield;
        }
        Obj_Delete(obj);
    }
	
	
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 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 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,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 {
	
	loop { 
	
	    wait(100);
		  
		  fire2;
		
		
	
	}
}

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

}

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

   

		
   

}   
task fire2{

if (GetCommonData("Difficulty", 3) == 3){
 let angleT = angleToPlayer;

ascent(i in 0..2){

ascent(i in 0..20){
			let obj = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 5);
			let obj2 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE, 10);
			let obj3 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 15);
			let obj4 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE, 20);
			let obj5 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 25);
			let obj6 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 30);
			SnakePattern(obj,angleT);
			SnakePattern(obj2,angleT);
			SnakePattern(obj3,angleT);
			SnakePattern(obj4,angleT);
			SnakePattern(obj5,angleT);
			SnakePattern(obj6,angleT);
			ReflectSides(obj);
			ReflectSides(obj2);
			ReflectSides(obj3);
			ReflectSides(obj4);
			ReflectSides(obj5);
			ReflectSides(obj6);
			
		
			
			angleT += 360/40;
		
		}
		}
		}
		if (GetCommonData("Difficulty", 2) == 2){

 let angleT = angleToPlayer;
ascent(i in 0..2){

ascent(i in 0..20){
			
			let obj = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 5);
			let obj2 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE, 10);
			let obj3 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 15);
			let obj4 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE, 20);
			let obj5 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 25);
			let obj6 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 30);
			
			SnakePattern(obj,angleT);
			SnakePattern(obj2,angleT);
			SnakePattern(obj3,angleT);
			SnakePattern(obj4,angleT);
			SnakePattern(obj5,angleT);
			SnakePattern(obj6,angleT);
		
			ReflectSides(obj);
			ReflectSides(obj2);
			ReflectSides(obj3);
			ReflectSides(obj4);
			ReflectSides(obj5);
			ReflectSides(obj6);
	
			
		
			
			angleT += 360/26;
		
		}
		}
		}
		
		if (GetCommonData("Difficulty", 1) == 1){

 let angleT = angleToPlayer;
ascent(i in 0..2){

ascent(i in 0..20){
			
			let obj = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 5);
			let obj2 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE, 10);
			let obj3 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 15);
			let obj4 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE, 20);
			let obj5 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 25);
			let obj6 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 30);
			
			SnakePattern(obj,angleT);
			SnakePattern(obj2,angleT);
			SnakePattern(obj3,angleT);
			SnakePattern(obj4,angleT);
			SnakePattern(obj5,angleT);
			SnakePattern(obj6,angleT);
		
			ReflectSides(obj);
			ReflectSides(obj2);
			ReflectSides(obj3);
			ReflectSides(obj4);
			ReflectSides(obj5);
			ReflectSides(obj6);
	
			
		
			
			angleT += 360/18;
		
		}
		}
		}
		
		if (GetCommonData("Difficulty", 0) == 0){

 let angleT = angleToPlayer;
ascent(i in 0..2){

ascent(i in 0..20){
			
			let obj = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 5);
			let obj2 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE, 10);
			let obj3 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 15);
			let obj4 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE, 20);
			let obj5 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 25);
			let obj6 = CreateShotA2(bossX, bossY, 4, 90, 0.01, 3, DS_KNIFE_KOUMA_PURPLE , 30);
			
			SnakePattern(obj,angleT);
			SnakePattern(obj2,angleT);
			SnakePattern(obj3,angleT);
			SnakePattern(obj4,angleT);
			SnakePattern(obj5,angleT);
			SnakePattern(obj6,angleT);
		
			ReflectSides(obj);
			ReflectSides(obj2);
			ReflectSides(obj3);
			ReflectSides(obj4);
			ReflectSides(obj5);
			ReflectSides(obj6);
	
			
		
			
			angleT += 360/10;
		
		}
		}
		}
		
}		
		
task ReflectSides(obj){
	while(ObjMove_GetX(obj)>0 && ObjMove_GetX(obj)<GetStgFrameWidth){yield;}
	if(ObjMove_GetX(obj)<=0){
	   
	   ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
	   
	   }
	   if (ObjMove_GetX(obj)>=GetStgFrameWidth){
  	      ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj)); 
	  
		}
		}
		
		
task SnakePattern(obj, angleT){
	let wvel =12;
	ascent(i in 0..12){
		ObjMove_AddPatternA2(obj, i * 9, ObjMove_GetSpeed(obj), angleT, 1, wvel, 3);
		wvel *= -1;
		wait(100);
		wvel *= 0;
		wait(20);
	wvel *= 0;
	wait(20);
	wvel *= 0;
	wait(20);
	wvel *= 0;
	wait(20);
	wvel *= 0;
	wait(20);
	wvel *= 0;
	wait(20);
	wvel *= 0;
	
	}
	
}

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;
	}StartSlow(TARGET_ALL, 20);
        PlaySE(bossdeathSFX);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(30);	
	StopSlow(TARGET_ALL);
	Obj_Delete(bossObj);
	wait(60);
	CloseScript(GetOwnScriptID());

}