let current = GetCurrentScriptDirectory();

@Initialize{
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(900);
	SetCameraElevationAngle(-30);
	SetCameraAzimuthAngle(90);
	SetFogParam(500, 1800, 0, 0, 64);
	
	TUnderWater;
	TMoon;
	TWater;
	TSakura1;
	TSakura2;
	
	TShade();
	}

@MainLoop{yield;}

task TUnderWater{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,current ~ "./img/stage03/stg3bg1.png");
	ObjSprite2D_SetSourceRect(obj,0,-512*3,384,512);
	Obj_SetRenderPriorityI(obj,20);
	loop{
		ascent(i in 0..512*3){
			ObjSprite2D_SetDestRect(obj,0,-512*3+i,384,512*2+i);
		yield;}
	}
}

task TMoon{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,current ~ "./img/moon.png");
	ObjSprite2D_SetSourceRect(obj,0,0,990,990);
	Obj_SetRenderPriorityI(obj,24);
	ObjRender_SetAlpha(obj,200);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	let move = 0;
	loop{
		ObjSprite2D_SetDestRect(obj,50,50+10*sin(move),334,334+10*sin(move));
		move+=2;
		yield;
	}
}

task TWater{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,current ~ "./img/water.png");
	ObjSprite2D_SetSourceRect(obj,0,-225*5,384,225*2);
	Obj_SetRenderPriorityI(obj,23);
	ObjRender_SetAlpha(obj,100);
	loop{
		ascent(i in 0..225*5){
			ObjSprite2D_SetDestRect(obj,0,-225*5+i,384,225*2+i);
		yield;}
	}
}

task TSakura1{
	let objA = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objA,current ~ "./img/stage03/stg3bg2.png");
	ObjSprite2D_SetSourceRect(objA,0,-1024*3,512,1024);
	Obj_SetRenderPriorityI(objA,21);
	
	let objB = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objB,current ~ "./img/stage03/stg3bg2.png");
	ObjSprite2D_SetSourceRect(objB,0,-1024*3,512,1024);
	ObjRender_SetAngleY(objB,180);
	Obj_SetRenderPriorityI(objB,21);
	
	loop{
		ascent(i in 0..512*2){
			ObjSprite2D_SetDestRect(objA,0,-512*3+i,256,512+i);
			
			ObjSprite2D_SetDestRect(objB,-256-128,-512*3+i,0-128,512+i);
		loop(4){yield;}}
	}
}

task TSakura2{
	let objA = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objA,current ~ "./img/stage03/stg3bg3.png");
	ObjSprite2D_SetSourceRect(objA,0,-1024*3,512,1024);
	Obj_SetRenderPriorityI(objA,24);
	
	let objB = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objB,current ~ "./img/stage03/stg3bg3.png");
	ObjSprite2D_SetSourceRect(objB,0,-1024*3,512,1024);
	ObjRender_SetAngleY(objB,180);
	Obj_SetRenderPriorityI(objB,24);
	
	let objC = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objC,current ~ "./img/stage03/stg3bg4.png");
	ObjSprite2D_SetSourceRect(objC,0,-1024*3,512,1024);
	Obj_SetRenderPriorityI(objC,27);
	
	let objD = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objD,current ~ "./img/stage03/stg3bg4.png");
	ObjSprite2D_SetSourceRect(objD,0,-1024*3,512,1024);
	ObjRender_SetAngleY(objD,180);
	Obj_SetRenderPriorityI(objD,27);
	
	loop{
		ascent(i in 0..(1024/1.5)*2){
			ObjSprite2D_SetDestRect(objA,-50,-(1024/1.5)*3+i,512/1.5-50,1024/1.5+i);
			ObjSprite2D_SetDestRect(objB,-512/1.5-90,-(1024/1.5)*3+i,-90,1024/1.5+i);
			ObjSprite2D_SetDestRect(objC,-60,-(1024/1.5)*3+i,512/1.5-60,1024/1.5+i);
			ObjSprite2D_SetDestRect(objD,-512/1.5-100,-(1024/1.5)*3+i+512,-100,1024/1.5+i+512);
		loop(4){yield;}}
	}
}

task TShade(){
	let shader = current ~ "./hlsl.txt";
	let renderTexture = GetReservedRenderTargetName(0);
	SetInvalidRenderPriorityA1(20, 25);

	let frameWidth = GetStgFrameWidth();
	let frameHeight = GetStgFrameHeight();
	let frameLeft = GetStgFrameLeft();
	let frameRight = frameLeft + frameWidth;
	let frameTop = GetStgFrameTop();
	let frameBottom = frameTop + frameHeight;
	
	let objShader = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objShader, 25);
	ObjPrim_SetTexture(objShader, renderTexture);
	ObjSprite2D_SetSourceRect(objShader, frameLeft, frameTop, frameRight, frameBottom);
	ObjSprite2D_SetDestRect(objShader, 0, 0, frameWidth, frameHeight);
	Obj_SetRenderPriorityI(objShader, 26);
	
	ObjShader_SetShaderF(objShader, shader);
	ObjShader_SetTechnique(objShader, "TecShader");
	
	while(!Obj_IsDeleted(objShader)){
		RenderToTextureA1(renderTexture, 20, 25, true);
	yield;}
	
	Obj_Delete(objShader);
	ClearInvalidRenderPriority();
}