' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample 052  :  load simples B3D animation and set,
'                  with shadows
'     29/03/09  19: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

_ChangeWorkingDirectory("media/animations")



'----------------------------------------
 ' Load an 3D Object 
  Dim obj1 as AnimatedMesh = _LoadAnimatedMesh("ninja.b3d")
  Dim obj2 as AnimatedMesh = _LoadAnimatedMesh("dwarf.b3d")
  Dim obj3 as Mesh = _LoadMesh("../plane.3ds")
  Dim obj4 as AnimatedMesh = _LoadAnimatedMesh("../woodcrate.3ds")


'----------------------------------------
' Create traditional light And set position
Dim light as LightNode = _CreateLight(&hffaaaaaa, 80)
  _PositionNode(light, 20,30,-30)
'----------------------------------------


'-----------------------------------------
' Create plane
Dim plane as EntityNode =  _CreateEntityNode(obj3)
_ScaleNode(plane, 1.5,1.5,1.5)
_PositionNode(plane, 0,-1.5,0)
'----------------------------------------



'--------------------------------------
' Create cube 
Dim cube as EntityNode = _CreateAnimation(obj4)
_ScaleNode(cube, 0.02,0.02,0.02)
_PositionNode(cube,0,-1,0)
_ShadowVolumeEntity(cube)


'--------------------------------------
' Create first animation B3D
Dim ninja as AnimatedNode = _CreateAnimation(obj1)
_SpeedAnimation(ninja, 10)
_RotateNode(ninja, 0,180,0)
_FrameLoopAnimation(ninja,  1, 14)
_ShadowVolumeAnimation(ninja)

' edit number total of the Joint inside the animation
'Debug _AnimationJointCount(ninja)
' affect on the joint nuber 18 the arrow
Dim bone As BoneNode = _AnimationJointNode(ninja, 18)
_AddChildNode(bone, cube)





'--------------------------------------
' Create second animation B3D
Dim dwarf as AnimatedNode = _CreateAnimation(obj2)
_SpeedAnimation(dwarf, 8)
_Scalenode(dwarf, 0.12,0.12,0.12)
_RotateNode(dwarf, 0,180,0)
_PositionNode(dwarf, -10,0,10)
_ShadowVolumeAnimation(dwarf)



'_ShadowColor( &h96000000)




'-----------------------------------
' Create a camera
Dim cam as CameraNode = _CreateCameraFPS( )
_PositionNode(cam, 0,10,-13)



'-----------------------------------
' Load font png
_LoadFont("../font2.bmp")
Dim font as IGUIFont = _GetFont()


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

' If Escape Key, Exit	
  If _GetKeyDown(KEY_ESCAPE) then
    Quit=1
  EndIf
  
  ' Select sequence
  If _GetKeyUp(KEY_F1) then
    If _GetKeyDown(KEY_CONTROL) Then
      _FrameLoopAnimation(ninja,  134, 145)
    Else
      _FrameLoopAnimation(ninja,  1, 14)
    EndIf
  EndIf
  If _GetKeyUp(KEY_F2) Then
    If _GetKeyDown(KEY_CONTROL) Then
      _FrameLoopAnimation(ninja,  146, 158)
    Else
      _FrameLoopAnimation(ninja,  15, 30)
    EndIf
  EndIf
  If _GetKeyUp(KEY_F3) Then
    If _GetKeyDown(KEY_CONTROL) Then
      _FrameLoopAnimation(ninja,  159,165)
    Else
      _FrameLoopAnimation(ninja,  32, 44)
    EndIf
  EndIf
  If _GetKeyUp(KEY_F4) Then
    If _GetKeyDown(KEY_CONTROL) Then
      _FrameLoopAnimation(ninja,  166,173)
    Else
      _FrameLoopAnimation(ninja,  45, 59)
    EndIf
  EndIf
  If _GetKeyUp(KEY_F5) Then
    If _GetKeyDown(KEY_CONTROL) Then
      _FrameLoopAnimation(ninja,  174,182)
    Else
      _FrameLoopAnimation(ninja,  60, 68)
    EndIf
  EndIf
  If _GetKeyUp(KEY_F6) Then
    If _GetKeyDown(KEY_CONTROL) Then
      _FrameLoopAnimation(ninja,  184,205)
    Else
      _FrameLoopAnimation(ninja,  69, 72)
    EndIf
  EndIf
  If _GetKeyUp(KEY_F7) Then
    If _GetKeyDown(KEY_CONTROL) Then
      _FrameLoopAnimation(ninja,  206,250)
    Else
      _FrameLoopAnimation(ninja,  73, 83)
    EndIf
  EndIf
  If _GetKeyUp(KEY_F8) Then
    If _GetKeyDown(KEY_CONTROL) Then
      _FrameLoopAnimation(ninja,  251,300)
    Else
      _FrameLoopAnimation(ninja,  84, 93)
    EndIf
  EndIf
  If _GetKeyUp(KEY_F9) Then
    _FrameLoopAnimation(ninja,  94, 102)
  EndIf
  If _GetKeyUp(KEY_F10) Then
    _FrameLoopAnimation(ninja,  103, 111)
  EndIf
  If _GetKeyUp(KEY_F11) Then
    _FrameLoopAnimation(ninja,  112, 125)
  EndIf
  If _GetKeyUp(KEY_F12) then
    _FrameLoopAnimation(ninja,  126, 133)
  EndIf




  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene(50, 50, 50)
     _DrawScene()
     _DrawText(font, "Animation Frame Number Ninja: "+ str(_AnimationFrameNumber(ninja)),  10,10,0,0, &hff00ffff)     
     _DrawText(font, "Pres F1-F12 and Ctrl+F1>-<Ctrl+F8 to change ninja animation",  10,26,0,0, &hffffff00)     
  _EndScene()


Wend 
' end
_FreeEngine()

