#Include "Ninfa3d.bi"

dim Shared camera As nCAMERA
dim As nLIGHT light01,light02

'Enable vertical synch.
EnableVsync()

'Starts the Ninfa3D Engine
InitEngine(640,480,32,0)
	BackGroundColor(128,128,128)'Background Color
	AmbientLight(132,132,132)'Ambient color
	WorldGravity(0,-200,0)
	WorldSize(20480,10240,20480)
	Fog(96,96,96,0.0003)' Fog
	PhysicQuality(Q_LINEAR)

'Include the file "SampleFuntions.bi.
'It contains useful features, such as drawing the interface and create the test area.
#Include "SampleFunctions.bi"

'Create a camera
camera = CreateCamera()

'Creates the lights
light01 = CreateLight(LGT_POINT)
	PositionEntity(light01,610,128,610)' Position
	
'JEEP
Dim As nMESH msh2 = LoadMesh("media\chassis.ms3d")'Load a mesh
	'MeshCastShadow(msh2)'Make the mesh and get cast shadows.
	
	PositionEntity(camera,30,20,-30)'Position
    EntityParent(camera,msh2)
	
Dim As nBODY bdy2 = CreateBodyHull(msh2,300.0)'Create a cube body
	EntityParent(bdy2,msh2)'This makes the mesh assigned, follow both
								  'Position as the rotation of the body.
	PositionEntity(bdy2,600,80,600)

Dim As nJOINT vehicle = CreateVehicle(0,0,1,bdy2)

Dim As nJOINT FLWheel = LoadMesh("media\FLWheel.ms3d")'FindChild(msh2,"RuedaDelIzq")
Dim As nJOINT FRWheel = LoadMesh("media\FRWheel.ms3d")'FindChild(msh2,"RuedaDelDer")

Dim As nJOINT BLWheel = LoadMesh("media\BLWheel.ms3d")'FindChild(msh2,"RuedaTraIzq")
Dim As nJOINT BRWheel = LoadMesh("media\BRWheel.ms3d")'FindChild(msh2,"RuedaTraDer")

'Configuration of the wheels.
Dim As single MASS = 15.0f
Dim As single FRICTION	= 2.5f
Dim As single sLENGTH = 7.0f
Dim As single sSPRING = 100.0f
Dim As single sDAMPER = 2.0f

'Add wheels to the chassis of the vehicle.
AddTire(vehicle,FLWheel,8,-5,10,MASS,3.25,4,FRICTION,sDAMPER,sSPRING,sDAMPER)
AddTire(vehicle,FRWheel,-8,-5,10,MASS,3.25,4,FRICTION,sDAMPER,sSPRING,sDAMPER)
AddTire(vehicle,BLWheel,8,-5,-10,MASS,3.25,4,FRICTION,sDAMPER,sSPRING,sDAMPER)
AddTire(vehicle,BRWheel,-8,-5,-10,MASS,3.25,4,FRICTION,sDAMPER,sSPRING,sDAMPER)


Dim As nTEXTURE tex01 = LoadTexture("media\Mud0029_1_M.jpg")

'Create a terrain mesh
Dim As nTERRAIN terrain = LoadTerrain("media\TOPOMAP1.jpg")
	ScaleMesh(terrain,10,1,10)
	EntityTexture(terrain,tex01,0)
	ScaleTexture(terrain,10,0)'Scale the texture of the terrain
	PositionEntity(terrain,-100,0,0)

Dim As nBODY PHSterrain = CreateBodyTerrain(terrain,5)
	PositionEntity(PHSterrain,-100,0,0)

Dim As nMESH msh
Dim As nBODY bdy
Dim i As integer
For i = 0 To 600

	msh = CreateMeshCube()'Load a mesh
		ScaleMesh(msh,10,2,10)

	bdy = CreateBodyCube(10,2,10,0.5)'Create a cube body
		EntityParent(bdy,msh)'This makes the mesh assigned, follow both
								  'Position as the rotation of the body.
		PositionEntity(bdy,1200-Rand(-500,1000),Rand(450,1024),2250-Rand(0,2000))'Position the body.
Next

While(EngineRun)'Returns 1 if the engine is running.

	'Begins scene
	BeginScene()
	
	'By pressing the "ESCAPE" closes the engine.
	If KeyHit(KEY_ESCAPE) Then CloseEngine()
	
	'Update the Engine.
	'Changing the value (only affects the physics) that will update the
	'Physical faster or slower.
	UpdateEngine(0.05)
    WorldMousePick(MouseX(),MouseY(),MouseDown(MOUSE_LEFT))

	If KeyDown(KEY_W) Then
		ApplyTorque(vehicle,0,5.0f)
		ApplyTorque(vehicle,1,5.0f)
		'ApplyTorque(vehicle,2,50.0f)
		'ApplyTorque(vehicle,3,50.0f)
	ElseIf KeyDown(KEY_S) Then
		ApplyTorque(vehicle,0,-5.0f)
		ApplyTorque(vehicle,1,-5.0f)
		'ApplyTorque(vehicle,2,-50.0f)
		'ApplyTorque(vehicle,3,-50.0f)
	else
		ApplyTorque(vehicle,0,0.0f)
		ApplyTorque(vehicle,1,0.0f)
		ApplyTorque(vehicle,2,0.0f)
		ApplyTorque(vehicle,3,0.0f)
	EndIf

	If KeyDown(KEY_A) Then
			ApplySteering(vehicle,0,-0.0001f)
			ApplySteering(vehicle,1,-0.0001f)
	ElseIf KeyDown(KEY_D) Then
			ApplySteering(vehicle,0,0.0001f)
			ApplySteering(vehicle,1,0.0001f)
	Else
		ApplySteering(vehicle,0,0.0f)
		ApplySteering(vehicle,1,0.0f)
	EndIf

	If KeyDown(KEY_SPACE) Then
		ApplyBrake(vehicle,0,1.0f)
		ApplyBrake(vehicle,1,1.0f)
        ApplyBrake(vehicle,2,1.0f)
		ApplyBrake(vehicle,3,1.0f)
	EndIf

	CameraTarget(camera,EntityX(bdy2),EntityY(bdy2),EntityZ(bdy2))

	'Displays the interface (FPS and triangles)
	'Interface()

	'End the scene
	EndScene()
	
Wend

'Ends Ninfa3D Engine
EndEngine()