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.

160 lines
4.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: 357 - hand gun
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "npcevent.h"
  9. #include "basehlcombatweapon.h"
  10. #include "basecombatcharacter.h"
  11. #include "ai_basenpc.h"
  12. #include "player.h"
  13. #include "gamerules.h"
  14. #include "in_buttons.h"
  15. #include "soundent.h"
  16. #include "game.h"
  17. #include "vstdlib/random.h"
  18. #include "engine/IEngineSound.h"
  19. #include "te_effect_dispatch.h"
  20. #include "gamestats.h"
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. //-----------------------------------------------------------------------------
  24. // CWeapon357
  25. //-----------------------------------------------------------------------------
  26. class CWeapon357 : public CBaseHLCombatWeapon
  27. {
  28. DECLARE_CLASS( CWeapon357, CBaseHLCombatWeapon );
  29. public:
  30. CWeapon357( void );
  31. void PrimaryAttack( void );
  32. void Operator_HandleAnimEvent( animevent_t *pEvent, CBaseCombatCharacter *pOperator );
  33. float WeaponAutoAimScale() { return 0.6f; }
  34. DECLARE_SERVERCLASS();
  35. DECLARE_DATADESC();
  36. };
  37. LINK_ENTITY_TO_CLASS( weapon_357, CWeapon357 );
  38. PRECACHE_WEAPON_REGISTER( weapon_357 );
  39. IMPLEMENT_SERVERCLASS_ST( CWeapon357, DT_Weapon357 )
  40. END_SEND_TABLE()
  41. BEGIN_DATADESC( CWeapon357 )
  42. END_DATADESC()
  43. //-----------------------------------------------------------------------------
  44. // Purpose: Constructor
  45. //-----------------------------------------------------------------------------
  46. CWeapon357::CWeapon357( void )
  47. {
  48. m_bReloadsSingly = false;
  49. m_bFiresUnderwater = false;
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Purpose:
  53. //-----------------------------------------------------------------------------
  54. void CWeapon357::Operator_HandleAnimEvent( animevent_t *pEvent, CBaseCombatCharacter *pOperator )
  55. {
  56. CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
  57. switch( pEvent->event )
  58. {
  59. case EVENT_WEAPON_RELOAD:
  60. {
  61. CEffectData data;
  62. // Emit six spent shells
  63. for ( int i = 0; i < 6; i++ )
  64. {
  65. data.m_vOrigin = pOwner->WorldSpaceCenter() + RandomVector( -4, 4 );
  66. data.m_vAngles = QAngle( 90, random->RandomInt( 0, 360 ), 0 );
  67. data.m_nEntIndex = entindex();
  68. DispatchEffect( "ShellEject", data );
  69. }
  70. break;
  71. }
  72. }
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose:
  76. //-----------------------------------------------------------------------------
  77. void CWeapon357::PrimaryAttack( void )
  78. {
  79. // Only the player fires this way so we can cast
  80. CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );
  81. if ( !pPlayer )
  82. {
  83. return;
  84. }
  85. if ( m_iClip1 <= 0 )
  86. {
  87. if ( !m_bFireOnEmpty )
  88. {
  89. Reload();
  90. }
  91. else
  92. {
  93. WeaponSound( EMPTY );
  94. m_flNextPrimaryAttack = 0.15;
  95. }
  96. return;
  97. }
  98. m_iPrimaryAttacks++;
  99. gamestats->Event_WeaponFired( pPlayer, true, GetClassname() );
  100. WeaponSound( SINGLE );
  101. pPlayer->DoMuzzleFlash();
  102. SendWeaponAnim( ACT_VM_PRIMARYATTACK );
  103. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  104. m_flNextPrimaryAttack = gpGlobals->curtime + 0.75;
  105. m_flNextSecondaryAttack = gpGlobals->curtime + 0.75;
  106. m_iClip1--;
  107. Vector vecSrc = pPlayer->Weapon_ShootPosition();
  108. Vector vecAiming = pPlayer->GetAutoaimVector( AUTOAIM_SCALE_DEFAULT );
  109. pPlayer->FireBullets( 1, vecSrc, vecAiming, vec3_origin, MAX_TRACE_LENGTH, m_iPrimaryAmmoType, 0 );
  110. pPlayer->SetMuzzleFlashTime( gpGlobals->curtime + 0.5 );
  111. //Disorient the player
  112. QAngle angles = pPlayer->GetLocalAngles();
  113. angles.x += random->RandomInt( -1, 1 );
  114. angles.y += random->RandomInt( -1, 1 );
  115. angles.z = 0;
  116. pPlayer->SnapEyeAngles( angles );
  117. pPlayer->ViewPunch( QAngle( -8, random->RandomFloat( -2, 2 ), 0 ) );
  118. CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), 600, 0.2, GetOwner() );
  119. if ( !m_iClip1 && pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) <= 0 )
  120. {
  121. // HEV suit - indicate out of ammo condition
  122. pPlayer->SetSuitUpdate( "!HEV_AMO0", FALSE, 0 );
  123. }
  124. }