Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 3 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic stamina system
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#1
Basic stamina system

This is something i've been meaning to create for a while but was busy deciding how i should handle this. Unfortunately (though not entirely), Amnesia uses an outdated version of AngelScript, so i had to rely on classes as a means of imitating namespaces. Likewise unfortunate, in AngelScript functions are taken as "read-only" and the engine doesn't look for member functions for what i was trying to do, so i couldn't create proxies for the AddTimer function by changing the "pointers" of functions to member functions.

Here is the code for the stamina system:

PHP Code: (Select All)
const string STAMINA_TIMER_CALLBACK_NAME "STAMINA_TIMER_CALLBACK";
const 
string STAMINA_PLAYER_SPEED "STAMINA_PLAYER_SPEED";
const 
string STAMINA_PLAYER_LENGTH "STAMINA_PLAYER_LENGTH";
const 
string STAMINA_PLAYER_BREATH_SLOW "STAMINA_PLAYER_BREATH_SLOW";
const 
string STAMINA_PLAYER_BREATH_SNT "react_breath_slow.snt";
const 
string STAMINA_PLAYER_HEAD_BOB "STAMINA_PLAYER_HEAD_BOB";

const 
float STAMINA_TIMER_SPEED_TIME 0.5f;
const 
float STAMINA_PLAYER_HEAD_BOB_SPEED 0.7f;

enum StaminaStages
    
{
        
Full,
        
Low,
        
Exhausted
    
}

class 
StaminaSystem
    
{
        private 
int state;
        private 
uint length;
        private 
bool head_bobbing;
        private 
bool is_running;

        
StaminaSystem()
            {
                
PreloadSound(STAMINA_PLAYER_BREATH_SNT);
                
this.state StaminaStages::Full;
                
this.length 10;
                
this.head_bobbing false;
                
this.is_running false;
            }

        ~
StaminaSystem()
            {
                
this.Stop();
            }

        
void AddDebugMessage(string &in messagebool checkbool to_file)
            {
                ::
AddDebugMessage("Stamina system: " messagecheck);
                if (
to_file) Print("Stamina system: " message);
            }

        
bool IsPlayerRunning() const
            {
                return 
this.is_running;
            }

        
void PlayBreathSound()
            {
                
PlaySoundAtEntity(STAMINA_PLAYER_BREATH_SLOWSTAMINA_PLAYER_BREATH_SNT"Player"0false);
            }

        
void Start()
            {
                
SetPlayerRunSpeedMul(1);
                
SetPlayerMoveSpeedMul(1);
                
AddTimer(STAMINA_PLAYER_SPEEDSTAMINA_TIMER_SPEED_TIMESTAMINA_TIMER_CALLBACK_NAME);
            }

        
void StartPlayerHeadBob()
            {
                if (
this.head_bobbing)
                    
MovePlayerHeadPos(0, -0.350STAMINA_PLAYER_HEAD_BOB_SPEED0.6);
                else
                    
MovePlayerHeadPos(000STAMINA_PLAYER_HEAD_BOB_SPEED0.6);

                
this.head_bobbing = !this.head_bobbing;
                
AddTimer(STAMINA_PLAYER_HEAD_BOB0.75fSTAMINA_TIMER_CALLBACK_NAME);
            }

        
void StartPlayerTiredEffect()
            {
                
this.PlayBreathSound();
                
AddTimer(STAMINA_PLAYER_BREATH_SLOW1.5fSTAMINA_TIMER_CALLBACK_NAME);
            }

        
void Stop()
            {
                
this.StopPlayerTiredEffect();
                
RemoveTimer(STAMINA_PLAYER_SPEED);
                
RemoveTimer(STAMINA_PLAYER_LENGTH);
                
SetPlayerRunSpeedMul(1);
                
SetPlayerMoveSpeedMul(1);
                
SetPlayerJumpDisabled(false);
            }

        
void StopPlayerTiredEffect()
            {
                
this.head_bobbing false;
                
RemoveTimer(STAMINA_PLAYER_BREATH_SLOW);
                
RemoveTimer(STAMINA_PLAYER_HEAD_BOB);
                
MovePlayerHeadPos(0,0,011);
                
// StopSound(STAMINA_PLAYER_BREATH_SLOW, 1); // Crashes the game upon exit.
            
}

        
void SetStaminaLength(uint length)
            {
                if (
length != 0)
                    
this.length length;
                else
                    
this.AddDebugMessage("Length cannot be 0!"falsetrue);
            }

        
void Update(string &in timer_name)
            {
                if (
timer_name == STAMINA_PLAYER_SPEED)
                {
                    
this.is_running = (GetPlayerSpeed() > 3) ? true false;

                    if (
this.is_running && GetTimerTimeLeft(STAMINA_PLAYER_LENGTH) == 0)
                        
AddTimer(STAMINA_PLAYER_LENGTHthis.lengthSTAMINA_TIMER_CALLBACK_NAME);

                    else if(!
this.is_running)
                    {
                        if (
GetTimerTimeLeft(STAMINA_PLAYER_LENGTH) > && this.state == StaminaStages::Full)
                            
RemoveTimer(STAMINA_PLAYER_LENGTH);

                        else if(
this.state == StaminaStages::Low)
                            
AddTimer(STAMINA_PLAYER_LENGTHthis.length/4STAMINA_TIMER_CALLBACK_NAME);

                        else if(
this.state == StaminaStages::Exhausted)
                            
AddTimer(STAMINA_PLAYER_LENGTHthis.length/2STAMINA_TIMER_CALLBACK_NAME);
                    }

                    
AddTimer(timer_nameSTAMINA_TIMER_SPEED_TIMESTAMINA_TIMER_CALLBACK_NAME);
                }

                else if (
timer_name == STAMINA_PLAYER_BREATH_SLOW)
                    
this.StartPlayerTiredEffect();

                else if (
timer_name == STAMINA_PLAYER_HEAD_BOB)
                    
this.StartPlayerHeadBob();

                else if (
timer_name == STAMINA_PLAYER_LENGTH)
                {
                    if (
this.is_running)
                    {
                        if (
this.state == StaminaStages::Full)
                        {
                            
this.state++;
                            
SetPlayerRunSpeedMul(0.3);
                        }

                        else if (
this.state == StaminaStages::Low)
                        {
                            
this.state++;
                            
SetPlayerRunSpeedMul(0);
                            
SetPlayerMoveSpeedMul(0);
                            
SetPlayerJumpDisabled(true);
                            
this.StartPlayerTiredEffect();
                            
this.StartPlayerHeadBob();
                        }
                    }

                    else
                    {
                        
this.StopPlayerTiredEffect();

                        
SetPlayerRunSpeedMul(1);
                        
SetPlayerMoveSpeedMul(1);
                        
SetPlayerJumpDisabled(false);

                        if (
this.state == StaminaStages::Low)
                            
this.state StaminaStages::Full;

                        else if (
this.state == StaminaStages::Exhausted)
                            
this.state StaminaStages::Full;
                    }
                }
            }
    }

