#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["midspelltest"]
#Text["stage 2 midboss"]
#include "script/default_system/Default_ShotConst.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/gefsprite.png";
let animFrame = 0; 
let animFrame2 = 0;
let spellcardSFX = GetCurrentScriptDirectory ~ "SFX/SPELLCARD.wav";
let bossdeathSFX = GetCurrentScriptDirectory ~ "SFX/DEFEATED.wav";
let burstSFX = GetCurrentScriptDirectory ~ "SFX/ATTACK3.wav";
let burst2SFX = GetCurrentScriptDirectory ~ "SFX/LASER.wav";
let objText = ObjText_Create();
let objText2 = ObjText_Create();
let sky = GetCurrentScriptDirectory ~ "sprite/gefbg.jpg";
let overlay = GetCurrentScriptDirectory ~ "sprite/gefoverlay.png";
let circleObj = ObjPrim_Create(OBJ_SPRITE_2D);

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

let CSD = GetCurrentScriptDirectory;

//textures



@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,186,210,7); //movement function

   
              LoadSound (spellcardSFX);
        LoadSound(bossdeathSFX);
     PlaySE(spellcardSFX);
	 LoadSound(burstSFX);
	 LoadSound(burst2SFX);
 ObjText_SetText(objText, "unknown ~ to hell, to the land of mist");
    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, "GEF THE MONGOOSE");
    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, 1, 1, 1);
    ObjText_SetFontBorderWidth(objText2, 1);
   ObjText_SetHorizontalAlignment(objText2, ALIGNMENT_LEFT);
    ObjText_SetMaxWidth(objText2, 450);
    ObjRender_SetX(objText2, 5);
	ObjRender_SetY(objText2, 10);

     mainTask;
	 

}




@MainLoop {
     bossX = ObjMove_GetX(bossObj);
     bossY = ObjMove_GetY(bossObj);
     
//enemy collision
    ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
    ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
   
  SetCommonData("hee",true);
   SetCommonData("hee2",true); 
    yield;
}


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


} 


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

}

@Finalize {

}
task renderBG {

 
        let scrollTex = 0;

        //background
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20); 
	ObjPrim_SetTexture(obj,sky); 
	ObjSprite2D_SetSourceRect(obj,0,0,1000,1416);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);

        //spell card overlay
        let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
        Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,overlay); 
        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);
} 




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,85,85);
        ObjSprite2D_SetDestCenter(bossObj);
        ObjRender_SetScaleXYZ(bossObj,1,1,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,0,0,85,85); }
			if(animFrame >= 10 && animFrame < 15) { ObjSprite2D_SetSourceRect(bossObj,85,0,170,85); }
			if(animFrame >= 15 && animFrame < 20) { ObjSprite2D_SetSourceRect(bossObj,85,0,170,85); }
			if(animFrame >= 25) { ObjSprite2D_SetSourceRect(bossObj,172,0,254,85); }
			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);
			if(animFrame < 10) { ObjSprite2D_SetSourceRect(bossObj,0,0,85,85); }
			if(animFrame >= 10 && animFrame < 15) { ObjSprite2D_SetSourceRect(bossObj,85,0,172,85); }
			if(animFrame >= 16) { ObjSprite2D_SetSourceRect(bossObj,0,0,85,85); }
		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);
			if(animFrame < 10) { ObjSprite2D_SetSourceRect(bossObj,0,0,85,85); }
			if(animFrame >= 10 && animFrame < 15) { ObjSprite2D_SetSourceRect(bossObj,85,0,172,85); }
			if(animFrame >= 16) { ObjSprite2D_SetSourceRect(bossObj,0,0,85,85); }
		}
		animFrame++;		// count animFrame. (++ is +1)
		animFrame2+=2;		// count animFrame2.
		if(animFrame > 26) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}




 task SpellImage{
        let obj = ObjPrim_Create(OBJ_SPRITE_2D);
        let imgpath = GetCurrentScriptDirectory() ~ "sprite/gefspell.png";
        ObjPrim_SetTexture(obj, imgpath);
		Obj_SetRenderPriorityI(obj,60);
	
        ObjSprite2D_SetSourceRect(obj, 0, 0, 512, 512); 
        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,150);
		wait(5);
		ObjRender_SetAlpha(obj,100);
		wait(5);
		ObjRender_SetAlpha(obj,50);
		wait(5);
		ObjRender_SetAlpha(obj,20);
		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);
        
	
}

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


