diff --git a/engine/src/resource/resource.cpp b/engine/src/resource/resource.cpp index 119ed5863..e1bd227b1 100644 --- a/engine/src/resource/resource.cpp +++ b/engine/src/resource/resource.cpp @@ -24,6 +24,10 @@ #include #include +#include + +#include +#include #include "random_utils.h" @@ -38,9 +42,49 @@ Resource::Resource(const T &value, const T &min_value, const T &max_value): adjusted_max_value_(max_value), no_max_(max_value==-1) {} +template<> +Resource::Resource(const std::string input, const double modifier, const double minimum_functionality): + value_(0), + min_value_(0), + max_value_(1), + adjusted_max_value_(1), + no_max_(false) { + if(input.empty()) { + value_ = max_value_ = adjusted_max_value_ = 0.0; + return; + } + + std::vector result; + boost::split(result, input, boost::is_any_of("/")); + + switch(result.size()) { + case 1: + value_ = max_value_ = adjusted_max_value_ = std::stod(result[0]) * modifier; + break; + case 2: + value_ = adjusted_max_value_ = std::stod(result[0]) * modifier; + max_value_ = std::stod(result[1]) * modifier; + break; + case 3: + value_ = std::stod(result[0]) * modifier; + adjusted_max_value_ = std::stod(result[1]) * modifier; + max_value_ = std::stod(result[2]) * modifier; + break; + default: + value_ = max_value_ = adjusted_max_value_ = 0.0; + } + + min_value_ = max_value_ * minimum_functionality; +} + /* * Methods */ +template<> +const std::string Resource::Serialize(const double modifier) const { + return (boost::format("%1$.2f/%2$.2f/%3$.2f") % (value_/modifier) % + (adjusted_max_value_/modifier) % (max_value_/modifier)).str(); +} template @@ -69,7 +113,7 @@ template void Resource::Set(const T &value) { value_ = value; if(!no_max_) { - value_ = std::min(max_value_, value_); + value_ = std::min(adjusted_max_value_, value_); } value_ = std::max(min_value_, value_); } @@ -218,7 +262,7 @@ template Resource Resource::operator=(const T &value) { value_ = value; if(!no_max_) { - value_ = std::min(max_value_, value_); + value_ = std::min(adjusted_max_value_, value_); } value_ = std::max(min_value_, value_); @@ -260,6 +304,11 @@ Resource Resource::operator-=(T &value) { return *this; } +template +bool operator==(const Resource &lhs, const Resource &rhs) { + return lhs.Value() == rhs.Value(); +} + template bool operator==(const Resource &lhs, const T &rhs) { return lhs.Value() == rhs; @@ -325,6 +374,7 @@ T operator/(const T &lhs, const Resource &rhs) { template class Resource; +template bool operator==(const Resource &lhs, const Resource &rhs); template bool operator==(const Resource &lhs, const float &rhs); template bool operator>(const Resource &lhs, const float &rhs); template bool operator<(const Resource &lhs, const float &rhs); @@ -344,6 +394,7 @@ template float operator/(const float &lhs, const Resource &rhs); template class Resource; +template bool operator==(const Resource &lhs, const Resource &rhs); template bool operator==(const Resource &lhs, const double &rhs); template bool operator>(const Resource &lhs, const double &rhs); template bool operator<(const Resource &lhs, const double &rhs); @@ -360,6 +411,7 @@ template double operator/(const double &lhs, const Resource &rhs); template class Resource; +template bool operator==(const Resource &lhs, const Resource &rhs); template bool operator==(const Resource &lhs, const int &rhs); template bool operator>(const Resource &lhs, const int &rhs); template bool operator<(const Resource &lhs, const int &rhs); diff --git a/engine/src/resource/resource.h b/engine/src/resource/resource.h index 96f572fd9..a2256d757 100644 --- a/engine/src/resource/resource.h +++ b/engine/src/resource/resource.h @@ -22,6 +22,8 @@ #ifndef VEGA_STRIKE_ENGINE_RESOURCE_RESOURCE_H #define VEGA_STRIKE_ENGINE_RESOURCE_RESOURCE_H +#include + /** * @brief A resource is any part of the game that can be used up and filled up. * The purpose of this class is to simplify code throughout the game by placing it here. @@ -36,9 +38,14 @@ class Resource { T min_value_; T max_value_; T adjusted_max_value_; + + // TODO: make this const bool no_max_; public: Resource(const T &value = 0, const T &min_value = 0, const T &max_value = -1); + Resource(const std::string input, const double modifier = 1.0, const double minimum_functionality = 0.0); + + const std::string Serialize(const double modifier = 1.0) const; //const T operator=(Resource value) const; Resource operator=(const T &value); @@ -116,6 +123,8 @@ class Resource { T* MaxValuePtr() { return &max_value_; } }; +template +bool operator==(const Resource &lhs, const Resource &rhs); template bool operator==(const Resource &lhs, const T &rhs); template diff --git a/engine/src/resource/tests/resource_test.cpp b/engine/src/resource/tests/resource_test.cpp index a3b75265b..10c1d5c3f 100644 --- a/engine/src/resource/tests/resource_test.cpp +++ b/engine/src/resource/tests/resource_test.cpp @@ -77,6 +77,11 @@ TEST(Resource, Operators) { EXPECT_EQ(resource, 0.0); --resource; EXPECT_EQ(resource, 0.0); + + Resource a(1,0,1); + Resource b(1,0,1); + + EXPECT_TRUE(a == b); } @@ -174,3 +179,41 @@ TEST(Resource, Damage_Repair) { EXPECT_EQ(resource.AdjustedValue(), 10.0); EXPECT_EQ(resource.Value(), 10.0); } + +TEST(Resource, Serialization) { + const double value = 15.0; + const double adjusted = 21.0; + const double max = 30.0; + const double modifier = 3.0; + const std::string one = "10.0"; + const std::string two = "7.0/10.0"; + const std::string three = "5.0/7.0/10.0"; + + const std::string three_ten = "10.00/10.00/10.00"; + const std::string seven_seven_ten = "7.00/7.00/10.00"; + const std::string five_seven_ten = "5.00/7.00/10.00"; + + Resource resource = Resource(one, modifier); + std::string serialization_string = resource.Serialize(modifier); + + EXPECT_EQ(resource.Value(), max); + EXPECT_EQ(resource.AdjustedValue(), max); + EXPECT_EQ(resource.MaxValue(), max); + EXPECT_EQ(serialization_string, three_ten); + + resource = Resource(two, modifier); + serialization_string = resource.Serialize(modifier); + + EXPECT_EQ(resource.Value(), adjusted); + EXPECT_EQ(resource.AdjustedValue(), adjusted); + EXPECT_EQ(resource.MaxValue(), max); + EXPECT_EQ(serialization_string, seven_seven_ten); + + resource = Resource(three, modifier); + serialization_string = resource.Serialize(modifier); + + EXPECT_EQ(resource.Value(), value); + EXPECT_EQ(resource.AdjustedValue(), adjusted); + EXPECT_EQ(resource.MaxValue(), max); + EXPECT_EQ(serialization_string, five_seven_ten); +} \ No newline at end of file