StaminaSystem stamina_obj;

void STAMINA_TIMER_CALLBACK(string &in timer_name)
    { 
stamina_obj.Update(timer_name); } 
  • StaminaSystem:Confusedtate is the current "StaminaStage" of the player.
  • StaminaSystem::length is the amount of time in seconds that the player is allowed to run for before entering a more exhausted state.
  • StaminaSystem::is_running holds whether or not the player is understood to be "running." Use StaminaSystem::IsPlayerRunning() to access the variable outside of the class. (Note: StaminaSystem must have been started in order for this to return expected values.)
  • StaminaSystem::head_bobbing is used to store a toggle state to determine which direction the player's head should move in.
The StaminaStages values should be self-explanatory.

When the player reaches the StaminaStages::Exhausted state, the player is prevented from moving for 1/2 the time of StaminaSystem::length; and when the player reaches StaminaStages::Low and is not running, 1/4 the time of StaminaSystem::length is required to regain full stamina.

To activate it for your map, having something as simple as this is enough:

PHP Code: (Select All)
void OnEnter()
{
    
stamina_obj.Start();
}

void OnLeave()
{
    
stamina_obj.Stop();


In action:



Tutorials: From Noob to Pro
(This post was last modified: 07-14-2012, 03:24 AM by Your Computer.)
07-13-2012, 10:22 PM
Website Find
Bas Offline
Junior Member

Posts: 45
Threads: 6
Joined: Feb 2012
Reputation: 0
#2
RE: Basic stamina system

Hilarious if you could gain stamina after enabling an energydrink.
07-14-2012, 11:19 AM
Find
Ironbard Offline
Junior Member

Posts: 3
Threads: 1
Joined: Jul 2012
Reputation: 0
#3
RE: Basic stamina system

Pretty sweet, I am trying to make something similar for a mod me and some friends are working on Big Grin
07-15-2012, 04:51 PM
Find
nemesis567 Offline
Posting Freak

Posts: 874
Threads: 65
Joined: May 2011
Reputation: 10
#4
RE: Basic stamina system

I think I'm missing something. Why do you use the this pointer all the time? Is that necessary?

Today I dreamt the life I could live forever. You only know that when you feel it for you know not what you like until you've experienced it.
07-17-2012, 11:00 PM
Find
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#5
RE: Basic stamina system

(07-17-2012, 11:00 PM)nemesis567 Wrote: I think I'm missing something. Why do you use the this pointer all the time? Is that necessary?

Mostly for readability. It's not required to do that when accessing a member (except when a variable with the same name as another member is declared within the same, local scope).

Tutorials: From Noob to Pro
07-18-2012, 12:17 AM
Website Find
finScript Offline
Member

Posts: 59
Threads: 35
Joined: Jan 2012
Reputation: 0
#6
RE: Basic stamina system

Where should I put this code to make it work?

08-27-2012, 03:23 PM
Find
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#7
RE: Basic stamina system

I took it and works perfectly fine!
Also it is sort of random here for the breathing and walking again!
Awesome feature!
Also i added some FadeImageTrailTo(5,1); for the exausted part, gives it that little extra.
Nice toutch Your computer, + rep for that !Wink
(This post was last modified: 08-23-2013, 05:45 PM by DnALANGE.)
08-23-2013, 05:44 PM
Find
Daemian Offline
Posting Freak

Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation: 49
#8
RE: Basic stamina system

How nice. I actually did something alike in my mod.
It works different, it stops when the player stops running.

I didn't know how to call it, so i put "auto sprint".
But it's basically the guy running faster over time.

PHP Code: (Select All)
Onstart{
AutoSprintInit(); 


PHP Code: (Select All)
// SPRINT {

void AutoSprintInit ()
{    
AddTimer ( EMPTY, 1.0"AutoSprintCheck" ); }

void AutoSprintCheck string &in asTimer )
{
    
float pSpeed GetPlayerSpeed();
    
    if ( 
pSpeed 3.7f ) { AutoSprintNormalize(); }
    if ( 
pSpeed >= 3.8f ) { AutoSprintIncreasepSpeed ); }
    
    
AddTimer ( EMPTY, 1.0"AutoSprintCheck" );
}

