|
|
|
 |
wanggogo  |
Trainee | Member since: 27.10.2008, 12:02 | Posts: 153 | Location: CHINA |
Likes: 0 |
| |
|
Post Topic: Energy system help Posted 26.04.2012, 05:36 |
How can I implement Energy system like crysis1 or crysis2.The energy value will be reduced when I jump or run.I thought it might need C++ ,so who can give me some ideas about this. thx in advance!
|
|
|
|
|
|
|
|
 |
taylorsnead  |
Beginner | Member since: 18.08.2011, 04:49 | Posts: 81 | Location: New Orleans |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 26.04.2012, 06:20 |
The best way would probably be to declare an energy variable in Player.h, then go to PlayerInput.cpp, and decrease that variable by however much in the OnActionJump and OnActionSprint functions. Then when you land or stop sprinting, tell it to start regen (with boolean, then regen in Update function maybe?).
And at that, Albi cried a single tear which became a jellybean all colors of the rainbow! Suddenly, Albi wasn't racist anymore. So they sat in the cave; and ate bubblegum pie, YUM! Albi the racist -- well, not anymore -- dragon!
|
|
|
|
|
|
|
 |
wanggogo  |
Trainee | Member since: 27.10.2008, 12:02 | Posts: 153 | Location: CHINA |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 26.04.2012, 07:08 |
Thanks taylorsneed, I will check out Player.h and PlayerInput.cpp for more details. Any help will be grateful.
|
|
|
|
|
|
|
 |
wanggogo  |
Trainee | Member since: 27.10.2008, 12:02 | Posts: 153 | Location: CHINA |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 01.05.2012, 12:23 |
Can you tell me which update function should be used to restore energy.
|
|
|
|
|
|
|
 |
