#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["dizzolve"]
#Text["ze"]
#BGM["./dizzolve.mp3"]

let objBoss;
let objScene = GetEnemyBossSceneObjectID();
let bossX = 0;
let bossY = 0;
let count = 0;
let imgBoss = GetCurrentScriptDirectory ~ "image/chiken.png";
let background = GetCurrentScriptDirectory ~ "image/id2.png";
let overlay = GetCurrentScriptDirectory ~ "image/haha.png";
let ID = 0;
let sc = GetCurrentScriptDirectory ~ "image/scdeclare.wav";
let bonus = GetCurrentScriptDirectory ~ "image/scbonus.wav";
SetPlayerLife(99);

#include "./AllStarShot_Const.dnh"
#include"script/default_system/Default_Effect.txt"

@Initialize { 
	// define a boss in objBoss
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	//teleport boss tới vị trí chỉ định 
	ObjMove_SetPosition(objBoss,192,-100);
	//di chuyển boss tới vị trí chỉ định
	ObjMove_SetDestAtSpeed(objBoss,192,120,5);
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	
	//if (count == 0){movement;}
	
	TFinalize;
	MainTask;	//chạy mainTask
	
} 

	


@Event { 
	// chỉnh máu và thời gian timeout
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(999999);
	}
	case(EV_REQUEST_TIMER) { 
		SetScriptResult(137);
	}
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(9999999);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(60);
	}
}	

@MainLoop {
	bossX = ObjMove_GetX(objBoss);
	bossY = ObjMove_GetY(objBoss);
	
	//collision for shots and player
	//ObjEnemy_SetIntersectionCircleToShot(objBoss,bossX,bossY,24);
	//ObjEnemy_SetIntersectionCircleToPlayer(objBoss,bossX,bossY,32);
	
	yield;
}


@Finalize {

}

function wait(w) { loop(w) { yield; } }

task MainTask { 
	renderBoss;
	stuff1;
	wait(420);
	loop(7){
			wait(13);
		laser;
	}
	wait(200);
	rng1;
	wait(320);
	loop(7){
		wait(13);
		laser;
	}
	wait(80);
	nd1(ID);
	nd2(ID);
	wait(120);
	ring;
	wait(420);
	loop(7){
		wait(13);
		laser;
		}
	wait(60);
	nd1(ID);
	nd2(ID);
	ring;
	wait(700);
	bowap;
	wait(1150);
	loop(18){
		wait(25);
		laser1;
		laser2;
		}
	loop(8){
		wait(12);
		laser4;
	}
	loop(24){
		wait(25);
		laser1;
		laser2;
		}
	loop(10){
		wait(13);
		laser4;
	}
	wait(60);
	rng2;
	wait(1120);
	rng3;
	ring2;
} 

task renderBoss {
	let dir;
	let speed;
	let animframe = 0;
	
	// texture the boss xd
	ObjPrim_SetTexture(objBoss,imgBoss);
	ObjSprite2D_SetSourceRect(objBoss,0,0,64,96);
	ObjSprite2D_SetDestCenter(objBoss);
	ObjRender_SetScaleXYZ(objBoss,1,1,0);
	
	while(!Obj_IsDeleted(objBoss)) {
	ObjSprite2D_SetSourceRect(objBoss, 0 + 64*floor(animframe/8), 0, 64 + 64*floor(animframe/8), 96);
            //We can do the above because the sprites are organized into a grid of 64x64 squares.
            animframe++;
            if(animframe >= 32){animframe = 0;}//reset if too high
            
		//cập nhập vị trí và tốc độ của boss
		dir = ObjMove_GetAngle(objBoss);
		speed = ObjMove_GetSpeed(objBoss);
	

	
		yield;
	}
}