void AutoSprintIncrease float pSpeed )
{    
    
float fBonus pSpeed 0.4;
    
float fBonusMax 3;

    if ( 
fBonus fBonusMax ) { fBonus fBonusMax; }
    
SetPlayerRunSpeedMulfBonus );
}

void AutoSprintNormalize ()
SetPlayerRunSpeedMul1.0 ); }

// } SPRINT 

It's useful for large levels.
If you change fBonusMax speed goes to hell.

Hey YC, just a question, why you declare these constants?
STAMINA_PLAYER_HEAD_BOB = "STAMINA_PLAYER_HEAD_BOB";
Just to avoid writing quotation marks? I'm curious.

(This post was last modified: 08-23-2013, 07:45 PM by Daemian.)
08-23-2013, 07:29 PM
Find
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#9
RE: Basic stamina system

(08-23-2013, 07:29 PM)Amn Wrote: Hey YC, just a question, why you declare these constants?
STAMINA_PLAYER_HEAD_BOB = "STAMINA_PLAYER_HEAD_BOB";
Just to avoid writing quotation marks? I'm curious.

I used them in multiple places in my script. So, if i ever choose to edit the string value, i won't have to search for every location i used it in, i can just change the value and it'll be reflected across the script.

Tutorials: From Noob to Pro
08-23-2013, 08:29 PM
Website Find
Kullin Offline
Member

Posts: 218
Threads: 23
Joined: May 2013
Reputation: 3
#10
RE: Basic stamina system

Hey, this might sound noobish, but where should i put everything? :p
09-03-2013, 08:34 AM
Find




Users browsing this thread: 2 Guest(s)