Author Topic: Прикрепляем оружие к FPS-камере  (Read 909 times)

0 Members and 1 Guest are viewing this topic.

Offline Alec - WS3D Developer

  • Producer
  • Administrator
  • Marquess
  • *
  • Posts: 932
  • Reputation 36
  • Game making is my hobby for life!
Прикрепляем оружие к FPS-камере
« on: November 19, 2014, 07:20:17 PM »
Как прикрепить оружие к FPS камере?

Через функцию wAddChildToParent.
Code: [Select]
'' ----------------------------------------------------------------------------
'' Пример сделал Frank Dodd
'' Изменил Alec (WorldSim3D developer)
'' ----------------------------------------------------------------------------
'' Пример: Прикрепление оружия к FPS камере, управляемой клавишами WASD
'' Пример показывает, как прикрепить оружие к FPS камере, используя
'' функцию wAddChildToParent
'' ----------------------------------------------------------------------------

'' Includes for extension libraries
#include "WorldSim3D.bi"

'' global variables

' WorldSim3D objects
DIM BSPMesh as wMesh
DIM BSPNode as wNode
DIM Camera as wCamera
DIM CameraNode as wNode
DIM GunNode as wNode
Dim GunMesh As wMesh
Dim GunTexture As wTexture

' -----------------------------------------------------------------------------
' start the WorldSim3D interface
wStart( wDT_DIRECT3D9, 800, 600, wBITS_PER_PIXEL_32,_
          wWINDOWED, wNO_SHADOWS, wCAPTURE_EVENTS, wVERTICAL_SYNC_ON )

' send the window caption
wSetWindowCaption( "Attach a weapon to a camera" )

' first we add the pk3 archive to our filing system. once we have done this
' we can open any of the files in the archive as if they were in the current
' working directory
wAddZipFile( "../media/BSPmaps/ctfcomp02_hazard.pk3", wIGNORE_CASE, wIGNORE_PATHS )

' load the BSP map from the archive as a mesh object. any polygons in the mesh
' that do not have textures will be removed from the scene!
BSPMesh = wGetMesh( "ctfcomp02_hazard.bsp" )

' add the map to the scene as a node. when adding the mesh this call uses a
' mechanism called an octtree that if very efficient at rendering large amounts
' of complext geometry most of which cant be seen, using this call for maps
' will greatly improve your framerates
BSPNode = wAddMeshToSceneAsOcttree( BSPMesh )

' add a first person perspective camera into the scene that is controlled with
' the mouse and the cursor keys. if however you capture events when starting
' WorldSim3D this will become a normal camera that can only be moved by code
'Camera = wAddFPSCamera
DIM keyMapArray(4) as SKeyMap
keyMapArray(1).Action = KA_MOVE_FORWARD
keyMapArray(1).KeyCode = KEY_KEY_W
keyMapArray(2).Action = KA_MOVE_BACKWARD
keyMapArray(2).KeyCode = KEY_KEY_S
keyMapArray(3).Action = KA_STRAFE_LEFT
keyMapArray(3).KeyCode = KEY_KEY_A
keyMapArray(4).Action = KA_STRAFE_RIGHT
keyMapArray(4).KeyCode = KEY_KEY_D

Camera = wAddFPSCamera ( _
    wNO_OBJECT, _   ' parent, none
    50.0, _           ' rotate speed
    0.1, _             ' move speed
    -1, _              ' camera ID
    @keyMapArray(1), _ ' the address of the keyMap
    4, _               ' number of entries in the keymap
    0, _               ' no vertical movement
    0.0 )              ' jumpspeed

' when we add a camera we are returned a camera object however we can perform
' node operations on many different object types that are entities within the
' scene however first we need to convert the camera type into a node type
CameraNode = Camera

' reposition and rotate the camera to look at a nice part of the map
wSetNodePosition( CameraNode, 386, 366, 2726 )
wSetCameraTarget( CameraNode, -534, 299, 5345 )

' Загружаем модель оружия
GunMesh = wGetMesh( "../media/Models/Psionic/4guns/lazer.3ds")

' Загружаем текстуру для модели оружия
GunTexture = wGetTexture( "../media/Models/Psionic/4guns/lazer.jpg" )
' Добавляем на сцену нод оружия
GunNode = wAddMeshToScene( GunMesh )
' применяем текстуру оружия к ноду оружия
wSetNodeMaterialTexture( GunNode, GunTexture, 0 )

' для тестовых целей отключим освещение оружия, а то по умолчанию оно будет чёрным
wSetNodeMaterialFlag( GunNode, wMF_LIGHTING, wOFF )

' Прикрепляем оружие к камере
wAddChildToParent( GunNode, CameraNode )

' Меняйте здесь положение оружия относительно камеры, как вам надо
wSetNodePosition( GunNode, 7, -3, 10 )

' hide the mouse pointer
wHideMouse

' -----------------------------------------------------------------------------
' while the WorldSim3D environment is still running
WHILE wRunning
        ' begin the scene, erasing the canvas with sky-blue before rendering
        wBeginScene( 240, 255, 255 )
   
        ' draw the scene
        wDrawScene
   
        ' end drawing the scene and display it
        wEndScene
        wSetFPS
      wCloseESC()
WEND

' -----------------------------------------------------------------------------
' Stop the WorldSim3D engine and release resources
wStop
« Last Edit: November 19, 2014, 08:17:26 PM by Alec - WS3D Developer »
WorldSim3D = 3D + FreeBasic. Программируй в удовольствие, а не "в тягость"! Make your project with pleasure, not with strain!