#Include "Ninfa3d.bi"

dim Shared camera As nCAMERA
dim As nLIGHT light01
Dim sprite As nSPRITE
Dim tex01 As nTEXTURE
Dim As nMESH mesh,mesh2,mesh03
Dim As nBODY body,body2
Dim sound02 As nSOUND

'Enable vertical synch.
EnableVsync()

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

'Creates the lights
light01 = CreateLight(LGT_POINT)
	PositionEntity(light01,0,128,256)' Position
	LightCastShadow(light01)'It makes the light cast shadows.
	LightRadius(light01,256)' Radius of the light
	
sprite = LoadSprite("media/glow.bmp")
	EntityParent(sprite,light01)
	ScaleSprite(sprite,30,30)

CreateGround()'Creates the test area

'Player
mesh = CreateMeshCylinder(12)
	EntityColor(mesh,255,0,0)
	ScaleMesh(mesh,10,10,10)
	PositionEntity(mesh,0,10,100)'Move the mesh

'Sensor
mesh2 = CreateMeshCube()
	EntityColor(mesh2,0,0,255)
	ScaleMesh(mesh2,100,10,10)
	positionEntity(mesh2,0,10,0)

'Load the sound.
sound02 = LoadSound("media\alarm.wav")
	SoundType(sound02,1)'Type sound, in this case 3D.
	LoopSound(sound02)'Loop

Dim snd As Integer = 1
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)
	
	If KeyDown(KEY_W) Then MoveEntity(mesh,0,0,0.1)
	If KeyDown(KEY_S) Then MoveEntity(mesh,0,0,-0.1)

	If KeyDown(KEY_A) Then MoveEntity(mesh,-0.1,0,0)
	If KeyDown(KEY_D) Then MoveEntity(mesh,0.1,0,0)
	
	If MeshesCollide(mesh,mesh2) Then
		Text(25,125,"Detected!",0,0)
		LightColor(light01,255,0,0)
		SpriteColor(sprite,255,0,0)
		If snd = 1 Then
			PlaySound(sound02)
			snd = 0
		EndIf
	Else
		snd = 1
		LightColor(light01,255,255,255)
		SpriteColor(sprite,255,255,255)
		StopSound(sound02)
	endif

	'Displays the interface (FPS and triangles)
	Interface()
	
	Text(10,100,"Move the player using: W,S,A,D",0,0)

	'End the scene
	EndScene()
	
Wend

'Ends Ninfa3D Engine
EndEngine()