#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["2nonspell"]
#Text["2ndboss[r]"]
#include "script/default_system/Default_ShotConst.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/hersheet.png";
let animFrame = 0; 
let animFrame2 = 0;
let spellcardSFX = GetCurrentScriptDirectory ~ "SFX/SPELLCARD.wav";
let bossdeathSFX = GetCurrentScriptDirectory ~ "SFX/DEAD.wav";
let objText = ObjText_Create();
let shootSFXball = GetCurrentScriptDirectory ~ "SFX/TWINKLE3.wav";
let sky = GetCurrentScriptDirectory ~ "sprite/starbg.jpg";
let overlay = GetCurrentScriptDirectory ~ "sprite/swirl.png";
let count = 0;
let circleObj = ObjPrim_Create(OBJ_SPRITE_2D);

let CSD = GetCurrentScriptDirectory;

//textures


@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(bossdeathSFX);
     PlaySE(spellcardSFX);
	 LoadSound(shootSFXball);

     mainTask;

}




@MainLoop {
     bossX = ObjMove_GetX(bossObj);
     bossY = ObjMove_GetY(bossObj);
     
//enemy collision
    ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
    ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
	
	 if(count == 100){
            ascent(i in 0..6) {CreateOrb(i, 6, 1, 28);}
        }
        count++;
        
   SetCommonData("hee",true);
   SetCommonData("hee2",true);

    yield;
}

 task CreateOrb(ID, num, dir, rad) {
        let objcount = 0; //counter to control firing of bullets and movement
        let angleO = ID*360/num; //Initial angle (will be updated)
        //obtain object ID
        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 0, ID*360/num,DS_BALL_M_R_PURPLE, 0); 
        //If using a bullet as a familiar, ensure it doesn't get accidentally deleted
        ObjShot_SetSpellResist(obj, true); 
        ObjShot_SetAutoDelete(obj, false);
        while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) { //linked to the parent enemy's life
            //ID*360/num sets the angle relative to the other familiars. objcount*dir handles the rotation
            angleO = ID*180/num + objcount*dir; //Update angle
            ObjMove_SetPosition(obj, ObjMove_GetX(bossObj) + rad*cos(angleO), 
                                     ObjMove_GetY(bossObj) + rad*sin(angleO)); 
            ObjMove_SetAngle(obj, angleO); 
			if (GetCommonData("Difficulty", 3) == 3){
            if (objcount % 6 == 0) { //control firing rate
                let currx = ObjMove_GetX(obj);
                let curry = ObjMove_GetY(obj);
				
                ascent(i in 0..5) {
				PlaySE(shootSFXball);
                    CreateShotA1(currx, curry, 1.5, angleO + 200 + i * 120,DS_BALL_BS_R_WHITE	, 0);
                }
            }
			
			}
				if (GetCommonData("Difficulty", 2) == 2){
            if (objcount % 12 == 0) { //control firing rate
                let currx = ObjMove_GetX(obj);
                let curry = ObjMove_GetY(obj);
				
                ascent(i in 0..5) {
				PlaySE(shootSFXball);
                    CreateShotA1(currx, curry, 1.5, angleO + 200 + i * 120,DS_BALL_BS_R_WHITE	, 0);
                }
            }
			
			}
				if (GetCommonData("Difficulty", 1) == 1){
            if (objcount % 18 == 0) { //control firing rate
                let currx = ObjMove_GetX(obj);
                let curry = ObjMove_GetY(obj);
				
                ascent(i in 0..5) {
				PlaySE(shootSFXball);
                    CreateShotA1(currx, curry, 1.5, angleO + 200 + i * 120,DS_BALL_BS_R_WHITE	, 0);
                }
            }
			
			}
				if (GetCommonData("Difficulty", 0) == 0){
            if (objcount % 22 == 0) { //control firing rate
                let currx = ObjMove_GetX(obj);
                let curry = ObjMove_GetY(obj);
				
                ascent(i in 0..5) {
				PlaySE(shootSFXball);
                    CreateShotA1(currx, curry, 1.5, angleO + 200 + i * 120,DS_BALL_BS_R_WHITE	, 0);
                }
            }
			
			
			}
            objcount++;
            yield;
        }
        ObjShot_FadeDelete(obj);
    }

task mainTask { 
	renderBoss;
    MagicCircle;
	movement;
        TEnd;


} 

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

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

task movement {
	
	loop { 
	    wait(60);
		
	
	}
}

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

}

@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.6,0.6,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,12,0,65,68); }
			if(animFrame >= 20 && animFrame < 30) { ObjSprite2D_SetSourceRect(bossObj,68,0,115,68); }
			if(animFrame >= 30 && animFrame < 40) { ObjSprite2D_SetSourceRect(bossObj,115,0,160,68); }
			if(animFrame >= 50) { ObjSprite2D_SetSourceRect(bossObj,160,0,212,68); }

			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,210,0,263,68); }
			if(animFrame2 >= 15 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,259,0,313,68); }
			if(animFrame2 >= 30 && animFrame2 < 40){ ObjSprite2D_SetSourceRect(bossObj,210,0,263,68); }
			if(animFrame2 >= 40) { ObjSprite2D_SetSourceRect(bossObj,259,0,313,68); }
			
		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,210,0,263,68); }
			if(animFrame2 >= 15 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,259,0,313,68); }
			if(animFrame2 >= 30 && animFrame2 < 40){ ObjSprite2D_SetSourceRect(bossObj,210,0,263,68); }
			if(animFrame2 >= 40) { ObjSprite2D_SetSourceRect(bossObj,259,0,313,68); }
		}
		animFrame++;		// count animFrame. (++ is +1)
		animFrame2+=2;		// count animFrame2.
		if(animFrame > 60) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}


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

   let angleT = angleToPlayer;
   
    loop(13){
	
		
		}
    }


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


task TEnd 
{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
        
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	wait(20);	
	CloseScript(GetOwnScriptID());

}