Jump to content
  • Advertisement
Sign in to follow this  
jblevins1991

stumped

This topic is 2500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im making a simple game to test my abilities. Lets use the following code as an example:

class player
{
int hp;
};

this is my theoretical player class and lets make int hp equal to 80. i want to know how i make it to where 80 is the max hp can be and will not be able to go over. like for example if i use a potion and potion in my game revives 60 hp and i already have 50 how do i make it to where it tops off at 80 and doesn't rise to 110?

Share this post


Link to post
Share on other sites
Advertisement

class Player
{
int hp; // set it to some value in the constructor (maybe the same as maxHp at first)
int maxHp; // set it to some value in the constructor (80 in your example)

public:
void applyHp(int amount)
{
hp += amount;
if (hp < 0) hp = 0; // if amount is negative (i.e. damage), don't let an invalid amount of hp be assigned
if (hp > maxHp) hp = maxHp; // if amount is positive (i.e. drank a potion), clamp the hp to the max
}
};


Maybe not the best function name or the best way to abstract applying damage/health bonuses, but that's the idea behind how you clamp a value.

Share this post


Link to post
Share on other sites


class Player
{
int hp; // set it to some value in the constructor (maybe the same as maxHp at first)
int maxHp; // set it to some value in the constructor (80 in your example)

public:
void applyHp(int amount)
{
hp += amount;
if (hp < 0) hp = 0; // if amount is negative (i.e. damage), don't let an invalid amount of hp be assigned
if (hp > maxHp) hp = maxHp; // if amount is positive (i.e. drank a potion), clamp the hp to the max
}
};


Maybe not the best function name or the best way to abstract applying damage/health bonuses, but that's the idea behind how you clamp a value.

Thank you very much sir.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>

namespace terribad {
template<class T, T MinValue, T MaxValue>
struct bounding_trait_check_always {
static T set(T const& value) {
if(value < MinValue)
return MinValue;
if(value > MaxValue)
return MaxValue;
return value;
}
static T get(T const& value) {
if(value < MinValue)
return MinValue;
if(value > MaxValue)
return MaxValue;
return value;
}
};
template<class T, T MinValue, T MaxValue>
struct bounding_trait_check_set {
static T set(T const& value) {
if(value < MinValue)
return MinValue;
if(value > MaxValue)
return MaxValue;
return value;
}
static T get(T const& value) {
return value;
}
};
template<class T, T MinValue, T MaxValue>
struct bounding_trait_check_get {
static T set(T const& value) {
return value;
}
static T get(T const& value) {
if(value < MinValue)
return MinValue;
if(value > MaxValue)
return MaxValue;
return value;
}
};
template<class T, T MinValue, T MaxValue, class BoundingTrait = bounding_trait_check_set<T, MinValue, MaxValue> >
struct bounded {
bounded(T const& newVal = T()) : value(BoundingTrait::set(newVal)) { }
operator T () const {
return BoundingTrait::get(value);
}
void operator=(T const& newValue) {
value = BoundingTrait::set(newValue);
}
void operator+=(T const& newValue) {
value = BoundingTrait::set(value + newValue);
}
void operator-=(T const& newValue) {
value = BoundingTrait::set(value - newValue);
}
private:
T value;
};
}

class Player {
terribad::bounded<int, 0, 80> hp;
public:
void applyHp(int amount) {
hp += amount;
}
int getCurrentHp() {
return hp;
}
};

int main() {
Player p = Player();
p.applyHp(1000);
std::cout<<p.getCurrentHp()<<std::endl;
}


Because somebody had to post it.

Share this post


Link to post
Share on other sites


#include <iostream>
#include <string>

namespace terribad {
template<class T, T MinValue, T MaxValue>
struct bounding_trait_check_always {
static T set(T const& value) {
if(value < MinValue)
return MinValue;
if(value > MaxValue)
return MaxValue;
return value;
}
static T get(T const& value) {
if(value < MinValue)
return MinValue;
if(value > MaxValue)
return MaxValue;
return value;
}
};
template<class T, T MinValue, T MaxValue>
struct bounding_trait_check_set {
static T set(T const& value) {
if(value < MinValue)
return MinValue;
if(value > MaxValue)
return MaxValue;
return value;
}
static T get(T const& value) {
return value;
}
};
template<class T, T MinValue, T MaxValue>
struct bounding_trait_check_get {
static T set(T const& value) {
return value;
}
static T get(T const& value) {
if(value < MinValue)
return MinValue;
if(value > MaxValue)
return MaxValue;
return value;
}
};
template<class T, T MinValue, T MaxValue, class BoundingTrait = bounding_trait_check_set<T, MinValue, MaxValue> >
struct bounded {
bounded(T const& newVal = T()) : value(BoundingTrait::set(newVal)) { }
operator T () const {
return BoundingTrait::get(value);
}
void operator=(T const& newValue) {
value = BoundingTrait::set(newValue);
}
void operator+=(T const& newValue) {
value = BoundingTrait::set(value + newValue);
}
void operator-=(T const& newValue) {
value = BoundingTrait::set(value - newValue);
}
private:
T value;
};
}

class Player {
terribad::bounded<int, 0, 80> hp;
public:
void applyHp(int amount) {
hp += amount;
}
int getCurrentHp() {
return hp;
}
};

int main() {
Player p = Player();
p.applyHp(1000);
std::cout<<p.getCurrentHp()<<std::endl;
}


Because somebody had to post it.


I realize your post was tongue-in-cheek (namespace terribad, and all), but this guy is an obvious beginner, and there's a good chance he's now going to try and understand what you've done and add it to his game. We all know how smart you are, but this is a bit over-the-top IMO.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!