#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["spellcard5"]
#Text["Code by Sawmey[r]"]
#include "script/default_system/Default_ShotConst.txt"
#include "script/sample/nociception/BFX/BFX.dnh"


let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/lightgirlsheet.png";
let animFrame = 0; 
let animFrame2 = 0;
let spellcardSFX = GetCurrentScriptDirectory ~ "SFX/SPELLCARD.wav";
let bossdeathSFX = GetCurrentScriptDirectory ~ "SFX/DEFEATED.wav";
let shootSFX = GetCurrentScriptDirectory ~ "SFX/TWINKLE.wav";
let attackSFX = GetCurrentScriptDirectory ~ "SFX/ATTACK3.wav";
let deadSFX = GetCurrentScriptDirectory ~ "SFX/DEFEATED.wav";
let sky = GetCurrentScriptDirectory ~ "sprite/starbg.jpg";
let overlay = GetCurrentScriptDirectory ~ "sprite/swirl.png";
let textoverlay = GetCurrentScriptDirectory ~ "sprite/spelltext.png";
let objText = ObjText_Create();
let circleObj = ObjPrim_Create(OBJ_SPRITE_2D);

let CSD = GetCurrentScriptDirectory;

//textures




@Initialize {
   BFX_Init();
    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
	

	createText;

		
   LoadSound(attackSFX);
   LoadSound(deadSFX);

   LoadSound (shootSFX);
              LoadSound (spellcardSFX);
        LoadSound(bossdeathSFX);
     PlaySE(spellcardSFX);
	 

     mainTask;

}

task createText {
ObjText_SetText(objText, "spectrum ~ parallel rivers of light");
    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, 450);
    ObjRender_SetX(objText, 5 - 80);
	ObjRender_SetY(objText, 20);
	   
	
}

task moveText {
let var4 = 0;

loop (10){
    ObjRender_SetPosition(objText, var4, var4, 0);
    var4 += 4;
    yield;}
}

@MainLoop {
     bossX = ObjMove_GetX(bossObj);
     bossY = ObjMove_GetY(bossObj);
     
//enemy collision
    ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
    ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
   

    yield;
}


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


} 

task MagicCircle
{    let alpha = 0;
      let spin = 0;
	  let path = GetCurrentScriptDirectory ~ "sprite/lightcircle.png";
	  

     ObjPrim_SetTexture(circleObj,path);
     ObjRender_SetBlendType(circleObj,BLEND_ADD_ARGB);
     Obj_SetRenderPriorityI(circleObj,40);
     ObjRender_SetAlpha(circleObj,120);
     ObjRender_SetScaleXYZ(circleObj,0.8,0.8,0);
     ObjSprite2D_SetSourceRect(circleObj,0,0,182,182);
     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;
  
 }
 

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

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

@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.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,75,95); }
			if(animFrame >= 5 && animFrame < 10) { ObjSprite2D_SetSourceRect(bossObj,75,0,150,95); }
			if(animFrame >= 15) { ObjSprite2D_SetSourceRect(bossObj,150,0,220,95); }
			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,0,102,75,192); }
			if(animFrame2 >= 15 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,75,0,150,95); }
			if(animFrame2 >= 30) { ObjSprite2D_SetSourceRect(bossObj,155,0,222,95); }
		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,0,102,75,192); }
			if(animFrame2 >= 15 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,75,0,150,95); }
			if(animFrame2 >= 30) { ObjSprite2D_SetSourceRect(bossObj,155,0,222,95); }
		}
		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/baby.png";
        ObjPrim_SetTexture(obj, imgpath);
		Obj_SetRenderPriorityI(obj,60);
        ObjSprite2D_SetSourceRect(obj, 0, 1, 512, 522); 
        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 movement {
	wait(100);
	loop { 
	
		wait(10);
		
		wait(20);
		fire2;
		wait(20);
		fire2;
		wait(20);
		fire2;
		wait(20);
		fire2;
		wait(30);
		ObjMove_SetDestAtSpeed(bossObj,200,120,8);		// move to the right
		wait(45);
		fire2;
		ObjMove_SetDestAtSpeed(bossObj,2,20,8);
		fire2;
		wait(30);
		ObjMove_SetDestAtSpeed(bossObj,350,20,8);
		fire2;
		wait(10);
		fire;
		wait(10);
		fire2;
		wait(20);
		fire;
		
		wait(10);
		fire2;
		wait(45);
		ObjMove_SetDestAtSpeed(bossObj,200,120,8);
	}
}

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

task SnakePattern(obj, angleT){
	let wvel = 1;
	ascent(i in 0..12){
		ObjMove_AddPatternA2(obj, i * 40, ObjMove_GetSpeed(obj), 90, 0.01, wvel, 3);
		wvel *= -1;
	}
}
task SnakePattern2(obj2, angleT){
	let wvel = 1;
	ascent(i in 0..12){
		ObjMove_AddPatternA2(obj2, i * 40, ObjMove_GetSpeed(obj2), 95, 0.01, wvel, 3);
		wvel *= -1;
	}
}

