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

游戏

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // File: EnemyShip.cpp
  3. //
  4. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  5. //-----------------------------------------------------------------------------
  6. #include "stdafx.h"
  7. //-----------------------------------------------------------------------------
  8. // Name: CEnemyShip()
  9. // Desc:
  10. //-----------------------------------------------------------------------------
  11. CEnemyShip::CEnemyShip() 
  12.     : C3DDisplayObject( OBJ_ENEMY ) 
  13. {
  14.     m_fChangeDirCountdown = 0.0f;
  15.     m_bDeathAnimationActive = false;
  16.     m_fDeathAnimationCountdown = 0.0f;
  17.     m_fHitAnimationCountdown = 0.0f;
  18.     m_fInvulnerableCountdown = 0.0f;
  19.     m_fThrustCountdown = 0.0f;
  20.     m_fTurnCountdown = 0.0f;
  21.     m_fMoment = 0.0f;
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Name: ~CEnemyShip()
  25. // Desc:
  26. //-----------------------------------------------------------------------------
  27. CEnemyShip::~CEnemyShip(void)
  28. {
  29. }
  30. //-----------------------------------------------------------------------------
  31. // Name: 
  32. // Desc:
  33. //-----------------------------------------------------------------------------
  34. HRESULT CEnemyShip::OneTimeSceneInit( const DWORD dwStyle, const D3DXVECTOR3& vStartPos, const D3DXMATRIX* pmOrientation, const C3DModel* pModel )
  35. {
  36.     CEnemyStyle* pEnemyStyle = &g_Profile.aEnemyStyles[ m_dwStyle ];
  37.     m_fMass = pEnemyStyle->fMass;
  38.     m_fHealth = pEnemyStyle->fMaxHealth;
  39.     return C3DDisplayObject::OneTimeSceneInit( dwStyle, vStartPos, pmOrientation, pModel );
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Name: GetArtificialForces()
  43. // Desc:
  44. //-----------------------------------------------------------------------------
  45. HRESULT CEnemyShip::GetArtificialForces( const float fElapsedTime )
  46. {
  47.     // Update the position and animation frame
  48.     CEnemyStyle* pEnemyStyle = &g_Profile.aEnemyStyles[ m_dwStyle ];
  49.     m_fInvulnerableCountdown -= fElapsedTime;
  50.     m_fHitAnimationCountdown -= fElapsedTime;
  51.     if( m_bDeathAnimationActive )
  52.     {
  53.         m_fDeathAnimationCountdown -= fElapsedTime;
  54.         if( m_fDeathAnimationCountdown < 0.0f )
  55.         {
  56.             if( g_pApp->m_pExplosionDonutSound )
  57.                 g_pApp->m_pExplosionDonutSound->Play();
  58.             if( g_pApp->m_pMusicScript )
  59.             {
  60.                 g_pApp->m_pMusicScript->SetVariableNumber("ObjectCount",g_pTerrain->m_dwEnemyCount);
  61.                 g_pApp->m_pMusicScript->CallRoutine("ExplodeObject");
  62.             }
  63.             m_bActive = false;
  64.         }
  65.     }
  66.     else if( m_fHealth <= 0.0f )
  67.     {
  68.         m_bDeathAnimationActive = true;
  69.         m_fDeathAnimationCountdown = pEnemyStyle->fDeathAnimationCountdown;
  70.         // Keep object still while it is dying
  71.         m_bAllowObjectMovement = false; 
  72.     }
  73.     D3DXMATRIX mOInv;
  74.     D3DXMatrixInverse( &mOInv, NULL, &m_pSource->m_mOrientation );
  75.     D3DXVECTOR3 vXLocal = D3DXVECTOR3(1.0f,0.0f,0.0f);
  76.     D3DXVECTOR3 vYLocal = D3DXVECTOR3(0.0f,1.0f,0.0f);
  77.     D3DXVECTOR3 vZLocal = D3DXVECTOR3(0.0f,0.0f,1.0f);
  78.     D3DXVECTOR3 vXWorldInv;
  79.     D3DXVECTOR3 vYWorldInv;
  80.     D3DXVECTOR3 vZWorldInv;
  81.     D3DXVECTOR3 vXWorld;
  82.     D3DXVECTOR3 vYWorld;
  83.     D3DXVECTOR3 vZWorld;
  84.     D3DXVec3TransformNormal( &vXWorldInv, &vXLocal, &mOInv ); 
  85.     D3DXVec3TransformNormal( &vYWorldInv, &vYLocal, &mOInv ); 
  86.     D3DXVec3TransformNormal( &vZWorldInv, &vZLocal, &mOInv ); 
  87.     D3DXVec3TransformNormal( &vXWorld, &vXLocal, &m_pSource->m_mOrientation ); 
  88.     D3DXVec3TransformNormal( &vYWorld, &vYLocal, &m_pSource->m_mOrientation ); 
  89.     D3DXVec3TransformNormal( &vZWorld, &vZLocal, &m_pSource->m_mOrientation ); 
  90.     m_pResult->m_vForcesLocal = D3DXVECTOR3(0,0,0);
  91.     m_pResult->m_vMoments = D3DXVECTOR3(0,0,0);
  92.     if( g_pApp->m_bDebugMode && this == g_pDebugFirstEnemy )
  93.     {
  94.         float fScale = 1.0f;
  95.         if( g_pApp->m_pInputManager->m_UserInput.bButtonShift )
  96.             fScale = 0.1f;
  97.         m_pSource->m_vCMPos.x += g_vDebugMove.x * fScale;
  98.         m_pSource->m_vCMPos.y += g_vDebugMove.y * fScale;
  99.         m_pSource->m_vCMPos.z += g_vDebugMove.z * fScale;
  100.         m_pResult->m_vMoments += g_vDebugRotate.x * vXWorld * 1000.f;
  101.         m_pResult->m_vMoments += g_vDebugRotate.y * vYWorld * 1000.f;
  102.         m_pResult->m_vMoments += g_vDebugRotate.z * vZWorld * 1000.f;
  103.     }
  104.     switch( pEnemyStyle->EnemyMovementType ) 
  105.     {
  106.         case EMT_None:
  107.         {
  108.             break;
  109.         }
  110.         case EMT_MoveRandom:
  111.         {
  112.             m_fChangeDirCountdown -= fElapsedTime;
  113.             m_fThrustCountdown -= fElapsedTime;
  114.             m_fTurnCountdown -= fElapsedTime;
  115.             if( m_fChangeDirCountdown < 0.0f )
  116.             {
  117.                 m_fChangeDirCountdown = pEnemyStyle->fMoveRandomCountdown;
  118.                 m_fThrustCountdown = pEnemyStyle->fThrustCountdown;
  119.                 m_fTurnCountdown = pEnemyStyle->fTurnCountdown;
  120.                 m_fMoment = rnd();
  121.             }
  122.             if( m_fTurnCountdown > 0.0f )
  123.             {
  124.                 m_pResult->m_vMoments += m_fMoment * vYWorld * 1000.f;
  125.             }
  126.             if( m_fThrustCountdown > 0.0f )
  127.             {
  128.                 float fThrust = 0.3f;
  129.                 m_pResult->m_vForcesLocal -= fThrust * vXLocal * 10000.f;
  130.             }
  131.             break;
  132.         }
  133.         case EMT_MoveTowards:
  134.         {
  135.             // Not implemented
  136.             break;
  137.         }
  138.     }
  139.     return S_OK;
  140. }
  141. //-----------------------------------------------------------------------------
  142. // Name: Render()
  143. // Desc:
  144. //-----------------------------------------------------------------------------
  145. HRESULT CEnemyShip::Render( const float fWrapOffsetX, const float fWrapOffsetZ, DWORD* pdwNumVerts )
  146. {
  147.     float fX = m_pResult->m_vCMPos.x;
  148.     float fY = m_pResult->m_vCMPos.y;
  149.     float fZ = m_pResult->m_vCMPos.z;
  150.     CEnemyStyle* pEnemyStyle = &g_Profile.aEnemyStyles[m_dwStyle];
  151.     D3DMATERIAL9 mtrl;
  152.     D3DXMATRIX matPos;
  153.     D3DXMATRIX matScale;
  154.     D3DXMATRIX matRot;
  155.     D3DXMATRIX matWorld;
  156.     g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, pEnemyStyle->dwEnemyAmbientLight );       
  157.     g_pd3dDevice->SetTexture( 0, NULL );
  158. /*
  159.     if( m_fHitAnimationCountdown > 0.0f )
  160.     {
  161.         D3DUtil_InitMaterial( mtrl, 0.0f, 0.0f, 0.0f, 0.0f );
  162.     }
  163.     else
  164.     {
  165.         D3DXCOLOR clrOut;
  166.         D3DXColorLerp( &clrOut, &pEnemyStyle->clrDead, &pEnemyStyle->clrAlive, m_fHealth/pEnemyStyle->fMaxHealth );
  167.         D3DUtil_InitMaterial( mtrl, clrOut.r, clrOut.g, clrOut.b, clrOut.a );
  168.     }
  169. */
  170.     ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
  171.     mtrl.Diffuse.r = mtrl.Ambient.r = pEnemyStyle->clrAlive.r;
  172.     mtrl.Diffuse.g = mtrl.Ambient.g = pEnemyStyle->clrAlive.g;
  173.     mtrl.Diffuse.b = mtrl.Ambient.b = pEnemyStyle->clrAlive.b;
  174.     mtrl.Diffuse.a = mtrl.Ambient.a = pEnemyStyle->clrAlive.a;
  175.     pEnemyStyle->pModel->m_pMesh->m_pMaterials[1] = mtrl;
  176.     D3DXMatrixTranslation( &matPos, fX + fWrapOffsetX, fY, fZ + fWrapOffsetZ );
  177.     if( m_fDeathAnimationCountdown > 0.0f )
  178.     {
  179.         D3DXMATRIX matScale;
  180.         D3DXMATRIX matJitter;
  181.         float fZeroToOne = (pEnemyStyle->fDeathAnimationCountdown - m_fDeathAnimationCountdown)/pEnemyStyle->fDeathAnimationCountdown;
  182.         float fScale = fZeroToOne*5.0f + 1.0f;
  183.         D3DXMatrixTranslation( &matJitter, rnd()*0.3f, rnd()*0.3f, rnd()*0.3f );
  184.         D3DXMatrixScaling( &matScale, fScale, fScale, fScale );
  185.         D3DXMatrixMultiply( &matPos, &matScale, &matPos );
  186.         D3DXMatrixMultiply( &matPos, &matJitter, &matPos );
  187.     }
  188.     D3DXMatrixMultiply( &matWorld, &m_pResult->m_mOrientation, &matPos );
  189.     g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  190.     if( pEnemyStyle->pModel )
  191.         pEnemyStyle->pModel->Render( pdwNumVerts );
  192.     return S_OK;
  193. }