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.

204 lines
3.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "weapon_csbase.h"
  8. #include "fx_cs_shared.h"
  9. #if defined( CLIENT_DLL )
  10. #define CWeaponP228 C_WeaponP228
  11. #include "c_cs_player.h"
  12. #else
  13. #include "cs_player.h"
  14. #endif
  15. class CWeaponP228 : public CWeaponCSBase
  16. {
  17. public:
  18. DECLARE_CLASS( CWeaponP228, CWeaponCSBase );
  19. DECLARE_NETWORKCLASS();
  20. DECLARE_PREDICTABLE();
  21. CWeaponP228();
  22. virtual void Spawn();
  23. virtual void PrimaryAttack();
  24. virtual bool Deploy();
  25. virtual bool Reload();
  26. virtual void WeaponIdle();
  27. virtual float GetInaccuracy() const;
  28. virtual CSWeaponID GetWeaponID( void ) const { return WEAPON_P228; }
  29. private:
  30. CWeaponP228( const CWeaponP228 & );
  31. float m_flLastFire;
  32. };
  33. #if defined CLIENT_DLL
  34. BEGIN_PREDICTION_DATA( CWeaponP228 )
  35. DEFINE_FIELD( m_flLastFire, FIELD_FLOAT ),
  36. END_PREDICTION_DATA()
  37. #endif
  38. LINK_ENTITY_TO_CLASS( weapon_p228, CWeaponP228 );
  39. PRECACHE_WEAPON_REGISTER( weapon_p228 );
  40. CWeaponP228::CWeaponP228()
  41. {
  42. m_flLastFire = gpGlobals->curtime;
  43. }
  44. void CWeaponP228::Spawn( )
  45. {
  46. m_flAccuracy = 0.9;
  47. BaseClass::Spawn();
  48. }
  49. bool CWeaponP228::Deploy( )
  50. {
  51. m_flAccuracy = 0.9;
  52. return BaseClass::Deploy();
  53. }
  54. IMPLEMENT_NETWORKCLASS_ALIASED( WeaponP228, DT_WeaponP228 )
  55. BEGIN_NETWORK_TABLE( CWeaponP228, DT_WeaponP228 )
  56. END_NETWORK_TABLE()
  57. float CWeaponP228::GetInaccuracy() const
  58. {
  59. if ( weapon_accuracy_model.GetInt() == 1 )
  60. {
  61. CCSPlayer *pPlayer = GetPlayerOwner();
  62. if ( !pPlayer )
  63. return 0.0f;
  64. if ( !FBitSet( pPlayer->GetFlags(), FL_ONGROUND ) )
  65. return 1.5f * (1 - m_flAccuracy);
  66. else if (pPlayer->GetAbsVelocity().Length2D() > 5)
  67. return 0.255f * (1 - m_flAccuracy);
  68. else if ( FBitSet( pPlayer->GetFlags(), FL_DUCKING ) )
  69. return 0.075f * (1 - m_flAccuracy);
  70. else
  71. return 0.15f * (1 - m_flAccuracy);
  72. }
  73. else
  74. return BaseClass::GetInaccuracy();
  75. }
  76. void CWeaponP228::PrimaryAttack( void )
  77. {
  78. CCSPlayer *pPlayer = GetPlayerOwner();
  79. if ( !pPlayer )
  80. return;
  81. // Mark the time of this shot and determine the accuracy modifier based on the last shot fired...
  82. m_flAccuracy -= (0.3)*(0.325 - (gpGlobals->curtime - m_flLastFire));
  83. if (m_flAccuracy > 0.9)
  84. m_flAccuracy = 0.9;
  85. else if (m_flAccuracy < 0.6)
  86. m_flAccuracy = 0.6;
  87. m_flLastFire = gpGlobals->curtime;
  88. if (m_iClip1 <= 0)
  89. {
  90. if ( m_bFireOnEmpty )
  91. {
  92. PlayEmptySound();
  93. m_flNextPrimaryAttack = gpGlobals->curtime + 0.1f;
  94. m_bFireOnEmpty = false;
  95. }
  96. return;
  97. }
  98. pPlayer->m_iShotsFired++;
  99. m_iClip1--;
  100. pPlayer->DoMuzzleFlash();
  101. //SetPlayerShieldAnim();
  102. SendWeaponAnim( ACT_VM_PRIMARYATTACK );
  103. // player "shoot" animation
  104. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  105. // Aiming
  106. FX_FireBullets(
  107. pPlayer->entindex(),
  108. pPlayer->Weapon_ShootPosition(),
  109. pPlayer->EyeAngles() + 2.0f * pPlayer->GetPunchAngle(),
  110. GetWeaponID(),
  111. Primary_Mode,
  112. CBaseEntity::GetPredictionRandomSeed() & 255,
  113. GetInaccuracy(),
  114. GetSpread());
  115. m_flNextPrimaryAttack = m_flNextSecondaryAttack = gpGlobals->curtime + GetCSWpnData().m_flCycleTime;
  116. if (!m_iClip1 && pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) <= 0)
  117. {
  118. // HEV suit - indicate out of ammo condition
  119. pPlayer->SetSuitUpdate("!HEV_AMO0", false, 0);
  120. }
  121. SetWeaponIdleTime( gpGlobals->curtime + 2 );
  122. //ResetPlayerShieldAnim();
  123. // update accuracy
  124. m_fAccuracyPenalty += GetCSWpnData().m_fInaccuracyImpulseFire[Primary_Mode];
  125. QAngle angle = pPlayer->GetPunchAngle();
  126. angle.x -= 2;
  127. pPlayer->SetPunchAngle( angle );
  128. }
  129. bool CWeaponP228::Reload()
  130. {
  131. if ( !DefaultPistolReload() )
  132. return false;
  133. m_flAccuracy = 0.9;
  134. return true;
  135. }
  136. void CWeaponP228::WeaponIdle()
  137. {
  138. if (m_flTimeWeaponIdle > gpGlobals->curtime)
  139. return;
  140. // only idle if the slid isn't back
  141. if (m_iClip1 != 0)
  142. {
  143. SetWeaponIdleTime( gpGlobals->curtime + 3.0 ) ;
  144. SendWeaponAnim( ACT_VM_IDLE );
  145. }
  146. }