task fire{
      
	 let angleT = angleToPlayer;
    loop(20){

        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, angleT, DS_BALL_M_R_WHITE, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(60);
		ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	
		wait(200);}
		
		yield;
    }
	}
	
	task fire2{
      
	 let angleT = angleToPlayer;
    loop(10){
	PlaySE(burstSFX);
        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2.3, rand(angleT, angleT+20), DS_ICE_R_WHITE, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0.6);
		
		wait(60);
		ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 1.5);
	
		wait(200);}
		
		yield;
    }
	}
	
	task fire3{
      
	 let angleT = angleToPlayer;
    loop(10){

        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2.8, rand(angleT, angleT-20), DS_MISSILE_R_WHITE	, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(60);
		ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 1.5);
	
		wait(200);}
		
		yield;
    }
	
	}
	
	task fire4{
      
	 let angleT = angleToPlayer;
    loop(10){
	PlaySE(burstSFX);
        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 1.8, rand(angleT, angleT-20), DS_LIGHT_R_WHITE	, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(60);
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0.6);
		
		wait(10);
			ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	
		wait(200);}
		
		yield;
    }
	
	}
	
	
	///---------------------
	
	
	task fire5{
      
	 let angleT = angleToPlayer;
    loop(30){

        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 0.5, angleT+180, DS_BALL_M_WHITE, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(90);
		ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	
		wait(1200);}
		
		yield;
    }
	}
	
	task fire6{
      if (GetCommonData("Difficulty", 3) == 3){
	 let angleT = angleToPlayer;
    loop(10){
	PlaySE(burstSFX);
        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 0.8, rand(angleT+180, angleT+180+100), DS_ICE_WHITE, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(90);
		ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 1.5);
	
		wait(1200);}
		
		yield;
    }
	}
	if (GetCommonData("Difficulty", 2) == 2){
	 let angleT = angleToPlayer;
    loop(10){
	PlaySE(burstSFX);
        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 0.8, rand(angleT+180, angleT+180+100), DS_ICE_WHITE, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(100);
		ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 1.5);
	
		wait(1200);}
		
		yield;
    }
	}
	}
	
	task fire7{
      	if (GetCommonData("Difficulty", 3) == 3){
	 let angleT = angleToPlayer;
    loop(10){

        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 1.5, rand(angleT+180, angleT+180-100), DS_MISSILE_WHITE	, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(90);
		ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 1.5);
	
		wait(1200);}
		
		yield;
    }
	}
	if (GetCommonData("Difficulty", 2) == 2){
	 let angleT = angleToPlayer;
    loop(20){

        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 1.5, rand(angleT+180, angleT+180-100), DS_MISSILE_WHITE	, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(90);
		ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 1.5);
	
		wait(1200);}
		
		yield;
    }
	}
	if (GetCommonData("Difficulty", 1) == 1){
	 let angleT = angleToPlayer;
    loop(10){

        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 1.5, rand(angleT+180, angleT+180-100), DS_MISSILE_WHITE	, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(120);
		ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 1.5);
	
		wait(1200);}
		
		yield;
    }
	}
	
	}
	
	task fire8{
	
	if (GetCommonData("Difficulty", 3) == 3){
      
	 let angleT = angleToPlayer;
    loop(10){
	PlaySE(burstSFX);
        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 1, rand(angleT+180, angleT+180-100), DS_LIGHT_WHITE	, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(90);
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0.6);
		
		wait(10);
			ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	
		wait(1200);}
		
		yield;
    }
	}
	if (GetCommonData("Difficulty", 2) == 2){
      
	 let angleT = angleToPlayer;
    loop(10){
	PlaySE(burstSFX);
        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 1, rand(angleT+180, angleT+180-100), DS_LIGHT_WHITE	, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(100);
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0.6);
		
		wait(10);
			ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	
		wait(1200);}
		
		yield;
    }
	}
	
	if (GetCommonData("Difficulty", 1) == 1){
      
	 let angleT = angleToPlayer;
    loop(10){
	PlaySE(burstSFX);
        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 1, rand(angleT+180, angleT+180-100), DS_LIGHT_WHITE	, 5);
		
		
        angleT += 360/20;
		loop{
		ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
		
		wait(150);
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0.6);
		
		wait(10);
			ObjRender_SetScaleXYZ(obj, 0.8, 0.8, 0.8);
		wait(10);
		ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	
		wait(1200);}
		
		yield;
    }
	}
	
	}
	


task movement {
wait(100);
loop{fire8;
fire5;
fire6;
fire7;
wait(20);
fire5;
fire6;
fire7;
fire8;}



}

task movement2{
wait(100);
loop{fire;
fire2;
fire3;
fire4;
wait(5);
fire2;
fire3;
fire4;
fire;}
}

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


task TShake()
	{
		loop(40){
			Set2DCameraFocusX(GetStgFrameWidth / 2 + rand(-8, 8));
			Set2DCameraFocusY(GetStgFrameHeight / 2 + rand(-8, 8));
			yield;
		}

		Reset2DCamera();
	}

task TEnd 
{
while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}StartSlow(TARGET_ALL, 20);
        
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(30);	
	StopSlow(TARGET_ALL);
	PlaySE(bossdeathSFX);
	TShake();
	Obj_Delete(bossObj);
	wait(40);
	CloseScript(GetOwnScriptID());
}