#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["3spellcard3"]
#Text["3rdboss"]
#include "script/default_system/Default_ShotConst.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/teachersheet.png";
let animFrame = 0; 
let animFrame2 = 0;
let spellcardSFX = GetCurrentScriptDirectory ~ "SFX/SPELLCARD.wav";
let bossdeathSFX = GetCurrentScriptDirectory ~ "SFX/DEFEATED.wav";
let objText = ObjText_Create();
let objText2 = ObjText_Create();
let shootSFX = GetCurrentScriptDirectory ~ "SFX/POWER UP.wav";
let shootSFX2 = GetCurrentScriptDirectory ~ "SFX/TWINKLE.wav";
let BG = GetCurrentScriptDirectory ~ "sprite/opsinBG.png";
let overlay1 = GetCurrentScriptDirectory ~ "sprite/school.png";
let count = 0;

let CSD = GetCurrentScriptDirectory;

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


@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, "opposite signs ~ borrowed from an angel");
    ObjText_SetFontSize(objText, 14);
    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, 450);
    ObjRender_SetX(objText, 5 - 80);
	ObjRender_SetY(objText, 20);
	//--------
	ObjText_SetText(objText2, "opposite signs ~ borrowed from a demon");
    ObjText_SetFontSize(objText2, 14);
    ObjText_SetFontType(objText2, "Russell Square Regular");
    ObjText_SetFontBold(objText2, true);
    ObjText_SetFontColorTop(objText2, 255, 255, 255);
    ObjText_SetFontColorBottom(objText2, 255, 255, 255);
    ObjText_SetFontBorderType(objText2, BORDER_FULL);
    ObjText_SetFontBorderColor(objText2, 100, 64, 192);
    ObjText_SetFontBorderWidth(objText2, 1);
   ObjText_SetHorizontalAlignment(objText2, ALIGNMENT_RIGHT);
    ObjText_SetMaxWidth(objText2, 450);
    ObjRender_SetX(objText2, 5 - 80);
	ObjRender_SetY(objText2, 40);
	//----
              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, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 32); 
        ObjEnemy_SetIntersectionCircleToPlayer(bossObj, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 24);
      
        
        
        count++;
        yield;
    }
    
    
 

 


task renderBG {

 
        let scrollTex = 0;

        //background
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20); 
	ObjPrim_SetTexture(obj,BG); 
	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;
    renderBG;
	movement;
	
	wait(460);
	PlaySE(shootSFX);
        TEnd;


} 

task movement {
wait(120);  

  loop(50){
	
	fire;
	wait(120);
	fire;
	fire2;
	fire3;
	wait(40);
	fire2;
	
	}
	
	
	}
	
