
LEVEL_START();

gSGR_Scene.fogColor = vec3(0.15,0.16,0.17);
gSGR_Scene.fogDensity = 0.15;
gSGR_Scene.fogHeightDensity = 0.0;
gSGR_Scene.fogStartHeight = 0;
gSGR_Scene.fogHeightFactor = 0;
gSGR_Scene.ambientLightColor = vec3(0.01,0.03,0.05)*2;
//gSGR_Scene.skyTexture = gSGR_SS3D.getTexture( "testdata/cubemap_beach_skybox.dds" );

////////////
items = [];

vdecl1 =  gSGR_SS3D.createVertexDecl( "pf3nf3tf40f2" );
water_mesh = gSGR_SS3D.createMesh();
vdata = fmt_pack( "12f 12f 12f 12f",
	-100, -100, 0,  0, 0, 1,  1, 0, 0, 1,  00, 00,
	 100, -100, 0,  0, 0, 1,  1, 0, 0, 1,  20, 00,
	 100,  100, 0,  0, 0, 1,  1, 0, 0, 1,  20, 20,
	-100,  100, 0,  0, 0, 1,  1, 0, 0, 1,  00, 20 );
water_mesh.setVertexData( vdata, vdecl1, false );
water_mesh.setAABBFromVertexData( vdata, vdecl1 );
water_mesh.setIndexData( fmt_pack( "6w", 0, 1, 2, 2, 3, 0 ), false );
water_mesh.numParts = 1;
water_mesh.setPartRanges( 0, 0, 4, 0, 6 );
water_mesh.transparent = true;
water_mesh.setPartShader( 0, "water" );
water_mesh.setPartTexture( 0, 0, gSGR_SS3D.getTexture( "rsrc/water_bump.png" ) );
water_mesh_inst = gSGR_Scene.createMeshInstance();
water_mesh_inst.mesh = water_mesh;
water_mesh_inst.matrix = mat4().translate(0,-102,0);
items.push( water_mesh_inst );

clouds = floatarray_buffer( 128 * 2 );
clouds.randbox( vec3(-500,-500,100), vec3(500,500,200) );
clouds_colors = floatarray( 0.3, 0.4, 0.5, 0.1 );
clouds_sizes = floatarray( 200 );
cloud_mesh = gSGR_SS3D.createMesh( true );
cloud_mesh.numParts = 1;
cloud_mesh.transparent = true;
cloud_mesh.setPartShader( 0, "sprite" );
cloud_mesh.setPartTexture( 0, 0, gSGR_SS3D.getTexture( "rsrc/smoke.png" ) );
cloud_mesh.setPartTexture( 0, 1, gSGR_SS3D.getTexture( "rsrc/smoke_normal.png" ) );
cloud_mesh_inst = gSGR_Scene.createMeshInstance();
cloud_mesh_inst.mesh = cloud_mesh;
cloud_mesh_inst.setConstV3( 1, vec3( 0.5 ) );
cloud_data = [ cloud_mesh, clouds, clouds_sizes, clouds_colors ];
items.push( cloud_mesh_inst );

function MKPOINTLIGHT( pos, range, power, color )
{
	light = gSGR_Scene.createLight();
	light.type = SS3DLIGHT_POINT;
	light.color = color;
	light.position = pos;
	light.range = range;
	light.power = power;
	light.isEnabled = true;
	return light;
}

// spotlight 1 backing
light = gSGR_Scene.createLight();
light.type = SS3DLIGHT_POINT;
light.color = vec3(255/255,214/255,170/255)*0.2;
light.position = vec3(0,0,3);
light.range = 100;
light.power = 0.5;
light.isEnabled = true;
items.push( light );

// underground 1
light = gSGR_Scene.createLight();
light.type = SS3DLIGHT_POINT;
light.color = vec3(255/255,214/255,170/255)*0.5;
light.position = vec3(-3.00406,13.04432,-13.03516);
light.range = 10;
light.power = 1;
light.isEnabled = true;
items.push( light );

// LIGHT_3
items.push( MKPOINTLIGHT( vec3(27.5,58.5,7.3), 10, 1, vec3(255/255,214/255,170/255)*0.5 ) );
gSGR_World.add_entity( ENTITY_CREATE_sprite({ position = items.last.position, color = color(255/255*5,214/255*5,170/255*5), size = 0.5, texture = "rsrc/lightsprite.png" }) );
// LIGHT_4
items.push( MKPOINTLIGHT( vec3(20.5,48.1,7.35), 10, 1, vec3(255/255,214/255,170/255)*0.5 ) );
gSGR_World.add_entity( ENTITY_CREATE_sprite({ position = items.last.position, color = color(255/255*5,214/255*5,170/255*5), size = 0.5, texture = "rsrc/lightsprite.png" }) );
// END LIGHT
items.push( MKPOINTLIGHT( vec3(-45,110,20), 50, 1, vec3(255/255,214/255,170/255)*0.5 ) );
items.push( MKPOINTLIGHT( vec3(-5,90,10), 50, 2, vec3(170/255,214/255,255/255)*0.1 ) );

