Inventory Add,Remove,Use Management functions implemented.

pull/28/head
sigonasr2 1 year ago
parent 48a8165086
commit fe6737117d
  1. 12
      Crawler/Attributable.h
  2. 3
      Crawler/Crawler.vcxproj
  3. 9
      Crawler/Crawler.vcxproj.filters
  4. 2
      Crawler/DEFINES.h
  5. 27
      Crawler/InventoryWindow.cpp
  6. 60
      Crawler/Item.cpp
  7. 35
      Crawler/Item.h
  8. 19
      Crawler/ItemComponent.h
  9. 1
      Crawler/Menu.cpp
  10. 1
      Crawler/Menu.h
  11. 23
      Crawler/MenuItemButton.h
  12. 2
      Crawler/MenuType.h
  13. 1
      Crawler/MonsterAttribute.h

@ -37,4 +37,16 @@ protected:
}
return std::get<vf2d>(attributes[a]);
};
inline std::vector<int>&GetIntVec(Attribute a){
if(attributes.count(a)==0){
attributes[a]=std::vector<int>{};
}
return std::get<std::vector<int>>(attributes[a]);
};
inline std::vector<std::string>&GetStringVec(Attribute a){
if(attributes.count(a)==0){
attributes[a]=std::vector<std::string>{};
}
return std::get<std::vector<std::string>>(attributes[a]);
};
};

@ -273,8 +273,8 @@
<ClInclude Include="Emitter.h" />
<ClInclude Include="GameState.h" />
<ClInclude Include="Item.h" />
<ClInclude Include="ItemComponent.h" />
<ClInclude Include="MenuIconButton.h" />
<ClInclude Include="MenuItemButton.h" />
<ClInclude Include="MenuLabel.h" />
<ClInclude Include="MenuType.h" />
<ClInclude Include="Key.h" />
@ -316,6 +316,7 @@
<ClCompile Include="FallingDebris.h" />
<ClCompile Include="FireBolt.cpp" />
<ClCompile Include="GameState.cpp" />
<ClCompile Include="InventoryWindow.cpp" />
<ClCompile Include="Item.cpp" />
<ClCompile Include="Key.cpp" />
<ClCompile Include="LightningBolt.cpp" />

@ -180,12 +180,12 @@
<ClInclude Include="GameState.h">
<Filter>Header Files\Game States</Filter>
</ClInclude>
<ClInclude Include="ItemComponent.h">
<Filter>Header Files\Interface</Filter>
</ClInclude>
<ClInclude Include="Item.h">
<Filter>Configurations</Filter>
</ClInclude>
<ClInclude Include="MenuItemButton.h">
<Filter>Header Files\Interface</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Player.cpp">
@ -320,6 +320,9 @@
<ClCompile Include="Item.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="InventoryWindow.cpp">
<Filter>Source Files\Interface</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />

@ -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<int>,std::vector<std::string>
#define INFINITE 999999

@ -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;i<totalItemSlots;i++){
inventoryWindow.GetStringVec(A::INDEXED_ITEMS).push_back();
}
return testSubMenu;
}

@ -9,13 +9,11 @@ INCLUDE_DATA
INCLUDE_GFX
safemap<std::string,ItemInfo>ITEM_DATA;
safemap<std::string,std::function<bool(Crawler*,ItemProps)>>ITEM_SCRIPTS;
safemap<std::string,ItemScript>ITEM_SCRIPTS;
safemap<std::string,std::set<std::string>>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<<ITEM_SCRIPTS.size()<<" item scripts have been loaded."<<std::endl;
}
void Inventory::AddItem(IT it,int amt){
Item::Item(uint32_t amt,IT item)
:amt(amt),it(&ITEM_DATA.at(item)){}
void Inventory::AddItem(IT it,int amt){
//There are two places to manipulate items in (Both the sorted inventory and the actual inventory)
if(!_inventory.count(it)){
_inventory[it]=Item{amt,it};
InsertIntoSortedInv(it);
}else{
_inventory.at(it).amt+=amt;
}
}
void Inventory::GetItemCount(IT it){
uint32_t Inventory::GetItemCount(IT it){
if(!_inventory.count(it)){
return 0;
}else{
return _inventory.at(it).Amt();
}
}
void Inventory::UseItem(IT it,int amt){
//There are two places to manipulate items in (Both the sorted inventory and the actual inventory)
for(int i=0;i<amt;i++){
if(ExecuteAction(it)){
RemoveItem(it);
}
}
}
void Inventory::UseItem(IT it,int amt)
{
void Inventory::RemoveItem(IT it,int amt){
//There are two places to manipulate items in (Both the sorted inventory and the actual inventory)
if(!_inventory.count(it))return;
if(amt>=_inventory.at(it).Amt()){
int count=0;
std::vector<IT>&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<IT>&Inventory::get(ITCategory itemCategory){
return sortedInv.at(itemCategory);
}
void Inventory::RemoveItem(IT it,int amt)
{
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);
}

@ -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<IT>&get(ITCategory itemCategory);
private:
static std::map<std::string,Item>inventory;
static void InsertIntoSortedInv(IT item);
static bool ExecuteAction(IT item);
static std::map<IT,Item>_inventory;
static std::map<ITCategory,std::vector<IT>>sortedInv;
};
class ItemProps{
@ -36,7 +54,10 @@ public:
std::string GetStringProp(std::string prop);
};
typedef std::function<bool(Crawler*,ItemProps)> 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);
};

@ -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::rect<float>rect,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);
}
};

@ -22,6 +22,7 @@ void Menu::InitializeMenus(){
stack.reserve(32);
menus[TEST]=InitializeTestMenu();
menus[TEST_2]=InitializeTestSubMenu();
menus[INVENTORY]=InitializeInventoryWindow();
for(MenuType type=TEST;type<MenuType::ENUM_END;type=MenuType(int(type+1))){
if(menus.count(type)==0){

@ -42,6 +42,7 @@ private:
void CheckClickAndPerformMenuSelect(Crawler*game);
static const Menu InitializeTestMenu();
static const Menu InitializeTestSubMenu();
static const Menu InitializeInventoryWindow();
//X (0-3), Y (0-2) for specific 9-patch tile (tiled version).
static Renderable&GetPatchPart(int x,int y);

@ -0,0 +1,23 @@
#pragma once
#include "MenuIconButton.h"
#include "DEFINES.h"
#include "Crawler.h"
#include "Item.h"
INCLUDE_game
class MenuItemButton:public MenuIconButton{
private:
Decal*icon;
public:
inline MenuItemButton(geom2d::rect<float>rect,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);
}
};

@ -8,6 +8,8 @@ typedef std::function<void(Menu&,Crawler*)> MenuFunc;
enum MenuType{
TEST,
TEST_2,
INVENTORY,
///////////////////////////////////////////////////////////
/*DO NOT REMOVE!!*/ENUM_END////////////////////////////////
///////////////////////////////////////////////////////////

@ -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,
};
Loading…
Cancel
Save