task fire {
let angleT = angleToPlayer;

if (GetCommonData("Difficulty", 3) == 3){
ascent(i in 0..390){
PlaySE(shootSFX2);
angleT += 360/90;
let obj = CreateShotA1(bossX, bossY, 1, angleT, DS_LIGHT_R_WHITE, 20);
		 
		angleT += 360/120;
		  ObjMove_AddPatternA3(obj, 70, 1, angleT, 0.5, 3 , 3, DS_LIGHT_RED);
		 ObjMove_AddPatternA3(obj, 300, 2, angleT, 0.5, 0 , 3, DS_LIGHT_RED);
		angleT += 360/120;
		yield; }
		
		}
		
		if (GetCommonData("Difficulty", 2) == 2){
ascent(i in 0..390){
PlaySE(shootSFX2);
angleT += 360/90;
let obj = CreateShotA1(bossX, bossY, 1, angleT, DS_LIGHT_R_WHITE, 20);
		 
		angleT += 360/120;
		wait(2);
		  ObjMove_AddPatternA3(obj, 70, 1, angleT, 0.5, 3 , 3, DS_LIGHT_RED);
		 ObjMove_AddPatternA3(obj, 300, 2, angleT, 0.5, 0 , 3, DS_LIGHT_RED);
		angleT += 360/120;
		yield; }
		
		}
		
				if (GetCommonData("Difficulty", 1) == 1){
ascent(i in 0..390){
PlaySE(shootSFX2);
angleT += 360/90;
let obj = CreateShotA1(bossX, bossY, 1, angleT, DS_LIGHT_R_WHITE, 20);
		 
		angleT += 360/120;
		wait(5);
		  ObjMove_AddPatternA3(obj, 70, 1, angleT, 0.5, 3 , 3, DS_LIGHT_RED);
		 ObjMove_AddPatternA3(obj, 300, 2, angleT, 0.5, 0 , 3, DS_LIGHT_RED);
		angleT += 360/120;
		yield; }
		
		}
		
						if (GetCommonData("Difficulty", 0) == 0){
ascent(i in 0..390){
PlaySE(shootSFX2);
angleT += 360/90;
let obj = CreateShotA1(bossX, bossY, 1, angleT, DS_LIGHT_R_WHITE, 20);
		 
		angleT += 360/120;
		wait(10);
		  ObjMove_AddPatternA3(obj, 70, 1, angleT, 0.5, 3 , 3, DS_LIGHT_RED);
		 ObjMove_AddPatternA3(obj, 300, 2, angleT, 0.5, 0 , 3, DS_LIGHT_RED);
		angleT += 360/120;
		yield; }
		
		}
		
}

task fire2 {
let angleT = angleToPlayer;

if (GetCommonData("Difficulty", 3) == 3){
ascent(i in 0..300){
angleT += 360/2;
let obj = CreateShotA1(bossX, bossY, 1, angleT, DS_LIGHT_WHITE, 20);
		 
		angleT += 360/90;
		  ObjMove_AddPatternA3(obj, 70, 1, angleT, 0.5, 3 , 3, DS_LIGHT_SKY);
		 ObjMove_AddPatternA3(obj, 180, 4, angleT, 0.5, 0 , 3, DS_LIGHT_SKY);
		angleT += 360/90;
		yield; }
		
		
		
}

if (GetCommonData("Difficulty", 2) == 2){
ascent(i in 0..300){
angleT += 360/2;
let obj = CreateShotA1(bossX, bossY, 1, angleT, DS_LIGHT_WHITE, 20);
		 
		angleT += 360/90;
		wait(2);
		  ObjMove_AddPatternA3(obj, 70, 1, angleT, 0.5, 3 , 3, DS_LIGHT_SKY);
		 ObjMove_AddPatternA3(obj, 180, 4, angleT, 0.5, 0 , 3, DS_LIGHT_SKY);
		angleT += 360/90;
		yield; }
		
		
		
}

if (GetCommonData("Difficulty", 1) == 1){
ascent(i in 0..300){
angleT += 360/2;
let obj = CreateShotA1(bossX, bossY, 1, angleT, DS_LIGHT_WHITE, 20);
		 
		angleT += 360/90;
		wait(5);
		  ObjMove_AddPatternA3(obj, 70, 1, angleT, 0.5, 3 , 3, DS_LIGHT_SKY);
		 ObjMove_AddPatternA3(obj, 180, 4, angleT, 0.5, 0 , 3, DS_LIGHT_SKY);
		angleT += 360/90;
		yield; }
		
		
		
}
if (GetCommonData("Difficulty", 0) == 0){
ascent(i in 0..300){
angleT += 360/2;
let obj = CreateShotA1(bossX, bossY, 1, angleT, DS_LIGHT_WHITE, 20);
		 
		angleT += 360/90;
		wait(10);
		  ObjMove_AddPatternA3(obj, 70, 1, angleT, 0.5, 3 , 3, DS_LIGHT_SKY);
		 ObjMove_AddPatternA3(obj, 180, 4, angleT, 0.5, 0 , 3, DS_LIGHT_SKY);
		angleT += 360/90;
		yield; }
		
		
		
}


}

