#Include "Ninfa3d.bi"
'Thanks to Evolved for  Map - Scape map
'Thanks to Ramadan for impreza model (iphy example)
'Don't use the impreza model for comercial purpose
dim Shared camera As nCAMERA


dim As nLIGHT light01,light02,light03,light04,light05
Dim As nSPRITE sprite01,sprite02,sprite03,sprite04,sprite05
Dim castle As nMESH
Dim  texcub As nTexture
Dim As nJOINT joint(20 to 29)
dim as single xt,yt,zt,yc
Dim fire As nPARTICLE
Dim emitter01 As nEMITTER
Dim tex_particle As nTEXTURE
'Enable vertical synch.
EnableVsync()



'Starts the Ninfa3D Engine
InitEngine(1024,768,32,0)
	BackGroundColor(128,128,200)'Background Color
	WorldGravity(0,-200,0)
	WorldSize(20480,5120,20480)
	PhysicQuality(Q_linear)
    AmbientLight(0,0,0)'Ambient color
   
Dim As nMESH mshsph = CreateMeshSphere(12)'use this sphere as pivot camera
ScaleMesh( mshsph, 0.1, 0.1, 0.1 )
PositionEntity(mshsph,0,-90,-40)

castle = LoadMesh("media2/test.x")



Dim As nBODY bdy10 = CreateBodyTree(castle,0)'Create a cube body
PositionEntity(bdy10,0,0,0)

camera = CreateCamera()
camerarange(camera,1,10000)


Dim As nMESH msh2 = LoadMesh("media2\impreza1.3ds")'Load a mesh
	    
    entityflag(msh2,6,0)
    EntityParent(mshsph,msh2)
    PositionEntity(camera,0,-40,-50)
	
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,50,-70,100)
    PositionEntity(bdy2,0,-70,0)
    
light01 = CreateLight(LGT_POINT)
    lightradius(light01,100)
    lightcolor(light01,255,0,0)
	PositionEntity(light01,0,72,320)' Position
    
sprite01 = LoadSprite("media/red.bmp")
	EntityParent(sprite01,light01)
	ScaleSprite(sprite01,30,30)

light02 = CreateLight(LGT_POINT)
    lightradius(light02,100)
    lightcolor(light02,0,0,255)
	PositionEntity(light02,320,72,-16)' Position
    
sprite02 = LoadSprite("media/blue.bmp")
	EntityParent(sprite02,light02)
	ScaleSprite(sprite02,30,30)

light03 = CreateLight(LGT_POINT)
    lightradius(light03,100)
    lightcolor(light03,0,255,0)
	PositionEntity(light03,-180,-30,-200)' Position
    
sprite03 = LoadSprite("media/green.bmp")
	EntityParent(sprite03,light03)
	ScaleSprite(sprite03,30,30)

light04 = CreateLight(LGT_SPOT)
    lightradius(light04,250)
    lightcolor(light04,250,250,0)
    LightConeAngles( light04, 20.0, 60.0 )
	PositionEntity(light04,5,-0.4,18)' Position
	entityparent(light04,msh2)
    'LightAttenuation(light04,1,0,0)
    
sprite04 = LoadSprite("media/glow.bmp")
	
    positionentity(sprite04,0,0,0)
    EntityParent(sprite04,light04)
	ScaleSprite(sprite04,15,15)

light05 = CreateLight(LGT_SPOT)
    lightradius(light05,250)
    lightcolor(light05,250,250,0)
     LightConeAngles( light05, 20.0, 60.0 )
	PositionEntity(light05,-5,-0.4,18)' Position
    entityparent(light05,msh2)
    'LightAttenuation(light05,1,0,0)
    
sprite05 = LoadSprite("media/glow.bmp")
	
    positionentity(sprite05,0,0,0)
    EntityParent(sprite05,light05)
	ScaleSprite(sprite05,15,15)


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

Dim As nJOINT FLWheel = LoadMesh("media2\tire1.3ds")'FindChild(msh2,"RuedaDelIzq")
Dim As nJOINT FRWheel = LoadMesh("media2\tire1.3ds")'FindChild(msh2,"RuedaDelDer")

