// Background.

let CSD = GetCurrentScriptDirectory;
let texFloor = CSD ~ "./back_img/stage02a.png";
let texDust = CSD ~ "./back_img/stars2.png";
let texFogBG = CSD ~ "./back_img/fog.png";
let scroll = 0;
let glide = 0;

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, 128);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetZWrite(obj, true);
	
	ObjRender_SetAngleXYZ(obj, 90, 0, 0);
	
	ObjRender_SetPosition(obj, locX, locY, locZ);
}

function makeDust(locX, locY, locZ){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, texDust);
	Obj_SetRenderPriorityI(obj, 20);
	ObjSprite3D_SetSourceDestRect(obj, 0, 0, 512, 256);
	ObjRender_SetScaleXYZ(obj, 0.5, 1, 0);
	ObjRender_SetZWrite(obj, true);
	ObjRender_SetAngleXYZ(obj, 90, 0, 0);
	ObjRender_SetPosition(obj, locX, locY, locZ);
	ObjRender_SetAlpha(obj, 150);
}

function makeFogBG(locX, locY, locZ, red, green, blue){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, texFogBG);
	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);
	
}

@Initialize{
	RenderStage;
	ScrollStage;
}

@MainLoop{
	scroll=scroll+5;
	if(glide < 200){
	glide++;
	}
	yield;
}

task RenderStage{
	SetFogEnable(true);
	
	SetFogParam(640,1536,25,20,25);
	
	// Floor.
	makeFloor(0,0,896+128+128+128+128+128+128+128);
	makeFloor(0,0,896+128+128+128+128+128+128);
	makeFloor(0,0,896+128+128+128+128+128);
	makeFloor(0,0,896+128+128+128+128);
	makeFloor(0,0,896+128+128+128);
	makeFloor(0,0,896+128+128);
	makeFloor(0,0,896+128);
	makeFloor(0,0,896);
	makeFloor(0,0,768);
	makeFloor(0,0,640);
	makeFloor(0,0,512);
	makeFloor(0,0,384);
	makeFloor(0,0,256);
	makeFloor(0,0,128);
	makeFloor(0,0,0);
	makeFloor(0,0,-128);
	
	// Dust.
	makeDust(0,60,256+256+256+256+256+256+256);
	makeDust(0,60,256+256+256+256+256+256);
	makeDust(0,60,256+256+256+256+256);
	makeDust(0,60,256+256+256+256);
	makeDust(0,60,256+256+256);
	makeDust(0,60,256+256);
	makeDust(0,60,256);
	makeDust(0,60,0);
	makeDust(0,60,-256);
		
	//Fog.
	makeFogBG(0,0,2048,255,255,255);
}

task ScrollStage{

	SetCameraPerspectiveClip(16, 4096);
	SetCameraAzimuthAngle(-90-glide);
	SetCameraElevationAngle(80);
	SetCameraRadius(16);
	SetCameraFocusX(0);
	SetCameraFocusY(200);
	SetCameraFocusZ(-128);
	
	loop{
		SetCameraFocusZ(-128+scroll);
		if(scroll > 512){
			scroll = 0;
		}
		yield;
	}
	loop{
		SetCameraFocusY(20+glide);
		if(glide == 200){
			glide = 0;
		}
		yield;
	}
}