#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["spellcard2"]
#Text["Code by Sawmey[r]"]
#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/ATTACK3.wav";

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 {
   

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

     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,344,128,128);

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

}



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,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 < 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 {
	
	loop { 
		ObjMove_SetDestAtSpeed(bossObj,100,120,5);		// move to the left
		wait(45);
		fire;
		wait(30);
		ObjMove_SetDestAtSpeed(bossObj,280,120,5);		// move to the right
		wait(45);
		fire;
		wait(30);
	}
}

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 * 30, ObjMove_GetSpeed(obj), angleT, 0.01, wvel, 3);
		wvel *= -1;
	}
}

task fire{
	let angleT = 0;
	 let dir = angleToPlayer;
	loop(6){
		let obj = CreateCurveLaserA1(bossX, bossY, 2, angleT, 120, 20, DS_ICE_WHITE, 10);
                ObjCrLaser_SetTipDecrement(obj, 0);
		SnakePattern(obj,angleT);
		angleT += 360/6;
       wait(3);
	}
}





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(120);	
	CloseScript(GetOwnScriptID());

}