
include "sgs-bullet";

global GameConfig =
{
	GameName = "Connected worlds",
	AppDataDir = "SGRConnWorlds",
	SplashScreen = null,
	StartupLevel = "level0",
};

// ENGINE CONFIGURATION & TOOLS

function clamp( x, vmin, vmax ){ return max( min( x, vmax ), vmin ); }
function lerp( a, b, t ){ return a * (1-t) + b * t; }
function smoothstep( t ){ return t * t * ( 3 - 2 * t ); }

function gSGR_GetMesh( name )
{
	if( !isset( gSGR_SS3D.store, "meshes" ) ) gSGR_SS3D.store.meshes = {};
	meshes = gSGR_SS3D.store.meshes;
	if( !isset( meshes, name ) )
	{
		mesh = gSGR_SS3D.createMesh();
		mesh.loadFromBuffer( io_file_read( "rsrc/"$name$".ssm" ) );
		if( mesh )
			meshes[ name ] = mesh;
	}
	else
		mesh = meshes[ name ];
	return mesh;
}

function gSGR_GetColMesh( name )
{
	meshdata = io_file_read( "rsrc/"$name$".ssm" );
	viarrays = SS3D_MeshData_GetVertexIndexArrays( meshdata );
	colmesh = Bullet_CreateTriangleMesh( false );
	foreach( viarr : viarrays )
	{
		verts = viarr.vertices;
		inds = viarr.indices;
		for( i = 0; i < inds.size - 2; i += 3 )
		{
			colmesh.addTriangle( verts[ inds[ i ] ], verts[ inds[ i + 1 ] ], verts[ inds[ i + 2 ] ] );
		}
	}
	return colmesh;
}

global PauseScreen =
{
};
function PauseScreen.init()
{
	this.font = SS_CreateFont( "fonts/gauge-heavy.ttf", gSGR_Window.height / 30 );
}
function PauseScreen.on_event( e )
{
	if( e.type == SDL_KEYDOWN )
	{
		if( e.keycode == SDLK_Q )
			global sys_exit = true;
		if( e.keycode == SDLK_R )
			Game.Restart();
		if( e.keycode == SDLK_SPACE && SGR_GameState )
		{
			if( SGR_UnserializeState() )
				println( "unserialization successful!" );
			else
				ERROR( "unserialization failed: " $ gSGR_LastStateError );
			Game.OverlayScreens.remove( this );
			Game.SetPaused( false );
		}
	}
}
function PauseScreen.draw( dt )
{
	SS_DrawColorRect( 0, 0, gSGR_Window.width, gSGR_Window.height, 0, 0, 0, 0.5 );
	first = if( mission_complete, "- MISSION COMPLETE -\n\nWell done!", if( PLAYER.health <= 0, "- YOU WERE KILLED -", "- PAUSED -" ) );
	text = first$"\n\npress Q to quit the game\npress R to restart the game";
	if( SGR_GameState )
		text $= "\npress <Space> to reload the game from the last checkpoint";
	SS_DrawTextRect( text, this.font, color(1), DT_CENTER | DT_VCENTER,
		gSGR_Window.width / 4, gSGR_Window.width * 3 / 4, gSGR_Window.height * 1 / 5, gSGR_Window.height * 3 / 5 );
	return !Game.Paused;
}

function gSGR_HandleEvent( e )
{
	if( @PLAYER )
	{
		if( e.type == SDL_KEYDOWN && e.repeat == 0 && ( e.keycode == SDLK_ESCAPE || e.keycode == SDLK_P ) )
		{
			if( PLAYER.health > 0 && !mission_complete )
			{
				Game.SetPaused( !Game.Paused );
				if( Game.Paused )
					Game.OverlayScreens.push( PauseScreen );
			}
		}
	}
	
//	if( e.type == SDL_KEYDOWN && e.repeat == 0 )
//	{
//		if( e.keycode == SDLK_I ) println( "serialization " $ if( SGR_SerializeState(), "successful!", "failed..." ) );
//		if( e.keycode == SDLK_O ) println( "unserialization " $ if( SGR_UnserializeState(), "successful!", "failed: " $ gSGR_LastStateError ) );
//	}
}

function _SS3D_Texture_GetType( filepath )
{
	if( string_find( filepath, "normal" ) !== null || string_find( filepath, "bump" ) !== null ) return SS3DTEXTURE_USAGE_NORMAL;
	if( string_find( string_tolower( filepath ), "ui/" ) !== null ) return SS3DTEXTURE_USAGE_MISC;
	if( string_find( string_tolower( filepath ), "spotlight/" ) !== null ) return SS3DTEXTURE_USAGE_MISC;
	return SS3DTEXTURE_USAGE_ALBEDO;
}