Dim As nJOINT BLWheel = LoadMesh("media2\tire1.3ds")'FindChild(msh2,"RuedaTraIzq")
Dim As nJOINT BRWheel = LoadMesh("media2\tire1.3ds")'FindChild(msh2,"RuedaTraDer")

'Configuration of the wheels.
Dim As single MASS = 15.0f
Dim As single FRICTION	= 1.0f
Dim As single sLENGTH = 7.5f
Dim As single sSPRING = 150.0f
Dim As single sDAMPER = 2.5f

'Add wheels to the chassis of the vehicle.
AddTire(vehicle,FLWheel,7.0,-4,11,MASS,4.0,2,FRICTION,sDAMPER,sSPRING,sDAMPER)
AddTire(vehicle,FRWheel,-7.0,-4,11,MASS,4.0,2,FRICTION,sDAMPER,sSPRING,sDAMPER)
AddTire(vehicle,BLWheel,7.0,-4,-10.5,MASS,4.0,2,FRICTION,sDAMPER,sSPRING,sDAMPER)
AddTire(vehicle,BRWheel,-7.0,-4,-10.5,MASS,4.0,2,FRICTION,sDAMPER,sSPRING,sDAMPER)

dim as single x1,y1,z1,x2,y2,z2

tex_particle = LoadTexture("media/glow.BMP") 'Load the texture.
'Create the particles, in this case to create fire type 1.
fire = CreateParticle()'Creates particles
	PositionEntity(fire,-4,-3,-20)'Position
	ParticleEndSize(fire,-10,-10)'Size of the final particles
	EntityTexture(fire,tex_particle,0)'Texture
    
emitter01 = CreateRingEmitter(fire,300,300)
    RingEmitterCenter(emitter01,0,0,0)
    RingEmitterRadius(emitter01,0.5)
    RingEmitterThickness(emitter01,0.5)
    EmitterDirection(emitter01,0,0,0) 'Direction of emitter.
	EmitterPPS(emitter01,50,50)'Particles per second to be emitt by the emitter.
	EmitterStartColor(emitter01,200,200,200,200,200,200)'Color of the minimum and maximum particle.
	EmitterStartSize(emitter01,0,0,5,5)'Minimum and maximum size of the particle.

entityparent(fire,msh2)

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)

	x1=entityX(camera)
    y1=entityY(camera)
    z1=entityZ(camera)
    x2=targetX(camera)
    y2=targetY(camera)
    z2=targetZ(camera)
    
    
    
	 
    yt=0
    xt=entityX(camera)
    zt=entityZ(camera)
    yc=entityY(camera)
    If KeyDown(KEY_W) Then
		ApplyTorque(vehicle,0,100.0f)
		ApplyTorque(vehicle,1,100.0f)
		'ApplyTorque(vehicle,2,100.0f)
		'ApplyTorque(vehicle,3,100.0f)
	ElseIf KeyDown(KEY_S) Then
		ApplyTorque(vehicle,0,-100.0f)
		ApplyTorque(vehicle,1,-100.0f)
		ApplyTorque(vehicle,2,0.0f)
		ApplyTorque(vehicle,3,0.0f)
	else
        ApplyBrake(vehicle,0,0.01f)
		ApplyBrake(vehicle,1,0.01f)
        ApplyBrake(vehicle,2,0.01f)
		ApplyBrake(vehicle,3,0.01f)
		'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
    
    'PositionEntity( camera, CurveValue( entityX(mshsph), entityX(camera), 0.000001 ), entityY(mshsph)+90, CurveValue( entityZ(mshsph), entityZ(camera), 0.000001 ) )
	CameraTarget(camera,EntityX(bdy2),EntityY(bdy2),EntityZ(bdy2))
    WorldMousePick(MouseX(),MouseY(),MouseDown(MOUSE_LEFT))
	Text(15,75,"WASD -control",0,0)

	EndScene()
	
Wend

'Ends Ninfa3D Engine
EndEngine()