-- -----------------------------------------------------------------------------
-- SimNetwork: The network simulator
-- simnetwork.lua
-- Main file: initializes and starts game
-- -----------------------------------------------------------------------------

if not SimNetwork_GLOBAL	then dofile("global.lua")	end
if not SimNetwork_INPUT	then dofile("input.lua")	end
if not SimNetwork_TOOLBAR	then dofile("toolbar.lua")	end
if not SimNetwork_SCENE	then dofile("scene.lua")	end

--turn debuggin on/off
debug = false
  
--------------------------------------------------------------------------------
-- Function: dprint(string,entity)
-- Inputs: string, entity
-- Returns: none
-- Writes debugging info to the console
function dprint(string,entity)
	if debug == true then
		E.print_console(string,entity,"\n")
	end
end


--------------------------------------------------------------------------------
-- Function: create_scene_2d()
-- Inputs: none
-- Returns: none
--
-- Sets up 2D scene to be used for game's toolbar interface (and maybe 
-- other things in the future)
function create_scene_2d()

	-- create 2D camera, place it in the center of screen
	camera2d = E.create_camera(E.camera_type_orthogonal)
	E.set_entity_position(camera2d, -viewport.k * viewport.w/2, 
				-viewport.k * viewport.h/2, 0)
	E.set_entity_scale(scene2d, viewport.k, viewport.k, viewport.k)
	
	-- create 2D scene
	scene2d = E.create_pivot()
	
	-- add toolbar to 2D scene
	E.parent_entity(create_toolbar(), scene2d)
	E.parent_entity(scene2d, camera2d)
end

function do_contact(a, b, px, py, pz, xn, ny, nz, d)

	if debug then
		ax, ay, az = E.get_entity_position(a)
		bx, by, bz = E.get_entity_position(b)
		print("** Collision: ", ax, ay, az, bx, by, bz)
	end
	
	-- disable mouse ray collision
	gameScene:disableMouseRay()
	
	-- report collision
	gameScene:mousePick(a, b, {px, py, pz})
end

-- collect information about the display
init_viewport()

-- create toolbar
create_scene_2d()

-- create 3D game scene
gameScene = Scene:init()
gameScene:loadDemoScene()

E.enable_timer(true)

