-
-
Save MaanooAk/58cded6e8ac2fac9a48f66eda0630aba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <functional> | |
| namespace openage { | |
| /** | |
| * probably becomes part of the nyan game spec in future | |
| */ | |
| enum class game_resource { | |
| wood, | |
| food, | |
| gold, | |
| stone, | |
| RESOURCE_TYPE_COUNT | |
| }; | |
| class ResourceBoundle { | |
| public: | |
| ResourceBoundle() : value{0} { } | |
| float& operator[] (game_resource res) { return value[(int) res]; } | |
| float& operator[] (int index) { return value[index]; } | |
| float get(game_resource res) const { return value[(int) res]; } | |
| float get(int index) const { return value[index]; } | |
| bool operator> (const ResourceBoundle& other) const { | |
| for (int i=0; i<(int) game_resource::RESOURCE_TYPE_COUNT; i++) { | |
| if (!(this->get(i) > other.get(i))) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| bool operator>= (const ResourceBoundle& other) const { | |
| for (int i=0; i<(int) game_resource::RESOURCE_TYPE_COUNT; i++) { | |
| if (!(this->get(i) >= other.get(i))) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| ResourceBoundle& operator+= (const ResourceBoundle& other) { | |
| for (int i=0; i<(int) game_resource::RESOURCE_TYPE_COUNT; i++) { | |
| (*this)[i] += other.get(i); | |
| } | |
| return *this; | |
| } | |
| ResourceBoundle& operator-= (const ResourceBoundle& other) { | |
| for (int i=0; i<(int) game_resource::RESOURCE_TYPE_COUNT; i++) { | |
| (*this)[i] -= other.get(i); | |
| } | |
| return *this; | |
| } | |
| ResourceBoundle& operator*= (float a) { | |
| for (int i=0; i<(int) game_resource::RESOURCE_TYPE_COUNT; i++) { | |
| (*this)[i] *= a; | |
| } | |
| return *this; | |
| } | |
| bool deduct(const ResourceBoundle& amount) { | |
| if(*this >= amount) { | |
| *this -= amount; | |
| return true; | |
| } | |
| return false; | |
| } | |
| private: | |
| float value[(int) game_resource::RESOURCE_TYPE_COUNT]; | |
| }; | |
| } // namespace openage | |
| namespace std { | |
| /** | |
| * hasher for game resource | |
| */ | |
| template<> struct hash<openage::game_resource> { | |
| typedef underlying_type<openage::game_resource>::type underlying_type; | |
| typedef hash<underlying_type>::result_type result_type; | |
| result_type operator()( const openage::game_resource& arg ) const { | |
| hash<underlying_type> hasher; | |
| return hasher(static_cast<underlying_type>(arg)); | |
| } | |
| }; | |
| } | |
| // ============================ | |
| // ==== Testing | |
| using namespace openage; | |
| int main() { | |
| ResourceBoundle x; | |
| x[game_resource::food] = 200; | |
| x[game_resource::wood] += 20; | |
| ResourceBoundle y; | |
| y[game_resource::food] = 2000; | |
| y[game_resource::wood] = 400; | |
| bool y_has_x = y >= x; | |
| bool x_has_y = x >= y; | |
| std::cout | |
| << y[game_resource::wood] << " w " | |
| << y[game_resource::food] << " f " | |
| << y[game_resource::gold] << " g " | |
| << y[game_resource::stone] << " s " | |
| << "\n"; | |
| if(y >= x) y -= x; | |
| // or | |
| y.deduct(x); | |
| std::cout | |
| << y[game_resource::wood] << " w " | |
| << y[game_resource::food] << " f " | |
| << y[game_resource::gold] << " g " | |
| << y[game_resource::stone] << " s " | |
| << "\n"; | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment