#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell2"]
#Text["second nonspell"]
#include "script/default_system/Default_ShotConst.txt"

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/DEAD.wav";
let hugeSFX = GetCurrentScriptDirectory ~ "SFX/ATTACK4.wav";
let smallSFX = GetCurrentScriptDirectory ~ "SFX/TWINKLE3.wav";
let objText = ObjText_Create();
let circleObj = ObjPrim_Create(OBJ_SPRITE_2D);

let CSD = GetCurrentScriptDirectory;



@Initialize {
    

   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

   
              LoadSound (spellcardSFX);
        LoadSound(bossdeathSFX);
     //PlaySE(spellcardSFX);
	 LoadSound(hugeSFX);
	 LoadSound(smallSFX);


     mainTask;

}




@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;
    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,100);
     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+32,bossY+12,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(1200);
       }
       case(EV_REQUEST_TIMER) {
          SetScriptResult(25);
     }

}

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

let angleT = 0;
        let angle=0;
	
	loop { 
			
		wait(45);
		fire;
		wait(30);
		fire2;
			
		
}
}

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


task fire{
  ObjMove_SetDestAtFrame(bossObj, bossX, 60, 60);
    wait(60);
    while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){
	PlaySE(smallSFX);
	
	CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), rand(2, 3), rand(0, 359), DS_BALL_SS_SKY, 0);
	wait(10);
	CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), rand(2, 3), rand(0, 359), DS_BALL_SS_SKY, 0);
		wait(20);
	

	yield;
    }
    
}

task fire2{

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

let angleT = angleToPlayer;

 loop(20){
	
     ascent(i in 0..2){
	 PlaySE(hugeSFX);
 
CreateShotA1(bossX,bossY,3,angleT,DS_LIGHT_WHITE,0);
CreateShotA1(bossX,bossY,3,angleT+10,DS_LIGHT_WHITE,0);
CreateShotA1(bossX,bossY,3,angleT-10,DS_LIGHT_WHITE,0);

 angleT += 360/8;
 

yield;


}
}
}
if (GetCommonData("Difficulty", 3) == 3){

let angleT = angleToPlayer;

 loop(20){
	
     ascent(i in 0..2){
	 PlaySE(hugeSFX);
 
 CreateShotA1(bossX,bossY,3,angleT,DS_LIGHT_WHITE,25);
CreateShotA1(bossX,bossY,3,angleT,DS_LIGHT_WHITE,0);
CreateShotA1(bossX,bossY,3,angleT+10,DS_LIGHT_WHITE,0);
CreateShotA1(bossX,bossY,3,angleT-10,DS_LIGHT_WHITE,0);

 angleT += 360/8;
 

yield;
}


}
}

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

let angleT = angleToPlayer;

 loop(20){
	
     ascent(i in 0..2){
	 PlaySE(hugeSFX);
 
CreateShotA1(bossX,bossY,3,angleT,DS_LIGHT_WHITE,0);
CreateShotA1(bossX,bossY,3,angleT+10,DS_LIGHT_WHITE,0);


 angleT += 360/8;
 

yield;


}
}
}

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

let angleT = angleToPlayer;

 loop(20){
	
     ascent(i in 0..2){
	 PlaySE(hugeSFX);
 
CreateShotA1(bossX,bossY,3,angleT,DS_LIGHT_WHITE,0);


 angleT += 360/8;
 

yield;


}
}
}
}

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




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;
	}
        
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(20);	
	CloseScript(GetOwnScriptID());

}