#TouhouDanmakufu[Stage]
#Title["Hyouga Kazu"]
#Text["RaNGE Contest 9 entry by       Karanum. Come, go through the  gap into an unknown world."]
#ScriptVersion[3]

let gcsd = GetCurrentScriptDirectory;
let bgm = gcsd~"bgm/bgm.ogg";
let bgm2 = gcsd~"bgm/bgm2.ogg";
let azimuth = 5;
let bgmobj;
let boom = gcsd~"sfx/enep00.wav";

@Event
{
	if(GetEventType == EV_USER)
	{
		ObjSound_SetFade(bgmobj, 1); ObjSound_Stop(bgmobj); loop(60){yield;} PlayBGM(bgm2, 0, 0);
	}
}

@Initialize
{
	KazuStage;
	Background;
	LoadSound(bgm2);
	bgmStart;

	SetPauseScriptPath(gcsd~"system/Eng_Pause.txt");
	SetEndSceneScriptPath(gcsd~"system/Eng_EndScene.txt");
	SetReplaySaveSceneScriptPath(gcsd~"system/Eng_ReplaySaveScene.txt");
}

@MainLoop
{
	yield;
}

@Finalize
{
}

task bgmStart
{
	bgmobj = ObjSound_Create;
	ObjSound_Load(bgmobj, bgm);
	ObjSound_Play(bgmobj);
}

task KazuStage
{
	loop(360)
	{
		azimuth+=(45/360);yield;
	} 
	let part1 = LoadScriptInThread(gcsd~"C9Kazu1.txt");
	loop(60){yield;}
	StartScript(part1);
	while(!IsCloseScript(part1)){yield;}
	loop(60){yield;}
	let part2 = LoadScriptInThread(gcsd~"C9Kazu2.txt");
	loop(60){yield;}
	StartScript(part2);
	while(!IsCloseScript(part2)){yield;}
	loop(60){yield;}
	let part3 = LoadScriptInThread(gcsd~"C9Kazu3.txt");
	loop(60){yield;}
	StartScript(part3);
	while(!IsCloseScript(part3)){yield;}
	loop(120){yield;}
	CloseStgScene();
}