task renderBG {
	
	let scrollTex = 0;
	
	//let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	//Obj_SetRenderPriorityI(obj,20);
	//ObjPrim_SetTexture(obj,background);
	//ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	//ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);
	// overlay 
	let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,overlay); 
	ObjRender_SetBlendType(overlayObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(overlayObj,200);
	ObjSprite2D_SetSourceRect(overlayObj,0,0,512,512);
	ObjSprite2D_SetDestRect(overlayObj,0,0,GetStgFrameWidth,GetStgFrameHeight);
	
	// As long as the boss is alive, scroll the overlay diagonally towards bottom right.
	while(!Obj_IsDeleted(objBoss)) {
		ObjSprite2D_SetSourceRect(overlayObj,0-scrollTex,0-scrollTex,512-scrollTex,512-scrollTex);
		scrollTex += 2;
		yield;
	}
	Obj_Delete(overlayObj);
} 


task ring {
		loop(4){
			wait(100);
			//movement;
			let angleT = GetAngleToPlayer(objBoss);
			loop(60){
			ascent(i in 0..2){
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5 - i/3, angleT -15, 99+ i, 5);
			}
			angleT += 360/60;
			}
		}
	}


task rng1 {
	while(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 117){
		ObjMove_SetDestAtFrame(objBoss, GetCenterX, 112, 60);
			ascent(i in 0..3){
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(4, 6), rand(0, 360), 101, 10);
			}
	yield;
	}
}

task rng2 {
	while(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 35){
		ObjMove_SetDestAtFrame(objBoss, GetCenterX, 112, 60);
			ascent(i in 0..3.3){
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(4, 5), rand(0, 360), 103, 10);
			}
	yield;
	}
}

task rng3 {
	while(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 16){
		ObjMove_SetDestAtFrame(objBoss, GetCenterX, 112, 60);
			ascent(i in 0..3.3){
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(4, 5), rand(0, 360), 105, 10);
			}
	yield;
	}
}

task ring2 {
	while(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 18){
		loop(1){
			wait(120);
			//movement;
			let angleT = GetAngleToPlayer(objBoss);
			loop(60){
			ascent(i in 0..1){
				CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.5 - i/3, angleT -15, 292, 5);
				}
			angleT += 360/60;
			}
		}
	}
}



	
function AngleToPlayer { 		//hướng boss tới ng` chơi?
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}


task TFinalize {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 3){yield;}
    if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT)
            +ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
        AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
    }
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}


 function GetCenterX(){
     return GetStgFrameWidth() / 2;
 }
 function GetCenterY(){
     return GetStgFrameHeight() / 2;
 }


task laser {
	loop(1){
		wait(120);
		let angleT = 90; //Base angle faces directly right
		loop(10){ //A ring of 6 lasers
		ascent(i in 0..2){
			let obj = CreateStraightLaserA1(rand(0,384), 0 , rand(0,180), 600, 25, 10, 1057, 40);
			ObjLaser_SetGrazeInvalidFrame(obj, 1); //Allows for up to 1 graze each 5 frames
			ObjLaser_SetInvalidLength(obj, 10, 10); //5 percent of each end of the laser is safe
			angleT += 360/10; //Increments by 60 degrees clockwise each time the block is run
		}
		}
		loop(1){
		ascent(i in 0..4){
			let obj = CreateStraightLaserA1(0, rand(GetCenterY-120,448) , 0, 600, 25, 10, 1057, 80);
			ObjStLaser_SetSource(obj, true); //Do not show the laser source
			ObjLaser_SetGrazeInvalidFrame(obj, 1); //Allows for up to 1 graze each 5 frames
			ObjLaser_SetInvalidLength(obj, 10, 10); //5 percent of each end of the laser is safe
			angleT += 360/1; //Increments by 60 degrees clockwise each time the block is run	
		}
		}
	yield;
	}
}

