Leaning in Singleplayer Mod

Grab your favourite IDE and tinker with the innards of game engines

Leaning in Singleplayer Mod

Postby ileekor on Sun Feb 13, 2011 10:55 pm

Hi, I'm trying to create an mod similar to Amnesia.
Right now, I'm trying to apply leaning functionality to my mod.

This is the code I've gotten so far:

gamemovement.cpp

Code: Select all
void CGameMovement::LeanRight( void )
{
   CBasePlayer *pPlayer = player;
   Vector right, left, curOffset, newOffset;
   curOffset = player->GetViewOffset();
   AngleVectors(player->EyeAngles(), NULL, &right, NULL);

   float length = curOffset.Length2D();
   bool bInAir = ( player->GetGroundEntity() == NULL );
   
   trace_t tr;
   Vector vecStart = player->EyePosition();
   Vector vecEnd = player->EyePosition() + (right * 40);

   UTIL_TraceLine( vecStart, vecEnd, MASK_SOLID, pPlayer, COLLISION_GROUP_NONE, &tr );

   if( IsDead() || bInAir )
      return;   

   if( (mv->m_nButtons & IN_FORWARD) || (mv->m_nButtons & IN_BACK)
      || (mv->m_nButtons & IN_MOVELEFT) || (mv->m_nButtons & IN_MOVERIGHT) )
   {
      return;
   }

   if (tr.fraction == 1.0)      // trace hit nothing... player can safely lean to maximum distance
   {

      if ( length < 20.0f )      // peek transition
      {
         right *= 1.5;
         newOffset = curOffset;
         newOffset += right;
      }
      if ( length > 20.0f )   // full peeking
      {
         right *= 23;
         newOffset = curOffset;
         newOffset.x = clamp(right.x, -40,40 );
         newOffset.y = clamp(right.y, -40,40 );
         player->SetViewOffset( newOffset );
         #if defined( CLIENT_DLL )
         {}
         #else
         //player->LeanControl(false);
         #endif

      }
      player->SetViewOffset( newOffset );
      
   }

   else         // trace hit something along the way
   {
      /*right *= 23;
      left = -right;
      newOffset = curOffset;
      newOffset.x = clamp(left.x, ( -20 * tr.fraction ),( 20 * tr.fraction ) );
      newOffset.y = clamp(left.y, ( -20 * tr.fraction ),( 20 * tr.fraction ) ); */
      return;
   }
}


Its a slightly modified version of a code I've found on the Valve forums. It doesn't enter lean mode if there's something in traceline.

Code: Select all
void CGameMovement::Resetview( void )
{
if ( player->m_Local.m_bDucking && ( player->GetFlags() & FL_DUCKING ) )
{
player->SetViewOffset( VEC_DUCK_VIEW );
}
else
{
player->SetViewOffset( VEC_VIEW );
}
#if defined( CLIENT_DLL )
{

}
#else
//player->LeanControl(true);
#endif
}


and in PlayerMove function

Code: Select all
//Ben - new lean key stuff
   int buttonsChanged = ( mv->m_nOldButtons ^ mv->m_nButtons ); // These buttons have changed this frame
   int buttonsReleased = buttonsChanged & mv->m_nOldButtons; // The changed ones which were previously down are "released"

   if (mv->m_nButtons & IN_LEAN_R)
   {
   LeanRight();
   }
   if ( ( buttonsReleased & IN_LEAN_R ) )
   {
   Resetview();
   }
   if ( mv->m_nButtons & IN_LEAN_L)
   {
   LeanLeft();
   }
   if ( ( buttonsReleased & IN_LEAN_L ) )
   {
   Resetview();
   }
   //



what I have left is to smooth the transition from lean to normal, and disable keyboard & mouse input in lean mode.

If you see above, I've tried to disable input by adding FL_ATCONTROLS flag through a custom function LeanControl() to implement the input disable, but I've also found out that FL_ATCONTROLS disable the current key I'm pressing, leaving me straight to Resetview() (in PlayerMove function)

And about the transition from lean to normal, can anyone explain to me how the lean code works? I thought it would be pretty simple because its the opposite thing of normal -> lean... I've searched for hours on this problem, but still, no nothing.

I've just started learning source programming, so it would be great if someone could explain.
ileekor
Just Joined
Just Joined
 
Joined: Sun Feb 13, 2011 10:52 pm

Return to Programming

Who is online

Users browsing this forum: No registered users