task Background
{
	SetCameraFocusX(0);SetCameraFocusY(0);SetCameraFocusZ(0);
	SetCameraRadius(800-350*(azimuth/50));
	SetCameraAzimuthAngle(90);
	SetCameraElevationAngle(azimuth);
	SetFogEnable(true);
	SetFogParam(1190, 1200, 25, 25, 85);

	let z1 = 0; let z2 = 0;
	let wr = 200; let wg = 0; let wb = 0;
	let ang = 0;

	let floor = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(floor, gcsd~"floor.png");
	ObjSprite3D_SetSourceDestRect(floor, 0, 0, 640, 640);
	ObjRender_SetAngleXYZ(floor, 90, 0, 0);
	ObjRender_SetScaleXYZ(floor, 2, 2, 1);
	ObjRender_SetPosition(floor, 0, -64, -148);
	Obj_SetRenderPriorityI(floor, 20);
	ObjRender_SetZWrite(floor, true);
	ObjRender_SetZTest(floor, true);
	
	let roof = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(roof, gcsd~"roof.png");
	ObjSprite3D_SetSourceDestRect(roof, 0, 0, 640, 1280);
	ObjRender_SetAngleXYZ(roof, 90, 0, 0);
	ObjRender_SetPosition(roof, 0, 224, 0);
	Obj_SetRenderPriorityI(roof, 20);
	ObjRender_SetZWrite(roof, true);
	ObjRender_SetZTest(roof, true);

	let wall1 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(wall1, gcsd~"wall.png");
	ObjSprite3D_SetSourceDestRect(wall1, 0, 0, 360, 320);
	ObjRender_SetAngleXYZ(wall1, 0, 90, -90);
	ObjRender_SetScaleXYZ(wall1, 1, 4, 1);
	ObjRender_SetPosition(wall1, -128, 42, -128);
	Obj_SetRenderPriorityI(wall1, 21);
	ObjRender_SetZWrite(wall1, true);
	ObjRender_SetZTest(wall1, true);

	let wall2 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(wall2, gcsd~"wall.png");
	ObjSprite3D_SetSourceDestRect(wall2, 0, 0, 360, 320);
	ObjRender_SetAngleXYZ(wall2, 0, 90, -90);
	ObjRender_SetScaleXYZ(wall2, 1, 4, 1);
	ObjRender_SetPosition(wall2, 128, 42, -128);
	Obj_SetRenderPriorityI(wall2, 21);
	ObjRender_SetZWrite(wall2, true);
	ObjRender_SetZTest(wall2, true);

	let wave1 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(wave1, gcsd~"wave.png");
	ObjSprite3D_SetSourceRect(wave1, 0, 0, 1280, 19);
	ObjSprite3D_SetDestRect(wave1, 0, -9, 21, 19);
	ObjRender_SetScaleXYZ(wave1, 1, 70, 1);
	ObjRender_SetAngleXYZ(wave1, 0, 90, 90);
	ObjRender_SetPosition(wave1, 127, 24, 0);
	Obj_SetRenderPriorityI(wave1, 21);
	ObjRender_SetBlendType(wave1, BLEND_ADD_RGB);
	ObjRender_SetZWrite(wave1, true);
	ObjRender_SetZTest(wave1, true);
	ObjRender_SetColor(wave1, wr, wg, wb);

	let wave2 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(wave2, gcsd~"wave.png");
	ObjSprite3D_SetSourceRect(wave2, 0, 0, 1280, 19);
	ObjSprite3D_SetDestRect(wave2, 0, -9, 21, 19);
	ObjRender_SetScaleXYZ(wave2, 1, 70, 1);
	ObjRender_SetAngleXYZ(wave2, 0, 90, 90);
	ObjRender_SetPosition(wave2, -127, 24, 0);
	Obj_SetRenderPriorityI(wave2, 21);
	ObjRender_SetBlendType(wave2, BLEND_ADD_RGB);
	ObjRender_SetZWrite(wave2, true);
	ObjRender_SetZTest(wave2, true);
	ObjRender_SetColor(wave2, 255-wr, 255-wg, 255-wb);

	let back = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(back, gcsd~"roof.png");
	ObjSprite3D_SetSourceDestRect(back, 0, 0, 640, 640);
	ObjRender_SetPosition(back, 0, 0, -1190);
	Obj_SetRenderPriorityI(back, 20);
	ObjRender_SetZWrite(back, true);
	ObjRender_SetZTest(back, true);

	ColorSwitcher;

	let r = 750;

	loop
	{
		if(r>450){r-=3.5; yield;}

		SetCameraRadius(r);
		SetFogParam(0, 1200, 15, 15, 45);

		ObjRender_SetColor(wave1, wr, wg, wb);
		ObjRender_SetColor(wave2, 200-wr, 200-wg, 200-wb);

		z1+=4; z2+=4;
		while(z1>=128){z1-=128;}
		while(z2>=21*4){z2-=(21*4);}

		ObjSprite3D_SetSourceDestRect(floor, -z1/2, 0, 640-z1/2, 640);
		ObjSprite3D_SetSourceDestRect(roof, -z1/2, 0, 640-z1/2, 1280);
		ObjSprite3D_SetSourceRect(wall1, z2/4, 0, 360+z2/4, 320);
		ObjSprite3D_SetSourceRect(wall2, z2/4, 0, 360+z2/4, 320);
		ObjSprite3D_SetSourceRect(wave1, -z2/2, 0, 1280-z2/2, 19);
		ObjSprite3D_SetSourceRect(wave2, -z2/2, 0, 1280-z2/2, 19);

		ang+=0.2;
		SetCameraAzimuthAngle(90+3*sin(ang));
		SetCameraElevationAngle(azimuth+1-cos(ang*2));

		if(azimuth==25){Obj_Delete(roof);}

		yield;
	}

	task ColorSwitcher
	{
		loop
		{
			loop(240){wg+=(200/240);yield;}
			loop(240){wr-=(200/240);yield;}
			loop(240){wb+=(200/240);yield;}
			loop(240){wg-=(200/240);yield;}
			loop(240){wr+=(200/240);yield;}
			loop(240){wb-=(200/240);yield;}
		}
	}
}