task stuff1 {
	wait(120);
	while(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) >= 128){
		ObjMove_SetDestAtFrame(objBoss, GetCenterX, 112, 60);
		wait(0);
		ascent(i in 0..2){
			CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(4, 5), rand(0, 360), 101, 10);
		}
	yield;
	}
}

    task nd1(ID){
			wait(80);	
			loop(3){
				wait(27);
				let ID = 6;
				let objcount = 0;
				let obj = CreateStraightLaserA1(ObjMove_GetX(objBoss) + 60*cos(ID*30), ObjMove_GetY(objBoss) + 60*sin(ID*30), 20, 512, 24, 80, 1001, 20);
				while(!Obj_IsDeleted(obj)){
				ObjMove_SetPosition(obj, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)-45);
				ObjStLaser_SetAngle(obj, ID*30 + objcount);
				objcount--;
				yield;				
			}
		}
	}
	
	task nd2(ID){
		wait(30);	
		wait(80);
			loop(3){
			wait(60);
			let ID = 0;
			let objcount = 0;
			let obj2 = CreateStraightLaserA1(ObjMove_GetX(objBoss) + 60*cos(ID*30), ObjMove_GetY(objBoss) + 60*sin(ID*30), 20, 512, 24, 60, 1001, 20);
			while(!Obj_IsDeleted(obj2)){
				ObjMove_SetPosition(obj2, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)-45);
				ObjStLaser_SetAngle(obj2, ID*30 + objcount);
				objcount++;
				yield;
			}
		}
	}

task bowap{    
		let angleT = rand(0, 360);
		let objcount = 0;
		while(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 75){
		loop(4){    
			loop(5){
				let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, angleT, 57, 5);
				angleT += 360/5;
			}
		angleT += sin(objcount) * 10; //BOWAP
		objcount++;
		yield;;
		}
	}
}

task laser1 {
	loop(1){
		let angleT = 90; //Base angle faces directly right
		loop(1){
		ascent(i in 0..4){
			let obj = CreateStraightLaserA1(0, rand(0,448) , 0, 512, 25, 15, 1057, 30);
			ObjStLaser_SetSource(obj, true); //Do not show the laser source
			ObjLaser_SetGrazeInvalidFrame(obj, 1); //Allows for up to 1 graze each 5 frames
			ObjLaser_SetInvalidLength(obj, 0, 0); //5 percent of each end of the laser is safe
			angleT += 360/1; //Increments by 60 degrees clockwise each time the block is run	
		}
		}
	yield;
	}
}

task laser2 {
	loop(1){
		let angleT = 90; //Base angle faces directly right
		loop(1){ //A ring of 6 lasers
			ascent(i in 0..4){
				let obj = CreateStraightLaserA1(rand(0,384)+i*10, 0 , 90, 512, 25, 15, 1057, 30);
				ObjLaser_SetGrazeInvalidFrame(obj, 1); //Allows for up to 1 graze each 5 frames
				ObjLaser_SetInvalidLength(obj, 0, 0); //5 percent of each end of the laser is safe
				angleT += 360/10; //Increments by 60 degrees clockwise each time the block is run
			}
		}
	}

}

task laser4 {
	loop(1){
		let angleT = 90; //Base angle faces directly right
		loop(10){ //A ring of 6 lasers
		ascent(i in 0..2){
			let obj = CreateStraightLaserA1(rand(0,384), 0 , rand(0,180), 600, 25, 10, 1057, 40);
			ObjLaser_SetGrazeInvalidFrame(obj, 1); //Allows for up to 1 graze each 5 frames
			ObjLaser_SetInvalidLength(obj, 10, 10); //5 percent of each end of the laser is safe
			angleT += 360/10; //Increments by 60 degrees clockwise each time the block is run
		}
		}
		loop(1){
		ascent(i in 0..4){
			let obj = CreateStraightLaserA1(0, rand(GetCenterY-120,448) , 0, 600, 25, 10, 1057, 80);
			ObjStLaser_SetSource(obj, true); //Do not show the laser source
			ObjLaser_SetGrazeInvalidFrame(obj, 1); //Allows for up to 1 graze each 5 frames
			ObjLaser_SetInvalidLength(obj, 10, 10); //5 percent of each end of the laser is safe
			angleT += 360/1; //Increments by 60 degrees clockwise each time the block is run	
		}
		}
	yield;
	}
}