#include "./../FunctionData/Function_Info.dnh"

let CSD = GetCurrentScriptDirectory;

let objScene = ID_INVALID;

@Initialize{
	BGThread_InitializeWorld(1);
}

@MainLoop{
	objScene = GetEnemyBossSceneObjectID;
	yield;
}

task BGThread_InitializeWorld(let worldID){
	alternative(worldID)
	case(1){
		BGThread_SetWorld01;
	}
}

task BGThread_SetWorld01{
// 	Texture Initializing -------------------------
	let TOWER_BASE 		= CSD ~ "./../ImageData/BackGround/World01_BG1.png";
	let TOWER_WINDOW	= CSD ~ "./../ImageData/BackGround/World01_BG2.png";
	let TOWER_EFFECT	= CSD ~ "./../ImageData/BackGround/World01_BG3.png";
	
	let SPELL_MAIN 		= CSD ~ "./../ImageData/BackGround/Spell_BG1.png";
	let SPELL_OVERLAY 	= CSD ~ "./../ImageData/BackGround/Spell_BG2.png";
	
	LoadTexture(TOWER_BASE);
	LoadTexture(TOWER_WINDOW);
	LoadTexture(TOWER_EFFECT);
	LoadTexture(SPELL_MAIN);
	LoadTexture(SPELL_OVERLAY);
	
// 	Camera Initializing --------------------------

	SetCameraFocusXYZ(0,0,-(256/sqrt(2))-128);
	SetCameraRadius(1024);
	SetCameraAzimuthAngle(90);
	SetCameraElevationAngle(30);
	SetCameraRoll(30);
	SetFogEnable(true);
	let iniDist = 256;
	let finDist = 256+512+128;
	SetFogParam(iniDist,finDist,4,0,32);
	
// 	Background Control ---------------------------
	
	let objFog = BGThread_SetBackgroundColor(4,0,32,0.20);
	ObjRender_SetPhasingRoutine(objFog);
	BGThread_ManageCamera;
	BGThread_NormalStage;
	loop{
		while(!ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL)){ yield; }
		BGThread_SpellCard;
		while(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL)){ yield; }
	}
	
	task BGThread_ManageCamera{
		let camAngle = 0;
		let ccCount = 0;
		loop{
			SetCameraAzimuthAngle(90+camAngle);
			local{
				SetCameraRadius(GetGapX(1024-96,96,ccCount/2));
				SetCameraRoll(camAngle/2);
			}
			camAngle = modc(camAngle+0.25,360*2);
			ccCount++;
			yield;
		}
	}
	
	task BGThread_NormalStage{
		let sideLeng = 256;
		let restLeng = 256/sqrt(2);
		
		ObjBGTower_SetLayerSide(TOWER_WINDOW,0,0,0,0,0,0);
		ObjBGTower_SetLayerSide(TOWER_EFFECT,0,0,0,0,0,0);
	
		ObjBGTower_SetLayerSide(TOWER_BASE,((sideLeng+restLeng)/2)-1,0,-restLeng/2,0,45,0);
		ObjBGTower_SetLayerSide(TOWER_BASE,-((sideLeng+restLeng)/2)+1,0,-restLeng/2,0,-45,0);
		
		ObjBGTower_SetLayerSide(TOWER_WINDOW,(sideLeng/2+restLeng)-1,0,-(restLeng+sideLeng/2)+1,0,90,0);
		ObjBGTower_SetLayerSide(TOWER_EFFECT,(sideLeng/2+restLeng)-1,0,-(restLeng+sideLeng/2)+1,0,90,0);
		ObjBGTower_SetLayerSide(TOWER_WINDOW,-(sideLeng/2+restLeng)+1,0,-(restLeng+sideLeng/2)+1,0,90,0);
		ObjBGTower_SetLayerSide(TOWER_EFFECT,-(sideLeng/2+restLeng)+1,0,-(restLeng+sideLeng/2)+1,0,90,0);
		
		ObjBGTower_SetLayerSide(TOWER_BASE,-((sideLeng+restLeng)/2)+1,0,-(sideLeng+restLeng*1.5)+1,0,45,0);
		ObjBGTower_SetLayerSide(TOWER_BASE,((sideLeng+restLeng)/2)-1,0,-(sideLeng+restLeng*1.5)+1,0,-45,0);
		
		ObjBGTower_SetLayerSide(TOWER_WINDOW,0,0,-(sideLeng+restLeng*2),0,0,0);
		ObjBGTower_SetLayerSide(TOWER_EFFECT,0,0,-(sideLeng+restLeng*2),0,0,0);
		
		task ObjBGTower_SetLayerSide(let BGConst, let pX, let pY, let pZ, let xAngle, let yAngle, let zAngle){
			let objBG = ObjPrim_Create(OBJ_SPRITE_3D);
			
			ObjPrim_SetTexture(objBG,BGConst);
			Obj_SetRenderPriority(objBG,0.21);
			ObjRender_SetPosition(objBG,pX,pY,pZ);
			ObjRender_SetAngleXYZ(objBG,xAngle,yAngle,zAngle);
			if(BGConst == TOWER_EFFECT){
				ObjRender_SetColor(objBG,64,64,64);
			}
		
			ObjSprite3D_SetSourceDestRect(objBG,-128,-1024,128,1024);
			ObjRender_SetPermitCamera(objBG,true);
			ObjRender_SetZWrite(objBG,true);
			ObjRender_SetZTest(objBG,true);
			
			ObjRender_SetPhasingRoutine(objBG);
			
			let yScroll = 0;
			let ccCount = 0;
			while(!Obj_IsDeleted(objBG)){
				ObjSprite3D_SetSourceRect(objBG,0,0+yScroll,256,2048+yScroll);
				yScroll = modc(yScroll+4,256);
				if(!modc(ccCount,60*5) && BGConst == TOWER_EFFECT){
					SubTask_SetLight(objBG);
				}
				ccCount++;
				yield;
			}
			
			task SubTask_SetLight(let objTarget){
				let flashTime = 60;
				ascent(let i in 0..4){
					ObjRender_SetColor(objTarget,255,255,255);
					wait(2);
					ObjRender_SetColor(objTarget,64,64,64);
					if(i < 3){
						wait(2);
					}
				}
				descent(let d in 1..flashTime+1){
					let R = 64+d*192/flashTime;
					let G = 64+d*192/flashTime;
					let B = 64+d*192/flashTime;
					ObjRender_SetColor(objTarget,R,G,B);
					yield;
				}
			}
		}
	}
	
	task BGThread_SpellCard{
	// 	Spell Background -------------------------
		let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj1,SPELL_MAIN);
		Obj_SetRenderPriority(obj1,0.20);
		ObjRender_SetPosition(obj1,CenX,CenY,0);
		ObjRender_SetBlendType(obj1,BLEND_ALPHA);
		
		ObjSprite2D_SetSourceRect(obj1,-128,0,512,512);
		ObjSprite2D_SetDestCenter(obj1);
		ObjRender_ManageScrollingA1(obj1,[-128,0,512,512],0.5);
		
	// 	Spell Overlay ----------------------------
		let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj2,SPELL_OVERLAY);
		Obj_SetRenderPriority(obj2,0.21);
		ObjRender_SetPosition(obj2,CenX,CenY,0);
		ObjRender_SetBlendType(obj2,BLEND_SUBTRACT);
		
		ObjSprite2D_SetSourceRect(obj2,0,0,638,851);
		ObjSprite2D_SetDestCenter(obj2);
		ObjRender_ManageScrollingA1(obj2,[0,0,638,851],-0.5);
		
		let aMax = 60;
		ascent(let a in 1..aMax+1){
			let addAlpha = a*255/aMax;
			ObjRender_SetAlpha(obj1,addAlpha);
			ObjRender_SetAlpha(obj2,addAlpha);
			yield;
		}
		while(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL)){ yield; }
		let dMax = 15;
		descent(let d in 1..dMax+1){
			let decAlpha = d*255/dMax;
			ObjRender_SetAlpha(obj1,decAlpha);
			ObjRender_SetAlpha(obj2,decAlpha);
			yield;
		}
		Obj_Delete(obj1);
		Obj_Delete(obj2);
		
		task ObjRender_ManageScrollingA1(let objTarget, let LTRB, let scSpeed){
			let scValue = 0;
			while(!Obj_IsDeleted(objTarget)){
				ObjSprite2D_SetSourceRect(objTarget,LTRB[0],LTRB[1]+scValue,LTRB[2],LTRB[3]+scValue);
				scValue += scSpeed;
				yield;
			}
		}
	}
}

