Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "weapon_sdkbase.h"
  8. #include "sdk_fx_shared.h"
  9. #if defined( CLIENT_DLL )
  10. #define CWeaponMP5 C_WeaponMP5
  11. #include "c_sdk_player.h"
  12. #else
  13. #include "sdk_player.h"
  14. #endif
  15. class CWeaponMP5 : public CWeaponSDKBase
  16. {
  17. public:
  18. DECLARE_CLASS( CWeaponMP5, CWeaponSDKBase );
  19. DECLARE_NETWORKCLASS();
  20. DECLARE_PREDICTABLE();
  21. CWeaponMP5();
  22. virtual void PrimaryAttack();
  23. virtual bool Deploy();
  24. virtual bool Reload();
  25. virtual void WeaponIdle();
  26. virtual SDKWeaponID GetWeaponID( void ) const { return WEAPON_MP5; }
  27. private:
  28. CWeaponMP5( const CWeaponMP5 & );
  29. void Fire( float flSpread );
  30. };
  31. IMPLEMENT_NETWORKCLASS_ALIASED( WeaponMP5, DT_WeaponMP5 )
  32. BEGIN_NETWORK_TABLE( CWeaponMP5, DT_WeaponMP5 )
  33. END_NETWORK_TABLE()
  34. BEGIN_PREDICTION_DATA( CWeaponMP5 )
  35. END_PREDICTION_DATA()
  36. LINK_ENTITY_TO_CLASS( weapon_mp5, CWeaponMP5 );
  37. PRECACHE_WEAPON_REGISTER( weapon_mp5 );
  38. CWeaponMP5::CWeaponMP5()
  39. {
  40. }
  41. bool CWeaponMP5::Deploy( )
  42. {
  43. CSDKPlayer *pPlayer = GetPlayerOwner();
  44. pPlayer->m_iShotsFired = 0;
  45. return BaseClass::Deploy();
  46. }
  47. bool CWeaponMP5::Reload( )
  48. {
  49. CSDKPlayer *pPlayer = GetPlayerOwner();
  50. if (pPlayer->GetAmmoCount( GetPrimaryAmmoType() ) <= 0)
  51. return false;
  52. int iResult = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_VM_RELOAD );
  53. if ( !iResult )
  54. return false;
  55. pPlayer->SetAnimation( PLAYER_RELOAD );
  56. #ifndef CLIENT_DLL
  57. if ((iResult) && (pPlayer->GetFOV() != pPlayer->GetDefaultFOV()))
  58. {
  59. pPlayer->SetFOV( pPlayer, pPlayer->GetDefaultFOV() );
  60. }
  61. #endif
  62. pPlayer->m_iShotsFired = 0;
  63. return true;
  64. }
  65. void CWeaponMP5::PrimaryAttack( void )
  66. {
  67. const CSDKWeaponInfo &pWeaponInfo = GetSDKWpnData();
  68. CSDKPlayer *pPlayer = GetPlayerOwner();
  69. float flCycleTime = pWeaponInfo.m_flCycleTime;
  70. bool bPrimaryMode = true;
  71. float flSpread = 0.01f;
  72. // more spread when jumping
  73. if ( !FBitSet( pPlayer->GetFlags(), FL_ONGROUND ) )
  74. flSpread = 0.05f;
  75. pPlayer->m_iShotsFired++;
  76. // Out of ammo?
  77. if ( m_iClip1 <= 0 )
  78. {
  79. if (m_bFireOnEmpty)
  80. {
  81. PlayEmptySound();
  82. m_flNextPrimaryAttack = gpGlobals->curtime + 0.2;
  83. }
  84. }
  85. SendWeaponAnim( ACT_VM_PRIMARYATTACK );
  86. m_iClip1--;
  87. // player "shoot" animation
  88. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  89. FX_FireBullets(
  90. pPlayer->entindex(),
  91. pPlayer->Weapon_ShootPosition(),
  92. pPlayer->EyeAngles() + pPlayer->GetPunchAngle(),
  93. GetWeaponID(),
  94. bPrimaryMode?Primary_Mode:Secondary_Mode,
  95. CBaseEntity::GetPredictionRandomSeed() & 255,
  96. flSpread );
  97. pPlayer->DoMuzzleFlash();
  98. m_flNextPrimaryAttack = m_flNextSecondaryAttack = gpGlobals->curtime + flCycleTime;
  99. if (!m_iClip1 && pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) <= 0)
  100. {
  101. // HEV suit - indicate out of ammo condition
  102. pPlayer->SetSuitUpdate("!HEV_AMO0", false, 0);
  103. }
  104. // start idle animation in 5 seconds
  105. SetWeaponIdleTime( gpGlobals->curtime + 5.0 );
  106. }
  107. void CWeaponMP5::WeaponIdle()
  108. {
  109. if (m_flTimeWeaponIdle > gpGlobals->curtime)
  110. return;
  111. // only idle if the slid isn't back
  112. if ( m_iClip1 != 0 )
  113. {
  114. SetWeaponIdleTime( gpGlobals->curtime + 5.0f );
  115. SendWeaponAnim( ACT_VM_IDLE );
  116. }
  117. }