diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-09-05 12:54:48 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-09-05 12:54:48 -0400 |
commit | 643e94897ba5fab0570d118a7aafc7772949d4e3 (patch) | |
tree | 0c9580cccee3bba95664eddef37572f496573cc3 | |
parent | b1f93a4f8a5a3e84db9f00d0b41749d4fb32ed26 (diff) |
saving inventory
-rw-r--r-- | include/common.hpp | 4 | ||||
-rw-r--r-- | include/inventory.hpp | 3 | ||||
-rw-r--r-- | main.cpp | 3 | ||||
-rw-r--r-- | src/inventory.cpp | 44 | ||||
-rw-r--r-- | src/systems/dialog.cpp | 2 | ||||
-rwxr-xr-x | src/tinyxml2.cpp | 6 | ||||
-rw-r--r-- | src/ui.cpp | 5 |
7 files changed, 57 insertions, 10 deletions
diff --git a/include/common.hpp b/include/common.hpp index 9ecd912..a03a888 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -23,11 +23,11 @@ constexpr float PI = 3.1415926535f; */ unsigned int millis(void); -namespace std { +/*namespace std { template<class T> constexpr const T& clamp(const T& v, const T& lo, const T& hi) { return (v > hi) ? hi : ((v > lo) ? v : lo); } -} +}*/ #endif // COMMON_HPP_ diff --git a/include/inventory.hpp b/include/inventory.hpp index 448e27f..f340a24 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -124,6 +124,9 @@ public: static inline Item* getItem(const std::string& s) { return &itemList[s]; } + + static bool save(void); + static void load(void); }; #endif // INVENTORY_HPP_ @@ -106,6 +106,8 @@ int main(int argc, char *argv[]) WorldSystem::loader(); + InventorySystem::load(); + ///////////////////////////// // // // actually start the game // @@ -172,6 +174,7 @@ int main(int argc, char *argv[]) // save game::briceSave(); + InventorySystem::save(); WorldSystem::save(); // exit diff --git a/src/inventory.cpp b/src/inventory.cpp index f200705..b96d5cf 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -4,12 +4,15 @@ #include <components.hpp> #include <engine.hpp> #include <error.hpp> +#include <fileio.hpp> #include <font.hpp> #include <player.hpp> #include <render.hpp> #include <ui.hpp> #include <forward_list> +#include <iostream> +#include <fstream> #include <unordered_map> #include <tinyxml2.h> @@ -248,7 +251,7 @@ bool InventorySystem::receive(const MouseClickEvent &mce) game::events.emit<UseItemEvent>(mce.position, items[0].item, &attack->second); } } - return true; + return false; } bool InventorySystem::receive(const MouseReleaseEvent &mre) @@ -384,3 +387,42 @@ bool InventorySystem::take(const std::string& name, int count) return true; } + +bool InventorySystem::save(void) +{ + std::ofstream s (game::config::xmlFolder + "inventory.dat"); + + // signature? + s << "831998\n"; + + for (const auto& i : items) { + if (i.item != nullptr && i.count > 0) + s << std::string(i.item->name) << '\n' << i.count << '\n'; + } + + // end of list? + s.close(); + return true; +} + +void InventorySystem::load(void) +{ + // attempt to load data + std::ifstream sf (game::config::xmlFolder + "inventory.dat"); + if (sf.good()) { + sf.close(); + auto lines = readFileA(game::config::xmlFolder + "inventory.dat"); + + // check signature + if (std::stoi(lines[0]) != 831998) + UserError("Save file signature is invalid... (delete it)"); + + for (unsigned int i = 1; i < lines.size(); i += 2) { + if (lines[i].size() > 0) { + int count = std::stoi(lines[i + 1]); + if (count > 0) + add(lines[i], count); + } + } + } +} diff --git a/src/systems/dialog.cpp b/src/systems/dialog.cpp index dd07ab8..ee7c834 100644 --- a/src/systems/dialog.cpp +++ b/src/systems/dialog.cpp @@ -144,7 +144,7 @@ bool DialogSystem::receive(const MouseClickEvent &mce) } } }); - return true; + return false; } void DialogSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) diff --git a/src/tinyxml2.cpp b/src/tinyxml2.cpp index 7473881..d765357 100755 --- a/src/tinyxml2.cpp +++ b/src/tinyxml2.cpp @@ -409,17 +409,17 @@ void XMLUtil::ConvertUTF32ToUTF8(unsigned long input, char* output, int* length) --output;
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
- //[[fallthrough]];
+ [[fallthrough]];
case 3:
--output;
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
- //[[fallthrough]];
+ [[fallthrough]];
case 2:
--output;
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
- //[[fallthrough]];
+ [[fallthrough]];
case 1:
--output;
*output = (char)(input | FIRST_BYTE_MARK[*length]);
@@ -691,10 +691,9 @@ bool InputSystem::receive(const MainSDLEvent& event) case SDL_KEYUP: ev.emit<KeyUpEvent>(SDL_KEY); - if (SDL_KEY == SDLK_ESCAPE) + if (SDL_KEY == SDLK_ESCAPE) { ui::menu::toggle(); - - if (SDL_KEY == SDLK_h) { + } else if (SDL_KEY == SDLK_h) { quest::toggle(); } else switch (SDL_KEY) { case SDLK_F3: |