PlayerShip.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:6k
源码类别:

游戏

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // File: PlayerShip.cpp
  3. //
  4. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  5. //-----------------------------------------------------------------------------
  6. #include "stdafx.h"
  7. //-----------------------------------------------------------------------------
  8. // Name: CPlayerShip()
  9. // Desc:
  10. //-----------------------------------------------------------------------------
  11. CPlayerShip::CPlayerShip() 
  12.     : C3DDisplayObject(OBJ_PLAYER) 
  13. {
  14.     m_fBulletRechargeTime   = 0.0f; 
  15.     m_fShield = 0.0f;
  16.     m_vEulerAngles = D3DXVECTOR3(0,0,0);
  17. }
  18. //-----------------------------------------------------------------------------
  19. // Name: ~CPlayerShip()
  20. // Desc:
  21. //-----------------------------------------------------------------------------
  22. CPlayerShip::~CPlayerShip(void)
  23. {
  24. }
  25. //-----------------------------------------------------------------------------
  26. // Name: 
  27. // Desc:
  28. //-----------------------------------------------------------------------------
  29. HRESULT CPlayerShip::OneTimeSceneInit( const DWORD dwStyle, const D3DXVECTOR3& vStartPos, const D3DXMATRIX* pmOrientation, const C3DModel* pModel )
  30. {
  31.     m_fMass                 = g_Profile.Ship.fMass;
  32.     m_bAffectByGravity      = true;
  33.     m_bHover                = false;
  34.     m_bReallyGoodHover      = true;
  35.     m_bAutoLevel            = false;
  36.     m_fGravityFactor        = 15.0f;
  37.     m_fAngularDragFactor    = -10.0f;
  38.     m_fBulletRechargeTime   = 0.0f; 
  39.     m_fShield               = 0.0f;
  40.     m_vEulerAngles          = D3DXVECTOR3(0,0,0);
  41.     m_fShield               = g_Profile.Ship.fMaxShield;
  42.     return C3DDisplayObject::OneTimeSceneInit( dwStyle, vStartPos, pmOrientation, pModel );
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Name: GetArtificialForces()
  46. // Desc:
  47. //-----------------------------------------------------------------------------
  48. HRESULT CPlayerShip::GetArtificialForces( const float fElapsedTime )
  49. {
  50.     if( NULL == g_pUserInput )
  51.         return E_FAIL;
  52.     if( g_pApp->m_bDebugMode )
  53.     {
  54.         D3DXVECTOR3 vUDThrust = D3DXVECTOR3(0.0f,1.0f,0.0f);
  55.         vUDThrust *= g_pUserInput->fAxisFlyUD * g_Profile.Ship.fAccelFactor;
  56.         m_pResult->m_vForcesLocal += vUDThrust;
  57.     }
  58.     D3DXVECTOR3 vForwardThrust = D3DXVECTOR3(0.0f,0.0f,1.0f);
  59.     vForwardThrust *= g_pUserInput->fAxisMoveUD * g_Profile.Ship.fAccelFactor;
  60.     m_pResult->m_vForcesLocal += vForwardThrust;
  61.     D3DXVECTOR3 vSidewaysThrust = D3DXVECTOR3(1.0f,0.0f,0.0f);
  62.     vSidewaysThrust *= g_pUserInput->fAxisMoveLR * g_Profile.Ship.fAccelFactor;
  63.     m_pResult->m_vForcesLocal += vSidewaysThrust;
  64.     // Mouselook based on euler angles
  65.     m_vEulerAngles.y += 5.0f * g_pUserInput->fAxisRotateLR * fElapsedTime;
  66.     m_vEulerAngles.x += -5.0f * g_pUserInput->fAxisRotateUD * fElapsedTime;
  67.     // Trap the up-down to good values
  68.     if( m_vEulerAngles.x < -D3DX_PI*0.5f )
  69.         m_vEulerAngles.x = -D3DX_PI*0.5f;
  70.     if( m_vEulerAngles.x > D3DX_PI*0.5f )
  71.         m_vEulerAngles.x = D3DX_PI*0.5f;
  72.     // Update the orientation based on the euler angles.  
  73.     // This overrides the angluar momement code
  74.     D3DXMatrixRotationYawPitchRoll( &m_pSource->m_mOrientation, 
  75.                                     m_vEulerAngles.y, // look left-right
  76.                                     m_vEulerAngles.x, // look up-down
  77.                                     0.0f );           // no roll
  78.     return S_OK;
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Name: FrameMoveFinalize()
  82. // Desc:
  83. //-----------------------------------------------------------------------------
  84. HRESULT CPlayerShip::FrameMoveFinalize( const float fElapsedTime )
  85. {
  86.     if( NULL == g_pUserInput )
  87.         return E_FAIL;
  88.     // Fire a bullet
  89.     m_fBulletRechargeTime -= fElapsedTime;
  90.     if( g_pUserInput->bButtonFireWeapons && m_fBulletRechargeTime <= 0.0f )
  91.     {
  92.         FireWeapon();
  93.         m_fBulletRechargeTime = g_Profile.Blaster.fRechargeTime;
  94.     }
  95.     return C3DDisplayObject::FrameMoveFinalize( fElapsedTime );
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Name: Render()
  99. // Desc:
  100. //-----------------------------------------------------------------------------
  101. HRESULT CPlayerShip::Render( const float fWrapOffsetX, const float fWrapOffsetZ, DWORD* pdwNumVerts )
  102. {
  103.     *pdwNumVerts = 0;
  104.     return S_OK;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Name: FireWeapon()
  108. // Desc:
  109. //-----------------------------------------------------------------------------
  110. HRESULT CPlayerShip::FireWeapon()
  111. {
  112.     if( g_pApp->m_pBullet1Sound )
  113.         g_pApp->m_pBullet1Sound->Play();
  114.     D3DXVECTOR3 vAheadLocal = D3DXVECTOR3(0.0,0.0f,1.0f);
  115.     D3DXVECTOR3 vAheadWorld;
  116.     D3DXVec3TransformNormal( &vAheadWorld, &vAheadLocal, &m_pSource->m_mOrientation ); 
  117.     CBullet* pBullet = new CBullet();
  118.     if( pBullet )
  119.     {
  120.         D3DXMATRIX mOrientation;
  121.         D3DXMatrixRotationYawPitchRoll( &mOrientation, rnd(-D3DX_PI,D3DX_PI), rnd(-D3DX_PI,D3DX_PI), rnd(-D3DX_PI,D3DX_PI) );
  122.         float fDot = D3DXVec3Dot( &m_pSource->m_vCMVel, &vAheadWorld );
  123.         D3DXVECTOR3 vStartPos = m_pSource->m_vCMPos + (fDot/10.0f + 1.0f) * vAheadWorld;
  124.         D3DXVECTOR3 vStartVel = D3DXVECTOR3(m_pSource->m_vCMVel.x,0.0f,m_pSource->m_vCMVel.z) + g_Profile.Blaster.fSpeed * vAheadWorld;
  125.         pBullet->OneTimeSceneInit( &g_Profile.Blaster, vStartPos, &mOrientation, vStartVel );
  126.         pBullet->InitDeviceObjects();
  127.         pBullet->RestoreDeviceObjects();
  128.         g_pTerrain->AddDisplayObject( pBullet );
  129.     }
  130.     m_fBulletRechargeTime = g_Profile.Blaster.fRechargeTime;
  131.     return S_OK;
  132. }