From 3766c45c62eeca442e9ab700c09c547f08615b62 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 29 Jun 2016 17:56:48 -0400 Subject: chests, item dropping --- Changelog | 8 ++++++++ brice.dat | 8 ++++---- include/entities.hpp | 3 +-- include/inventory.hpp | 7 ++++++- include/texture.hpp | 3 +++ include/world.hpp | 2 +- main.cpp | 11 ++++++++--- src/entities.cpp | 3 ++- src/inventory.cpp | 8 ++++++++ src/mob.cpp | 17 ++++++++++++++++- src/ui.cpp | 7 +++++++ src/world.cpp | 14 ++++++++------ xml/bobshouse.xml | 2 +- xml/playerSpawnHill1.xml | 6 +++--- xml/playerSpawnHill1_Building1.xml | 6 +++--- xml/town.xml | 5 +++-- 16 files changed, 82 insertions(+), 28 deletions(-) diff --git a/Changelog b/Changelog index ee05b85..b530132 100644 --- a/Changelog +++ b/Changelog @@ -1071,3 +1071,11 @@ Late June (6/16/2016 - 6/27/2016) - redid forest trees, mountains, houses - fixed major issue with entering structures - exploring more ideas for soundtracks + +6/29/2016: +========== + + - added chests + - added q to drop items, as objects + - chests snag dropped objects, right click to reclaim + - control setting is good, needs saving though diff --git a/brice.dat b/brice.dat index 2033bae..4dad711 100644 --- a/brice.dat +++ b/brice.dat @@ -1,7 +1,7 @@ 3 -canSprint -1 -canJump -0 Slow 0 +canJump +0 +canSprint +1 diff --git a/include/entities.hpp b/include/entities.hpp index 4abb3cb..2d96a9f 100644 --- a/include/entities.hpp +++ b/include/entities.hpp @@ -386,9 +386,8 @@ public: }; class Object : public Entity{ -private: - std::string iname; public: + std::string iname; std::string pickupDialog; bool questObject = false; diff --git a/include/inventory.hpp b/include/inventory.hpp index a85e537..ca402b3 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -219,13 +219,16 @@ public: /*********************************************************************************** * OLD STUFF THAT NEEDS TO GET UPDATED * **********************************************************************************/ + +using InventorySlot = std::pair; + class Inventory { private: unsigned int size; //how many slots our inventory has unsigned int sel; //what item is currently selected int os = 0; public: - std::vector> Items; + std::vector Items; bool invOpen = false; //is the inventory open bool invOpening = false; //is the opening animation playing @@ -253,6 +256,8 @@ public: void setSelectionDown(); void draw(void); // Draws a text list of items in this inventory (should only be called for the player for now) + + const Item* getCurrentItem(void); }; void initInventorySprites(void); diff --git a/include/texture.hpp b/include/texture.hpp index c301af1..7f22a79 100644 --- a/include/texture.hpp +++ b/include/texture.hpp @@ -46,6 +46,9 @@ public: TextureIterator(void) { position = std::begin(textures); } + ~TextureIterator(void) { + textures.clear(); + } TextureIterator(const std::vector &l) { for (const auto &s : l) textures.emplace_back(Texture::loadTexture(s), s); diff --git a/include/world.hpp b/include/world.hpp index a2de048..9a892d8 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -66,7 +66,7 @@ extern std::string currentXML; /** * Defines how many game ticks it takes to go from day to night or vice versa. */ -constexpr const unsigned int DAY_CYCLE = 12000; +constexpr const unsigned int DAY_CYCLE = 10000; /** * Defines the velocity of player when moved by the keyboard diff --git a/main.cpp b/main.cpp index fd8ed4d..1bfce77 100644 --- a/main.cpp +++ b/main.cpp @@ -265,8 +265,8 @@ int main(int argc, char *argv[]) for (const auto &xf : xmlFiles) { if (xf[0] != '.') { XMLDocument xmld; - auto file = (xmlFolder + xf).c_str(); - xmld.LoadFile(file); + auto file = xmlFolder + xf; + xmld.LoadFile(file.c_str()); auto xmle = xmld.FirstChildElement("World"); @@ -286,7 +286,8 @@ int main(int argc, char *argv[]) xmle->DeleteAttribute("dindex"); xmle = xmle->NextSiblingElement(); } - xmld.SaveFile(file, false); + + xmld.SaveFile(file.c_str(), false); } } @@ -328,6 +329,10 @@ int main(int argc, char *argv[]) arena->setBackground(WorldBGType::Forest); arena->setBGM("assets/music/embark.wav"); + + player->inv->addItem("Wood Sword", 10); + + // the main loop, in all of its gloriousness.. std::thread([&]{ while (gameRunning) { diff --git a/src/entities.cpp b/src/entities.cpp index 0c312e2..f4c020c 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -188,6 +188,7 @@ Player::Player() : Entity() Player::~Player() { delete inv; + delete &tex; } void Player::createFromXML(XMLElement *e, World *w=nullptr) @@ -716,7 +717,7 @@ COMMONAIFUNC: // handle give tags if ((oxml = exml->FirstChildElement("give"))) { do player->inv->addItem(oxml->Attribute("id"), oxml->UnsignedAttribute("count")); - while ((oxml = oxml->NextSiblingElement())); + while ((oxml = oxml->NextSiblingElement("give"))); } // handle take tags diff --git a/src/inventory.cpp b/src/inventory.cpp index ede5eeb..7e8c94e 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -727,3 +727,11 @@ bool Inventory::detectCollision(vec2 one, vec2 two) { (void)two; return false; } + +const Item* Inventory::getCurrentItem(void) +{ + if (Items.size() > 0) + return Items[sel].first; + else + return nullptr; +} diff --git a/src/mob.cpp b/src/mob.cpp index 20273d6..99d6f34 100644 --- a/src/mob.cpp +++ b/src/mob.cpp @@ -432,11 +432,26 @@ Chest::Chest(void) : Mob() width = HLINES(10); height = HLINES(5); tex = TextureIterator({"assets/chest.png"}); + inv = new Inventory(1); } void Chest::act(void) { - //die(); + if (isInside(ui::mouse) && player->isNear(this)) { + if ((SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)) && !ui::dialogBoxExists) + for (auto &i : inv->Items) { + player->inv->addItem(i.first->name, i.second); + inv->takeItem(i.first->name, i.second); + } + } + + for (auto &e : currentWorld->entity) { + if (e->type == OBJECTT && e->isNear(this)) { + auto o = dynamic_cast(e); + inv->addItem(o->iname, 1); + e->health = 0; + } + } } void Chest::onHit(unsigned int _health) diff --git a/src/ui.cpp b/src/ui.cpp index f3523f6..18bae1b 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1533,6 +1533,13 @@ EXIT: action::disable(); heyOhLetsGo = 0; + } else if (SDL_KEY == SDLK_q) { + auto item = player->inv->getCurrentItem(); + if (item != nullptr) { + if (player->inv->takeItem(item->name, 1) == 0) + currentWorld->addObject(item->name, "o shit waddup", + player->loc.x + player->width / 2, player->loc.y + player->height / 2); + } } else switch (SDL_KEY) { case SDLK_F3: debug ^= true; diff --git a/src/world.cpp b/src/world.cpp index 7294fda..9c51ec8 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -780,26 +780,26 @@ singleDetect(Entity *e) if (entity[i] == e) { switch (e->type) { case STRUCTURET: - killed = "structure"; + killed = " structure"; build.erase(std::find(std::begin(build), std::end(build), e)); break; case NPCT: - killed = "NPC"; + killed = "n NPC"; npc.erase(std::find(std::begin(npc), std::end(npc), e)); break; case MOBT: - killed = "mob"; + killed = " mob"; mob.erase(std::find(std::begin(mob), std::end(mob), e)); break; case OBJECTT: - killed = "object"; + killed = "n object"; object.erase(std::find(std::begin(object), std::end(object), *Objectp(e))); break; default: break; } - std::cout << "Killed a " << killed << "...\n"; + std::cout << "Killed a" << killed << "...\n"; entity.erase(entity.begin() + i); return; } @@ -1823,7 +1823,9 @@ loadWorldFromXMLNoSave(std::string path) { return nullptr; _currentXML = xmlFolder + path; - _currentXMLRaw = readFile(_currentXML.c_str()); + const char *worthless = readFile(_currentXML.c_str()); + _currentXMLRaw = worthless; + delete[] worthless; // create a temporary XMLDocument if this isn't the main world if (!loadedLeft && !loadedRight) diff --git a/xml/bobshouse.xml b/xml/bobshouse.xml index ebf020b..2d69813 100644 --- a/xml/bobshouse.xml +++ b/xml/bobshouse.xml @@ -3,5 +3,5 @@