function BGThread_SetBackgroundColor(let R, let G, let B, let priority){
	let FOG_MAIN = CSD ~ "./../ImageData/BackGround/Dummy_Fog.png";
	
	let objRender = ObjPrim_Create(OBJ_SPRITE_2D);
	
	ObjPrim_SetTexture(objRender,FOG_MAIN);
	Obj_SetRenderPriority(objRender,priority);
	ObjRender_SetPosition(objRender,CenX,CenY,0);
	ObjRender_SetBlendType(objRender,BLEND_ALPHA);
	ObjRender_SetColor(objRender,R,G,B);
	
	ObjSprite2D_SetSourceRect(objRender,0,0,AbsMaxX*2,AbsMaxY*2);
	ObjSprite2D_SetDestCenter(objRender);
	
	return objRender;
}

task ObjRender_SetPhasingRoutine(let objTarget){
	while(!Obj_IsDeleted(objTarget)){
		while(!ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL)){ yield; }
		let dMax = 60;
		descent(let d in 0..dMax){
			let decAlpha = d*255/(dMax-1);
			ObjRender_SetAlpha(objTarget,decAlpha);
			yield;
		}
		while(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL)){ yield; }
		let aMax = 15;
		ascent(let a in 1..aMax+1){
			let addAlpha = a*255/aMax;
			ObjRender_SetAlpha(objTarget,addAlpha);
			yield;
		}
	}
}