task fire3 {
wait(300);
let angleT = 60;
ascent(i in 0..600){
angleT += 360/24;
let obj = CreateShotA1(bossX, bossY, 3, angleT, DS_SCALE_A_YELLOW, 20);
		 
		angleT += 360/24;
		
		yield; }
		
		
		
}
@Event {
//boss health and timer
       alternative(GetEventType())
	   
	   
       case(EV_REQUEST_LIFE) {
          SetScriptResult(3000);
       }
	   
       case(EV_REQUEST_TIMER) {
          SetScriptResult(70);
     }
	 


}

@Finalize {

}


function wait(w) { loop(w) { yield; } }
task SpellImage{
        let obj = ObjPrim_Create(OBJ_SPRITE_2D);
        let imgpath = GetCurrentScriptDirectory() ~ "sprite/spellteacher1.png";
        ObjPrim_SetTexture(obj, imgpath);
		Obj_SetRenderPriorityI(obj,60);
        ObjSprite2D_SetSourceRect(obj, 0, 1, 500,684); 
        ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetScaleXYZ(obj,0.8,0.8,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 renderBoss {
	let dir;
        let speed;

        //texture the boss and set the center
        ObjPrim_SetTexture(bossObj,imgBoss);
        ObjSprite2D_SetSourceRect(bossObj,0,0,131,284);
        ObjSprite2D_SetDestCenter(bossObj);
        ObjRender_SetScaleXYZ(bossObj,0.5,0.5,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 < 5) { ObjSprite2D_SetSourceRect(bossObj,0,0,131,288); }
			if(animFrame >= 10 && animFrame < 25) { ObjSprite2D_SetSourceRect(bossObj,150,3,287,290); }
			if(animFrame >= 25) { ObjSprite2D_SetSourceRect(bossObj,300,3,433,290); }
			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,742,0,884,290); }
			if(animFrame2 >= 15 && animFrame2 < 25) { ObjSprite2D_SetSourceRect(bossObj,905,0,1058,290); }
			if(animFrame2 >= 25 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,1072,14,1236,296); }
			if(animFrame2 >= 30 && animFrame2 < 35) { ObjSprite2D_SetSourceRect(bossObj,905,12,1058,290); }
			if(animFrame2 >= 40 && animFrame2 < 45) { ObjSprite2D_SetSourceRect(bossObj,1072,14,1236,296); }
			if(animFrame2 >= 45 && animFrame2 < 50) { ObjSprite2D_SetSourceRect(bossObj,905,12,1058,290); }
		}
		else if(cos(dir) > 0){ 
		ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,742,0,884,290); }
			if(animFrame2 >= 15 && animFrame2 < 25) { ObjSprite2D_SetSourceRect(bossObj,905,0,1058,290); }
			if(animFrame2 >= 25 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,1072,14,1236,296); }
			if(animFrame2 >= 30 && animFrame2 < 35) { ObjSprite2D_SetSourceRect(bossObj,905,12,1058,290); }
			if(animFrame2 >= 40 && animFrame2 < 45) { ObjSprite2D_SetSourceRect(bossObj,1072,14,1236,296); }
			if(animFrame2 >= 45 && animFrame2 < 50) { ObjSprite2D_SetSourceRect(bossObj,905,12,1058,290); }
		}
		animFrame++;		// count animFrame. (++ is +1)
		animFrame2+=2;		// count animFrame2.
		if(animFrame > 25) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}


function angleToPlayer {
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}
     
        
   
	
task TShake()
	{
		loop(40){
			Set2DCameraFocusX(GetStgFrameWidth / 2 + rand(-8, 8));
			Set2DCameraFocusY(GetStgFrameHeight / 2 + rand(-8, 8));
			yield;
		}

		Reset2DCamera();
	}

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);
	TShake();
	Obj_Delete(bossObj);
	wait(40);
	CloseScript(GetOwnScriptID());

}