
' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample  101   as   Collide test.
'   Historique   as 
'     29/03/09  19 as 16    TMyke
'
' ------------------------------------------------------------

' Includes libraries
#include "N3xtD.bi"

' Dimes
Dim app As any Ptr
Dim Quit As Integer


'----------------------------------------------------------
' open n3xt-D screen
app = _CreateGraphics3D(800,600)
If app=NULL Then
  End
End If  
 
  
'----------------------------------------
' Create traditional light And set position
 Dim light as LightNode = _CreateLight(&hffdddddd, 200)
  _PositionNode(light, 5,25,3)
 _AmbientLight(&hff222222)



'-----------------------------------------
' Create a static base
 Dim cubemesh as Mesh = _CreateCubeMesh(1.0)
  
  Dim cube as EntityNode = _CreateEntityNode(cubemesh)
  _PositionNode(cube, 0,-2,0) 
  _LoadTextureNode(cube, "media/grille1.bmp") 
  Dim mat as NMaterial = _NodeMaterial(cube)
  _ColorMaterial(mat, ECM_NONE)     
  _ScaleNode( cube, 20,1,20)
 

'-----------------------------------------
' Create a cube 
  For i as Integer = 0 To 15
    cube = _CreateEntityNode(cubemesh)
    _PositionNode(cube, -6+Rnd()*12,-1,-6+Rnd()*12)
    _LoadTextureNode(cube, "media/body.bmp") 
    Dim mat as NMaterial = _NodeMaterial(cube)
    _ColorMaterial(mat, ECM_NONE)     
    ' Create collide Type
    _CreateNodeCollide(cube, BOX_PRIMITIVE, 1)
  Next

   cube = _CreateEntityNode(cubemesh)
    _PositionNode(cube, 0,-1,-5)
    _LoadTextureNode(cube, "media/body.bmp") 
    mat = _NodeMaterial(cube)
    _ColorMaterial(mat, ECM_NONE)     
    ' Create collide Type
    _CreateNodeCollide(cube, BOX_PRIMITIVE, 1)

    cube = _CreateEntityNode(cubemesh)
    _PositionNode(cube, 0,1,-5)
    _LoadTextureNode(cube, "media/body.bmp") 
    mat = _NodeMaterial(cube)
    _ColorMaterial(mat,  ECM_NONE)     
    ' Create collide Type
    _CreateNodeCollide(cube, BOX_PRIMITIVE, 1)


'-----------------------------------------
' Create sphere
Dim spheremesh as Mesh = _CreateSphereMesh(1)
 
dim sphere as EntityNode = _CreateEntityNode(spheremesh)
 _PositionNode(sphere, 0,-0.25,0)
 _LoadTextureNode(sphere, "media/body.bmp") 
    ' Create collide Type
    _CreateNodeCollide(sphere, SPHERE_PRIMITIVE ,1)



 

'-----------------------------------------
' Create first  camera
Dim cam as CameraNode = _CreateCamera( )
_PositionNode(cam, 0,7,-12)
_RotateNode(cam, 38,0,0)

'-----------------------------------------
_LoadFont("media/courriernew.png")
dim font as IGUIFont = _GetFont()

' ---------------------------------------
'           Main loop
' ---------------------------------------
While Quit=0
 

' If Escape Key, Exit	
  If _GetKeyDown(KEY_ESCAPE) then
    Quit=1
  EndIf
  
 
 	' move sphere with dir key
  If _GetKeyDown(KEY_ARROW_UP) then
      _TranslateNode(sphere, 0,0,0.1)
      Dim num as Integer = _NodeCollideAll(sphere)
      For i as Integer = 0 To num 
        Dim res As EntityNode =_CollideNode(i)
        If res Then
            _DiffuseColorNode(res, &hff0000ff)
        EndIf 
      Next    
  EndIf
  If _GetKeyDown(KEY_ARROW_DOWN) Then
      _TranslateNode(sphere, 0,0,-0.1)
      Dim num as Integer = _NodeCollideAll(sphere)
      For i as Integer = 0 To num 
        Dim res As EntityNode =_CollideNode(i)
        If res Then
            _DiffuseColorNode(res, &hff0000ff)
        EndIf 
      Next    
  EndIf
  If _GetKeyDown(KEY_ARROW_LEFT) Then
      _TranslateNode(sphere, -0.1,0,0)
      Dim num as Integer = _NodeCollideAll(sphere)
      For i as Integer = 0 To num 
        Dim res As EntityNode =_CollideNode(i)
        If res Then
            _DiffuseColorNode(res, &hff0000ff)
        EndIf 
      Next    
  EndIf
  If _GetKeyDown(KEY_ARROW_RIGHT) Then
      _TranslateNode(sphere, 0.1,0,0)
      Dim num as Integer = _NodeCollideAll(sphere)
      For i as Integer = 0 To num 
        dim res as EntityNode =_CollideNode(i)
        If res Then
            _DiffuseColorNode(res, &hff0000ff)
        EndIf 
      Next    
 EndIf


  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene()
     _DrawScene()
     _DrawText(font, "Use Dir Key to move Sphere",  10,10,0,0)
  _EndScene()



Wend 
' end
_FreeEngine()
