#Include "Ninfa3d.bi"

dim Shared camera As nCAMERA
dim light01 As nLIGHT
DIM AS nTEXTURE tex01,tex02
Dim font01 As nFONT

'Enable vertical synch.
EnableVsync()

'Starts the Ninfa3D Engine
InitEngine(640,480,32,0)
	BackGroundColor(128,128,128)'Background Color
	AmbientLight(32,32,32)'Ambient color
	WorldGravity(0,-200.0,0)
	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 = CreateCameraFPS(200,0.25)
	PositionEntity(camera,0,64,-256)

'Creates the lights
light01 = CreateLight(LGT_POINT)
    lightcolor(light01,200,200,0)
	PositionEntity(light01,0,60,0)' Position

CreateGround()'Creates the test area

sub CreateChain( ByVal x As Single,ByVal y As Single,ByVal z As Single,ByVal mass As Single)

	Dim Body(30) As nBODY
	Dim c As nMESH
	Dim ball As nJOINT
	Dim i As UInteger
	Dim As nTEXTURE text01 = LoadTexture( "media\wall1.jpg")

	For i = 1 To 10
		If i = 10 Then
			c = CreateMeshSphere(32)
			entityTexture(c,text01,0)
			ScaleMesh(c, 20, 20, 20)
		Else
			c = CreateMeshSphere(12)
			entityTexture(c,text01,0)
			ScaleMesh(c, 5, 5, 5)
		EndIf
		EntityShininess(c,20)
		if i = 10 Then
			Body(i) = CreateBodySphere(20,20,20,mass)' 1 + (i = height) * 2)
		Else
			If i = 1 then
				Body(i) = CreateBodySphere(5,5,5,0)
			Else
				Body(i) = CreateBodySphere(5,5,5,mass/1.5)
			EndIf
		EndIf
	
		EntityParent(Body(i),c)
		PositionEntity(Body(i),x,y - i * 13,z)
		PositionEntity(c,x,y - i * 13,z)
	
		If i <> 1 Then
			ball = CreateJointBall(x,y - i * 13,z,1,1,1,Body(i), Body(i - 1))'Create joint
				BallLimits(ball,45.0,-10.0,10.0)'Limits!!!
		EndIf
	
	Next

End sub

CreateChain( 128,200,0,10)
CreateChain( 64,200,0,10)
CreateChain( 0,200,0,20)
CreateChain( -64,200,0,10)
CreateChain( -128,200,0,10)

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)

	'Pressing "MOUSE_LEFT" creates a bullet.
	'(To see the code, open the file functions SampleFunctions.bb)
	'If MouseHit(MOUSE_LEFT) Then CreateBullet()

	WorldMousePick(MouseX(),MouseY(),MOUSEDOWN(MOUSE_LEFT))
	
	'Displays the interface (FPS and triangles)
	Interface()

	'End the scene
	EndScene()
	
Wend

'Ends Ninfa3D Engine
EndEngine()