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

游戏

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // File: Bullet.cpp
  3. //
  4. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  5. //-----------------------------------------------------------------------------
  6. #include "stdafx.h"
  7. //-----------------------------------------------------------------------------
  8. // Name: CBullet()
  9. // Desc:
  10. //-----------------------------------------------------------------------------
  11. CBullet::CBullet() 
  12.     : C3DDisplayObject(OBJ_BULLET) 
  13. {
  14.     m_fExpireCountdown = 0.0f;
  15. }
  16. //-----------------------------------------------------------------------------
  17. // Name: ~CBullet()
  18. // Desc:
  19. //-----------------------------------------------------------------------------
  20. CBullet::~CBullet(void)
  21. {
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Name: OneTimeSceneInit()
  25. // Desc:
  26. //-----------------------------------------------------------------------------
  27. HRESULT CBullet::OneTimeSceneInit( CBulletParameter* pBulletParam, const D3DXVECTOR3& vStartPos, 
  28.                                    const D3DXMATRIX* pmOrientation, const D3DXVECTOR3& vVel )
  29. {
  30.     m_pBulletParam = pBulletParam;
  31.     m_pSource->m_vCMVel = vVel;
  32.     m_fMass = pBulletParam->fMass;
  33.     m_fLinearDragFactor = 0.1f;
  34.     m_fExpireCountdown = pBulletParam->fExpireCountdown;
  35.     m_bAutoLevel        = false;
  36.     m_bHover            = false;
  37.     m_bReallyGoodHover  = false;
  38.     m_bAffectByGravity  = false;
  39.     ZeroMemory( &m_mtrl, sizeof(D3DMATERIAL9) );
  40.     m_mtrl.Diffuse.r = m_mtrl.Ambient.r = rnd(0);
  41.     m_mtrl.Diffuse.g = m_mtrl.Ambient.g = rnd(0);
  42.     m_mtrl.Diffuse.b = m_mtrl.Ambient.b = rnd(0);
  43.     m_mtrl.Diffuse.a = m_mtrl.Ambient.a = 1.0f;
  44.     return C3DDisplayObject::OneTimeSceneInit( 0, vStartPos, pmOrientation, pBulletParam->pModel );
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Name: FrameMove()
  48. // Desc:
  49. //-----------------------------------------------------------------------------
  50. HRESULT CBullet::FrameMove( const float fElapsedTime )
  51. {
  52.     m_fExpireCountdown -= fElapsedTime;
  53.     if( m_fExpireCountdown < 0.0f )
  54.         m_bActive = false;
  55.     return C3DDisplayObject::FrameMove( fElapsedTime );
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Name: GetArtificialForces()
  59. // Desc:
  60. //-----------------------------------------------------------------------------
  61. HRESULT CBullet::GetArtificialForces( const float fElapsedTime )
  62. {
  63. /*
  64.             // Move the bullets
  65.             CBullet2* pBullet = (CBullet2*)pObject;
  66.             if( pBullet->bActive )
  67.             {
  68.                 // Update the position and animation frame
  69.                 pBullet->vPos += pBullet->vVel * m_fElapsedTime;
  70.                 if( g_pTerrain )
  71.                     g_pTerrain->GetWorldFromUniversal( pBullet->vPos.x, pBullet->vPos.z, &pBullet->vPos.x, &pBullet->vPos.z );
  72.                 pBullet->fExpireCountdown -= m_fElapsedTime;
  73.             }
  74.             // TODO: kill bullet if it hits mountain (optional)
  75.             if( g_pTerrain )
  76.             {
  77.                 FLOAT fTerrianHeight = g_pTerrain->GetHeight( pObject->vPos.x, pObject->vPos.z );
  78.                 if( fTerrianHeight > pBullet->pBulletStyle->fFollowTerrianStartHeight ) 
  79.                     pBullet->vPos.y = fTerrianHeight + pBullet->pBulletStyle->fFollowTerrianOffset;
  80.                 else
  81.                     pBullet->vPos.y = pBullet->pBulletStyle->fMinHeight;
  82.             }
  83.             if( pBullet->pParticleSystem && g_Profile.bRenderParticles )
  84.             {
  85.                 D3DXVECTOR3 vEmitterPostion = pBullet->vPos;
  86.                 // Update particle system
  87.                 pBullet->pParticleSystem->Update( m_fElapsedTime, 
  88.                                         pBullet->pBulletStyle->dwNumParticlesToEmit,
  89.                                         pBullet->pBulletStyle->dwParticleColor,
  90.                                         pBullet->pBulletStyle->dwParticleColorFade, 
  91.                                         pBullet->pBulletStyle->fEmitVel,
  92.                                         vEmitterPostion, pBullet->bActive );
  93.             }
  94.             // Remove bullets when the expire countdown is done && particles gone
  95.             if( pBullet->fExpireCountdown < 0.0 )
  96.                 pBullet->bActive = FALSE;
  97.             if( !pBullet->bActive )
  98.             {
  99.                 if( (pBullet->pParticleSystem && pBullet->pParticleSystem->GetNumActiveParticles() == 0) ||
  100.                     (pBullet->pParticleSystem == NULL) )
  101.                 {
  102.                     CDisplayObject2* pVictim = pObject;
  103.                     pObject = pObject->pPrev;
  104.                     DeleteFromList( pVictim );
  105.                 }
  106.             }
  107. */
  108.     return S_OK;
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Name: Render()
  112. // Desc:
  113. //-----------------------------------------------------------------------------
  114. HRESULT CBullet::Render( const float fWrapOffsetX, const float fWrapOffsetZ, DWORD* pdwNumVerts )
  115. {
  116.     float fX = m_pResult->m_vCMPos.x;
  117.     float fY = m_pResult->m_vCMPos.y;
  118.     float fZ = m_pResult->m_vCMPos.z;
  119.     D3DXMATRIX matWorld;
  120.     g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, m_pBulletParam->dwAmbientLight );       
  121.     g_pd3dDevice->SetTexture( 0, NULL );
  122.     g_pd3dDevice->SetMaterial( &m_mtrl );
  123.     D3DXMatrixTranslation( &matWorld, fX + fWrapOffsetX, fY, fZ + fWrapOffsetZ );
  124.     D3DXMatrixMultiply( &matWorld, &m_pResult->m_mOrientation, &matWorld );
  125.     g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  126.     if( m_pBulletParam->pModel )
  127.         m_pBulletParam->pModel->Render( pdwNumVerts );
  128.     return S_OK;
  129. }