Welcome, Guest! Login | Register

Dash Key Part2 [Print this Article]
Posted by: X-0ut
Date posted: Jun 25 2003
User Rating: 5 out of 5.0
Number of views: 4912
Number of comments: 3
Description: Stamina controlled sprint
You will need to have completed the first part of this tutorial which can be found in the shared section here Part1
This addition to the dash/sprint code requires only clientside modification, so open up your client workspace and hud.h - at the bottom of the CHud class after this line float GetSensitivity();
place this variable declaration:
 CODE  

    int g_iStamina;//stores our stamina

We are going to store our stamina in that variable.
Next open up input.cpp and find the previous code we alltered:
 CODE  

void IN_DashDown(void) {KeyDown(&in_dash);}
void IN_DashUp(void) {KeyUp(&in_dash);}

Replace it with this slighty modified version:
 CODE  

///Stamina///
void IN_DashDown(void)
{
    if(gHUD.g_iStamina>0)
        KeyDown(&in_dash);
}
void IN_DashUp(void)
{
    KeyUp(&in_dash);
}

That simply stops the key from being pressed if we are out of stamina.
Now scroll down input.cpp until you find the CL_CreateMove function, after this line static vec3_t oldangles; add this little chunk of code:
 CODE  

    //alter stamina amount
    if(in_dash.state & 1)
        gHUD.g_iStamina -= gHUD.m_flTimeDelta * 300;//key is pressed - decrement stamina
    else
        gHUD.g_iStamina += gHUD.m_flTimeDelta * 100;//key is not pressed - increment stamina

    if(gHUD.g_iStamina<0)
    {
        gHUD.g_iStamina =0;//cap stamina at zero so we dont have negative amounts
        in_alt1.state &= ~1;//release the dash button so the client cant keep sprinting
    }
    if(gHUD.g_iStamina>1000) //cap stamina at 1000
        gHUD.g_iStamina = 1000;

Thats self explanatry im sure, but basically whats happening is if the dash key is pressed the stamina drains, if it reaches zero the key is released forcing the client to stop sprinting, if the key is not pressed the stamina slowly regains.
Thats all for input.cpp, next we need somewhere to display our stamina bar - we'll use health.cpp, so open it up and find the Draw function.
Near the bottom of the function you will find this line:
 CODE  

        FillRGBA(x, y, iWidth , iHeight, 255, 160, 0, a);

Place this directly after it:
 CODE  

        //draw stamina
        FillRGBA(XRES(10), y - gHUD.m_iFontHeight, iWidth+(gHUD.g_iStamina/10), iHeight, 0, 255, 0, 255);

That simply draws a green rectangle above the health/armor to the width of our current status amount.

Well thats it, a very simple tutorial - but it makes the dash/sprint effect more complete.

Rate This Article
This article is currently rated: 5 out of 5.0 (2 Votes)

You have to register to rate this article.
User Comments Showing comments 1-3

Posted By: Bryan on Mar 12 2004 at 17:19:25
xout where it says
 CODE  

    if(gHUD.g_iStamina<0)
    {
        gHUD.g_iStamina =0;//cap stamina at zero so we dont have negative amounts
        in_alt1.state &= ~1;//release the dash button so the client cant keep sprinting
    }

i think you mean
 CODE  

    if(gHUD.g_iStamina<0)
    {
        gHUD.g_iStamina =0;//cap stamina at zero so we dont have negative amounts
        in_dash.state &= ~1;//release the dash button so the client cant keep sprinting
    }


otherwise, in_alt1.state come up as a unidentified modifier since its commented out
also one thing i see is there is nothing to prevent stamina from decreasing if the person is standing still, lemme see if i can fix thatEdited by Bryan on Mar 15 2004, 04:31:49

Posted By: XxAvalanchexX on Mar 09 2005 at 08:04:51
To make it so they don't lose stamina unless they are moving, change the following:

 CODE  
if(in_dash.state & 1) {


To this:

 CODE  
if((in_dash.state & 1) && (in_forward.state & 1)) {


This will make sure that they are holding down the dash key AND the forward key. You will also want to go back and edit some of the code in pm_shared.c (the areas that make sure they are on the ground) so that they can't dash backwards (pmove->cmd.buttons & IN_BACK), or if they do it goes slower than dashing forwards.

Good luck!Edited by XxAvalanchexX on Mar 09 2005, 08:05:59

Posted By: hagai on Jun 15 2006 at 15:08:30
That stamina code is not secured. Since its done on client side only, it would mean that its very easy to cheat to make it so you have unlimited stamina without the server knowing about it. Its better to store the player's stamina on the server and then retrieve it from the client.Edited by hagai on Jun 15 2006, 15:08:43


You must register to post a comment. If you have already registered, you must login.

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 05 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005


Latest Comments
knock knock
General | News
By: omega | Dec 22 2016
 
knock knock
General | News
By: MIFUNE | Oct 10 2015
 
New HL HUD Message System
Half-Life | Coding | Shared Tutorials
By: chbrules | Dec 31 2011
 
knock knock
General | News
By: Whistler | Nov 05 2011
 
Particle Engine tutorial part 4
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 18 2010
 
Particle Engine tutorial part 2
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Particle Engine tutorial part 3
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Game Movement Series #2: Analog Jumping and Floating
Half-Life 2 | Coding | Shared Tutorials
By: mars3554 | Oct 26 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Deadpool | Aug 02 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Persuter | Aug 02 2009
 

Site Info
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (7 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!