task SnakePattern3(obj3, angleT){
	let wvel = 1;
	ascent(i in 0..12){
		ObjMove_AddPatternA2(obj3, i * 40, ObjMove_GetSpeed(obj3), 85, 0.01, wvel, 3);
		wvel *= -1;
	}
}


task fire{
	let angleT = 0;
	 let dir = angleToPlayer;
	loop(1){
	PlaySE(attackSFX);
	
		let obj = CreateCurveLaserA1(bossX, bossY, 2, dir-40, 120, 20, DS_BEAM_RAINBOW	, 10);
		let obj2 = CreateCurveLaserA1(bossX, bossY, 2, dir, 120, 20, DS_BEAM_RAINBOW	, 10);
		let obj3 = CreateCurveLaserA1(bossX, bossY, 2, dir+40, 120, 20, DS_BEAM_RAINBOW	, 10);
                ObjCrLaser_SetTipDecrement(obj, 0);
				ObjCrLaser_SetTipDecrement(obj2, 0);
				ObjCrLaser_SetTipDecrement(obj3, 0);
		SnakePattern(obj,angleT);
		SnakePattern2(obj2,angleT);
		SnakePattern3(obj3,angleT);
		
       wait(3);
	}
}

task fire2 {

if (GetCommonData("Difficulty", 3) == 3){
let a = 0;
      
        
        loop(100)
        {   PlaySE(shootSFX);
            CreateShotA1(bossX, bossY, 3, a+20, DS_KUNAI_SKY, 0);
			a += 1;
			CreateShotA1(bossX, bossY, 3, a, DS_KUNAI_WHITE	, 10);
			a += 2;
			CreateShotA1(bossX, bossY, 3, a+40, DS_KUNAI_PURPLE	, 20);
            a += 3;
			CreateShotA1(bossX, bossY, 3, a+40, DS_KUNAI_SKY, 30);
            a += 6;
        }
    


}

if (GetCommonData("Difficulty", 2) == 2){
let a = 0;
      
        
        loop(100)
        {   PlaySE(shootSFX);
            CreateShotA1(bossX, bossY, 3, a+20, DS_KUNAI_SKY, 0);
			a += 1;
			CreateShotA1(bossX, bossY, 3, a, DS_KUNAI_WHITE	, 10);
			a += 2;
			CreateShotA1(bossX, bossY, 3, a+40, DS_KUNAI_PURPLE	, 20);
            a += 12;
        }
    


}


if (GetCommonData("Difficulty", 0) == 0){
let a = 0;
      
        
        loop(100)
        {   PlaySE(shootSFX);
            CreateShotA1(bossX, bossY, 3, a+5, DS_KUNAI_SKY, 0);
			a += 20;
			CreateShotA1(bossX, bossY, 3, a+10, DS_KUNAI_WHITE	, 20);
			a += 20;
			CreateShotA1(bossX, bossY, 3, a+15, DS_KUNAI_PURPLE	, 20);
			
			
        }
    


}

if (GetCommonData("Difficulty", 1) == 1){
let a = 0;
      
        
        loop(30)
        {   PlaySE(shootSFX);
            CreateShotA1(bossX, bossY, 3, a+20, DS_KUNAI_SKY, 0);
			a += 40;
			CreateShotA1(bossX, bossY, 3, a+40, DS_KUNAI_WHITE	, 30);
			a += 25;
			
			
        }
    


}

}

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

		Reset2DCamera();
	}


task TExplosion{
    let path = GetCurrentScriptDirectory() ~ "pow.png";
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
    Obj_SetRenderPriority(obj, 0.60);
    ObjPrim_SetTexture(obj, path);
    ObjSprite2D_SetSourceRect(obj, 0, 0, 100, 100);
    ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
	

 
    let alpha = 255;
    let scale = 0;
    loop(30)
    {
        scale += 0.2;
        alpha -= 8;
        ObjRender_SetColor(obj, alpha, alpha, alpha);
        ObjRender_SetPosition(obj, bossX, bossY, 0);
        ObjRender_SetScaleXYZ(obj, scale, scale, 1);
        yield;
    }
    Obj_Delete(obj);
}

task TDrawLoop {
    
    ObjPrim_SetTexture(bossObj, imgBoss);
    ObjSprite2D_SetSourceRect(bossObj, 64, 1, 127, 64);
    ObjSprite2D_SetDestCenter(bossObj);
}

task TEnd{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	



if (GetCommonData("Difficulty", 0) == 0){
StartSlow(TARGET_ALL, 20);
        PlaySE(bossdeathSFX);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(30);	
	StopSlow(TARGET_ALL);
		
	BFX_Explosion(bossX,bossY,400,50);
	TShake();
	wait(20);
	Obj_Delete(bossObj);
	wait(40);
	CloseScript(GetOwnScriptID());
    


}

if (GetCommonData("Difficulty", 1) == 1){

StartSlow(TARGET_ALL, 20);
        PlaySE(bossdeathSFX);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(30);	
	StopSlow(TARGET_ALL);
		
	BFX_Explosion(bossX,bossY,400,50);
	TShake();
	wait(20);
	Obj_Delete(bossObj);
	wait(40);
	CloseScript(GetOwnScriptID());
    


}
        
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(20);	
	CloseScript(GetOwnScriptID());
	
	
}
