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

let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/redseal.png";
let animFrame = 0; 
let animFrame2 = 0;
let spellcardSFX = GetCurrentScriptDirectory ~ "SFX/SPELLCARD.wav";
let bossdeathSFX = GetCurrentScriptDirectory ~ "SFX/ATTACK3.wav";
let burstSFX = GetCurrentScriptDirectory ~ "SFX/ATTACK2.wav";
let burst2SFX = GetCurrentScriptDirectory ~ "SFX/LASER.wav";
let objText = ObjText_Create();
let sky = GetCurrentScriptDirectory ~ "sprite/chainbg3.jpg";
let overlay = GetCurrentScriptDirectory ~ "sprite/sealoverlay.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, "hell magic ~ center of ms. devil's heart");
    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);

     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(2200);
       }
       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,1024,768);
	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,172,85); }
			if(animFrame >= 16) { ObjSprite2D_SetSourceRect(bossObj,0,0,85,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 > 20) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}




 task SpellImage{
        let obj = ObjPrim_Create(OBJ_SPRITE_2D);
        let imgpath = GetCurrentScriptDirectory() ~ "sprite/twinseals.png";
        ObjPrim_SetTexture(obj, imgpath);
		Obj_SetRenderPriorityI(obj,60);
	
          ObjSprite2D_SetSourceRect(obj, 0, 1, 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,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);
        
	
}

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


task fire{

if (GetCommonData("Difficulty", 3) == 3){
      
	 let angleT = 90;
    loop(20){
	PlaySE(burstSFX);
        CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, angleT, DS_BALL_S_RED, 5);
		CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, angleT+10, DS_MISSILE_R_RED, 5);
        angleT += 360/20;
    }
	
	}
	if (GetCommonData("Difficulty", 2) == 2){
      
	 let angleT = 90;
    loop(20){
	PlaySE(burstSFX);
        CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, angleT, DS_BALL_S_RED, 5);
		CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, angleT+10, DS_MISSILE_R_RED, 5);
        angleT += 360/16;
    }
	
	}
		if (GetCommonData("Difficulty", 1) == 1){
      
	 let angleT = 90;
    loop(20){
	PlaySE(burstSFX);
        CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, angleT, DS_BALL_S_RED, 5);
		CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, angleT+10, DS_MISSILE_R_RED, 5);
        angleT += 360/10;
    }
	
	}
			if (GetCommonData("Difficulty", 0) == 0){
      
	 let angleT = 90;
    loop(10){
	PlaySE(burstSFX);
        CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, angleT, DS_BALL_S_RED, 5);
		CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, angleT+10, DS_MISSILE_R_RED, 5);
        angleT += 360/10;
    }
	
	}
	
}

task movement {
wait(100);
ascent(i in 0..12){
        SpawnNDLaser(i);
 SpawnNDLaser2(i);
  SpawnNDLaser3(i);
   SpawnNDLaser4(i);
     SpawnNDLaser5(i);
   SpawnNDLaser6(i);
     SpawnNDLaser7(i);
   SpawnNDLaser8(i);


}


}

task movement2{
wait(100);
loop{fire;
wait(20);
fire;}
}

	  
task SpawnNDLaser(offset){
    let objcount = 0; // timer
    let obj = CreateStraightLaserA1(bossX + 30*cos(offset*30), bossY + 30*sin(offset*30), offset * 30, 512, 12, 99999, DS_BEAM_RED, 60);
    while(!Obj_IsDeleted(obj)){ // runs as long as the laser exists
    ObjMove_SetPosition(obj, bossX+30*cos(offset*30+objcount), bossY+30*sin(offset*30+objcount));
    ObjStLaser_SetAngle(obj, offset*30 + objcount);
    objcount += 0.5;
    yield;
    if((objcount*2) % 40 == 0){
    work(obj);
    }
    }
    task work(obj){ //code by kanako
    ObjLaser_SetRenderWidth(obj, 2);
	ObjShot_SetIntersectionEnable(obj, false);
    wait(200);
    ObjLaser_SetRenderWidth(obj, 20);
	ObjShot_SetIntersectionEnable(obj, true);
	wait(100);
    }
    }
	
	task SpawnNDLaser2(offset){
    let objcount = 0; // timer
    let obj = CreateStraightLaserA1(bossX + 30*cos(offset*30), bossY + 30*sin(offset*30), offset * 180, 90, 12, 99999, DS_BEAM_RED, 60);
    while(!Obj_IsDeleted(obj)){ // runs as long as the laser exists
    ObjMove_SetPosition(obj, bossX+120*cos(offset*30+objcount), bossY+120*sin(offset*30+objcount));
    ObjStLaser_SetAngle(obj, offset*270 + objcount);
    objcount += 0.5;
    yield;
    if((objcount*2) % 80 == 40){
    work(obj);
    }
    }
    task work(obj){ //code by kanako
    ObjLaser_SetRenderWidth(obj, 2);
	ObjShot_SetIntersectionEnable(obj, false);
    wait(200);
    ObjLaser_SetRenderWidth(obj, 20);
	ObjShot_SetIntersectionEnable(obj, true);
	wait(100);
    }
    }
	
	task SpawnNDLaser3(offset){
    let objcount = 0; // timer
    let obj = CreateStraightLaserA1(bossX + 30*cos(offset*30), bossY + 30*sin(offset*30), offset * 180, 90, 12, 99999, DS_BEAM_RED, 60);
    while(!Obj_IsDeleted(obj)){ // runs as long as the laser exists
    ObjMove_SetPosition(obj, bossX+160*cos(offset*30+objcount), bossY+160*sin(offset*30+objcount));
    ObjStLaser_SetAngle(obj, offset*270 + objcount);
    objcount += 0.5;
    yield;
    if((objcount*2) % 80 == 40){
    work(obj);
    }
    }
    task work(obj){ //code by kanako
    ObjLaser_SetRenderWidth(obj, 2);
	ObjShot_SetIntersectionEnable(obj, false);
    wait(200);
    ObjLaser_SetRenderWidth(obj, 20);
	ObjShot_SetIntersectionEnable(obj, true);
	wait(100);
    }
    }

