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

let bossObj;
let bossX = 0;
let bossY = 0;
let objScene = GetEnemyBossSceneObjectID();
let imgBoss = GetCurrentScriptDirectory ~ "sprite/testsheet.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 CSD = GetCurrentScriptDirectory;

//textures

let texFog = CSD ~ "sprite/fog.png";

let texTree = CSD ~ "sprite/tree.png"; 

let texFloor = CSD ~ "sprite/floor.png";

//--------------------------------------------
//function to spawn a floor piece
function makeFloor(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFloor);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,90,0,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}
//function to spawn wall
function makeWall(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFloor);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,0,90,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}


function makeTree(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texTree);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,300,300);
        ObjRender_SetScaleXYZ(obj,0.5,0.5,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,0,180,180);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}
function makeFog(locX,locY,locZ,red,green,blue) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFog);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,4096,4096);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

      
       ObjRender_SetPosition(obj,locX,locY,locZ);


    ObjSprite3D_SetBillboard(obj, true); //makes this object face camera no matter where the camera is facing 
}

function makeWall2(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFloor);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,0,90,90);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}
//--------------------------------


@Initialize {
    renderStage;
    scrollStage;

   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;
        //renderBG;
	movement;
        TEnd;


} 

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

}

@Finalize {

}

task renderStage {

//enable the fog

SetFogEnable(true);

//set begining and end of fog and color
SetFogParam(1024,1563,0,0,0);

makeFog(0,0,2048,344,255,255);

makeFloor(0,0,1024);
makeFloor(0,0,512);
makeFloor(0,0,0);

makeWall(-256,256,1024);
makeWall(-256,256,512);
makeWall(-256,256,0);

makeWall2(256,256,1024);
makeWall2(256,256,512);
makeWall2(256,256,0);

makeTree (0,100,1024);
makeTree (0,100,512);
makeTree (0,100,0);

}

task scrollStage {


 let i = 0;
 let f = 0;
 let scroll = 0;


  //set the minimum and maximum render distance for your camera
    SetCameraPerspectiveClip(16,4096);


 //azimuth camera
    SetCameraAzimuthAngle (-90);
    SetCameraElevationAngle (0);
    SetCameraRadius(64);

 //focus camera
     SetCameraFocusX(0);                //left or right
    SetCameraFocusY(256);               //up or down
    SetCameraFocusZ(-512+scroll);         //depth forward and back 

 // ? camera    
     SetCameraYaw(0);
     SetCameraPitch(0);
     SetCameraRoll(0);   



    loop {

       i = sin(f)*25;
       f+=0.5;
       scroll+=4;
      
       SetCameraRoll(0+i);
       SetCameraYaw(0+i/2);


       SetCameraFocusZ(-512+scroll); 
       if(scroll > 512) {
          scroll = 0;

        }
        scroll+=2; //speed of the counting

        yield; 
   }



}

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,90,130);
        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 < 15) { ObjSprite2D_SetSourceRect(bossObj,0,0,98,134); }
			if(animFrame >= 15 && animFrame < 30) { ObjSprite2D_SetSourceRect(bossObj,98,0,196,134); }
			
			animFrame2 = 0;
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,0,134,98,134); }
			if(animFrame2 >= 15 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,98,0,196,134); }
		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(bossObj,0,180,0);	// flip vertical
			if(animFrame2 < 15) { ObjSprite2D_SetSourceRect(bossObj,0,196,98,134); }
			if(animFrame2 >= 15 && animFrame2 < 30) { ObjSprite2D_SetSourceRect(bossObj,98,98,196,134); }
		}
		animFrame++;		// count animFrame. (++ is +1)
		animFrame2+=2;		// count animFrame2.
		if(animFrame > 60) { 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, 112, 60);
    wait(60);
    while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){
	PlaySE(smallSFX);
	CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), rand(2, 6), rand(100, 359), DS_BALL_M_SKY, 0);
	PlaySE(smallSFX);
	CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), rand(2, 6), rand(75, 0), DS_BALL_M_SKY, 0);

	yield;
    }
    
}

task fire2{
loop(10){
PlaySE(hugeSFX);
CreateShotA1(bossX,bossY,5,angleToPlayer,DS_BEAM_RAINBOW,0);
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());

}