#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["3nonspell1"]
#Text["3rdboss"]
#include "script/default_system/Default_ShotConst.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/teachersheet.png";
let animFrame = 0; 
let animFrame2 = 0;
let spellcardSFX = GetCurrentScriptDirectory ~ "SFX/SPELLCARD.wav";
let bossdeathSFX = GetCurrentScriptDirectory ~ "SFX/DEFEATED.wav";
let objText = ObjText_Create();
let shootSFX = GetCurrentScriptDirectory ~ "SFX/LASER.wav";
let shootSFX2 = GetCurrentScriptDirectory ~ "SFX/TWINKLE.wav";
let circleObj = ObjPrim_Create(OBJ_SPRITE_2D);

let count = 0;

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);
     
	 LoadSound(shootSFX);
	 LoadSound(shootSFX2);

     mainTask;
	
   SetCommonData("hee",true);
   SetCommonData("hee2",true);

}




@MainLoop {
     bossX = ObjMove_GetX(bossObj);
     bossY = ObjMove_GetY(bossObj);
     
//enemy collision
  
   
        ObjEnemy_SetIntersectionCircleToShot(bossObj, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 32); 
        ObjEnemy_SetIntersectionCircleToPlayer(bossObj, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 24);
      
          if(count == 0){
            ascent(i in 0..10) {CreateOrb(i, 6, 1, 90);}
        }
		
		
        count++;
        yield;
    
    }
    
    
 
 task CreateOrb(ID, num, dir, rad) {
 
 wait(120);
        let objcount = 0; 
        let angleO = ID*360/num; 
        //obtain object ID
        let obj = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 0, ID*360/num, DS_BALL_L_YELLOW, 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
            
            angleO = ID*360/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 % 8 == 0) { //control firing rate
                let currx = ObjMove_GetX(obj);
                let curry = ObjMove_GetY(obj);
                ascent(i in 0..3) {
				PlaySE(shootSFX2);
                    CreateShotA1(currx, curry, 2, angleO + 200 + i * 20, DS_LIGHT_GREEN, 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..3) {
				PlaySE(shootSFX2);
                    CreateShotA1(currx, curry, 2, angleO + 200 + i * 20, DS_LIGHT_GREEN, 0);
                }
            }
			}
			if (GetCommonData("Difficulty", 1) == 1){
            if (objcount % 14 == 0) { //control firing rate
                let currx = ObjMove_GetX(obj);
                let curry = ObjMove_GetY(obj);
                ascent(i in 0..3) {
				PlaySE(shootSFX2);
                    CreateShotA1(currx, curry, 2, angleO + 200 + i * 20, DS_LIGHT_GREEN, 0);
                }
            }
			}
			if (GetCommonData("Difficulty", 0) == 0){
            if (objcount % 20 == 0) { //control firing rate
                let currx = ObjMove_GetX(obj);
                let curry = ObjMove_GetY(obj);
                ascent(i in 0..3) {
				PlaySE(shootSFX2);
                    CreateShotA1(currx, curry, 2, angleO + 200 + i * 20, DS_LIGHT_GREEN, 0);
                }
            }
			}
            objcount++;
            yield;
        }
        ObjShot_FadeDelete(obj);
    }


task mainTask { 
    MagicCircle;
	renderBoss; 
	movement;
        TEnd;


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

     ObjPrim_SetTexture(circleObj,path);
     ObjRender_SetBlendType(circleObj,BLEND_ADD_ARGB);
     Obj_SetRenderPriorityI(circleObj,40);
     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,bossY,0);
       ObjRender_SetAngleZ(circleObj,spin);
       ObjRender_SetAlpha(circleObj,alpha);

         alpha = 120;
		 spin+=-1;
		yield;
        }
    
       yield;
  
 }

task movement {
	wait(120);
	
	
	ascent(i in 0..12){
        SpawnNDLaser(i);
	
  }
	  
	  }
	  
 task SpawnNDLaser(ID){
        while(!Obj_IsDeleted(bossObj)){
		PlaySE(shootSFX);
		let objcount = 0;
        let obj = CreateStraightLaserA1(bossX + 30*cos(ID*30), bossY + 30*sin(ID*30), ID * 30, 512, 12, 80, DS_BEAM_GREEN, 60);
        while(!Obj_IsDeleted(obj)){
            ObjMove_SetPosition(obj, bossX + 30*cos(ID*30 + objcount), bossY + 30 * sin(ID*30 + objcount));
            ObjStLaser_SetAngle(obj, ID*30 + objcount);
            objcount+=0.2;
            yield;
        }
    }
	}


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


}

@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,131,284);
        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,131,288); }
			if(animFrame >= 10 && animFrame < 25) { ObjSprite2D_SetSourceRect(bossObj,150,3,287,290); }
			if(animFrame >= 25) { ObjSprite2D_SetSourceRect(bossObj,300,3,433,290); }
			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,0,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,742,0,884,290); }
			if(animFrame2 >= 15 && animFrame2 < 25) { ObjSprite2D_SetSourceRect(bossObj,905,0,1058,290); }
			if(animFrame2 >= 25 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,1072,14,1236,296); }
			if(animFrame2 >= 30 && animFrame2 < 35) { ObjSprite2D_SetSourceRect(bossObj,905,12,1058,290); }
			if(animFrame2 >= 40 && animFrame2 < 45) { ObjSprite2D_SetSourceRect(bossObj,1072,14,1236,296); }
			if(animFrame2 >= 45 && animFrame2 < 50) { ObjSprite2D_SetSourceRect(bossObj,905,12,1058,290); }
		}
		else if(cos(dir) > 0){ 
		ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,742,0,884,290); }
			if(animFrame2 >= 15 && animFrame2 < 25) { ObjSprite2D_SetSourceRect(bossObj,905,0,1058,290); }
			if(animFrame2 >= 25 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,1072,14,1236,296); }
			if(animFrame2 >= 30 && animFrame2 < 35) { ObjSprite2D_SetSourceRect(bossObj,905,12,1058,290); }
			if(animFrame2 >= 40 && animFrame2 < 45) { ObjSprite2D_SetSourceRect(bossObj,1072,14,1236,296); }
			if(animFrame2 >= 45 && animFrame2 < 50) { ObjSprite2D_SetSourceRect(bossObj,905,12,1058,290); }
		}
		animFrame++;		// count animFrame. (++ is +1)
		animFrame2+=2;		// count animFrame2.
		if(animFrame > 25) { animFrame = 0; }	// reset animFrame when it is higher than 60.
		yield;
	}
}

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

	

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

}