task SpawnNDLaser4(offset){
    let objcount = 0; // timer
    let obj = CreateStraightLaserA1(bossX + 30*cos(offset*30), bossY + 30*sin(offset*30), offset * 180, 190, 12, 99999, DS_BEAM_RED, 60);
    while(!Obj_IsDeleted(obj)){ // runs as long as the laser exists
    ObjMove_SetPosition(obj, bossX+200*cos(offset*30+objcount), bossY+200*sin(offset*30+objcount));
    ObjStLaser_SetAngle(obj, offset*270 + objcount);
    objcount += 0.5;
    yield;
    if((objcount*2) % 80 == 40){
    work(obj);
    }
    }
    task work(obj){ //code by kanako
    ObjLaser_SetRenderWidth(obj, 2);
	ObjShot_SetIntersectionEnable(obj, false);
    wait(200);
    ObjLaser_SetRenderWidth(obj, 20);
	ObjShot_SetIntersectionEnable(obj, true);
	wait(100);
    }
    }
	
task SpawnNDLaser5(offset){
    let objcount = 0; // timer
    let obj = CreateStraightLaserA1(bossX + 30*cos(offset*30), bossY + 30*sin(offset*30), offset * 180, 190, 12, 99999, DS_BEAM_RED, 60);
    while(!Obj_IsDeleted(obj)){ // runs as long as the laser exists
    ObjMove_SetPosition(obj, bossX+240*cos(offset*30+objcount), bossY+240*sin(offset*30+objcount));
    ObjStLaser_SetAngle(obj, offset*270 + objcount);
    objcount += 0.5;
    yield;
    if((objcount*2) % 80 == 40){
    work(obj);
    }
    }
    task work(obj){ //code by kanako
    ObjLaser_SetRenderWidth(obj, 2);
	ObjShot_SetIntersectionEnable(obj, false);
    wait(200);
    ObjLaser_SetRenderWidth(obj, 20);
	ObjShot_SetIntersectionEnable(obj, true);
	wait(100);
    }
    }
	
task SpawnNDLaser6(offset){
    let objcount = 0; // timer
    let obj = CreateStraightLaserA1(bossX + 30*cos(offset*30), bossY + 30*sin(offset*30), offset * 180, 190, 12, 99999, DS_BEAM_RED, 60);
    while(!Obj_IsDeleted(obj)){ // runs as long as the laser exists
    ObjMove_SetPosition(obj, bossX+280*cos(offset*30+objcount), bossY+280*sin(offset*30+objcount));
    ObjStLaser_SetAngle(obj, offset*270 + objcount);
    objcount += 0.5;
    yield;
    if((objcount*2) % 80 == 40){
    work(obj);
    }
    }
    task work(obj){ //code by kanako
    ObjLaser_SetRenderWidth(obj, 2);
	ObjShot_SetIntersectionEnable(obj, false);
    wait(200);
    ObjLaser_SetRenderWidth(obj, 20);
	ObjShot_SetIntersectionEnable(obj, true);
	wait(100);
    }
    }
	
task SpawnNDLaser7(offset){
    let objcount = 0; // timer
    let obj = CreateStraightLaserA1(bossX + 30*cos(offset*30), bossY + 30*sin(offset*30), offset * 180, 90, 12, 99999, DS_BEAM_RED, 60);
    while(!Obj_IsDeleted(obj)){ // runs as long as the laser exists
    ObjMove_SetPosition(obj, bossX+320*cos(offset*30+objcount), bossY+320*sin(offset*30+objcount));
    ObjStLaser_SetAngle(obj, offset*270 + objcount);
    objcount += 0.5;
    yield;
    if((objcount*2) % 80 == 40){
    work(obj);
    }
    }
    task work(obj){ //code by kanako
    ObjLaser_SetRenderWidth(obj, 2);
	ObjShot_SetIntersectionEnable(obj, false);
    wait(200);
    ObjLaser_SetRenderWidth(obj, 20);
	ObjShot_SetIntersectionEnable(obj, true);
	wait(100);
    }
    }
	
task SpawnNDLaser8(offset){
    let objcount = 0; // timer
    let obj = CreateStraightLaserA1(bossX + 30*cos(offset*30), bossY + 30*sin(offset*30), offset * 180, 90, 12, 99999, DS_BEAM_RED, 60);
    while(!Obj_IsDeleted(obj)){ // runs as long as the laser exists
    ObjMove_SetPosition(obj, bossX+360*cos(offset*30+objcount), bossY+360*sin(offset*30+objcount));
    ObjStLaser_SetAngle(obj, offset*270 + objcount);
    objcount += 1;
    yield;
    if((objcount*2) % 80 == 40){
    work(obj);
    }
    }
    task work(obj){ //code by kanako
    ObjLaser_SetRenderWidth(obj, 2);
	ObjShot_SetIntersectionEnable(obj, false);
    wait(200);
    ObjLaser_SetRenderWidth(obj, 20);
	ObjShot_SetIntersectionEnable(obj, true);
	wait(100);
    }
    }

task TDrawLoop {
    let imgBoss = GetCurrentScriptDirectory() ~ "sprite/redseal.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;
	}
     PlaySE(bossdeathSFX);   
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(20);	
	CloseScript(GetOwnScriptID());

}