taylorsnead  |
Beginner | Member since: 18.08.2011, 04:49 | Posts: 81 | Location: New Orleans |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 02.05.2012, 01:17 |
In Player.cpp, simply put something like this inside the CPlayer::Update function: (Don't copy exactly.. just a concept) Code: if (Stamina < MaxStamina && !IsSprinting()) { UpdateStamina(); }
then the UpdateStamina function for cleanliness: Code: void CPlayer::UpdateStamina () { Stamina += StaminaRegen; // Any other stamina regeneration stuff here... }
And at that, Albi cried a single tear which became a jellybean all colors of the rainbow! Suddenly, Albi wasn't racist anymore. So they sat in the cave; and ate bubblegum pie, YUM! Albi the racist -- well, not anymore -- dragon!
|
|
|
|
|
|
|
 |
wanggogo  |
Trainee | Member since: 27.10.2008, 12:02 | Posts: 153 | Location: CHINA |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 02.05.2012, 05:02 |
taylorsneed, You're a great guy!
|
|
|
|
|
|
|
thefarcry  |
Beginner | Member since: 28.12.2011, 12:32 | Posts: 78 |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 03.05.2012, 23:03 |
Shouldn't it be a for or while statement? So, Code: while( Stamina <= 20 && isSprinting() ) //do your commands here. Correct me if I'm wrong. [/code]
|
|
|
|
|
|
|
 |
Cry-Ruan  |
Crytek Staff Member | Member since: 11.07.2008, 20:25 | Posts: 11625 | Location: Frankfurt am Main |
Likes: 10 |
| |
|
Post Topic: Re: Energy system help Posted 03.05.2012, 23:07 |
A while statement would block the entire game loop whilst executing (and there, unless you reduced stamina with each iteration, probably crash the engine). This is not a sensible thing to do.
*face specialist, anti-flowgraph campaigner.
|
|
|
|
|
|
|
thefarcry  |
Beginner | Member since: 28.12.2011, 12:32 | Posts: 78 |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 04.05.2012, 00:11 |
Quote by Cry-Ruan: A while statement would block the entire game loop whilst executing (and there, unless you reduced stamina with each iteration, probably crash the engine). This is not a sensible thing to do. Or perhaps a for statement to execute the block of code multiple times?
|
|
|
|
|
|
|
 |
Cry-Ruan  |
Crytek Staff Member | Member since: 11.07.2008, 20:25 | Posts: 11625 | Location: Frankfurt am Main |
Likes: 10 |
| |
|
Post Topic: Re: Energy system help Posted 04.05.2012, 00:19 |
Quote by thefarcry: Or perhaps a for statement to execute the block of code multiple times? Same principle, it iterates. A blocking iteration in an update loop will kill the engine. Common practice in the world of game dev is to do things per frame, but make them framerate independent by multiplying your values by the time delta.
*face specialist, anti-flowgraph campaigner.
|
|
|
|
|
|
|
 |
taylorsnead  |
Beginner | Member since: 18.08.2011, 04:49 | Posts: 81 | Location: New Orleans |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 04.05.2012, 05:28 |
Which is exactly what I did (after defining stamina variables in Player.h, and making StopSprint and StartSprint public in PlayerInput.h, and calling this function in CPlayer::Update): Code: /* Updates stamina based on the regen rate */ void CPlayer::UpdateStamina (float frameTime) { CPlayerInput *playerInput = (CPlayerInput*) GetPlayerInput(); if (IsSprinting()) { m_stats.Stamina -= m_stats.SprintStamina*frameTime; if(m_stats.Stamina <= 0) { playerInput->StopSprint(); } }
else if (m_stats.Stamina < m_stats.MaxStamina) { m_stats.Stamina += m_stats.StaminaRegen*frameTime; if (m_stats.Stamina > m_stats.MaxStamina) m_stats.Stamina = m_stats.MaxStamina; } }
Fairly simple system. Any noticeable mistakes? Should I have defined my variables elsewhere, not in playerstats?
And at that, Albi cried a single tear which became a jellybean all colors of the rainbow! Suddenly, Albi wasn't racist anymore. So they sat in the cave; and ate bubblegum pie, YUM! Albi the racist -- well, not anymore -- dragon!
|
|
|
|
|
|
|
 |
wanggogo  |
Trainee | Member since: 27.10.2008, 12:02 | Posts: 153 | Location: CHINA |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 17.05.2012, 02:57 |
Hi all, Sorry for bother you guys again because I was encountered a bug. I put some codes to recover energy in Player.cpp for my own energy system: Code: UpdateSounds(ctx.fFrameTime); //recover player's energy if(!(m_actions&ACTION_SPRINT) && (m_energy < 200.0f) && !(m_energy >= 200.0f)) { m_energy = m_energy + 1.0f; CryLogAlways("Value: %f",m_energy ); } Everything is ok when I was in the game world single alone but the energy will regen automatic when I drags some AI Grunt into the map. Note: I put the code in CPlayer::Update(SEntityUpdateContext& ctx, int updateSlot)
|
|
|
|
|
|
|
 |
taylorsnead  |
Beginner | Member since: 18.08.2011, 04:49 | Posts: 81 | Location: New Orleans |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 17.05.2012, 03:11 |
As was said in previous posts, you must multiply the addition by frameTime. so: Code: m_energy += 1.5*frameTime; or Code: m_energy = m_energy + 1.5*frameTime This will add 1.5 to the energy each second, instead of each frame.
And at that, Albi cried a single tear which became a jellybean all colors of the rainbow! Suddenly, Albi wasn't racist anymore. So they sat in the cave; and ate bubblegum pie, YUM! Albi the racist -- well, not anymore -- dragon!
|
|
|
|
|
|
|
 |
wanggogo  |
Trainee | Member since: 27.10.2008, 12:02 | Posts: 153 | Location: CHINA |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 17.05.2012, 03:19 |
The situation look like this:
My idea is energy recover 1.0f per frame when I doesn't sprint and reduce 10.0f during I was sprinting. I doubt the AI also call the CPlayer::Update( ) per frame  . So , how can I fix that? Who can give me a suggestion? Thx
Last edited by wanggogo on 17.05.2012, 03:29, edited 1 time in total.
|
|
|
|
|
|
|
 |
wanggogo  |
Trainee | Member since: 27.10.2008, 12:02 | Posts: 153 | Location: CHINA |
Likes: 0 |
| |
|
Post Topic: Re: Energy system help Posted 17.05.2012, 03:22 |
Ohhh,you respond so quickly ,taylorsnead !!! And did you see the image I just post. I am a chinese so I speak English very slow .
|
|
|
|
|