// "moon" light
light = gSGR_Scene.createLight();
light.type = SS3DLIGHT_POINT;
light.color = vec3(0.1,0.3,0.5)*3;
light.position = vec3(0,-540,65);
light.range = 650;
light.power = 1;
light.isEnabled = true;
items.push( light );
gSGR_World.add_entity( ENTITY_CREATE_sprite({ position = light.position, color = color(0.2*9,0.3*9,0.4*9), size = 95, texture = "rsrc/lightsprite.png" }) );

// spotlight 1
light = gSGR_Scene.createLight();
light.type = SS3DLIGHT_SPOT;
light.color = vec3(255/255,214/255,170/255)*5;
light.position = vec3(0,0,10);
light.range = 20;
light.power = 2;
light.direction = vec3(0,0,-1).normalized;
light.updir = vec3(0,1,0);
light.angle = 30;
light.isEnabled = 1;
light.genSpotLightMatrices();
light.cookieTexture = gSGR_SS3D.getTexture( "rsrc/cookie1.png" );
light.shadowTexture = gSGR_SS3D.createRT( 256, 256, SS3DRT_FORMAT_DEPTH );
items.push( light );

// spotlight 2
light = gSGR_Scene.createLight();
light.type = SS3DLIGHT_SPOT;
light.color = vec3(255/255,214/255,170/255)*5;
light.position = vec3(6,52.5,7);
light.range = 20;
light.power = 2;
light.direction = vec3(2,3,-1).normalized;
light.updir = vec3(0,1,0);
light.angle = 60;
light.isEnabled = 1;
light.genSpotLightMatrices();
light.cookieTexture = gSGR_SS3D.getTexture( "rsrc/cookie1.png" );
light.shadowTexture = gSGR_SS3D.createRT( 256, 256, SS3DRT_FORMAT_DEPTH );
items.push( light );
items.push( MKPOINTLIGHT( light.position, 5, 1, vec3(255/255,214/255,170/255)*0.5 ) );
gSGR_World.add_entity( ENTITY_CREATE_sprite({ position = light.position, color = color(255/255*5,214/255*5,170/255*5), size = 1, texture = "rsrc/lightsprite.png" }) );

var level_tick;
water_time = 0;
function level_tick( dt ) use( water_mesh_inst, water_time, cloud_data )
{
	water_time += dt;
	water_mesh_inst.setConstF( 0, water_time );
	SS3D_UpdateParticleMesh( gSGR_SS3D, cloud_data[0], gSGR_Scene.camera.viewMatrix, cloud_data[1], cloud_data[2], cloud_data[3] );
	UPDATE_LEVEL( dt );
}
function level_serialize() use( water_time ){ return "level0desc", null, { water_time = water_time }; }
function level_can_reuse( type ){ return type == "level0desc"; }
function level_unserialize( data ) use( water_time ){ water_time = data.water_time; }

gSGR_World.add_entity({ z_index = 10000, items = items, tick = level_tick, serialize = level_serialize, can_reuse = level_can_reuse, unserialize = level_unserialize }); // persist handles

// PLAYER
// - TEST
//gSGR_World.add_entity( ENTITY_CREATE_player({ x = 5, y = 73, z = 4.7 }) );
// - REAL
gSGR_World.add_entity( ENTITY_CREATE_player({ x = -6.5, y = -11, z = 1, direction = vec3(1,-0.5,0) }) );

// LEVEL
gSGR_World.add_entity( ENTITY_CREATE_static_mesh({ mesh = "level0", position = vec3(0,0,0) }) );
gSGR_World.add_entity( ENTITY_CREATE_static_mesh({ mesh = "level1", position = vec3(0,50,0) }) );
gSGR_World.add_entity( ENTITY_CREATE_static_mesh({ mesh = "level2", position = vec3(-18,102,-6) }) );

// TUNNELS
gSGR_World.add_entity( ENTITY_CREATE_portal({ x = -0.15899, y = 1.67601, z = -7.28232, d = 2.88, h = 18.432 }) );
gSGR_World.add_entity( ENTITY_CREATE_portal({ x = -6.00861, y = 25.62205, z = -4.53172, d = 2.88, h = 18.432 }) );
gSGR_World.add_entity( ENTITY_CREATE_portal({ x = 5.50923, y = 40.33553, z = -5.03039, d = 2.88, h = 18.432 }) );
gSGR_World.add_entity( ENTITY_CREATE_portal({ x = 19.44712, y = 41.07299, z = -4.16369, d = 2.88, h = 18.432 }) );
// - house
gSGR_World.add_entity( ENTITY_CREATE_portal({ x = 27.5, y = 58.5, z = -4.39048, d = 2.88, h = 18.432 }) );
gSGR_World.add_entity( ENTITY_CREATE_portal({ x = 19, y = 58.5, z = -4.39048, d = 2.88, h = 18.432 }) );
gSGR_World.add_entity( ENTITY_CREATE_portal({ x = 10.09581, y = 57.71785, z = -4.39048, d = 2.88, h = 18.432 }) );
gSGR_World.add_entity( ENTITY_CREATE_portal({ x = 4, y = 61, z = -4.39048, d = 2.88, h = 18.432 }) );

