Sunday, December 8, 2013

Disorientation player when bullets hit

For greater realism disorientation occurs player (similar to how it happens when fired from a "weapon 357"), when it misses an enemy bullet. If the health of at least 10 points, disorientation increases.

For this tutorial, we will be modifying the base player class. Open up server/player.cpp and edit the DamageEffect function. This function do some kind of damage effect for the type of damage. In this case we're adding the extra code for shoot damage (DMG_BULLET):

Copy Source | Copy HTML
  1. //------------------------------------------------------------------------------
  2. // Purpose : Do some kind of damage effect for the type of damage
  3. // Input   :
  4. // Output  :
  5. //------------------------------------------------------------------------------
  6. void CBasePlayer::DamageEffect(float flDamage, int fDamageType)
  7. {
  8.     if (fDamageType & DMG_CRUSH)
  9.     {
  10.         //Red damage indicator
  11.         color32 red = {128, 0, 0,128};
  12.         UTIL_ScreenFade( this, red, 1.0f,  0.1f, FFADE_IN );
  13.     }
  14.     else if (fDamageType & DMG_DROWN)
  15.     {
  16.         //Red damage indicator
  17.         color32 blue = { 0, 0,128,128};
  18.         UTIL_ScreenFade( this, blue, 1.0f,  0.1f, FFADE_IN );
  19.     }
  20.     else if (fDamageType & DMG_SLASH)
  21.     {
  22.         // If slash damage shoot some blood
  23.         SpawnBlood(EyePosition(), g_vecAttackDir, BloodColor(), flDamage);
  24.     }
  25.     else if (fDamageType & DMG_PLASMA)
  26.     {
  27.         // Blue screen fade
  28.         color32 blue = { 0, 0,255,100};
  29.         UTIL_ScreenFade( this, blue,  0.2,  0.4, FFADE_MODULATE );
  30.         // Very small screen shake
  31.         // Both -0.1 and 0.1 map to 0 when converted to integer, so all of these RandomInt
  32.         // calls are just expensive ways of returning zero. This code has always been this
  33.         // way and has never had any value. clang complains about the conversion from a
  34.         // literal floating-point number to an integer.
  35.         //ViewPunch(QAngle(random->RandomInt(-0.1,0.1), random->RandomInt(-0.1,0.1), random->RandomInt(-0.1,0.1)));
  36.         // Burn sound 
  37.         EmitSound( "Player.PlasmaDamage" );
  38.     }
  39.     else if (fDamageType & DMG_SONIC)
  40.     {
  41.         // Sonic damage sound 
  42.         EmitSound( "Player.SonicDamage" );
  43.     }
  44.     else if ( fDamageType & DMG_BULLET )
  45.     {
  46.         //Disorient the player
  47.         QAngle angles = GetLocalAngles();
  48.         angles.x += random->RandomInt( -1, 1 );
  49.         angles.y += random->RandomInt( -1, 1 );
  50.         angles.z =  0;
  51.         SnapEyeAngles( angles );
  52.         ViewPunch( QAngle( -8, random->RandomFloat( -2, 2 ),  0 ) );

  53.         EmitSound( "Flesh.BulletImpact" );
  54.     }
  55. }