From fe6737117d7dd1866c115d7b7e01be2c12658fe7 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 7 Oct 2023 13:33:27 -0500 Subject: [PATCH] Inventory Add,Remove,Use Management functions implemented. --- Crawler/Attributable.h | 48 +++++++++++++++--------- Crawler/Crawler.vcxproj | 3 +- Crawler/Crawler.vcxproj.filters | 9 +++-- Crawler/DEFINES.h | 2 +- Crawler/InventoryWindow.cpp | 27 ++++++++++++++ Crawler/Item.cpp | 66 ++++++++++++++++++++++++++------- Crawler/Item.h | 35 +++++++++++++---- Crawler/ItemComponent.h | 19 ---------- Crawler/Menu.cpp | 1 + Crawler/Menu.h | 1 + Crawler/MenuItemButton.h | 23 ++++++++++++ Crawler/MenuType.h | 2 + Crawler/MonsterAttribute.h | 1 + 13 files changed, 173 insertions(+), 64 deletions(-) create mode 100644 Crawler/InventoryWindow.cpp delete mode 100644 Crawler/ItemComponent.h create mode 100644 Crawler/MenuItemButton.h diff --git a/Crawler/Attributable.h b/Crawler/Attributable.h index bbbd5ad5..b31d7f37 100644 --- a/Crawler/Attributable.h +++ b/Crawler/Attributable.h @@ -8,33 +8,45 @@ class IAttributable{ protected: std::map>attributes; inline float&GetFloat(Attribute a){ - if(attributes.count(a)==0){ - attributes[a]=0.f; - } - return std::get(attributes[a]); + if(attributes.count(a)==0){ + attributes[a]=0.f; + } + return std::get(attributes[a]); }; inline int&GetInt(Attribute a){ - if(attributes.count(a)==0){ - attributes[a]=0; - } - return std::get(attributes[a]); + if(attributes.count(a)==0){ + attributes[a]=0; + } + return std::get(attributes[a]); }; - inline std::string&GetString(Attribute a){ - if(attributes.count(a)==0){ - attributes[a]=""; - } + inline std::string&GetString(Attribute a){ + if(attributes.count(a)==0){ + attributes[a]=""; + } return std::get(attributes[a]); }; inline bool&GetBool(Attribute a){ - if(attributes.count(a)==0){ + if(attributes.count(a)==0){ attributes[a]=false; - } - return std::get(attributes[a]); + } + return std::get(attributes[a]); }; inline vf2d&GetVf2d(Attribute a){ - if(attributes.count(a)==0){ + if(attributes.count(a)==0){ attributes[a]=vf2d{}; - } - return std::get(attributes[a]); + } + return std::get(attributes[a]); + }; + inline std::vector&GetIntVec(Attribute a){ + if(attributes.count(a)==0){ + attributes[a]=std::vector{}; + } + return std::get>(attributes[a]); + }; + inline std::vector&GetStringVec(Attribute a){ + if(attributes.count(a)==0){ + attributes[a]=std::vector{}; + } + return std::get>(attributes[a]); }; }; \ No newline at end of file diff --git a/Crawler/Crawler.vcxproj b/Crawler/Crawler.vcxproj index 4ed301a3..eb466818 100644 --- a/Crawler/Crawler.vcxproj +++ b/Crawler/Crawler.vcxproj @@ -273,8 +273,8 @@ - + @@ -316,6 +316,7 @@ + diff --git a/Crawler/Crawler.vcxproj.filters b/Crawler/Crawler.vcxproj.filters index 61e9dd84..2cef1dea 100644 --- a/Crawler/Crawler.vcxproj.filters +++ b/Crawler/Crawler.vcxproj.filters @@ -180,12 +180,12 @@ Header Files\Game States - - Header Files\Interface - Configurations + + Header Files\Interface + @@ -320,6 +320,9 @@ Source Files + + Source Files\Interface + diff --git a/Crawler/DEFINES.h b/Crawler/DEFINES.h index 3174e98f..f2ba7514 100644 --- a/Crawler/DEFINES.h +++ b/Crawler/DEFINES.h @@ -18,7 +18,7 @@ #define ACCESS_PLAYER Player*p=game->GetPlayer(); -#define VARIANTS float,int,std::string,bool,vf2d +#define VARIANTS float,int,std::string,bool,vf2d,std::vector,std::vector #define INFINITE 999999 diff --git a/Crawler/InventoryWindow.cpp b/Crawler/InventoryWindow.cpp new file mode 100644 index 00000000..6a2f7c6d --- /dev/null +++ b/Crawler/InventoryWindow.cpp @@ -0,0 +1,27 @@ +#pragma once +#include "Crawler.h" +#include "Menu.h" +#include "DEFINES.h" +#include "olcPixelGameEngine.h" +#include "safemap.h" +#include "Item.h" + +INCLUDE_GFX +typedef Attribute A; + +const Menu Menu::InitializeInventoryWindow(){ + constexpr int invWidth=5; + constexpr int invHeight=3; + constexpr int totalItemSlots=invWidth*invHeight; + + Menu inventoryWindow(CENTERED,{24*invWidth,24*(invHeight+1)}); + + for(auto&key:Inventory::get()){ + + } + for(int i=0;iITEM_DATA; -safemap>ITEM_SCRIPTS; +safemapITEM_SCRIPTS; safemap>ITEM_CATEGORIES; -typedef std::string IT; - ItemInfo::ItemInfo() - :customProps({nullptr,nullptr}){} + :customProps({nullptr,nullptr}),img(nullptr){} void ItemInfo::InitializeItems(){ @@ -62,7 +60,6 @@ void ItemInfo::InitializeItems(){ } ITEM_CATEGORIES.at(it.category).insert(it.name); it.img=img.Decal(); - it.script=scriptName; ItemProps&props=it.customProps; if(scriptName!=""){ props.scriptProps=&DATA["ItemScript"][scriptName]; @@ -107,20 +104,61 @@ void ItemInfo::InitializeScripts(){ std::cout<=_inventory.at(it).Amt()){ + int count=0; + std::vector&sortedList=sortedInv.at(_inventory.at(it).Category()); + for(std::string&itemName:sortedList){ + if(itemName==it)break; + count++; + } + sortedList.erase(sortedList.begin()+count); + _inventory.erase(it); + }else{ + _inventory.at(it).amt-=amt; + } +} + +std::vector&Inventory::get(ITCategory itemCategory){ + return sortedInv.at(itemCategory); +} + +void Inventory::InsertIntoSortedInv(IT item){ + sortedInv.at(ITEM_DATA[item].category).push_back(item); +} + +bool Inventory::ExecuteAction(IT item){ + return ITEM_SCRIPTS.at(ITEM_DATA.at(item).useFunc)(game,ITEM_DATA[item].customProps); } \ No newline at end of file diff --git a/Crawler/Item.h b/Crawler/Item.h index b84004ca..649ca7b5 100644 --- a/Crawler/Item.h +++ b/Crawler/Item.h @@ -9,20 +9,38 @@ class Crawler; class ItemInfo; +typedef std::string IT; +typedef std::string ITCategory; + class Item{ + friend class Inventory; private: - int amt; + uint32_t amt; ItemInfo*it; +public: + Item(uint32_t amt,IT item); + uint32_t Amt(); + std::string Name(); + std::string Description(); + ITCategory Category(); + Decal*Decal(); + ItemScript&OnUseAction(); }; class Inventory{ public: - void AddItem(std::string it,int amt=1); - void GetItemCount(std::string it); - void UseItem(std::string it,int amt=1); - void RemoveItem(std::string it,int amt=1); + static void AddItem(IT it,int amt=1); + static uint32_t GetItemCount(IT it); + static Item GetItem(IT it); + //Auto-executes its use function and removes the amt specified from the inventory. Multiple amounts will cause the item to execute its useFunc multiple times. + static void UseItem(IT it,int amt=1); + static void RemoveItem(IT it,int amt=1); + static std::vector&get(ITCategory itemCategory); private: - static std::mapinventory; + static void InsertIntoSortedInv(IT item); + static bool ExecuteAction(IT item); + static std::map_inventory; + static std::map>sortedInv; }; class ItemProps{ @@ -36,7 +54,10 @@ public: std::string GetStringProp(std::string prop); }; +typedef std::function ItemScript; + class ItemInfo{ + friend class Inventory; std::string name; std::string description; std::string category; @@ -45,7 +66,6 @@ class ItemInfo{ std::string useFunc=""; //Custom properties for this specific item's script. static utils::datafile NOPROPS; - std::string script; ItemProps customProps; private: @@ -56,5 +76,4 @@ public: /* For the useFunc, return true if the item can be used, false otherwise. */ - //ItemInfo(std::string name,std::string description,std::string category,Decal*img,utils::datafile&props,std::string useFunc); }; \ No newline at end of file diff --git a/Crawler/ItemComponent.h b/Crawler/ItemComponent.h deleted file mode 100644 index 9d387950..00000000 --- a/Crawler/ItemComponent.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "MenuIconButton.h" -#include "Item.h" - -INCLUDE_game - -class ItemComponent:public MenuIconButton{ - Item::ItemName item; -public: - inline ItemComponent(geom2d::rectrect,Item::ItemName*item,MenuFunc onClick) - :MenuIconButton(rect,,onClick){} -protected: - virtual void inline Update(Crawler*game)override{ - MenuIconButton::Update(game); - } - virtual void inline Draw(Crawler*game,vf2d parentPos,bool focused)override{ - MenuIconButton::Draw(game,parentPos,focused); - } -}; \ No newline at end of file diff --git a/Crawler/Menu.cpp b/Crawler/Menu.cpp index fc799a0e..b4457fa5 100644 --- a/Crawler/Menu.cpp +++ b/Crawler/Menu.cpp @@ -22,6 +22,7 @@ void Menu::InitializeMenus(){ stack.reserve(32); menus[TEST]=InitializeTestMenu(); menus[TEST_2]=InitializeTestSubMenu(); + menus[INVENTORY]=InitializeInventoryWindow(); for(MenuType type=TEST;typerect,Decal*icon,MenuFunc onClick) + :MenuIconButton(rect,icon,onClick){} +protected: + virtual void inline Update(Crawler*game)override{ + MenuComponent::Update(game); + } + virtual void inline Draw(Crawler*game,vf2d parentPos,bool focused)override{ + MenuComponent::Draw(game,parentPos,focused); + game->DrawRotatedDecal(parentPos+rect.middle(),icon,0,icon->sprite->Size()/2,{1,1},focused?WHITE:WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F); + } +}; \ No newline at end of file diff --git a/Crawler/MenuType.h b/Crawler/MenuType.h index 63cd5286..f21a463e 100644 --- a/Crawler/MenuType.h +++ b/Crawler/MenuType.h @@ -8,6 +8,8 @@ typedef std::function MenuFunc; enum MenuType{ TEST, TEST_2, + INVENTORY, + /////////////////////////////////////////////////////////// /*DO NOT REMOVE!!*/ENUM_END//////////////////////////////// /////////////////////////////////////////////////////////// diff --git a/Crawler/MonsterAttribute.h b/Crawler/MonsterAttribute.h index aadcf67d..5fb10cad 100644 --- a/Crawler/MonsterAttribute.h +++ b/Crawler/MonsterAttribute.h @@ -28,4 +28,5 @@ enum class Attribute{ JUMP_TOWARDS_PLAYER, HITS_UNTIL_DEATH, //When this is set, it is reduced by 1 each time the monster is hit. INDEXED_THEME, + INDEXED_ITEMS, }; \ No newline at end of file