// TURRETS
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(-8.09604,10.29221,3.55684), direction = vec3(1,-3,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(-7,41.5,4.6), direction = vec3(1,0,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(17,36,4.9), direction = vec3(-1,-1,0).normalized }) );
// - house
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(27.5,55.5,4.9), direction = vec3(0,-1,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(21.5,58.5,4.9), direction = vec3(1,0,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(19,62,4.9), direction = vec3(0,-1,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(13,57.7,4.9), direction = vec3(-1,0,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(7,57.7,4.9), direction = vec3(1,0,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(6.5,50.5,4.9), direction = vec3(0,1,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(10,53,4.9), direction = vec3(0,1,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(10,61,4.9), direction = vec3(0,-1,0).normalized }) );
gSGR_World.add_entity( ENTITY_CREATE_turret({ position = vec3(6.5,61.2,4.9), direction = vec3(2,1,0).normalized }) );

// SAVE TRIGGERS
gSGR_World.add_entity( ENTITY_CREATE_save_trigger({ trigger_pos = vec3(20.5,52,5), trigger_dist = 2 }) );

// CUTSCENES
// - 1: intro
gSGR_World.add_entity( ENTITY_CREATE_cutscene({ trigger_pos = vec3(-6.5,-11,0.5), trigger_dist = 5, length = 10, actions =
[
	// text
	{ from = 0.5, to = 2.5, type = "subtitle", text = "snake5 presents" },
	{ from = 3.5, to = 5.5, type = "subtitle", text = "a new stealth game" },
	{ from = 6.5, to = 9.5, type = "subtitle", text = "C O N N E C T E D   W O R L D S" },
	// camera
	{ from = 0.0, to = 8.0, type = "camera", p0 = vec3(-6.5,-13,2), p1 = vec3(1.5,-13,5), t0 = vec3(1.5,-15,-1), t1 = vec3(1.5,-20,5) },
]
}));
// - 2: turrets & tunnels
gSGR_World.add_entity( ENTITY_CREATE_cutscene({ trigger_pos = vec3(0.15144,-1.95121,1.96413), trigger_dist = 2, length = 10, save_end = true, actions =
[
	{ from = 0.5, to = 4.5, type = "subtitle", text = "Turrets will shoot if they see the target for some time." },
	{ from = 0.0, to = 4.0, type = "camera", p0 = vec3(0,-2,4), p1 = vec3(-8,10,3.8), t0 = vec3(-8,10,4.2), d0 = 0, d1 = 1.5, a0 = -90, a1 = -105 },
	{ from = 5.5, to = 8.5, type = "subtitle", text = "Use these tunnels to travel underground, it may help avoid turrets." },
	{ from = 5.0, to = 9.0, type = "camera", p0 = vec3(-0.15899,1.67601,2), p1 = vec3(-0.15899,1.67601,4), t0 = vec3(-0.15899,1.67601,2), d0 = 1, d1 = 3, a0 = 0, a1 = 270 },
]
}));
// - 3: disabling turrets
gSGR_World.add_entity( ENTITY_CREATE_cutscene({ trigger_pos = vec3(-6.00861,35.62205,-4.5317), trigger_dist = 18, length = 5, save_end = true, actions =
[
	{ from = 0.5, to = 4.5, type = "subtitle", text = "It is also possible to disable turrets by interacting with them while standing close." },
	{ from = 0.0, to = 5.0, type = "camera", p0 = vec3(-7,41.5,5), p1 = vec3(-7,41.5,6.6), t0 = vec3(-7,41.5,4.6), d0 = 1, d1 = 2, a0 = 0, a1 = 180 },
]
}));
// - 3: ending
gSGR_World.add_entity( ENTITY_CREATE_cutscene({ trigger_pos = vec3(1.2,76.8,4.7), trigger_dist = 3, length = 12, end = true, actions =
[
	{ from = 0.5, to = 5.5, type = "subtitle", text = "We have gone a long way... this adventure is over... will a new one come?" },
	{ from = 6.5, to = 12, type = "subtitle", text = "- T H E   E N D -" },
	// camera
	{ from = 0.0, to = 10.0, type = "camera", p0 = vec3(1.2,76.8,5), p1 = vec3(-22,113,10), t0 = vec3(-5,88,-5), t1 = vec3(-23,118,14), d0 = 0, d1 = 10, a0 = 0, a1 = -90 },
]
}));
