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.

205 lines
4.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Glock - hand gun
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "npcevent.h"
  9. #include "hl1mp_basecombatweapon_shared.h"
  10. //#include "basecombatcharacter.h"
  11. //#include "AI_BaseNPC.h"
  12. #ifdef CLIENT_DLL
  13. #include "c_baseplayer.h"
  14. #else
  15. #include "player.h"
  16. #endif
  17. #include "gamerules.h"
  18. #include "in_buttons.h"
  19. #ifdef CLIENT_DLL
  20. #else
  21. #include "soundent.h"
  22. #include "game.h"
  23. #endif
  24. #include "vstdlib/random.h"
  25. #include "engine/IEngineSound.h"
  26. #ifdef CLIENT_DLL
  27. #define CWeaponGlock C_WeaponGlock
  28. #endif
  29. class CWeaponGlock : public CBaseHL1MPCombatWeapon
  30. {
  31. DECLARE_CLASS( CWeaponGlock, CBaseHL1MPCombatWeapon );
  32. DECLARE_NETWORKCLASS();
  33. DECLARE_PREDICTABLE();
  34. public:
  35. CWeaponGlock(void);
  36. void Precache( void );
  37. void PrimaryAttack( void );
  38. void SecondaryAttack( void );
  39. bool Reload( void );
  40. void WeaponIdle( void );
  41. void DryFire( void );
  42. private:
  43. void GlockFire( float flSpread , float flCycleTime, bool fUseAutoAim );
  44. };
  45. IMPLEMENT_NETWORKCLASS_ALIASED( WeaponGlock, DT_WeaponGlock );
  46. BEGIN_NETWORK_TABLE( CWeaponGlock, DT_WeaponGlock )
  47. END_NETWORK_TABLE()
  48. LINK_ENTITY_TO_CLASS( weapon_glock, CWeaponGlock );
  49. BEGIN_PREDICTION_DATA( CWeaponGlock )
  50. END_PREDICTION_DATA()
  51. PRECACHE_WEAPON_REGISTER( weapon_glock );
  52. CWeaponGlock::CWeaponGlock( void )
  53. {
  54. m_bReloadsSingly = false;
  55. m_bFiresUnderwater = true;
  56. }
  57. void CWeaponGlock::Precache( void )
  58. {
  59. BaseClass::Precache();
  60. }
  61. void CWeaponGlock::DryFire( void )
  62. {
  63. WeaponSound( EMPTY );
  64. SendWeaponAnim( ACT_VM_DRYFIRE );
  65. m_flNextPrimaryAttack = gpGlobals->curtime + 0.2;
  66. }
  67. void CWeaponGlock::PrimaryAttack( void )
  68. {
  69. GlockFire( 0.01, 0.3, TRUE );
  70. }
  71. void CWeaponGlock::SecondaryAttack( void )
  72. {
  73. GlockFire( 0.1, 0.2, FALSE );
  74. }
  75. void CWeaponGlock::GlockFire( float flSpread , float flCycleTime, bool fUseAutoAim )
  76. {
  77. // Only the player fires this way so we can cast
  78. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  79. if ( !pPlayer )
  80. {
  81. return;
  82. }
  83. if ( m_iClip1 <= 0 )
  84. {
  85. if ( !m_bFireOnEmpty )
  86. {
  87. Reload();
  88. }
  89. else
  90. {
  91. DryFire();
  92. }
  93. return;
  94. }
  95. WeaponSound( SINGLE );
  96. pPlayer->DoMuzzleFlash();
  97. m_iClip1--;
  98. if ( m_iClip1 == 0 )
  99. SendWeaponAnim( ACT_GLOCK_SHOOTEMPTY );
  100. else
  101. SendWeaponAnim( ACT_VM_PRIMARYATTACK );
  102. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  103. m_flNextPrimaryAttack = gpGlobals->curtime + flCycleTime;
  104. m_flNextSecondaryAttack = gpGlobals->curtime + flCycleTime;
  105. Vector vecSrc = pPlayer->Weapon_ShootPosition();
  106. Vector vecAiming;
  107. if ( fUseAutoAim )
  108. {
  109. vecAiming = pPlayer->GetAutoaimVector( AUTOAIM_10DEGREES );
  110. }
  111. else
  112. {
  113. vecAiming = pPlayer->GetAutoaimVector( 0 );
  114. }
  115. // pPlayer->FireBullets( 1, vecSrc, vecAiming, Vector( flSpread, flSpread, flSpread ), MAX_TRACE_LENGTH, m_iPrimaryAmmoType, 0 );
  116. FireBulletsInfo_t info( 1, vecSrc, vecAiming, Vector( flSpread, flSpread, flSpread ), MAX_TRACE_LENGTH, m_iPrimaryAmmoType );
  117. info.m_pAttacker = pPlayer;
  118. pPlayer->FireBullets( info );
  119. EjectShell( pPlayer, 0 );
  120. pPlayer->ViewPunch( QAngle( -2, 0, 0 ) );
  121. #if !defined(CLIENT_DLL)
  122. pPlayer->SetMuzzleFlashTime( gpGlobals->curtime + 0.5 );
  123. CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), 400, 0.2 );
  124. #endif
  125. if ( !m_iClip1 && pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) <= 0 )
  126. {
  127. // HEV suit - indicate out of ammo condition
  128. pPlayer->SetSuitUpdate("!HEV_AMO0", FALSE, 0);
  129. }
  130. SetWeaponIdleTime( gpGlobals->curtime + random->RandomFloat( 10, 15 ) );
  131. }
  132. bool CWeaponGlock::Reload( void )
  133. {
  134. bool iResult;
  135. if ( m_iClip1 == 0 )
  136. iResult = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_GLOCK_SHOOT_RELOAD );
  137. else
  138. iResult = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_VM_RELOAD );
  139. return iResult;
  140. }
  141. void CWeaponGlock::WeaponIdle( void )
  142. {
  143. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  144. if ( pPlayer )
  145. {
  146. pPlayer->GetAutoaimVector( AUTOAIM_10DEGREES );
  147. }
  148. // only idle if the slid isn't back
  149. if ( m_iClip1 != 0 )
  150. {
  151. BaseClass::WeaponIdle();
  152. }
  153. }